Nirvana C++: Peer to Peer Stream-based Server Services

Nirvana Peer to Peer Stream-based Services communicate via input and output streams on both the Stream-based Client and the Stream-based Server Service.

Anything written to the output stream of the Stream-based Service Client is received via the input stream of the Stream-based Server Service and vice versa.

Creating an 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 how this is achieved:

std::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 Stream-based Server Service:

nServerService *Server = factory->createStreamService( "example", "Example Stream-based Service" );
while ( true ) {
	nEventService *serv = (nEventService) server->accept();
	Stream *inputstream = serv->getInputStream();
	Stream *outputstream = serv->getOutputStream();
	// your logic goes here....
	// e.g. query a database, make a connection, send an email, etc.
	printf("Got connection %s",(serv->getServiceInfo())->getName());
}

The code snippet above shows how to create an Stream-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 a connection is made to the Stream-based Server Service, the Service has an Input Stream (which can be read from), and an Output Stream (which can be written to).

Receiving Data from a Stream-based Client

The Server Service's Input Stream represents data coming from the client. The following code snippet shows how to obtain this Input Stream:

Stream *iStream = serv->getInputStream();

Sending Data to a Stream-based Client

The Server Service's Output Stream represents data going to the client. The following code snippet shows how to obtain this Output Stream:

Stream *oStream = serv->getOutputStream();

Examples

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

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