Home / NS
Name Modified Size Downloads / Week Status
Parent folder
This folder has no files.
Totals: 0 Items  
public class BasicExampleHowToUseNetworkService { public static void main(String[] args) throws Exception { //create a new instance of NetworkService NetworkService ns = new NetworkService(new ClientInterfaceImpl()); //register a ClientStateChangeListener to recieve notifications //about group members state. ns.addClientStateChangeListener(new ClientStateChangeListenerImpl()); //start the service. ns.startService(false); Random gen = new Random(); Thread.sleep(gen.nextInt(1000)); //send a message to all group members. //(in this example we are using the default multicast group address) //create your serializable message MyTextMessage msg = new MyTextMessage("hello all!"); ClientID group = ns.getPublicClientID(); //wrap it in a Message Object Message publicMessage = new Message(null, msg, group); //for verson older than ( use ns.broadcast("hello all!"); ns.sendMessage(publicMessage); //send a message to one of the group members. ClientID agroupmember = ns.getClientsTable().values().iterator().next(); msg = new MyTextMessage("hello!"); Message privateMessage = new Message(null, msg, agroupmember); //for verson older than ( use ns.sendMessage("hello!",agroupmember); ns.sendMessage(privateMessage); Thread.sleep(gen.nextInt(1000)); msg = new MyTextMessage("bye all!"); publicMessage = new Message(null,msg, group); ns.sendMessage(publicMessage); //stop the service ns.stopService(); } static class ClientInterfaceImpl implements ClientInterface { public ClientInterfaceImpl() { } public void publicMessageReceived(String msg, ClientID from) { //deprecated } public void privateMessageReceived(String msg, ClientID from) { //deprecated } public void clientConnected(SocketStreamRW connection) { //accept TCP connection here. //See: NetworkService.connect(ClientID); } public void publicMessageReceived(Message msg) { String from = msg.getClientID().name(); System.out.println(from + "says:"); MyTextMessage theMsg = (MyTextMessage) msg.getContent(); theMsg.print(); } public void privateMessageReceived(Message msg) { String from = msg.getClientID().name(); System.out.println(from + "says:"); MyTextMessage theMsg = (MyTextMessage) msg.getContent(); theMsg.print(); } public void errMessage(String error, String from) { //used for debugging } public void infMessage(String info, String from) { //used for debugging } } private static class ClientStateChangeListenerImpl implements ClientStateChangeListener { public ClientStateChangeListenerImpl() { } public void clientStateChanged(ClientStateChangeEvent evt) { String name = evt.getClient().name(); String host = evt.getClient().hostName(); int state = evt.getNewState(); String s; switch (state) { case ClientStateChangeEvent.ON: s = "on"; break; case ClientStateChangeEvent.OFF: s = "off"; break; default: s = "unknown"; } System.out.println(name + " on " + host + " is " + s); } } private static class MyTextMessage implements Serializable { private String msg; public MyTextMessage(String msg) { this.msg = msg; } public void print() { System.out.println(msg); } } }
Source: readme.txt, updated 2011-12-25

Thanks for helping keep SourceForge clean.

Screenshot instructions:
Red Hat Linux   Ubuntu

Click URL instructions:
Right-click on ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)

More information about our ad policies

Briefly describe the problem (required):

Upload screenshot of ad (required):
Select a file, or drag & drop file here.

Please provide the ad click URL, if possible:

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks