Nirvana Java: Peer to Peer Event-based Clients

Nirvana Peer to Peer Event-based Services communicate via events which are published by a Client, and received and responded to by an Event-based Server Service.

The Nirvana P2P API is simple to use. There are only a very small number of objects and calls that need to be made in order for you to construct a P2P Service Client, connect to a Realm, and find or list available Services.

Creating an Event-based Service Client

The nServiceFactory object establishes a connection with the Nirvana Realm, and is the factory object from which we can find our Service, or obtain a list of available Services:

String[] RNAME=({"nsp://127.0.0.1:9000"});
nSessionAttributes nsa = new nSessionAttributes(RNAME);
nServiceFactory factory = new nServiceFactory( nsa );

nServiceInfo info = factory.findService("example");
nEventService serv = (nEventService)factory.connectToService( info );

Once the Client has connected to an instance of a Server Service, the developer's custom business logic can then be applied.

Sending Events to Server Services

Once you have connected to the Service, and you have an instance of the Service, you can then begin publishing your Nirvana events to the Service, by using the following command:

serv.write(new nConsumeEvent("TAG", message.getBytes()));

The Client Service can recieve events from the Server Service either synchronously, or asynchronously via a callback interface.

Synchronously Receiving Events from the Server Service

Clients can synchronously read incoming events. The following code will return an event once one is received from the Server Service:

nConsumeEvent event = serv.read();

Asynchronously Receiving Events from the Server Service

A Client may alternatively asynchronously receive events from the Event-based Server Service by implementing the nEventServiceListener interface and its receivedEvent method:

public void receivedEvent(nConsumeEvent evt) {
	System.out.println("Consumed event " + evt.getEventID());
}

You will also need to call registerListener(your_listener_class) on the nEventService object.

Examples

The following full example source code shows how to implement an Event-based Server Service and Client:

For more information on Nirvana Peer to Peer Services please see the API documentation.