Commit 3b4c855a59964220626bcad871dce701156e1ee1

Authored by Georg Hopp
1 parent d738e591

add protocol handler

... ... @@ -6,6 +6,7 @@ nobase_include_HEADERS = tr/comm_end_point.h \
6 6 tr/connector.h \
7 7 tr/proto_message.h \
8 8 tr/protocol.h \
  9 + tr/protocol_handler.h \
9 10 tr/interface/comm_end_point.h \
10 11 tr/interface/comm_manager.h \
11 12 tr/interface/protocol.h
... ...
... ... @@ -31,6 +31,7 @@
31 31 TR_CLASS(TR_ProtoMessage) {
32 32 TR_Socket remote;
33 33 int ready;
  34 + int close;
34 35 };
35 36 TR_INSTANCE_INIT(TR_ProtoMessage);
36 37 TR_CLASSVARS_DECL(TR_ProtoMessage) {};
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#ifndef __TR_PROTOCOL_HANDLER_H__
  24 +#define __TR_PROTOCOL_HANDLER_H__
  25 +
  26 +#include <sys/types.h>
  27 +
  28 +#include "trbase.h"
  29 +#include "trevent.h"
  30 +
  31 +#include "tr/comm_end_point.h"
  32 +
  33 +TR_CLASS(TR_ProtocolHandler) {
  34 + TR_EXTENDS(TR_EventHandler);
  35 +};
  36 +TR_INSTANCE_INIT(TR_ProtocolHandler);
  37 +TR_CLASSVARS_DECL(TR_ProtocolHandler) {
  38 + TR_CV_EXTENDS(TR_EventHandler);
  39 +};
  40 +
  41 +#endif // __TR_PROTOCOL_HANDLER_H__
  42 +
  43 +// vim: set ts=4 sw=4:
  44 +
... ...
... ... @@ -9,6 +9,7 @@
9 9 #include "tr/connector.h"
10 10 #include "tr/proto_message.h"
11 11 #include "tr/protocol.h"
  12 +#include "tr/protocol_handler.h"
12 13 #include "tr/interface/comm_end_point.h"
13 14 #include "tr/interface/comm_manager.h"
14 15 #include "tr/interface/protocol.h"
... ...
... ... @@ -17,6 +17,7 @@ TRCOMM = cep_append_read_data.c \
17 17 connector.c \
18 18 proto_message.c \
19 19 protocol.c \
  20 + protocol_handler.c \
20 21 protocol_message_raw.c \
21 22 protocol_raw.c \
22 23 i_comm_end_point.c \
... ...
... ... @@ -37,6 +37,8 @@ commManagerCtor(void * _this, va_list * params)
37 37 {
38 38 TR_CommManager this = _this;
39 39
  40 + TR_PARENTCALL(_this, TR_Class, ctor, params);
  41 +
40 42 this->n_endpoints = sysconf(_SC_OPEN_MAX);
41 43 this->endpoints = TR_calloc(sizeof(TR_CommEndPoint), this->n_endpoints);
42 44
... ... @@ -53,8 +55,6 @@ commManagerDtor(void * _this)
53 55 for (i = 0; i < this->n_endpoints; i++) {
54 56 TR_delete(this->endpoints[i]);
55 57 }
56   -
57   - TR_PARENTCALL(_this, TR_Class, dtor);
58 58 }
59 59
60 60 static
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <unistd.h>
  24 +
  25 +#include "trbase.h"
  26 +#include "trevent.h"
  27 +
  28 +#include "tr/protocol.h"
  29 +#include "tr/connection.h"
  30 +#include "tr/protocol_handler.h"
  31 +#include "tr/proto_message.h"
  32 +#include "tr/comm_end_point.h"
  33 +#include "tr/interface/comm_end_point.h"
  34 +
  35 +static
  36 +int
  37 +protocolHandlerCtor(void * _this, va_list * params)
  38 +{
  39 + TR_PARENTCALL(_this, TR_Class, ctor, params);
  40 +
  41 + return 0;
  42 +}
  43 +
  44 +static void protocolHandlerDtor(void * _this) {}
  45 +
  46 +static
  47 +int
  48 +protocolHandlerParse(void * _this, TR_Event event)
  49 +{
  50 + /**
  51 + * TODO No upgrade for now. Add it later on.
  52 + */
  53 + TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject;
  54 + TR_ProtoMessage message = TR_cepNextMessage(endpoint);
  55 +
  56 + while (message) {
  57 + TR_eventHandlerIssueEvent(
  58 + (TR_EventHandler)_this,
  59 + event->subject,
  60 + TR_CEP_EVENT_NEW_MSG,
  61 + message);
  62 +
  63 + if (message->close) {
  64 + // also check that we are a response. Well this is how it is done
  65 + // in the python code...
  66 + TR_cepSetClose(endpoint);
  67 + }
  68 +
  69 + message = TR_cepNextMessage(endpoint);
  70 + }
  71 +
  72 + return TRUE;
  73 +}
  74 +
  75 +static
  76 +int
  77 +protocolHandlerCompose(void * _this, TR_Event event)
  78 +{
  79 + TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject;
  80 + TR_ProtoMessage message = (TR_ProtoMessage)event->data;
  81 +
  82 + if (TR_cepCompose(endpoint, message)) {
  83 + TR_eventHandlerIssueEvent(
  84 + (TR_EventHandler)_this,
  85 + event->subject,
  86 + TR_CEP_EVENT_WRITE_READY,
  87 + NULL);
  88 + }
  89 +
  90 + if (message->close) {
  91 + // also check that we are a response. Well this is how it is done
  92 + // in the python code...
  93 + TR_cepSetClose(endpoint);
  94 + }
  95 +
  96 + return TRUE;
  97 +}
  98 +
  99 +int
  100 +protocolHandlerUpgrade(void * _this, TR_Event event)
  101 +{
  102 + return TRUE;
  103 +}
  104 +
  105 +static
  106 +void
  107 +protocolHandlerCvInit(TR_class_ptr cls)
  108 +{
  109 + TR_EVENT_HANDLER_SET_METHOD(
  110 + cls,
  111 + TR_Connection,
  112 + TR_CEP_EVENT_NEW_DATA,
  113 + protocolHandlerParse);
  114 +
  115 + TR_EVENT_HANDLER_SET_METHOD(
  116 + cls,
  117 + TR_Connection,
  118 + TR_CEP_EVENT_SEND_MSG,
  119 + protocolHandlerCompose);
  120 +
  121 + TR_EVENT_HANDLER_SET_METHOD(
  122 + cls,
  123 + TR_Connection,
  124 + TR_CEP_EVENT_UPGRADE,
  125 + protocolHandlerUpgrade);
  126 +}
  127 +
  128 +TR_INSTANCE(TR_Hash, protocolHandlerEventMethods);
  129 +TR_INIT_IFACE(TR_Class, protocolHandlerCtor, protocolHandlerDtor, NULL);
  130 +TR_CREATE_CLASS(
  131 + TR_ProtocolHandler,
  132 + TR_EventHandler,
  133 + protocolHandlerCvInit,
  134 + TR_IF(TR_Class)) = {
  135 + { &(_protocolHandlerEventMethods.data) }
  136 +};
  137 +
  138 +// vim: set ts=4 sw=4:
... ...
Please register or login to post a comment