testserver.c 3.06 KB
#include <stdio.h>

#include "trbase.h"
#include "trcomm.h"
#include "trevent.h"

TR_CLASS(TestHandler) {
	TR_EXTENDS(TR_EventHandler);
};
TR_INSTANCE_INIT(TestHandler);
TR_CLASSVARS_DECL(TestHandler) {
	TR_CV_EXTENDS(TR_EventHandler);
};

static
int
testHandlerHeartbeat(TR_EventHandler this, TR_Event event)
{   
    puts("heartbeat");
	return FALSE;
}   

static
int
testHandlerNewMessage(TR_EventHandler this, TR_Event event)
{   
    puts("new message");
	return FALSE;
}   

static
int
testHandlerClose(TR_EventHandler this, TR_Event event)
{   
    puts("close");
	return FALSE;
}   

static
int
testHandlerUpgrade(TR_EventHandler this, TR_Event event)
{
    puts("upgrade");
	return FALSE;
}

static
int
testHandlerCtor(void * _this, va_list * params)
{
	TR_PARENTCALL(TestHandler, _this, TR_Class, ctor, params);
	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_EventDispatcher,
            TR_CEP_EVENT_CLOSE,
            testHandlerClose);
    TR_EVENT_HANDLER_SET_METHOD(
            class,
            TR_EventDispatcher,
            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_ConnEntryPoint  ep;
	TR_TcpSocket       ep_sock;
	TR_Protocol        protocol;

	TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger);

	TR_eventDispatcherRegisterHandler(dispatcher, (TR_EventHandler)cmgr);
	TR_eventDispatcherRegisterHandler(dispatcher,
			(TR_EventHandler)TR_new(TR_Connector));
	TR_eventDispatcherRegisterHandler(dispatcher,
			(TR_EventHandler)TR_new(TR_IoHandler));
	TR_eventDispatcherRegisterHandler(dispatcher,
			(TR_EventHandler)TR_new(TR_ProtocolHandler));
	TR_eventDispatcherRegisterHandler(dispatcher,
			(TR_EventHandler)TR_new(TestHandler));

	protocol = TR_new(TR_ProtocolRaw);
	ep_sock  = TR_new(TR_TcpSocket, TR_logger, "0.0.0.0", 5678, 0);
	ep       = TR_new(TR_ConnEntryPoint, ep_sock, protocol);

	TR_commManagerAddEndpoint(cmgr, (TR_CommEndPoint)ep);

	TR_eventDispatcherSetHeartbeat(dispatcher, 1000);
	TR_eventDispatcherStart(dispatcher);
	TR_eventHandlerClassCleanup(TestHandler);

	TR_delete(cmgr);
	TR_delete(dispatcher);

	return 0;
}

// vim: set ts=4 sw=4: