Communication protocol handler for the taskrambler framework. This does not contain specific protocol implementations but the abstract code to handle them.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

197 lines
4.7 KiB

#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "trbase.h"
#include "trcomm.h"
#include "trevent.h"
TR_CLASS(TestHandler) {
TR_EXTENDS(TR_EventHandler);
unsigned long long handled;
};
TR_INSTANCE_INIT(TestHandler);
TR_CLASSVARS_DECL(TestHandler) {
TR_CV_EXTENDS(TR_EventHandler);
};
static
TR_EventDone
testHandlerHeartbeat(TR_EventHandler this, TR_Event event)
{
printf("handled: %llu/s\n", ((TestHandler)this)->handled);
((TestHandler)this)->handled = 0;
return TR_EVENT_DONE;
}
static
TR_EventDone
testHandlerNewMessage(TR_EventHandler this, TR_Event event)
{
TR_ProtoMessageRaw msg = event->data;
TR_SizedData data = (TR_SizedData)msg->data;
char buf[data->size + 1];
int i;
((TestHandler)this)->handled++;
memcpy(buf, data->data, data->size);
buf[data->size] = 0;
for (i = 0; buf[i]; i++) {
if (! isprint(buf[i])) buf[i] = '.';
}
printf("echo message: %s(%zd)\n", buf, data->size);
TR_eventHandlerIssueEvent(
(TR_EventHandler)this,
TR_eventSubjectEmit(
event->subject,
TR_CEP_EVENT_SEND_MSG,
event->data));
return TR_EVENT_DONE;
}
static
TR_EventDone
testHandlerClose(TR_EventHandler this, TR_Event event)
{
puts("close");
return TR_EVENT_PENDING;
}
static
TR_EventDone
testHandlerUpgrade(TR_EventHandler this, TR_Event event)
{
printf("upgrade: %"PRIdPTR"\n", event->id);
return TR_EVENT_PENDING;
}
static
int
testHandlerCtor(void * _this, va_list * params)
{
TR_PARENTCALL(TestHandler, _this, TR_Class, ctor, params);
((TestHandler)_this)->handled = 0;
return 0;
}
static
void
testHandlerDtor(void * _this, va_list * params)
{
TR_PARENTCALL(TestHandler, _this, TR_Class, dtor);
}
static
void
testHandlerCvInit(TR_class_ptr class)
{
TR_EVENT_HANDLER_SET_METHOD(
class,
TR_EventDispatcher,
TR_DISPATCHER_EVENT_HEARTBEAT,
testHandlerHeartbeat);
TR_EVENT_HANDLER_SET_METHOD(
class,
TR_CommEndPoint,
TR_CEP_EVENT_NEW_MSG,
testHandlerNewMessage);
TR_EVENT_HANDLER_SET_METHOD(
class,
TR_CommEndPoint,
TR_CEP_EVENT_CLOSE,
testHandlerClose);
TR_EVENT_HANDLER_SET_METHOD(
class,
TR_CommEndPoint,
TR_CEP_EVENT_UPGRADE,
testHandlerUpgrade);
}
TR_INSTANCE(TR_Hash, testHandlerEventMethods);
TR_INIT_IFACE(TR_Class, testHandlerCtor, testHandlerDtor, NULL);
TR_CREATE_CLASS(
TestHandler,
TR_EventHandler,
testHandlerCvInit,
TR_IF(TR_Class)) = {
{ &(_testHandlerEventMethods.data) }
};
TR_INSTANCE(TR_LoggerSyslog, mylogger, {TR_LOGGER_DEBUG});
int
main (int argc, char * argv[])
{
TR_CommManager cmgr = (TR_CommManager)TR_new(TR_CommManagerPoll);
TR_EventDispatcher dispatcher = TR_new(TR_EventDispatcher);
TR_Connector connector = TR_new(TR_Connector);
TR_IoHandler io_handler = TR_new(TR_IoHandler);
TR_ProtocolHandler protocol_handler = TR_new(TR_ProtocolHandler);
TestHandler test_handler = TR_new(TestHandler);
#if 0
TR_ConnEntryPoint ep;
TR_TcpSocket ep_sock;
#else
TR_DatagramService ep;
TR_UdpSocket ep_sock;
#endif
TR_Protocol protocol;
TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger);
TR_eventDispatcherRegisterHandler(dispatcher, (TR_EventHandler)cmgr);
TR_eventDispatcherRegisterHandler(dispatcher, (TR_EventHandler)connector);
TR_eventDispatcherRegisterHandler(dispatcher, (TR_EventHandler)io_handler);
TR_eventDispatcherRegisterHandler(
dispatcher,
(TR_EventHandler)protocol_handler);
TR_eventDispatcherRegisterHandler(
dispatcher,
(TR_EventHandler)test_handler);
protocol = TR_new(TR_ProtocolRaw);
#if 0
ep_sock = TR_new(TR_TcpSocket, TR_logger, "0.0.0.0", 5678, 0);
ep = TR_new(TR_ConnEntryPoint, ep_sock, protocol);
#else
ep_sock = TR_new(TR_UdpSocket, TR_logger, "0.0.0.0", 5678, 0);
TR_socketBind((TR_Socket)ep_sock);
TR_socketNonblock((TR_Socket)ep_sock);
ep = TR_new(TR_DatagramService, ep_sock, protocol);
#endif
TR_commManagerAddEndpoint(cmgr, (TR_CommEndPoint)ep);
TR_eventDispatcherSetHeartbeat(dispatcher, 1000);
TR_eventDispatcherStart(dispatcher);
puts("cleanup...");
TR_delete(cmgr);
TR_delete(dispatcher);
TR_delete(connector);
TR_delete(io_handler);
TR_delete(protocol_handler);
TR_delete(test_handler);
TR_delete(protocol);
//TR_delete(ep);
TR_eventHandlerClassCleanup(TestHandler);
TR_eventHandlerClassCleanup(TR_ProtocolHandler);
TR_eventHandlerClassCleanup(TR_IoHandler);
TR_eventHandlerClassCleanup(TR_Connector);
TR_eventHandlerClassCleanup(TR_CommManagerPoll);
TR_cleanup();
return 0;
}
// vim: set ts=4 sw=4: