summaryrefslogtreecommitdiff
path: root/src/Sockets/tests/events.cpp
blob: 5d0897359bd810f7093040551657904c0f2c126f (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <EventHandler.h>
#include <TcpSocket.h>
#include <IEventOwner.h>
#include <ListenSocket.h>
#include <StdoutLog.h>
#include <iostream>


double start;

double gettime()
{
	struct timeval tv;
	Utility::GetTime(&tv);
	return tv.tv_sec + (double)tv.tv_usec / 1000000;
}


class MyEvHandler : public EventHandler
{
public:
	MyEvHandler() : EventHandler() {}
	MyEvHandler(StdLog *p) : EventHandler(p) {}

};


class EvThread : public Thread
{
public:
	EvThread(Socket *p) : m_socket(p) {}

	void Run();

private:
	Socket *m_socket;
};


class EvSocket : public TcpSocket, public IEventOwner
{
public:
	EvSocket(ISocketHandler& h) : TcpSocket(h)
	, IEventOwner( static_cast<MyEvHandler&>(h) ) {
	}

	void OnEvent(int id) {
		std::cout << "OnEvent" << std::endl;
		std::cout << gettime() - start << std::endl;
		SetCloseAndDelete();
	}

	void OnAccept() {
		std::cout << "Incoming" << std::endl;
	}

	void OnConnect() {
		std::cout << "Connected" << std::endl;
		EvThread *thr = new EvThread( this );
		thr -> SetDeleteOnExit();
	}

	void OnDelete() {
		std::cout << "EvSocket::OnDelete()" << std::endl;
	}
};


void EvThread::Run()
{
	Socket *p0 = (Socket *)m_socket;
	EvSocket *p = dynamic_cast<EvSocket *>(p0);
	if (p)
	{
#ifdef _WIN32
		Sleep( 5000 );
#else
		sleep(10);
#endif
		std::cout << "Add event" << std::endl;
		start = gettime();
		p -> AddEvent(1, 50000);
	}
	else
	{
		std::cout << "Thread: not an EvSocket" << std::endl;
	}
}


//------------------------------------------------------------
// myTimer class
//------------------------------------------------------------
class myTimer : public IEventOwner
{
public:
	// Prototype of user call back function
	typedef void (*myTimer_cb)(myTimer* user_func, void* user_id);

	myTimer(IEventHandler& h, long ssec, long susec,
		long psec, long pusec, myTimer_cb func, void* id);
	~myTimer();

private:
	void OnEvent(int id);
	long ssec_;
	long susec_;
	long psec_;
	long pusec_;
	myTimer_cb user_func_;
	void* user_id_;
	bool periodic_;
};


//------------------------------------------------------------
myTimer::myTimer(IEventHandler& h, long ssec, long susec,
	long psec, long pusec, myTimer_cb user_func,
	void* user_id) :
	IEventOwner(h),
	ssec_(ssec),
	susec_(susec),
	psec_(psec),
	pusec_(pusec),
	user_func_(user_func),
	user_id_(user_id),
	periodic_(psec != 0 || pusec != 0)
{
	AddEvent(ssec_, susec_);
}


//------------------------------------------------------------
myTimer::~myTimer()
{
	ClearEvents();
}


//------------------------------------------------------------
void myTimer::OnEvent(int id)
{
	if (periodic_)
	{
		int n = AddEvent(psec_, pusec_);
	}

	user_func_(this, user_id_);

	if (!periodic_)
	{
		delete this;
	}
}


//------------------------------------------------------------
static void myfunc(myTimer* t, void* user_id)
{
	printf("Event %s\n", (char*)user_id);
}


/*
int main(int argc, char *argv[])
{
	EventHandler h;

	// Example 1: Create a 2 sec one-shot timer.
	// No need to save the pointer as it will delete
	// itself upon expiry.
	new myTimer(h, 2, 0, 0, 0, myfunc, (void*)"My Timer 0");

	// Example 2: Create a 1 sec periodic timer.
	// Save the pointer because we can stop the timer
	// later with a "delete mytimer".
	myTimer* mytimer = new myTimer(h, 0, 0, 1, 0, myfunc,
		(void*)"My Timer 1");

	// Drop into the event handler ..
	h.EventLoop();
}
*/


int main(int argc, char *argv[])
{
	StdoutLog log;
	MyEvHandler h(&log);
	ListenSocket<EvSocket> l(h);
	l.Bind("localhost", 12344);
	h.Add(&l);

	EvSocket sock(h);
	sock.Open("localhost", 12344);
	h.Add(&sock);

	// Example 1: Create a 2 sec one-shot timer.
	// No need to save the pointer as it will delete
	// itself upon expiry.
	new myTimer(h, 2, 0, 0, 0, myfunc, (void*)"My Timer 0");

	// Example 2: Create a 1 sec periodic timer.
	// Save the pointer because we can stop the timer
	// later with a "delete mytimer".
	myTimer* mytimer = new myTimer(h, 0, 0, 1, 0, myfunc,
		(void*)"My Timer 1");

	h.EventLoop();
}