Commit a3e78fa4 authored by legoc's avatar legoc
Browse files

Context and Server refactored

parent fbb3ebbb
......@@ -2,4 +2,5 @@ package fr.ill.ics.cameo.base;
public interface Context {
void terminate();
}
......@@ -28,11 +28,8 @@ import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import fr.ill.ics.cameo.Zmq;
import fr.ill.ics.cameo.Zmq.Socket;
import fr.ill.ics.cameo.base.Application.State;
import fr.ill.ics.cameo.base.Application.This;
import fr.ill.ics.cameo.base.impl.CancelIdGenerator;
import fr.ill.ics.cameo.base.impl.Response;
import fr.ill.ics.cameo.base.impl.zmq.ContextZmq;
import fr.ill.ics.cameo.messages.JSON;
......@@ -50,8 +47,7 @@ public class Server {
private Endpoint serverEndpoint;
private int[] serverVersion = new int[3];
private int statusPort;
private Zmq.Context context;
private ContextZmq contextImpl;
private Context contextImpl;
private int timeout = 0; // default value because of ZeroMQ design
private RequestSocket requestSocket;
private JSON.Parser parser = new JSON.Parser();
......@@ -116,13 +112,11 @@ public class Server {
* Initializes the context and the request socket. The serverEndpoint must have been set.
*/
final private void init() {
context = new Zmq.Context();
contextImpl = new ContextZmq(context);
//TODO Replace with factory.
contextImpl = new ContextZmq();
requestSocket = this.createRequestSocket(serverEndpoint.toString());
}
private int getAvailableTimeout() {
int timeout = getTimeout();
if (timeout > 0) {
......@@ -155,11 +149,7 @@ public class Server {
public Context getContext() {
return contextImpl;
}
public void destroySocket(Socket socket) {
context.destroySocket(socket);
}
public JSONObject parse(byte[] data) throws ParseException {
return parser.parse(Messages.parseString(data));
}
......@@ -260,7 +250,7 @@ public class Server {
terminateStatusThread();
requestSocket.terminate();
context.destroy();
contextImpl.terminate();
}
public void registerEventListener(EventListener listener) {
......
......@@ -7,11 +7,15 @@ public class ContextZmq implements Context {
private Zmq.Context context;
public ContextZmq(Zmq.Context context) {
this.context = context;
public ContextZmq() {
context = new Zmq.Context();
}
public Zmq.Context getContext() {
return context;
}
public void terminate() {
context.destroy();
}
}
......@@ -217,6 +217,6 @@ public class EventStreamSocketZmq implements EventStreamSocketImpl {
}
public void destroy() {
server.destroySocket(subscriberSocket);
context.destroySocket(subscriberSocket);
}
}
\ No newline at end of file
......@@ -162,6 +162,6 @@ public class OutputStreamSocketZmq implements OutputStreamSocketImpl {
}
public void destroy() {
server.destroySocket(subscriberSocket);
context.destroySocket(subscriberSocket);
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment