Skip to main content

JXTA communication entre 2 peers ! Urgent SVP

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
sellamiwael
Offline
Joined: 2013-08-01

Bonjour,
Je voudrais effectuer une communication entre 2 peers par l'echange de messages en suivant les deux classes decrit ci-dessous et le fichier d'advertisement "pipe.adv" que je l'ai mis dans le repertoire du projet. Mais lors de l'exécution, les deux programmes restent en état bloquant et affiche par la suite une erreur (comme c'est indiqué ci-dessous)!!!!

Je vous en prie à m'aider ! Merci d'avance

------------------------------------------------------------------------
1) fichier pipe.adv



urn:jxta:uuid-59616261646162614E504720503250338944BCED387C4A2BBD8E9415B78C484104
JxtaUnicast
ServerPipe tutorial

2) La classe : 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) La classe : 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();
}
}

------------------------------------------------------------------------
Après l'exécution de deux classes, le deux programmes restent en etat de blocage et je n'ai reçu qu'un seul message erreur dans la classe "JxtaBidiPipeExample"

- Console de la classe "JxtaBidiPipeExample":

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 de la classe "JxtaServerPipeExample":

Reading in pipe.adv
starting ServerPipe