Connecting Apama Applications to External Components > Developing Custom Clients > Using the SDKs – C++ and Java Examples > A simple echo server in C++
A simple echo server in C++
Step 1 is to initialize the client-side library. This must be done only once during the lifetime of the client-side code.

com::apama::engine::engineInit();
Next, one must connect to a remote correlator. If the method fails an EngineException would be thrown.
EngineManagement* engine;
engine = com::apama::engine::connectToEngine(argv[1], atoi(argv[2]))
If the client code is to receive any events back from the event correlator it must register itself as an event receiver with the event correlator. In order to do this, the developer must create an object that inherits from EventConsumer, and implement its sendEvents method,
void receive_consumer::sendEvents(const Event* const * events) {
for (const Event* const * event=events; *event; event++) {
cout << *event << endl;
}
}
In this example, the developer-defined receive_consumer constructor is establishing the connection with the event correlator. The EventSupplier returned is a handle to private resources allocated by the event correlator to service this consumer. These resources handle event filtering for this connection. These must be freed by the developer, and in this example they are being freed by calling deleteEventSupplier() in the destructor for this class.
receive_consumer::receive_consumer(EngineManagement* engine)
: EventConsumer(), engine(engine), supplier(NULL)
{
supplier = engine->connectEventConsumer(this, NULL);
}
Once initialization is complete the example starts interacting with the event correlator. First it creates a MonitorScript object and defines an EPL monitor and associated event type within it,
MonitorScript* script = com::apama::engine::createMonitorScript(
"event TestEvent {" +
"string text;" +
"}" +
"monitor Echo {" +
"" +
"TestEvent test;" +
"" +
"action onload {" +
"on all TestEvent(*):test {" +
"emit TestEvent(test.text);" +
"}" +
"}" +
"}");
For a description of monitors and EPL syntax, see Getting Started with Apama EPL.
Then the EPL code is injected into the event correlator,
engine->injectMonitorScript(*script);
Finally, the now utilized MonitorScript object must be deleted. It is important to point out that the developer is responsible for calling this operation in order to free resources.
com::apama::engine::deleteMonitorScript(script);
The monitor is now active within the event correlator. To verify this, the example requests operational status from the event correlator. It then prints this out; after which it de-allocates the resources used by the EngineStatus object by calling deleteStatus.
EngineStatus* status = engine->getStatus();
cout << status << endl;
com::apama::engine::deleteStatus(status);
The next step is to send some events to the event correlator. The events here have been chosen to trigger the now active listener of the monitor injected earlier. Although events can only be represented as strings, nevertheless, the string format must still be equivalent to the TestEvent event type definition as contained within the Echo monitor injected into the event correlator earlier. Otherwise, the event instances will be rejected by the event correlator. In this example, the event type TestEvent only contains a single parameter of type string.
Event* events[3];
events[0] = com::apama::event::createEvent(
"TestEvent(\"Hello, World\")");
events[1] = com::apama::event::createEvent(
"TestEvent(\"Welcome to the Event Correlator\")");
events[2] = NULL;
engine->sendEvents(events);
com::apama::event::deleteEvent(events[0]);
com::apama::event::deleteEvent(events[1]);
Note that since sendEvents takes an array of Event references, the last element needs to be NULL. As before, the developer is responsible for deleting the Event objects after they have been passed to the event correlator.
If one wanted to pass in more complex events the procedure is identical. Consider the following event type definition in EPL,
event SharePrice {
integer price;
string companyName;}
This time the Event instance as passed to createEvent needs to look as in the following example,
Event* anEvent = com::apama::event::createEvent(
"SharePrice(25,\"ACME\")");
The Echo monitor’s active listener will be triggered by each of the TestEvent events passed into the event correlator. On every match it will execute the action specified; which is to create a new TestEvent containing a string parameter equivalent to the one matched upon. Clearly this is not very useful in practice, but here it serves as a good example.
The sendEvent method of the developer-defined receive_consumer should now have been invoked twice.
Finally, some cleanup is necessary before terminating.
First, disconnect and destroy the event consumer:
delete consumer;
then disconnect from the event correlator:
com::apama::engine::disconnectFromEngine(engine);
and finally clean up the client library:
com::apama::engine::engineShutdown();
Copyright © 2013-2015 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.
Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG.