ObjectQ - Overview - simClientAsynch.c

#include <Args.h>
#include <stdlib.h>
#include "cpDSAP.h"
#include "cpTransMgr.h"
#include "cpInstId.h"
#include "cpMessage.h"

#include "cpDispatch.h"
#include "simRequest.h"


cpStatus
poll(cpVendor vendor)
{
cpStatus ret;
cpTransport *xport;
cpEnvelope env;

// Get transport from Transport Manager
xport = cpXportMgr->transport(vendor);

// Process all messages on queue
while (1) {
ret = xport->receive(cpNOWAIT, env);
if (ret == cpFAIL) {
// Error
return cpFAIL;
} else if ((ret == cpSUCCESS) &&
(xport->lastRetCode() == cpNO_MESSAGES)) {
// No messages on the queue
return cpSUCCESS;
} else {
// Process message
ret = cpDispatch->dispatch(&env);
if (ret != cpSUCCESS)
return ret;
}
}
}

main(int argc, char **argv)
{
cpManagerResource resource;
cpHandle handle;
cpVendor vendor;
String serviceName;
String defDir;
int trLevel = -1;

// Get command line arguments
Args args(argc, argv, "s:d:x:");
if (args.isset('s')) serviceName = args.value('s');
if (args.isset('d')) defDir = args.value('d');
if (args.isset('x')) trLevel = atoi(args.value('x'));

// Initialize ObjectQ
cpInit(trLevel, defDir);

// Connect to the queueing engine
vendor = cpXportMgr->vendor(serviceName);
cpXportMgr->registerPrimary(vendor, handle);

// Instantiate a requestor for an asynchronous request
Requestor req(resource, serviceName);

// Issue the request
List<cpAttribute> attrList;
cpInstanceId id("Hello world!");

req.get("Simple.demoClass", attrList, id);

// Wait a while, then poll
sleep(10);
poll(vendor);


// Disconnect from the queueing engine
cpXportMgr->unregisterPrimary(vendor);

return 0;
}