Browse Source

Some fixes and adaptations on changes trevent code. Add a simple echo client.

1.0.0
Georg Hopp 11 years ago
parent
commit
42d139f31e
  1. 1
      .gitignore
  2. 1
      include/Makefile.am
  3. 2
      include/tr/interface/comm_manager.h
  4. 2
      include/tr/server.h
  5. 70
      include/tr/simple_client.h
  6. 1
      include/trcomm.h
  7. 2
      src/Makefile.am
  8. 15
      src/comm_manager.c
  9. 2
      src/comm_manager_epoll.c
  10. 3
      src/comm_manager_poll.c
  11. 12
      src/i_comm_manager.c
  12. 2
      src/server.c
  13. 4
      src/server_start.c
  14. 160
      src/simple_client.c
  15. 50
      src/simple_client_issue.c
  16. 1
      testers/build.sh
  17. 59
      testers/testclient.c

1
.gitignore

@ -41,3 +41,4 @@ test-driver
tags
/trcomm.h*
/testers/testserver*
/testers/testclient*

1
include/Makefile.am

@ -15,6 +15,7 @@ nobase_include_HEADERS = trcomm.h \
tr/protocol/message_raw.h \
tr/protocol_handler.h \
tr/server.h \
tr/simple_client.h \
tr/interface/comm_end_point.h \
tr/interface/comm_manager.h \
tr/interface/protocol.h

2
include/tr/interface/comm_manager.h

@ -31,7 +31,7 @@
#include "tr/comm_end_point.h"
typedef TR_EventDone (* fptr_TR_commManagerAddEndpoint)(void *, TR_CommEndPoint);
typedef TR_EventDone (* fptr_TR_commManagerSelect)(void *, TR_Event, int);
typedef TR_EventDone (* fptr_TR_commManagerSelect)(void *, TR_Event, unsigned long);
typedef TR_EventDone (* fptr_TR_commManagerPollWrite)(void *, TR_Event);
typedef TR_EventDone (* fptr_TR_commManagerPollRead)(void *, TR_Event);
typedef TR_EventDone (* fptr_TR_commManagerDisableWrite)(void *, TR_Event);

2
include/tr/server.h

@ -53,7 +53,7 @@ TR_CLASSVARS_DECL(TR_Server) {};
void TR_serverBindTcp(TR_Server, const char *, int, TR_Protocol);
void TR_serverBindUdp(TR_Server, const char *, int, TR_Protocol);
void TR_serverStart(TR_Server, int);
void TR_serverStart(TR_Server, unsigned long);
#define TR_serverClassCleanup() \
TR_eventHandlerClassCleanup(TR_ProtocolHandler); \

70
include/tr/simple_client.h

@ -0,0 +1,70 @@
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2014 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TR_SIMPLE_CLIENT_H__
#define __TR_SIMPLE_CLIENT_H__
#include <sys/types.h>
#include <stdint.h>
#include "trbase.h"
#include "trevent.h"
#include "tr/comm_manager.h"
#include "tr/comm_end_point.h"
#include "tr/io_handler.h"
#include "tr/protocol_handler.h"
#include "tr/protocol.h"
#include "tr/proto_message.h"
#include "tr/interface/comm_manager.h"
TR_CLASS(TR_SimpleClient) {
TR_EXTENDS(TR_EventHandler);
TR_CommEndPoint endpoint;
TR_CommManager comm_manager;
TR_ProtoMessage request;
TR_ProtoMessage response;
TR_IoHandler io_handler;
TR_ProtocolHandler protocol_handler;
TR_EventDispatcher dispatcher;
int send_issued;
TR_Timer timer;
};
TR_INSTANCE_INIT(TR_SimpleClient);
TR_CLASSVARS_DECL(TR_SimpleClient) {
TR_CV_EXTENDS(TR_EventHandler);
};
TR_ProtoMessage TR_simpleClientIssue(
TR_SimpleClient, TR_ProtoMessage, unsigned long);
#define TR_simpleClientClassCleanup() \
TR_eventHandlerClassCleanup(TR_ProtocolHandler); \
TR_eventHandlerClassCleanup(TR_IoHandler); \
TR_eventHandlerClassCleanup(TR_CommManagerEpoll); \
TR_eventHandlerClassCleanup(TR_SimpleClient)
#endif // __TR_SIMPLE_CLIENT_H__
// vim: set ts=4 sw=4:

1
include/trcomm.h

@ -17,6 +17,7 @@
#include "tr/protocol/message_raw.h"
#include "tr/protocol_handler.h"
#include "tr/server.h"
#include "tr/simple_client.h"
#include "tr/interface/comm_end_point.h"
#include "tr/interface/comm_manager.h"
#include "tr/interface/protocol.h"

2
src/Makefile.am

@ -28,6 +28,8 @@ TRCOMM = cet_accept.c \
server_bind_tcp.c \
server_bind_udp.c \
server_start.c \
simple_client.c \
simple_client_issue.c \
i_comm_end_point.c \
i_comm_manager.c \
i_protocol.c

15
src/comm_manager.c

@ -40,9 +40,14 @@ commManagerCtor(void * _this, va_list * params)
TR_PARENTCALL(TR_CommManager, _this, TR_Class, ctor, params);
this->accept = TR_new(TR_Hash);
this->write = TR_new(TR_Hash);
this->read = TR_new(TR_Hash);
this->accept = TR_new(TR_Hash);
this->write = TR_new(TR_Hash);
this->read = TR_new(TR_Hash);
this->accept->cleanup_no_free = TRUE;
this->write->cleanup_no_free = TRUE;
this->read->cleanup_no_free = TRUE;
this->n_endpoints = sysconf(_SC_OPEN_MAX);
this->endpoints = TR_calloc(sizeof(TR_CommEndPoint), this->n_endpoints);
@ -56,7 +61,7 @@ commManagerDtor(void * _this)
TR_CommManager this = _this;
nfds_t i;
for (i = 0; i < this->n_endpoints; i++) {
for (i=0; i<this->n_endpoints; i++) {
TR_delete(this->endpoints[i]);
}
TR_MEM_FREE(this->endpoints);
@ -85,7 +90,7 @@ TR_commManagerAddEndpointEvt(TR_CommManager this, TR_Event event)
return TR_EVENT_DONE;
}
TR_EventDone TR_commManagerSelect(void *, TR_Event, int);
TR_EventDone TR_commManagerSelect(void *, TR_Event);
TR_EventDone TR_commManagerPollWrite(void *, TR_Event);
TR_EventDone TR_commManagerPollRead(void *, TR_Event);
TR_EventDone TR_commManagerDisableRead(void *, TR_Event);

2
src/comm_manager_epoll.c

@ -83,7 +83,7 @@ TR_commManagerEpollAddEndpoint(void * _this, TR_CommEndPoint endpoint)
static
void
TR_commManagerEpollSelect(void * _this, TR_Event event, int timeout)
TR_commManagerEpollSelect(void * _this, TR_Event event, unsigned long timeout)
{
TR_CommManagerEpoll this = _this;
TR_CommManager cmgr = _this;

3
src/comm_manager_poll.c

@ -61,6 +61,7 @@ commManagerPollDtor(void * _this)
TR_CommManagerPoll this = _this;
TR_MEM_FREE(this->fds);
TR_PARENTCALL(TR_CommManagerPoll, _this, TR_Class, dtor);
}
static
@ -75,7 +76,7 @@ TR_commManagerPollAddEndpoint(void * _this, TR_CommEndPoint endpoint)
static
void
TR_commManagerPollSelect(void * _this, TR_Event event, int timeout)
TR_commManagerPollSelect(void * _this, TR_Event event, unsigned long timeout)
{
TR_CommManagerPoll this = _this;
TR_CommManager cmgr = _this;

12
src/i_comm_manager.c

@ -86,19 +86,19 @@ commManagerIssueReadEvents(const void * endpoint, const void * comm_manager)
TR_EventDone
TR_commManagerSelect(void * _this, TR_Event event)
{
TR_CommManager this = _this;
int timeout; // milliseconds
int * timeoutptr = event->data;
TR_EventDispatcher dispatcher = (TR_EventDispatcher)event->subject;
TR_CommManager this = _this;
TR_Timer timer = (TR_Timer)event->data;
TR_EventDispatcher dispatcher = (TR_EventDispatcher)event->subject;
unsigned long timeout; // milliseconds
if (! (TR_hashEmpty(this->read)
&& TR_hashEmpty(this->write)
&& TR_hashEmpty(this->accept))) {
timeout = 0;
} else if (NULL == timeoutptr) {
} else if (NULL == timer) {
timeout = TR_eventDispatcherGetDataWaitTime(dispatcher);
} else {
timeout = *timeoutptr;
timeout = TR_timerGet(timer, NULL);
}
TR_CALL(_this, TR_CommManager, select, event, timeout);

2
src/server.c

@ -48,7 +48,7 @@ serverCtor(void * _this, va_list * params)
#else
this->comm_manager = (TR_CommManager)TR_new(TR_CommManagerPoll);
#endif
this->dispatcher = TR_new(TR_EventDispatcher, TR_EVD_SERVER, NULL, 100);
this->dispatcher = TR_new(TR_EventDispatcher, TR_EVD_SERVER, NULL);
this->connector = TR_new(TR_Connector);
this->io_handler = TR_new(TR_IoHandler);
this->protocol_handler = TR_new(TR_ProtocolHandler);

4
src/server_start.c

@ -26,9 +26,9 @@
#include "tr/server.h"
void
TR_serverStart(TR_Server this, int heartbeat)
TR_serverStart(TR_Server this, unsigned long heartbeat)
{
TR_eventDispatcherSetHeartbeat(this->dispatcher, 1000);
TR_eventDispatcherSetHeartbeat(this->dispatcher, heartbeat);
TR_eventDispatcherStart(this->dispatcher);
}

160
src/simple_client.c

@ -0,0 +1,160 @@
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2014 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <sys/types.h>
#include <stdint.h>
#include "trbase.h"
#include "trio.h"
#include "trevent.h"
#include "tr/simple_client.h"
#include "tr/comm_end_point.h"
#include "tr/comm_manager.h"
#include "tr/comm_manager_poll.h"
#include "tr/comm_manager_epoll.h"
#include "tr/connector.h"
#include "tr/io_handler.h"
#include "tr/protocol_handler.h"
static
int
simpleClientCtor(void * _this, va_list * params)
{
TR_SimpleClient this = _this;
this->endpoint = va_arg(*params, TR_CommEndPoint);
#if 1
this->comm_manager = (TR_CommManager)TR_new(TR_CommManagerEpoll);
#else
this->comm_manager = (TR_CommManager)TR_new(TR_CommManagerPoll);
#endif
this->dispatcher = TR_new(TR_EventDispatcher, TR_EVD_CLIENT, NULL);
this->io_handler = TR_new(TR_IoHandler);
this->protocol_handler = TR_new(TR_ProtocolHandler);
this->timer = TR_new(TR_Timer, TR_TBASE_MIL, 1000);
TR_eventDispatcherRegisterHandler(
this->dispatcher,
(TR_EventHandler)this->comm_manager);
TR_eventDispatcherRegisterHandler(
this->dispatcher,
(TR_EventHandler)this->io_handler);
TR_eventDispatcherRegisterHandler(
this->dispatcher,
(TR_EventHandler)this->protocol_handler);
TR_eventDispatcherRegisterHandler(
this->dispatcher,
(TR_EventHandler)this);
TR_commManagerAddEndpoint(this->comm_manager, this->endpoint);
return 0;
}
static
void
simpleClientDtor(void * _this)
{
TR_SimpleClient this = _this;
TR_delete(this->protocol_handler);
TR_delete(this->io_handler);
TR_delete(this->comm_manager);
TR_delete(this->dispatcher);
TR_delete(this->timer);
}
static
TR_EventDone
simpleClientUserAction(void * _this, TR_Event event)
{
TR_SimpleClient this = _this;
if (this->send_issued) {
unsigned long missed;
TR_timerGet(this->timer, &missed);
if (this->response || missed) {
TR_eventDispatcherStop((TR_EventDispatcher)event->subject);
} else {
TR_eventHandlerIssueEvent(
(TR_EventHandler)_this,
TR_eventSubjectEmit(
event->subject,
TR_DISPATCHER_EVENT_DATA_WAIT,
this->timer));
}
} else {
TR_eventHandlerIssueEvent(
(TR_EventHandler)_this,
TR_eventSubjectEmit(
(TR_EventSubject)this->endpoint,
TR_CEP_EVENT_MSG_READY,
this->request));
this->send_issued = TRUE;
}
return TR_EVENT_DONE;
}
static
TR_EventDone
simpleClientHandleData(void * _this, TR_Event event)
{
((TR_SimpleClient)_this)->response = event->data;
return TR_EVENT_DONE;
}
static
void
simpleClientCvInit(TR_class_ptr cls)
{
TR_EVENT_HANDLER_SET_METHOD(
cls,
TR_EventDispatcher,
TR_DISPATCHER_EVENT_USER_WAIT,
simpleClientUserAction);
TR_EVENT_HANDLER_SET_METHOD(
cls,
TR_CommEndPoint,
TR_CEP_EVENT_NEW_MSG,
simpleClientHandleData);
}
TR_INSTANCE(TR_Hash, simpleClientEventMethods);
TR_INIT_IFACE(TR_Class, simpleClientCtor, simpleClientDtor, NULL);
TR_CREATE_CLASS(
TR_SimpleClient,
TR_EventHandler,
simpleClientCvInit,
TR_IF(TR_Class)) = {
{ &(_simpleClientEventMethods.data) }
};
// vim: set ts=4 sw=4:

50
src/simple_client_issue.c

@ -0,0 +1,50 @@
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2014 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "trbase.h"
#include "trevent.h"
#include "tr/simple_client.h"
#include "tr/proto_message.h"
TR_ProtoMessage
TR_simpleClientIssue(
TR_SimpleClient this,
TR_ProtoMessage request,
unsigned long timeout)
{
TR_ProtoMessage retval;
TR_timerSetMil(this->timer, timeout);
this->request = request;
this->response = NULL;
this->send_issued = FALSE;
TR_eventDispatcherStart(((TR_EventHandler)this)->dispatcher[0]);
retval = this->response;
this->request = this->response = NULL;
return retval;
}
// vim: set ts=4 sw=4:

1
testers/build.sh

@ -5,3 +5,4 @@ LIBS="-lcrypto -lssl -lrt -luuid"
gcc ${CFLAGS} -c -o test_handler.o test_handler.c
gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -o testserver testserver.c test_handler.o ${TRLIBS}
gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -o testserver2 testserver2.c test_handler.o ${TRLIBS}
gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -o testclient testclient.c ${TRLIBS}

59
testers/testclient.c

@ -0,0 +1,59 @@
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "trbase.h"
#include "trcomm.h"
#include "trio.h"
#include "trevent.h"
#include "test_handler.h"
TR_INSTANCE(TR_LoggerSyslog, mylogger, {TR_LOGGER_INFO});
TR_INSTANCE(TR_LoggerStderr, mylogger2, {TR_LOGGER_INFO});
int
main (int argc, char * argv[])
{
TR_TcpSocket socket;
TR_Connection connection;
TR_SimpleClient client;
TR_Protocol protocol;
TR_ProtoMessageRaw message;
TR_RemoteData data;
TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger2);
socket = TR_new(TR_TcpSocket, TR_logger, "192.168.2.13", 5678, 0);
protocol = TR_new(TR_ProtocolRaw);
connection = TR_new(TR_Connection, socket, protocol);
TR_socketConnect((TR_Socket)socket);
TR_socketNonblock((TR_Socket)socket);
client = TR_new(TR_SimpleClient, connection);
message = (TR_ProtoMessageRaw)TR_protoCreateRequest(protocol, (TR_Socket)socket);
data = TR_new(TR_RemoteData, "test", sizeof("test"), (TR_Socket)socket);
message->data = data;
message = (TR_ProtoMessageRaw)TR_simpleClientIssue(
client,
(TR_ProtoMessage)message,
10000000);
printf("%s\n", ((TR_SizedData)message->data)->data);
TR_delete(message->data);
TR_delete(message);
puts("cleanup...");
TR_delete(client);
TR_delete(protocol);
TR_simpleClientClassCleanup();
TR_cleanup();
return 0;
}
// vim: set ts=4 sw=4:
Loading…
Cancel
Save