Home    
 
Products Solutions Developers Nirvana Nirvana documentation Nirvana examples Nirvana FAQ Nirvana Archive Matrix Partners Download Company Contact
 
 

Peer to Peer Stream Based Services

home > developers > nirvana

What is a a stream based service

Nirvana peer to peer stream based services communicate via input and output streams on bothe the client and server service. Anything written to the output stream of the client stream service is received via the input stream of the server stream service and vice versa. This section will show you how to :

Create A Stream Based Server Service

Create A Stream Based Client Service

How do i create a stream 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 you how this is achieved :

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

Once the service factory is created, and we are connected to the realm, we can create the stream server 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());

}

When connections are made to the stream service, the service instance created by the server service contains both and input and an output stream, which can be written to and read from.

The input stream represents data coming from the client service, the output stream represents data going to the client service. To obtain the input stream of the server service instance, use the following:

InputStream is = serv.getInputStream();

To obtain the output stream of the server service instance, use the following:

OutputStream os = serv.getOutputStream();

How do i create a stream based Client Service

The nServiceFactory object establishes a connection with the nirvana realm, and is the factory object from which we can then find our service, or obtain a list of available services:

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

Once you have connected to the service, and you have an instance of the service, you can then begin writing data to the stream service. To do this you must first obtain the output stream of the service by using the following command:

OutputStream os = serv.getOutputStream();

And then write the data you wish to write to the output stream:

os.write("Hello".getBytes());
os.flush();

To receive responses from the server service, the client service needs to read from the input stream of the stream service. To do this you must first obtain the reference to the input stream:

InputStream is = serv.getInputStream();

And then read from the stream:

byte[] buff = new byte[ 100 ];
try {

InputStream is = serv.getInputStream();
while ( true ) {

is.read( buff );
System.out.println("Read "+new String(buff));

}

}
catch ( Exception ex ) {
}

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

 
   
© 2001-2008 my-channels.com. (Terms of Use)