Home
Name Modified Size Downloads / Week Status
Totals: 8 Items   1.1 MB 5
Examples 2014-07-11 11 weekly downloads
NetworkService1.0.0.3 2014-07-06 11 weekly downloads
NS 2010-02-23 0
readme.txt 2011-12-25 4.2 kB 11 weekly downloads
NetworkService1.0.0.2.zip 2011-12-17 414.1 kB 11 weekly downloads
NetworkService1.0.0.1_2.zip 2010-11-26 233.2 kB 11 weekly downloads
NetworkService1.0.0.1_1.rar 2010-03-08 232.4 kB 11 weekly downloads
NetworkService1.0.0.1.rar 2010-02-24 233.5 kB 11 weekly downloads
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 (1.0.0.0.2) 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 (1.0.0.0.2) 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