17 changed files with 369 additions and 18 deletions
-
1.gitignore
-
1include/Makefile.am
-
2include/tr/interface/comm_manager.h
-
2include/tr/server.h
-
70include/tr/simple_client.h
-
1include/trcomm.h
-
2src/Makefile.am
-
15src/comm_manager.c
-
2src/comm_manager_epoll.c
-
3src/comm_manager_poll.c
-
12src/i_comm_manager.c
-
2src/server.c
-
4src/server_start.c
-
160src/simple_client.c
-
50src/simple_client_issue.c
-
1testers/build.sh
-
59testers/testclient.c
@ -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: |
|||
|
|||
@ -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: |
|||
@ -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: |
|||
@ -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: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue