|
||||||||||
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |
See:
Description
Interface Summary | |
---|---|
ProtocolMBean | Interface for a JMX management bean. |
Class Summary | |
---|---|
MulticastChannel | Channel interface to a NeEM epidemic multicast group. |
Protocol | Implementation of the NeEM management bean. |
Exception Summary | |
---|---|
BufferTooSmallException | Attempt to receive with a buffer too small. |
Provides a simple and safe interface to NeEM targeted at the application programmer. As an example, consider a simple group chat application. This application grabs text from console, and sends it to the group. All messages delivered are printed to the console. Step by step:
The first step is to create an instance of MulticastChannel
:
MulticastChannel neem = new MulticastChannel(new InetSocketAddress(port));
After the channel is created, peers are added to connect to the overlay network:
for(InetSocketAddress addr: knownPeers) neem.connect(addr);
Any number of peers can be connected. The channel will keep only the appropriate number of active connections. A single peer is sufficient to boot, as the dynamic overlay maintenance protocol will grow the number of local peers. Peers can be added at any time, for instance, to merge two partitions.
Our chat application must then start a thread to poll for incoming messages using the channel interface using the following code:
public void run() { try { while(true) { byte[] buf = new byte[1000]; ByteBuffer bb=ByteBuffer.wrap(buf); neem.read(bb); System.out.println(new String(buf)); } } catch(Exception e) {e.printStackTrace();} }
Finally, grab each text line from the console, encapsulate it in a ByteBuffer and use the write method to spread the message to the group:
BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = r.readLine()) != null) { neem.write(ByteBuffer.wrap(line.getBytes())); } neem.close();
|
||||||||||
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |