More or less properly shutdown client / network node

This commit is contained in:
Christian Basler 2015-04-17 13:01:46 +02:00
parent 8d7b9f6457
commit e611e5e20a
4 changed files with 29 additions and 19 deletions

View File

@ -39,17 +39,18 @@ public class Main {
DatabaseRepository repo = new DatabaseRepository();
Context.init(repo, repo, networkNode, 48444);
Context.getInstance().addStream(1);
networkNode.setListener(new NetworkHandler.MessageListener() {
networkNode.start(new NetworkHandler.MessageListener() {
@Override
public void receive(ObjectPayload payload) {
// LOG.info("message received: " + payload);
// System.out.print('.');
}
});
networkNode.start();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter String");
System.out.print("Press Enter to exit\n");
br.readLine();
LOG.info("Shutting down client");
networkNode.stop();
}
}

View File

@ -22,9 +22,9 @@ import ch.dissem.bitmessage.entity.payload.ObjectPayload;
* Handles incoming messages
*/
public interface NetworkHandler {
void setListener(MessageListener listener);
void start(MessageListener listener);
void start();
void stop();
void send(ObjectPayload payload);

View File

@ -183,7 +183,7 @@ public class Connection implements Runnable {
}
}
private void disconnect() {
public void disconnect() {
try {
state = DISCONNECTED;
socket.close();

View File

@ -41,41 +41,34 @@ public class NetworkNode implements NetworkHandler {
private final static Logger LOG = LoggerFactory.getLogger(NetworkNode.class);
private final ExecutorService pool;
private final List<Connection> connections = new LinkedList<>();
private MessageListener listener;
private ServerSocket serverSocket;
private Thread connectionManager;
public NetworkNode() {
pool = Executors.newCachedThreadPool();
}
@Override
public void setListener(final MessageListener listener) {
if (this.listener != null) {
throw new IllegalStateException("Listener can only be set once");
}
this.listener = listener;
}
@Override
public void start() {
public void start(final MessageListener listener) {
final Context ctx = Context.getInstance();
if (listener == null) {
throw new IllegalStateException("Listener must be set at start");
}
try {
final ServerSocket serverSocket = new ServerSocket(Context.getInstance().getPort());
serverSocket = new ServerSocket(Context.getInstance().getPort());
pool.execute(new Runnable() {
@Override
public void run() {
try {
Socket socket = serverSocket.accept();
socket.setSoTimeout(20000);
socket.setSoTimeout(10000);
startConnection(new Connection(SERVER, socket, listener));
} catch (IOException e) {
LOG.debug(e.getMessage(), e);
}
}
});
Thread connectionManager = new Thread(new Runnable() {
connectionManager = new Thread(new Runnable() {
@Override
public void run() {
while (!Thread.interrupted()) {
@ -114,6 +107,22 @@ public class NetworkNode implements NetworkHandler {
}
}
@Override
public void stop() {
connectionManager.interrupt();
try {
serverSocket.close();
} catch (IOException e) {
LOG.debug(e.getMessage(), e);
}
synchronized (connections) {
for (Connection c : connections) {
c.disconnect();
}
}
pool.shutdownNow();
}
private void startConnection(Connection c) {
synchronized (connections) {
connections.add(c);