testserver.c 2.28 KB
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

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

#include "test_handler.h"

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);
	TR_ConnEntryPoint  tcp_ep;
	TR_TcpSocket       tcp_ep_sock;
	TR_DatagramService udp_ep;
	TR_UdpSocket       udp_ep_sock;
	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);
	tcp_ep_sock  = TR_new(TR_TcpSocket, TR_logger, "0.0.0.0", 5678, 0);
	tcp_ep       = TR_new(TR_ConnEntryPoint, tcp_ep_sock, protocol);
	udp_ep_sock  = TR_new(TR_UdpSocket, TR_logger, "0.0.0.0", 5678, 0);
	TR_socketBind((TR_Socket)udp_ep_sock);
	TR_socketNonblock((TR_Socket)udp_ep_sock);
	udp_ep       = TR_new(TR_DatagramService, udp_ep_sock, protocol);

	TR_commManagerAddEndpoint(cmgr, (TR_CommEndPoint)tcp_ep);
	TR_commManagerAddEndpoint(cmgr, (TR_CommEndPoint)udp_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: