37
40
protected void init() {
43
public Message send(Message message, int timeout) throws ServiceException {
45
out.println(message.toJson());
46
return receive(message.getAction(), timeout);
41
51
public Message send(Message message) throws ServiceException {
42
synchronized (clientSocket) {
43
out.println(message.toJson());
46
} catch (IOException ex) {
52
return send(message, 100);
55
private Message receive(String action, int timeout) throws ServiceException {
57
ResponseMessage message = null;
58
logger.log(Level.INFO, "Try recive action {0}", action);
59
for (int n = 0; n < RETRY_COUNT; n++) {
61
message = receive(ResponseMessage.class);
62
} catch (Exception e) {
63
logger.severe(e.getMessage());
66
if (message != null && message.responseTo() != null && message.responseTo().equals(action)) {
67
logger.log(Level.INFO, "Found {0}.", action);
69
} else if (message != null) {
74
logger.log(Level.INFO, "Retrying for {0} of 5 times", n + 1);
75
Thread.sleep(timeout);
76
} catch (InterruptedException ex) {
79
logger.info("Response not recieved! Returning null");
52
84
public void setRunning(boolean running) {
53
this.running = running;
86
this.running = running;
91
* Performe a standard handling of a Message
94
protected synchronized void doHandle(Message message) {
96
if (message != null) {
97
if (!handle(message, this, false)) {
98
handle(message, getParent(), false);
57
105
public void run() {
58
106
while (isRunning()) {
60
synchronized (clientSocket) {
61
logger.info("Waiting for input..." + clientSocket.isConnected());
108
synchronized (lock) {
109
logger.info("Waiting for input...");
63
logger.info("Trying to recivie");
64
111
Message message = receive();
65
logger.info("recived " + message);
67
if (message != null) {
68
if (!handle(message, this, false)) {
69
handle(message, server, false);
112
logger.log(Level.INFO, "Recived {0}", message);
72
114
} catch (SocketTimeoutException e) {
73
logger.info("Timed out..."); /// swallow
115
logger.info("Timed out...");
76
if (clientSocket.isInputShutdown() || clientSocket.isOutputShutdown()) {
77
logger.info("Things are shutdown...");
80
118
} catch (ServiceException ex) {
81
119
logger.log(Level.SEVERE, null, ex);
82
120
} catch (IOException ex) {