commore  1.0.6-SNAPSHOT
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
/examples/coding/Listener.cpp

Communication listener object To create a listener use the AutoRef factory and the create(const AString& spec) method. The first spec field (':' sparated) must contain the type name of implementation. Current avalaible implementation type in NtoolBox are: "SHM" for shared memory communication (restricted to interprocess) "TCPIP" for TCPIP communications "SHD" for shared directory communication The second field must contain the specification of the connection

SHM : the name of the shared memeory object

TCPIP : the "<IP NUMBER OR HOST NAME>:<PORT NUMBER>", both field can be empty. In this case the listener get dynamicaly a free port number on all network adapter

//
// Copyright (c) 2006-2014 Raphael David / CANTOR
//
#include "commore/Tuple.h"
#include "commore/Error.h"
#include <stdio.h>
CMR_MODULE_DECLARE("EXAMPLE_LISTENER");
using namespace std;
using namespace commore;
//
// Simple callback
//
struct TestCallback : public Listener::Callback
{
int count;
TestCallback() : count(0) {}
//
// The call implementation
//
int call(const Tuple& in_req, Tuple& out_req)
{
int r = 0;
CMR_INFO("Callback count=%0") << in_req.get_int("count");
out_req.set_int("count", count);
count++;
return r;
}
};
//
// This is an example of how to use the Listener class.
//
int main(int argc, const char* argv[])
{
int r= 0;
AString address = "TCPIP:localhost:6666";
init();
ConsoleLogHook console_log_hook(true);
//GlobVarLog::set_default(10);
CMR_INFO("Start example Listener");
TestCallback cb;
PListener listener;
listener.create(address);
if (listener)
{
//
// Set the callback listener
//
listener->set_callback(cb);
CMR_INFO("Address:%0") << listener->get_address();
int c = getchar();
//
// Wait '.' is typed on the keyboard
//
while (c != '.')
{
c = getchar();
}
} else {
CMR_ERROR("Cannot bind listener to %0") << address;
}
CMR_INFO("Stop example Listener");
return r;
}