Nirvana Java: Peer to Peer Event-based Server Services

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

Creating an Event-based Server Service

Firstly, in the same way that Publish/Subscribe and Message Queues use an RNAME, the P2P API also requires one to connect to the Realm. The code snippet below shows how this is achieved:

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

The nServiceFactory object establishes a connection with the Nirvana Realm, and is the factory object from which we can construct our Event-based Server Service:

nServerService Server = factory.createEventService( "example", "Example Event-based Service" );
while ( true ) {
	nEventService serv = (nEventService) server.accept();
	// your logic goes here....
	// e.g. query a database, make a connection, send an email, etc.
	System.out.println("Got connection " + serv.getServiceInfo().getName());
}

The code snippet above shows how to create an Event-based Server Service and wait for Client connections. Developers are free to decide how the Server Service should respond once a Client connects to the Server Service.

When connections are made to the Event-based Server Service, the Service can receive events from Clients either synchronously or asynchronously via a callback interface.

Synchronously Receiving Events from the Client

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

nConsumeEvent event = serv.read();

Asynchronously Receiving Events from the Client

The Server Service may alternatively asynchronously receive events by implementing the nEventServiceListener interface and its receivedEvent method:

public void receivedEvent(nConsumeEvent evt) {
	Console.WriteLine("Consumed event " + event.getEventID());
}

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

Sending Events to Clients

You can send events back to the Client as follows:

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

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.

Share this page with others:
Tell Your Tweets Facebook It! Add to Delicious Reddit! Digg It! Stumble Upon Add to Your Faves Mixx it
Follow Us:
Keep up with my-Channels on Twitter Become a fan on Facebook LinkedIn Profile Recent Highlights RSS Feed