1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| public class PeerA implements Runnable, DiscoveryListener ,OutputPipeListener, PipeMsgListener{
static PeerGroup netPeerGroup = null;
private DiscoveryService discovery;
private transient NetworkManager manager;
private PipeService pipeService;
private PipeAdvertisement pipeAdvIP,pipeAdvOP;
private InputPipe pipeIn = null;
private void startJxta() {
try {
manager = new NetworkManager(NetworkManager.ConfigMode.EDGE,
"peera", new File(new File(".cache"), "peera").toURI());
manager.startNetwork();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
//Get the NetPeerGroup
PeerGroup netPeerGroup = manager.getNetPeerGroup();
// get the discovery service
discovery = netPeerGroup.getDiscoveryService();
pipeService = netPeerGroup.getPipeService();
pipeAdvIP = (PipeAdvertisement)AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());
pipeAdvIP.setName("pipeAdvA");
pipeAdvIP.setPipeID((ID) IDFactory.newPipeID(netPeerGroup.getPeerGroupID()));
pipeAdvIP.setType(PipeService.UnicastType);
System.out.println("creation d'un input pipe");
try {
pipeIn = pipeService.createInputPipe(pipeAdvIP, this);
} catch (Exception e) {
System.out.println("erreur de creation de input pipe");
}
try {
discovery.publish(pipeAdvIP);
discovery.remotePublish(pipeAdvIP);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Erreur de publication");
}
}
public void run() {
try {
// Add ourselves as a DiscoveryListener for DiscoveryResponse events
discovery.addDiscoveryListener(this);
while (true) {
System.out.println("Sending a Discovery Message");
// look for any peer
discovery.getRemoteAdvertisements(null, DiscoveryService.ADV,
null, null,
5);
// wait a bit before sending next discovery message
try {
Thread.sleep(10 * 1000);
} catch(Exception e) {}
} //end while
} catch(Exception e) {
e.printStackTrace();
}
}
public void discoveryEvent(DiscoveryEvent ev) {
DiscoveryResponseMsg res = ev.getResponse();
System.out.println("un advertisment a ete recu");
try
{
FileWriter file = new FileWriter("src/pipeadv.xml");
MimeMediaType mimeType = new MimeMediaType("text/xml");
StructuredTextDocument document = (StructuredTextDocument) res.getDocument(mimeType);
// Output the XML for the advertisement to the file.
document.sendToWriter(file);
file.close();
}
catch (Exception e)
{e.printStackTrace();
}
try {
FileInputStream is = new FileInputStream("src/pipeadv.xml");
pipeAdvOP = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(new MimeMediaType("text/xml"), is);
is.close();
System.out.println("creation d'un OutputPipe***************");
pipeService.createOutputPipe(pipeAdvOP, this);
} catch (java.io.IOException e1) {
System.out.println("Echec de lecture de pipe advertisement***********");
e1.printStackTrace();
System.exit(-1);
}
}
public void outputPipeEvent(OutputPipeEvent event) {
System.out.println(" Got an output pipe event");
Message msg = null;
try {
OutputPipe op = event.getOutputPipe();
System.out.println("Sending message");
msg = new Message();
Date date = new Date(System.currentTimeMillis());
StringMessageElement sme = new StringMessageElement("pipeListenerMessage", date.toString() , null);
msg.addMessageElement(null, sme);
op.send(msg);
op.close();
} catch (IOException e) {
System.out.println("failed to send message");
e.printStackTrace();
System.exit(-1);
}
System.out.println("message sent");
}
public void pipeMsgEvent(PipeMsgEvent event) {
}
static public void main(String args[]) {
PeerA peera = new PeerA();
peera.startJxta();
peera.run();
}
} |
Partager