test_handler.c
2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "trbase.h"
#include "trcomm.h"
#include "trevent.h"
#include "test_handler.h"
static
TR_EventDone
testHandlerHeartbeat(TR_EventHandler this, TR_Event event)
{
printf("%zd beat(s) since last beat / handled: %llu/s\n",
((TR_EventDispatcher)event->subject)->n_beats,
((TestHandler)this)->handled);
((TestHandler)this)->handled = 0;
return TR_EVENT_DONE;
}
static
TR_EventDone
testHandlerNewMessage(TR_EventHandler this, TR_Event event)
{
TR_Event _event;
((TestHandler)this)->handled++;
_event = TR_eventSubjectEmit(
event->subject,
TR_CEP_EVENT_MSG_READY,
event->data);
TR_eventHandlerIssueEvent((TR_EventHandler)this, _event);
return TR_EVENT_DONE;
}
static
TR_EventDone
testHandlerClose(TR_EventHandler this, TR_Event event)
{
// puts("close");
return TR_EVENT_PENDING;
}
#if 0
static
TR_EventDone
testHandlerUpgrade(TR_EventHandler this, TR_Event event)
{
printf("upgrade: %"PRIdPTR"\n", event->id);
return TR_EVENT_PENDING;
}
#endif
static
int
testHandlerCtor(void * _this, va_list * params)
{
TR_PARENTCALL(TestHandler, _this, TR_Class, ctor, params);
((TestHandler)_this)->handled = 0;
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_CommEndPoint,
TR_CEP_EVENT_CLOSE,
testHandlerClose);
#if 0
TR_EVENT_HANDLER_SET_METHOD(
class,
TR_CommEndPoint,
TR_CEP_EVENT_UPGRADE,
testHandlerUpgrade);
#endif
}
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) }
};
// vim: set ts=4 sw=4: