org.coos.messaging.transport.TCPTransportManager Maven / Gradle / Ivy
/**
* COOS - Connected Objects Operating System (www.connectedobjects.org).
*
* Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*
* You may also contact one of the following for additional information:
* Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
* Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
*/
package org.coos.messaging.transport;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.coos.messaging.LinkManager;
import org.coos.messaging.Transport;
import org.coos.messaging.util.Log;
import org.coos.messaging.util.LogFactory;
/**
* A TCP transport manager
*
* @author Knut Eilif Husa, Tellu AS
*/
public class TCPTransportManager extends DefaultChannelServer {
static final String PROPERTY_LISTEN_PORT = "port";
private static final Log logger = LogFactory.getLog(TCPTransportManager.class.getName());
private int listenPort = 15666;
private ServerSocket serverSocket;
private Set transports = Collections.synchronizedSet(new HashSet());
private boolean running;
Thread thread;
boolean stopping = false;
public TCPTransportManager() {
}
public TCPTransportManager(int listenPort, LinkManager linkManager) {
this.listenPort = listenPort;
setLinkManager(linkManager);
}
public int getListenPort() {
return listenPort;
}
public void start() throws Exception {
if (properties.get(PROPERTY_LISTEN_PORT) != null) {
listenPort = Integer.valueOf((String) properties.get(PROPERTY_LISTEN_PORT));
}
serverSocket = new ServerSocket(listenPort);
listenPort = serverSocket.getLocalPort();
logger.info(" Listening on port " + listenPort);
serverSocket.setSoTimeout(500);
running = true;
thread = new Thread(new Runnable() {
public void run() {
try {
while (running) {
try {
Socket socket = serverSocket.accept();
TCPTransport transport = new TCPTransport(socket, TCPTransportManager.this);
initializeChannel(transport);
transport.start();
synchronized (transports) {
transports.add(transport);
}
} catch (SocketTimeoutException e) {
if (!running) {
if ((serverSocket != null) && !serverSocket.isClosed()) {
serverSocket.close();
}
return;
}
} catch (SocketException e) {
if (e.getMessage().equals("socket closed")) {
logger.info("Server connection closing");
running = false;
}
}
}
} catch (Exception e1) {
logger.error("Exception ignored", e1);
}
}
});
thread.start();
}
public void stop() throws Exception {
if (running) {
running = false;
serverSocket.close();
thread.interrupt();
stopTransports();
}
}
public void stopTransports() throws Exception {
synchronized (transports) {
stopping = true;
for (Transport transport : transports) {
transport.stop();
}
transports.clear();
stopping = false;
}
}
protected void disconnected(TCPTransport transport) {
if (stopping)
return;
synchronized (transports) {
transports.remove(transport);
}
}
}