Universal Messaging 9.10 | Universal Messaging Developer Guide | Enterprise APIs | Enterprise Developer's Guide for C++ | Peer to Peer | Peer to Peer Stream-based Clients
 
Peer to Peer Stream-based Clients
Universal Messaging 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 a Stream-based 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");
nStreamService *serv = (nStreamService *)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.
Writing Client Data to a Stream-based Server Service
Once a client has connected to a Service, the client can write data to the Service. The client can obtain a reference to the Service's Output Stream object and then write to it as follows:

Stream *os = serv->getOutputStream();
//Read a character from the standard input until a \n is reached which indicates
// the end of a command.
int pos = 0;
char command[256];
try
{
bool run = true;
while (run)
{
command[pos] = getchar();

if (command[pos] == '\n')
{
if (pos > 0)
{
pos++;
unsigned char *temp = new unsigned char[pos];
memcpy (temp, command, pos);
os->write(temp, 0, pos);
os->flush();
pos = 0;
}
else
{
run = false;
}
}
else
{
pos++;
}
}
}
Receiving Responses from a Stream-based Server Service
To receive responses from the Service, the client must first obtain a reference to the Service's Input Stream object, and then read from it as follows:

Stream *is = serv->getInputStream();
unsigned char *pBuf = new unsigned char[bufferSize];
try
{
int size = 0;
while (true)
{
is->wait();
is->lock();
is->setPosition (0);
int i = 0;

while ((i = is->readByte()) != -1)
{
putchar((char)i);
size++;
}

is->unlock();
}
}


Copyright © 2013-2019 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.