Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JXTA communication between two peers ! Urgent please

user13122683Aug 15 2013

Hello,

I would like to make a communication between two peers through the exchange of messages by following the two classes described below and the file advertisement "pipe.adv" which I put it in the directory of the project. But when running the two programs, it still in blocking state and displays the following error (as shown below)!!

Please help me! Thank you in advance

------------------------------------------------------------------------

1) file pipe.adv

<!DOCTYPE jxta:pipeAdvertisement>

<jxta:pipeAdvertisement xmlns:jxta="http://jxta.org">

<Id>urn:jxta:uuid-59616261646162614E504720503250338944BCED387C4A2BBD8E9415B78C484104 </Id>

<Type>JxtaUnicast</Type>

<Name>ServerPipe tutorial</Name>

</jxta:pipeAdvertisement>

2) Class : JxtaServerPipeExample

import java.io.FileInputStream;

import net.jxta.logging.Logging;

import net.jxta.peergroup.PeerGroup;

import net.jxta.peergroup.PeerGroupFactory;

import net.jxta.exception.PeerGroupException;

import net.jxta.document.AdvertisementFactory;

import net.jxta.document.MimeMediaType;

import net.jxta.util.JxtaServerPipe;

import net.jxta.util.JxtaBiDiPipe;

import net.jxta.protocol.PipeAdvertisement;

import net.jxta.endpoint.Messenger;

import net.jxta.endpoint.StringMessageElement;

import net.jxta.endpoint.Message;

import java.util.Date;

import java.util.logging.Level;

import net.jxta.endpoint.MessageElement;

/**

* This example illustrates how to utilize the JxtaBiDiPipe Reads in pipe.adv and attempts to bind to a JxtaServerPipe */

public class JxtaServerPipeExample {

private PeerGroup netPeerGroup = null;

private PipeAdvertisement pipeAdv;

private JxtaServerPipe serverPipe;

private final static String SenderMessage = "pipe_tutorial";

public static void main(String args[]) {

System.setProperty(Logging.JXTA_LOGGING_PROPERTY, Level.OFF.toString());

JxtaServerPipeExample eg = new JxtaServerPipeExample();

eg.startJxta();

System.out.println("Reading in pipe.adv");

try {

FileInputStream is = new FileInputStream("pipe.adv");

eg.pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is);

is.close();

eg.serverPipe = new JxtaServerPipe(eg.netPeerGroup, eg.pipeAdv);

// we want to block until a connection is established

eg.serverPipe.setPipeTimeout(0);

} catch (Exception e) {

System.out.println("failed to read/parse pipe advertisement");

e.printStackTrace();

System.exit(-1);

}

// run on this thread

eg.run();

}

private void receiveAndSendTestMessage (JxtaBiDiPipe pipe) {

try {

Message msg = pipe.getMessage(120000);

// get the message element named SenderMessage

MessageElement msgElement = msg.getMessageElement(null, SenderMessage);

// Get message

if (msgElement.toString() == null) {

System.out.println("null msg received");

} else {

Date date = new Date(System.currentTimeMillis());

System.out.println("Message received at :"+ date.toString());

System.out.println("Message created at :"+ msgElement.toString());

}

// get the messenger

Messenger out = (Messenger) pipe.getMessageListener();

msg = new Message();

msg.addMessageElement(null,

new StringMessageElement(SenderMessage,

"Hello World",

null));

System.out.println("Sending back 'Hello World'");

out.sendMessage(msg);

} catch (Exception ie) {

ie.printStackTrace();

}

}

/* wait for msgs */

public void run() {

System.out.println("starting ServerPipe");

while (true) {

try {

JxtaBiDiPipe bipipe = serverPipe.accept();

if (bipipe != null ) {

System.out.println("BiDi Pipe created");

receiveAndSendTestMessage(bipipe);

}

} catch (Exception e) {

e.printStackTrace();

return;

}}}

/* Starts jxta */

private void startJxta() {

try {

// create, and Start the default jxta NetPeerGroup

netPeerGroup = PeerGroupFactory.newNetPeerGroup();

} catch (PeerGroupException e) {

// could not instanciate the group, print the stack and exit

System.out.println("fatal error : group creation failure");

e.printStackTrace();

System.exit(1);

}}}

------------------------------------------------------------------------

3) Class: JxtaBidiPipeExample

import net.jxta.document.AdvertisementFactory;

import net.jxta.document.MimeMediaType;

import net.jxta.exception.PeerGroupException;

import java.io.FileInputStream;

import java.util.Date;

import net.jxta.logging.Logging;

import net.jxta.peergroup.PeerGroup;

import net.jxta.peergroup.PeerGroupFactory;

import net.jxta.protocol.PipeAdvertisement;

import net.jxta.util.JxtaBiDiPipe;

import net.jxta.endpoint.Message;

import net.jxta.endpoint.Messenger;

import net.jxta.endpoint.StringMessageElement;

import net.jxta.endpoint.MessageElement;

import net.jxta.pipe.PipeMsgEvent;

import net.jxta.pipe.PipeMsgListener;

import org.apache.log4j.Level;

import org.apache.log4j.Logger;

/* This example illustrates how to utilize the JxtaBiDiPipe Reads in pipe.adv

* and attempts to bind to a JxtaServerPipe*/

public class JxtaBidiPipeExample implements PipeMsgListener {

private PeerGroup netPeerGroup = null;

private PipeAdvertisement pipeAdv;

private JxtaBiDiPipe pipe;

private final static String SenderMessage = "pipe_tutorial";

private final static Logger LOG = Logger.getLogger(JxtaBidiPipeExample.class.getName());

/* wait for msgs */

public void run() {

try {

Messenger msgr = (Messenger) pipe.getMessageListener();

Message msg = new Message();

Date date = new Date(System.currentTimeMillis());

StringMessageElement sme = new StringMessageElement(SenderMessage, date.toString(), null);

msg.addMessageElement(null, sme);

System.out.println("Sending a message");

msgr.sendMessage(msg);

} catch (Exception e) {

e.printStackTrace();

return;

}

}

/*Starts jxta*/

private void startJxta() {

try {

// create, and Start the default jxta NetPeerGroup

netPeerGroup = PeerGroupFactory.newNetPeerGroup();

} catch (PeerGroupException e) {

// could not instanciate the group, print the stack and exit

System.out.println("fatal error : group creation failure");

e.printStackTrace();

System.exit(1);

}

}

/* when we get a message, extract the print message, push it onto the queue */

public void pipeMsgEvent(PipeMsgEvent event) {

Message msg = null;

try {

// grab the message from the event

msg = event.getMessage();

if (msg == null) {

System.out.println("Received an empty message, returning");

return;

}

System.out.println("Received a response :"+msg);

// get the message element named SenderMessage

MessageElement msgElement = msg.getMessageElement(null, SenderMessage);

// Get message

if (msgElement.toString() == null) {

System.out.println("null msg received " );

} else {

Date date = new Date(System.currentTimeMillis());

System.out.println("Message received at :"+ date.toString());

System.out.println("Message : "+ msgElement.toString());

}

} catch (Exception e) {

e.printStackTrace();

return;

}

}

public static void main(String args[]) {

System.setProperty(Logging.JXTA_LOGGING_PROPERTY, Level.OFF.toString());

JxtaBidiPipeExample eg = new JxtaBidiPipeExample();

eg.startJxta();

System.out.println("reading in pipe.adv");

try {

FileInputStream is = new FileInputStream("pipe.adv");

eg.pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is);

is.close();

System.out.println("creating the BiDi pipe");

eg.pipe = new JxtaBiDiPipe();

eg.pipe.connect(eg.netPeerGroup,null,eg.pipeAdv,180000,

// register as a message listener

eg);

} catch (Exception e) {

System.out.println("failed to read/parse pipe advertisement");

e.printStackTrace();

System.exit(-1);

}

//Run the example

eg.run();

}

}

------------------------------------------------------------------------

After running two classes, the two programs are in a state of blockade, and I only received one error message in the class "JxtaBidiPipeExample"

- Console of the "JxtaBidiPipeExample" class

reading in pipe.adv

creating the BiDi pipe

failed to read/parse pipe advertisement

java.io.IOException: connection timeout

at net.jxta.util.JxtaBiDiPipe.connect(JxtaBiDiPipe.java:362)

at net.jxta.util.JxtaBiDiPipe.connect(JxtaBiDiPipe.java:300)

at TestWael.JxtaBidiPipeExample.main(JxtaBidiPipeExample.java:124)

- Console of theĀ  "JxtaServerPipeExample" class

Reading in pipe.adv

starting ServerPipe

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 12 2013
Added on Aug 15 2013
0 comments
275 views