if (writeThread==null) {
writeThread = new Thread() {
public void run() {
try {
while ( !Thread.interrupted() ) {
Message message;
synchronized (sendQueue) {
while (sendQueue.isEmpty()) {
sendQueue.wait();
}
message = (Message)sendQueue.remove(0);
}
try {
// int size = access.computeAnonymousObjectSize(message);
// ByteArrayOutputStream bytes = new ByteArrayOutputStream(size);
// access.save(bytes, message);
// client.sendToNetwork(bytes.toByteArray());
TcpClient cl = client;
if (cl!=null) {
int size = access.computeAnonymousObjectSize(message);
ByteArrayOutputStream bytes = new ByteArrayOutputStream(size+4);
new DataOutputStream(bytes).writeInt(size);
access.save(bytes, message);
cl.send( ByteBuffer.wrap( bytes.toByteArray() ) );
}
}
catch (InterruptedException in) {
throw in; // do not want to catch this
}
catch (Exception ex) {
Level level = (ex instanceof IOException && "not connected".equals(ex.getMessage())) ||
(ex instanceof SocketException && "Broken pipe".equals(ex.getMessage())) ||
(ex instanceof SocketException && "sendto failed: EPIPE (Broken pipe)".equals(ex.getMessage()))
?Level.INFO:Level.WARNING;
logger.log(level,"could not send "+message,ex);
}
}
}
catch (InterruptedException ex) { }
}
};
writeThread.start();
}
System.out.println("sending message "+command+" "+param);
Message message = new Message();
message.setCommand(command);
message.setParam(param);
if (wait!=0) {
message.setWait(new Integer(wait));
}
synchronized (sendQueue) {
sendQueue.add(message);
sendQueue.notify();