Peer to Peer Event-based Clients
Universal Messaging Peer to Peer Event-based Services communicate via events which are published by a Client, and received and responded to by an Event-based Server Service.
The Universal Messaging 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 a P2P Service Client, connect to a Realm, and find or list available Services.
Creating an Event-based Service Client
The nServiceFactory object establishes a connection with the Universal Messaging Realm, and is the factory object from which we can find our Service, or obtain a list of available Services:
std::string[] RNAME={"nsp://127.0.0.1:9000"};
nSessionAttributes *nsa = new nSessionAttributes(RNAME);
nServiceFactory *factory = new nServiceFactory( nsa );
nServiceInfo *info = factory->findService("example");
nEventService *serv = (nEventService)factory->connectToService( info );
Once the Client has connected to an instance of a Server Service, the developer's custom business logic can then be applied.
Sending Events to Server Services
Once you have connected to the Service, and you have an instance of the Service, you can then begin publishing your Universal Messaging events to the Service, by using the following command:
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));
To receive responses from the Server Service, the client Service can receive events either synchronously or asynchronously via a callback interface.
Synchronously Receiving Events from the Server Service
Clients can synchronously read incoming events. The following code will return an event once one is received from the Server Service:
nConsumeEvent *event = serv->read();
Asynchronously Receiving Events from the Server Service
A Client may alternatively asynchronously receive events from the Event-based Server Service 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.