use of edu.iu.dsc.tws.common.net.tcp.TCPMessage in project twister2 by DSC-SPIDAL.
the class TWSTCPChannel method postReceive.
private void postReceive(TCPReceiveRequests requests) {
DataBuffer byteBuffer = requests.availableBuffers.poll();
if (byteBuffer != null) {
// post the receive
TCPMessage request = postReceive(requests.rank, requests.edge, byteBuffer);
requests.pendingRequests.add(new Request(request, byteBuffer));
}
}
use of edu.iu.dsc.tws.common.net.tcp.TCPMessage in project twister2 by DSC-SPIDAL.
the class TWSTCPChannel method postMessage.
/**
* Send a message to the given rank.
*
* @param requests the message
*/
private void postMessage(TCPSendRequests requests) {
ChannelMessage message = requests.message;
for (int i = 0; i < message.getNormalBuffers().size(); i++) {
sendCount++;
DataBuffer buffer = message.getNormalBuffers().get(i);
TCPMessage request = channel.iSend(buffer.getByteBuffer(), buffer.getSize(), requests.rank, message.getHeader().getEdge());
// register to the loop to make communicationProgress on the send
requests.pendingSends.add(new Request(request, buffer));
}
}
use of edu.iu.dsc.tws.common.net.tcp.TCPMessage in project twister2 by DSC-SPIDAL.
the class TCPChannelExample method sendMessagesTest.
/**
* send one tcp message to each worker and receive one tcp message from all workers
*/
public void sendMessagesTest() {
List<TCPMessage> sends = new ArrayList<>();
List<TCPMessage> receives = new ArrayList<>();
// first register receives
for (int i = 0; i < numberOfWorkers; i++) {
if (i == workerID) {
continue;
}
ByteBuffer buffer = ByteBuffer.allocate(128);
TCPMessage message = channel.iRecv(buffer, 10, i, 1);
receives.add(message);
}
// second register sends
for (int i = 0; i < numberOfWorkers; i++) {
if (i == workerID) {
continue;
}
String messageToSend = "hello: " + workerID;
byte[] toSend = messageToSend.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(toSend);
TCPMessage message = channel.iSend(buffer, toSend.length, i, 1);
sends.add(message);
}
// process sends and receives
for (int i = 0; i < numberOfWorkers * 3; i++) {
channel.progress();
}
long start = System.currentTimeMillis();
long nextLogTime = 5;
// process the channel if they have not completed yet
while (!sends.isEmpty() || !receives.isEmpty()) {
// check sends
Iterator<TCPMessage> tcpMessageIterator = sends.iterator();
while (tcpMessageIterator.hasNext()) {
TCPMessage tcpMessage = tcpMessageIterator.next();
if (tcpMessage.isComplete()) {
LOG.info("send completed: " + new String(tcpMessage.getByteBuffer().array()));
tcpMessageIterator.remove();
} else if (tcpMessage.isError()) {
String msg = new String(tcpMessage.getByteBuffer().array(), 0, tcpMessage.getLength());
LOG.info("xxx error on sending: " + msg);
tcpMessageIterator.remove();
}
}
// check receives
tcpMessageIterator = receives.iterator();
while (tcpMessageIterator.hasNext()) {
TCPMessage tcpMessage = tcpMessageIterator.next();
if (tcpMessage.isComplete()) {
String msg = new String(tcpMessage.getByteBuffer().array(), 0, tcpMessage.getLength());
LOG.info("receive completed: " + msg);
tcpMessageIterator.remove();
} else if (tcpMessage.isError()) {
String msg = new String(tcpMessage.getByteBuffer().array(), 0, tcpMessage.getLength());
LOG.info("xxx error on receiving: " + msg);
tcpMessageIterator.remove();
}
}
channel.progress();
long duration = (System.currentTimeMillis() - start) / 1000;
if (duration > nextLogTime) {
LOG.info("waiting messaging to complete for: " + duration + " seconds");
nextLogTime += 5;
}
}
}
use of edu.iu.dsc.tws.common.net.tcp.TCPMessage in project twister2 by DSC-SPIDAL.
the class RRClient method sendRequest.
public RequestID sendRequest(Message message) {
if (!client.isConnected()) {
return null;
}
String messageType = message.getDescriptorForType().getFullName();
if (!messageBuilders.containsKey(messageType)) {
throw new RuntimeException("Message without a message builder");
}
RequestID id = RequestID.generate();
byte[] data = message.toByteArray();
// lets serialize the message
int capacity = id.getId().length + data.length + 4 + messageType.getBytes().length + 4;
ByteBuffer buffer = ByteBuffer.allocate(capacity);
// we send message id, worker id and data
buffer.put(id.getId());
// pack the name of the message
ByteUtils.packString(messageType, buffer);
// pack the worker id
buffer.putInt(workerID);
// pack data
buffer.put(data);
TCPMessage request = client.send(channel, buffer, capacity, 0);
if (request != null) {
loop.wakeup();
return id;
} else {
return null;
}
}
use of edu.iu.dsc.tws.common.net.tcp.TCPMessage in project twister2 by DSC-SPIDAL.
the class TwoChannelTest method sendMessagesTest.
@Test
public void sendMessagesTest() {
List<TCPMessage> sends = new ArrayList<>();
List<TCPMessage> recvs = new ArrayList<>();
for (int i = 0; i < NO_OF_CHANNELS; i++) {
TCPChannel channel = channels.get(i);
for (int j = 0; j < NO_OF_CHANNELS; j++) {
if (j != i) {
ByteBuffer buffer = buffers.get(j);
buffer.clear();
TCPMessage message = channel.iRecv(buffer, 10, j, 1);
recvs.add(message);
}
}
}
for (int i = 0; i < NO_OF_CHANNELS; i++) {
TCPChannel channel = channels.get(i);
for (int j = 0; j < NO_OF_CHANNELS; j++) {
if (j != i) {
ByteBuffer buffer = buffers.get(j);
buffer.clear();
buffer.put(new byte[10]);
TCPMessage message = channel.iSend(buffer, 10, j, 1);
sends.add(message);
}
}
}
List<Integer> completedSends = new ArrayList<>();
List<Integer> completedRcvs = new ArrayList<>();
while (completedRcvs.size() != NO_OF_CHANNELS || completedSends.size() != NO_OF_CHANNELS) {
for (int i = 0; i < NO_OF_CHANNELS; i++) {
TCPMessage sendMsg = sends.get(i);
TCPMessage rcvMsg = recvs.get(i);
if (sendMsg.isComplete()) {
if (!completedSends.contains(i)) {
completedSends.add(i);
}
}
if (rcvMsg.isComplete()) {
if (!completedRcvs.contains(i)) {
completedRcvs.add(i);
}
}
TCPChannel ch = channels.get(i);
ch.progress();
}
}
}
Aggregations