This is archived documentation for an older version of Nirvana (v3.1). Please refer to documentation for the latest version if required.

Peer to Peer Server Services

What is a peer to peer server service

Nirvana peer to peer services consist of a server service and a clent service. The server service provides the ability to receive requests from clients that have connected to the server service and send responses. Requests are received via an instance of the service that uses either input / output streams or is event based.

The server service is a process that registers itself with a Nirvana Realm so it is visible for clients wishing to connect. When a client attempts to connect to the service, all communication between the client and server sevice is achieved through the Nirvana Realm, and so all communications is subject to the Nirvana communication protocols.

How do i create a 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 the service, connect to the realm and wait for connections. Once you have constructed these objects, it is entirely your logic that goes into what you do when somebody does connect to the 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 you 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 then being constructing our service, which is this instance is a stream service:

nServerService server = factory.createStreamService( "example",
"this service is an example" );
while ( true ) {

nStreamService serv = (nStreamService)server.accept();
/**
.your logic goes here....i.e. what does you server service do with the instance of nStreamService created once a connection is established?
does it go and query a database, make a connection send an email? etc etc.
*/
System.out.println("Got connection "+serv.getServiceInfo().getName());

}

Once the server service factory is created and it is waiting for connections, it's really all down to the devloper to decide what to do with the services when connections are made.

For more information on Nirvana p2p, please see the API documentation.