Created
January 28, 2011 15:14
-
-
Save endersonmaia/800368 to your computer and use it in GitHub Desktop.
Class responsible for generating an ADHOC Peer in a JXTA network.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestPeerBar { | |
public static void main(String[] args) { | |
try { | |
new ADHOCPeer("PeerBar").start().sendMessage("PeerFoo"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestPeerFoo { | |
public static void main(String[] args) { | |
new ADHOCPeer("PeerFoo").start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment