|
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. |