Skip to main content

JxtaBiDiPipe does not read all the messages

1 reply [Last post]
iliasr
Offline
Joined: 2009-04-09
Points: 0

Hello everyone,

there are two peers, who need to communicate through a BiDiPipe. The one reads data from a socket and sends them back to the other inside messages over the pipe. The strange thing is that while the sender sends all the messages successfully, the one who receives them cannot receive but the first one of the series...i have done all the required waiting, but the PipeMsgEvent handler seems to be invoked only once... i quote my code here as minimal as possible, and hopefully someone can help!

The sender waits on the pipe.accept, then spawns a thread which performs the following:
-----
int bytes;
byte[] data = new byte[32768];
Message msg = new Message();

while ((bytes = sockInStream.read(data)) > 0) {
msg.addMessageElement(new StringMessageElement("Response", new String(data, 0, bytes), null));
boolean ok = pipe.sendMessage(msg);
}
msg.addMessageElement(new StringMessageElement("Response","EOFSEQUENCE", null));
boolean ok = pipe.sendMessage(msg);
-----

The receiver
-----
pipe.connect(JxtaNode.getInstance().getNetPeerGroup(), null, pipeAdv, 60000, this);
while (true) {
synchronized (receipt) {
receipt.wait();
}

System.out.println(msgReceived);

if (msgReceived.endsWith("EOFSEQUENCE"))
break;
}
-----

and the pipeMsgEvent method:
-----
public void pipeMsgEvent(PipeMsgEvent event) {
Message msg = event.getMessage();
if (msg == null)
return;
//class variable, visible from the above code segment
msgReceived = msg.getMessageElement(null, "Response").toString();
synchronized(receipt) {
receipt.notify();
}
}
-----

note that a message "EOFSEQUENCE" is sent to indicate the end of the stream.
Every message from the sender is sent according to variable "ok", but then the receiver's handler only prints once... I am using the wait-notify pair because the receiver lies inside a thread and i do not want it to end before all the msgs are received...

Thanks in advance for your time!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
iliasr
Offline
Joined: 2009-04-09
Points: 0

I solved it myself... :)

before sending every message, one needs to clear it:

msg.clear();

or

msg = new Message();

it was simple as that...sorry for the unnecessary post!