ObjectQ - Overview - simServer.c

simServer_c.c

#include <Args.h>
#include <stdlib.h>
#include "cpTransMgr.h"
#include "cpEnvelope.h"
#include "simAgent.h"

void
error(const String& errMsg, cpStatus ret)
{
cerr << errMsg << " [" << (long)ret << "]" << endl;
}

void
usage(const String& prog)
{
cerr << "Usage: " << prog
<< " -s <service-name> -d <definition-directory>" << endl;
}

main(int argc, char **argv)
{
cpEnvelope env;
cpStatus ret;
cpResource res;
cpTransport *t;
String serviceName;
String defDir;
cpHandle handle;
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'));

if (serviceName.length() == 0) {
usage(argv[0]);
return 1;
}

// Initialize ObjectQ
if ((ret = cpInit(trLevel, defDir)) != cpSUCCESS) {
error("Initialization failed", ret);
return 2;
}

// Instantiate an agent
simAgent agent(serviceName);

// Connect to the queueing engine
if ((ret = cpXportMgr->registerPrimary(serviceName, handle)) != cpSUCCESS) {
error("Connection failed", ret);
return 3;
}

// Handle requests - wait for a message and dispatch it
for (;;) {
if ((t = cpXportMgr->transport(serviceName)) == 0) {
error("Get transport failed", cpFAIL);
return 4;
}

if ((ret = t->receive(cpWAIT, env)) != cpSUCCESS) {
error("Receive failed", ret);
return 5;
}

if ((ret = cpDispatch->dispatch(&env)) != cpSUCCESS) {
error("Dispatch failed", ret);
return 6;
}
}
}