public
Last active

Class responsible for generating an ADHOC Peer in a JXTA network.

  • Download Gist
ADHOCPeer.java
Java
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 136 137 138 139 140 141 142
import java.io.File;
import java.io.IOException;
 
import net.jxta.endpoint.EndpointAddress;
import net.jxta.endpoint.EndpointService;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.Messenger;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.id.IDFactory;
import net.jxta.impl.endpoint.netty.MessageArrivalListener;
import net.jxta.peer.PeerID;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.pipe.PipeService;
import net.jxta.platform.NetworkConfigurator;
import net.jxta.platform.NetworkManager;
 
public class ADHOCPeer implements MessageArrivalListener {
private String username;
private String tcpPort;
private File configFile;
 
public PeerID PID;
private PeerGroup netPeerGroup;
private NetworkManager netManager;
 
private EndpointService endpointService;
public ADHOCPeer(String username) {
this.username = username;
this.tcpPort = StringToTCPPortNumber.get(username);
this.configFile = new File( "." + System.getProperty("file.separator") +
"jxta" + System.getProperty("file.separator") +
username);
this.PID = getPIDforName(username);
}
private PeerID getPIDforName(String username) {
return IDFactory.newPeerID(PeerGroupID.defaultNetPeerGroupID, username.getBytes());
}
 
public void sendMessage(String username) throws IOException {
if ( this.canReach(username) ) {
System.out.println("OK : CAN REACH");
EndpointAddress addr = new EndpointAddress(getPIDforName(username).toURI());
Messenger messenger = endpointService.getMessenger(addr);
//TODO - extract message creation
Message msg = new Message();
StringMessageElement titleElement = new StringMessageElement("TITLE", "Hello from " + this.username, null);
StringMessageElement bodyElement = new StringMessageElement("BODY",
"Hi " + username + ", \n" +
"how are you ?"
, null);
msg.addMessageElement("MSG", titleElement);
msg.addMessageElement("MSG", bodyElement);
 
if ( messenger.sendMessage(msg) ) {
System.out.println("OK : MESSAGE SENT");
} else {
System.out.println("FAIL : MESSAGE NOT SENT");
}
} else {
System.out.println("FAIL: CANT REACH");
}
}
public ADHOCPeer start() {
try {
netManager = new NetworkManager(
NetworkManager.ConfigMode.ADHOC,
username,
configFile.toURI());
NetworkConfigurator netConfig = netManager.getConfigurator();
// Setting Configuration
netConfig.setUseMulticast(true);
netConfig.setTcpPort(Integer.parseInt(tcpPort));
netConfig.setTcpEnabled(true);
netConfig.setTcpIncoming(true);
netConfig.setTcpOutgoing(true);
netConfig.setPeerID(PID);
netConfig.save();
netPeerGroup = netManager.startNetwork();
 
endpointService = netPeerGroup.getEndpointService();
netPeerGroup.getRendezVousService().setAutoStart(false);
 
// Debug
System.out.println("Peer name: " + username);
System.out.println("PeerID :" + netPeerGroup.getPeerID());
System.out.println("Network started with ID :" + netPeerGroup.getPeerGroupID());
System.out.println("Listening on the port " + tcpPort.toString());
} catch (Exception e) {
e.printStackTrace();
}
return this;
}
 
 
public boolean canReach(String username) {
return endpointService.isReachable( getPIDforName(username), true);
}
 
@Override
public void connectionDied() {
System.out.println("Connection died");
stopWithError(1);
}
 
@Override
public void messageArrived(Message m) {
System.out.println("Message arrived");
System.out.println("Message : " + m.toString());
stopWithSuccess();
}
 
private void stopWithSuccess() {
this.netManager.stopNetwork();
System.exit(0);
}
private void stopWithError(int i) {
this.netManager.stopNetwork();
System.exit(i);
}
 
 
}
README
1 2 3 4 5 6 7 8 9 10 11 12 13
First start the class TestPeerFoo and then start the class TestPeerBar that will try to send a message to PeerFoo using it's name.
 
I'm having problems when sending the message, something is missing.
 
I get this error:
 
INFO: Line 136 net.jxta.impl.endpoint.netty.NettyTransportClient.getMessenger()
processing request to open connection to tcp://192.168.123.100:49825
28/01/2011 12:18:59 net.jxta.impl.endpoint.netty.NettyTransportClient getMessenger
INFO: succeeded in connecting to tcp://192.168.123.100:49825, remote peer has logical address jxta://uuid-59616261646162614E50472050325033506565724261420080008003
28/01/2011 12:18:59 net.jxta.logging.Logging logCheckedWarning
AVISO: Line 966 net.jxta.impl.endpoint.EndpointServiceImpl.processIncomingMessage()
dest serviceName must not be null, discarding message net.jxta.endpoint.Message@1159656515(8){2}
StringToTCPPortNumber.java
Java
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
public class StringToTCPPortNumber {
 
/**
* DUMB methods that returns a TCP port number in the range from 49152–65535 (dynamic, private or ephemeral ports).
*
* NUMBERS CAN COLLIDE.
*
* @param String - any string
* @return String - a "distinct" TCP Port Number
*/
public static final String get(String string) {
char[] localString = string.toCharArray();
final int MIN=49152;
final int MAX=65535;
int finalNumber = 0;
for (int i = 0; i < localString.length; i++) {
finalNumber += (int) localString[i];
}
 
finalNumber = MIN + finalNumber;
if ( finalNumber > MAX)
finalNumber = MAX - (finalNumber - MAX);
return String.valueOf(finalNumber);
}
}
TestPeerBar.java
Java
1 2 3 4 5 6 7 8 9
public class TestPeerBar {
public static void main(String[] args) {
try {
new ADHOCPeer("PeerBar").start().sendMessage("PeerFoo");
} catch (Exception e) {
e.printStackTrace();
}
}
}
TestPeerFoo.java
Java
1 2 3 4 5
public class TestPeerFoo {
public static void main(String[] args) {
new ADHOCPeer("PeerFoo").start();
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.