use of edu.iu.dsc.tws.common.net.tcp.TCPMessage in project twister2 by DSC-SPIDAL.
the class RRServer method sendMessage.
/**
* Send a non-response message to a worker or to the client
* @param message message
* @return true if response was accepted
*/
public boolean sendMessage(Message message, int targetID) {
SocketChannel channel;
if (targetID == CLIENT_ID) {
if (clientChannel == null) {
LOG.severe("Trying to send a message to the client, but it has not connected yet.");
return false;
}
channel = clientChannel;
} else if (workerChannels.containsValue(targetID)) {
channel = workerChannels.inverse().get(targetID);
} else {
LOG.severe("Trying to send a message to a worker that has not connected yet. workerID: " + targetID);
return false;
}
// this is most likely not needed, but just to make sure
if (channel == null) {
LOG.log(Level.SEVERE, "Channel is NULL for response");
return false;
}
// since this is not a request/response message, we put the dummy request id
RequestID dummyRequestID = RequestID.DUMMY_REQUEST_ID;
TCPMessage tcpMessage = sendMessage(message, dummyRequestID, channel);
return tcpMessage != null;
}
use of edu.iu.dsc.tws.common.net.tcp.TCPMessage in project twister2 by DSC-SPIDAL.
the class RRServer method sendMessage.
protected TCPMessage sendMessage(Message message, RequestID requestID, SocketChannel channel) {
byte[] data = message.toByteArray();
String messageType = message.getDescriptorForType().getFullName();
// lets serialize the message
int capacity = requestID.getId().length + data.length + messageType.getBytes().length + 8;
ByteBuffer buffer = ByteBuffer.allocate(capacity);
// we send message id, worker id and data
buffer.put(requestID.getId());
// pack the name of the message
ByteUtils.packString(messageType, buffer);
// pack the worker id
buffer.putInt(serverID);
// pack data
buffer.put(data);
TCPMessage send = server.send(channel, buffer, capacity, 0);
if (send != null) {
pendingSendCount++;
}
return send;
}
use of edu.iu.dsc.tws.common.net.tcp.TCPMessage in project twister2 by DSC-SPIDAL.
the class RRServer method sendResponse.
/**
* Send a response to a request id
* @param requestID request id
* @param message message
* @return true if response was accepted
*/
public boolean sendResponse(RequestID requestID, Message message) {
if (!requestChannels.containsKey(requestID)) {
LOG.log(Level.SEVERE, "Trying to send a response to non-existing request");
return false;
}
SocketChannel channel = requestChannels.get(requestID);
if (channel == null) {
LOG.log(Level.SEVERE, "Channel is NULL for response");
}
if (!workerChannels.containsKey(channel) && !channel.equals(clientChannel)) {
LOG.log(Level.WARNING, "Failed to send response on disconnected socket");
return false;
}
TCPMessage tcpMessage = sendMessage(message, requestID, channel);
if (tcpMessage != null) {
requestChannels.remove(requestID);
return true;
} else {
return false;
}
}
Aggregations