Universal Messaging Developer Guide : Enterprise APIs : Enterprise Developer's Guide for C++ : Peer to Peer : Peer to Peer Event-based Server Services
Peer to Peer Event-based Server Services
Universal Messaging Peer to Peer Event-based Services communicate via events which are published by an Event-based Client (see Peer to Peer Event-based Clients), 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:

std::string[] RNAME={"nsp://"};
nSessionAttributes *nsa = new nSessionAttributes(RNAME);
nServiceFactory *factory = new nServiceFactory( nsa );
The nServiceFactory object establishes a connection with the Universal Messaging 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();
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 %d",(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:

void receivedEvent(nConsumeEvent *evt) {
printf("Consumed event %d",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:

std::string strLine = "Hello World";
int length = 0;
unsigned char *pLine = nConstants::encode(strLine, length);
nEventProperties *pProps = new nEventProperties();
serv->write(new nConsumeEvent(pProps, pLine, length));
The code example "P2P Echo" source code shows how to implement an Event-based Server Service and Client.
Copyright © 2013-2016 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback