More or less properly shutdown client / network node
This commit is contained in:
parent
8d7b9f6457
commit
e611e5e20a
@ -39,17 +39,18 @@ public class Main {
|
|||||||
DatabaseRepository repo = new DatabaseRepository();
|
DatabaseRepository repo = new DatabaseRepository();
|
||||||
Context.init(repo, repo, networkNode, 48444);
|
Context.init(repo, repo, networkNode, 48444);
|
||||||
Context.getInstance().addStream(1);
|
Context.getInstance().addStream(1);
|
||||||
networkNode.setListener(new NetworkHandler.MessageListener() {
|
networkNode.start(new NetworkHandler.MessageListener() {
|
||||||
@Override
|
@Override
|
||||||
public void receive(ObjectPayload payload) {
|
public void receive(ObjectPayload payload) {
|
||||||
// LOG.info("message received: " + payload);
|
// LOG.info("message received: " + payload);
|
||||||
// System.out.print('.');
|
// System.out.print('.');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
networkNode.start();
|
|
||||||
|
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||||
System.out.print("Enter String");
|
System.out.print("Press Enter to exit\n");
|
||||||
br.readLine();
|
br.readLine();
|
||||||
|
LOG.info("Shutting down client");
|
||||||
|
networkNode.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,9 +22,9 @@ import ch.dissem.bitmessage.entity.payload.ObjectPayload;
|
|||||||
* Handles incoming messages
|
* Handles incoming messages
|
||||||
*/
|
*/
|
||||||
public interface NetworkHandler {
|
public interface NetworkHandler {
|
||||||
void setListener(MessageListener listener);
|
void start(MessageListener listener);
|
||||||
|
|
||||||
void start();
|
void stop();
|
||||||
|
|
||||||
void send(ObjectPayload payload);
|
void send(ObjectPayload payload);
|
||||||
|
|
||||||
|
@ -183,7 +183,7 @@ public class Connection implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void disconnect() {
|
public void disconnect() {
|
||||||
try {
|
try {
|
||||||
state = DISCONNECTED;
|
state = DISCONNECTED;
|
||||||
socket.close();
|
socket.close();
|
||||||
|
@ -41,41 +41,34 @@ public class NetworkNode implements NetworkHandler {
|
|||||||
private final static Logger LOG = LoggerFactory.getLogger(NetworkNode.class);
|
private final static Logger LOG = LoggerFactory.getLogger(NetworkNode.class);
|
||||||
private final ExecutorService pool;
|
private final ExecutorService pool;
|
||||||
private final List<Connection> connections = new LinkedList<>();
|
private final List<Connection> connections = new LinkedList<>();
|
||||||
private MessageListener listener;
|
private ServerSocket serverSocket;
|
||||||
|
private Thread connectionManager;
|
||||||
|
|
||||||
public NetworkNode() {
|
public NetworkNode() {
|
||||||
pool = Executors.newCachedThreadPool();
|
pool = Executors.newCachedThreadPool();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setListener(final MessageListener listener) {
|
public void start(final MessageListener listener) {
|
||||||
if (this.listener != null) {
|
|
||||||
throw new IllegalStateException("Listener can only be set once");
|
|
||||||
}
|
|
||||||
this.listener = listener;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void start() {
|
|
||||||
final Context ctx = Context.getInstance();
|
final Context ctx = Context.getInstance();
|
||||||
if (listener == null) {
|
if (listener == null) {
|
||||||
throw new IllegalStateException("Listener must be set at start");
|
throw new IllegalStateException("Listener must be set at start");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
final ServerSocket serverSocket = new ServerSocket(Context.getInstance().getPort());
|
serverSocket = new ServerSocket(Context.getInstance().getPort());
|
||||||
pool.execute(new Runnable() {
|
pool.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Socket socket = serverSocket.accept();
|
Socket socket = serverSocket.accept();
|
||||||
socket.setSoTimeout(20000);
|
socket.setSoTimeout(10000);
|
||||||
startConnection(new Connection(SERVER, socket, listener));
|
startConnection(new Connection(SERVER, socket, listener));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOG.debug(e.getMessage(), e);
|
LOG.debug(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Thread connectionManager = new Thread(new Runnable() {
|
connectionManager = new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (!Thread.interrupted()) {
|
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) {
|
private void startConnection(Connection c) {
|
||||||
synchronized (connections) {
|
synchronized (connections) {
|
||||||
connections.add(c);
|
connections.add(c);
|
||||||
|
Loading…
Reference in New Issue
Block a user