Search in sources :

Example 6 with TCPMessage

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;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) RequestID(edu.iu.dsc.tws.api.net.request.RequestID) TCPMessage(edu.iu.dsc.tws.common.net.tcp.TCPMessage)

Example 7 with TCPMessage

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;
}
Also used : TCPMessage(edu.iu.dsc.tws.common.net.tcp.TCPMessage) ByteBuffer(java.nio.ByteBuffer)

Example 8 with TCPMessage

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;
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) TCPMessage(edu.iu.dsc.tws.common.net.tcp.TCPMessage)

Aggregations

TCPMessage (edu.iu.dsc.tws.common.net.tcp.TCPMessage)8 ByteBuffer (java.nio.ByteBuffer)4 DataBuffer (edu.iu.dsc.tws.api.comms.packing.DataBuffer)2 RequestID (edu.iu.dsc.tws.api.net.request.RequestID)2 SocketChannel (java.nio.channels.SocketChannel)2 ArrayList (java.util.ArrayList)2 ChannelMessage (edu.iu.dsc.tws.api.comms.messaging.ChannelMessage)1 TCPChannel (edu.iu.dsc.tws.common.net.tcp.TCPChannel)1 Test (org.junit.Test)1