use of jp.ossc.nimbus.service.publish.MessageCommunicateException in project nimbus by nimbus-org.
the class ClientConnectionImpl method receive.
private Message receive() throws MessageCommunicateException {
if (socket == null) {
return null;
}
int length = 0;
try {
DataInputStream dis = new DataInputStream(socket.getInputStream());
length = dis.readInt();
if (length <= 0) {
return null;
}
if (receiveBytes == null || receiveBytes.length < length) {
receiveBytes = new byte[length];
}
dis.readFully(receiveBytes, 0, length);
ByteArrayInputStream bais = new ByteArrayInputStream(receiveBytes, 0, length);
MessageImpl message = MessageImpl.read(bais, externalizer, messageBuffer);
message.setClientConnection(this);
if (message != null) {
final short messageType = message.getMessageType();
switch(messageType) {
case MessageImpl.MESSAGE_TYPE_SERVER_CLOSE:
if (serverCloseMessageId != null) {
ServiceManagerFactory.getLogger().write(serverCloseMessageId, new Object[] { this });
}
isServerClosed = true;
close();
return null;
case MessageImpl.MESSAGE_TYPE_SERVER_RESPONSE:
if (isAcknowledge && requestMonitorMap != null) {
synchronized (requestMonitorMap) {
Object reqId = null;
try {
reqId = message.getObject();
} catch (MessageException e) {
return null;
}
SynchronizeMonitor responseMonitor = (SynchronizeMonitor) requestMonitorMap.get(reqId);
if (responseMonitor != null) {
responseMonitor.notifyAllMonitor();
}
}
}
return null;
case MessageImpl.MESSAGE_TYPE_APPLICATION:
default:
break;
}
}
return message;
} catch (SocketTimeoutException e) {
return null;
} catch (SocketException e) {
if (isClosing || !isConnected) {
return null;
}
if (reconnectCount > 0) {
if (receiveWarnMessageId != null) {
ServiceManagerFactory.getLogger().write(receiveWarnMessageId, new Object[] { this }, e);
}
reconnect();
return receive();
} else {
close();
return null;
}
} catch (EOFException e) {
if (isClosing || !isConnected) {
return null;
}
if (reconnectCount > 0) {
if (receiveWarnMessageId != null) {
ServiceManagerFactory.getLogger().write(receiveWarnMessageId, new Object[] { this }, e);
}
reconnect();
return receive();
} else if (length == 0) {
close();
return null;
} else {
throw new MessageCommunicateException("length=" + length + ", receiveBytes=" + (receiveBytes == null ? "null" : Integer.toString(receiveBytes.length)), e);
}
} catch (IOException e) {
if (isClosing || !isConnected) {
return null;
}
throw new MessageCommunicateException(e);
} catch (ClassNotFoundException e) {
if (isClosing || !isConnected) {
return null;
}
throw new MessageCommunicateException(e);
}
}
Aggregations