-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.hpp
370 lines (309 loc) · 8.19 KB
/
worker.hpp
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#ifndef COCAINE_GRAPE_WORKER
#define COCAINE_GRAPE_WORKER
#include <typeinfo>
#include <functional>
#include <string>
#include <map>
#include <boost/utility.hpp>
#include <cocaine/common.hpp>
#include <cocaine/api/stream.hpp>
#include <cocaine/asio/local.hpp>
#include <cocaine/asio/service.hpp>
#include <cocaine/asio/socket.hpp>
#include <cocaine/rpc/channel.hpp>
#include <cocaine/unique_id.hpp>
#include "logger.hpp"
class base_handler_t :
public cocaine::api::stream_t,
public boost::noncopyable
{
public:
virtual
void
invoke(const std::string& event,
std::shared_ptr<cocaine::api::stream_t>) = 0;
};
template<class AppT>
class handler_t :
public base_handler_t
{
public:
typedef AppT application_type;
public:
handler_t(application_type& a) :
app(a)
{
// pass
}
protected:
application_type& app;
};
struct bad_factory_exception :
std::exception
{
// pass
};
class base_factory_t {
public:
virtual
std::shared_ptr<base_handler_t>
make_handler() = 0;
};
class application_t;
template<class HandlerT>
class handler_factory_t :
public base_factory_t
{
friend class application_t;
typedef typename HandlerT::application_type application_type;
public:
handler_factory_t() :
m_app(nullptr)
{
// pass
}
std::shared_ptr<base_handler_t>
make_handler()
{
if (m_app) {
return std::shared_ptr<base_handler_t>(new HandlerT(*m_app));
} else {
throw bad_factory_exception();
}
}
protected:
void
set_application(application_type *a) {
m_app = a;
}
protected:
application_type *m_app;
};
class function_handler_t :
public base_handler_t
{
public:
typedef std::function<std::string(const std::string&, const std::vector<std::string>&)>
function_type;
public:
function_handler_t(function_type f) :
m_func(f)
{
// pass
}
void
invoke(const std::string& event,
std::shared_ptr<cocaine::api::stream_t> response)
{
m_response = response;
m_event = event;
}
void
write(const char *chunk,
size_t size)
{
m_input.push_back(std::string(chunk, size));
}
void
close() {
std::string result = m_func(m_event, m_input);
m_response->write(result.data(), result.size());
m_response->close();
}
void
error(cocaine::error_code code,
const std::string& message)
{
// pass
}
private:
function_type m_func;
std::vector<std::string> m_input;
std::string m_event;
std::shared_ptr<cocaine::api::stream_t> m_response;
};
template<class AppT>
class method_factory_t :
public base_factory_t
{
friend class application_t;
typedef AppT application_type;
typedef std::function<std::string(AppT*, const std::string&, const std::vector<std::string>&)>
method_type;
public:
method_factory_t(method_type f) :
m_func(f),
m_app(nullptr)
{
// pass
}
std::shared_ptr<base_handler_t>
make_handler()
{
if (m_app) {
return std::shared_ptr<base_handler_t>(
new function_handler_t(std::bind(m_func,
m_app,
std::placeholders::_1,
std::placeholders::_2))
);
} else {
throw bad_factory_exception();
}
}
protected:
void
set_application(application_type *a) {
m_app = a;
}
protected:
method_type m_func;
application_type *m_app;
};
class function_factory_t :
public base_factory_t
{
public:
function_factory_t(function_handler_t::function_type f) :
m_func(f)
{
// pass
}
std::shared_ptr<base_handler_t>
make_handler()
{
return std::shared_ptr<base_handler_t>(new function_handler_t(m_func));
}
private:
function_handler_t::function_type m_func;
};
//
//template<class MethodT, class ObjectT>
//std::shared_ptr<base_factory_t>
//method_factory(MethodT method,
// ObjectT *object)
//{
// return std::shared_ptr<base_factory_t>(
// new function_factory_t(std::bind(method,
// object,
// std::placeholders::_1,
// std::placeholders::_2))
// );
//}
class worker_t;
class application_t {
friend class worker_t;
typedef std::map<std::string, std::shared_ptr<base_factory_t>>
handlers_map;
public:
virtual
~application_t()
{
// pass
}
virtual
std::shared_ptr<base_handler_t>
invoke(const std::string& event,
std::shared_ptr<cocaine::api::stream_t> response);
const std::string&
name() const {
return m_name;
}
protected:
virtual
void
on(const std::string& event,
std::shared_ptr<base_factory_t> factory);
template<class HandlerT, template<class> class FactoryT = handler_factory_t>
void
on(const std::string& event,
const FactoryT<HandlerT>& factory = FactoryT<HandlerT>());
virtual
void
on_unregistered(std::shared_ptr<base_factory_t> factory);
template<class HandlerT, template<class> class FactoryT = handler_factory_t>
void
on_unregistered(const FactoryT<HandlerT>& factory = FactoryT<HandlerT>());
virtual
void
initialize(const std::string& name,
std::shared_ptr<cocaine::logger::logger_t> logger);
private:
std::string m_name;
handlers_map m_handlers;
std::shared_ptr<base_factory_t> m_default_handler;
std::shared_ptr<cocaine::logger::log_t> m_log;
};
class worker_t :
public boost::noncopyable
{
struct io_pair_t {
std::shared_ptr<cocaine::api::stream_t> upstream;
std::shared_ptr<cocaine::api::stream_t> downstream;
};
typedef std::map<uint64_t, io_pair_t> stream_map_t;
public:
worker_t(const std::string& name,
const std::string& uuid);
~worker_t();
void
run();
template<class AppT>
void
add(const std::string& name, const AppT& a);
template<class Event, typename... Args>
void
send(Args&&... args);
private:
void
on_message(const cocaine::io::message_t& message);
void
on_heartbeat(ev::timer&, int);
void
on_disown(ev::timer&, int);
void
terminate(int code,
const std::string& reason);
private:
const cocaine::unique_id_t m_id;
cocaine::io::service_t m_service;
ev::timer m_heartbeat_timer,
m_disown_timer;
std::shared_ptr<cocaine::logger::log_t> m_log;
std::shared_ptr<cocaine::io::channel<cocaine::io::socket<cocaine::io::local>>> m_channel;
std::string m_app_name;
std::shared_ptr<application_t> m_application;
stream_map_t m_streams;
};
template<class Event, typename... Args>
void
worker_t::send(Args&&... args) {
m_channel->wr->write<Event>(std::forward<Args>(args)...);
}
template<class AppT>
void
worker_t::add(const std::string& name, const AppT& a) {
if (name == m_app_name) {
m_application.reset(new AppT(a));
auto logger = std::shared_ptr<cocaine::logger::logger_t>(
new cocaine::logger::remote_t("remote", Json::Value(), m_service));
m_application->initialize(name, logger);
}
}
template<class HandlerT, template<class> class FactoryT>
void
application_t::on(const std::string& event,
const FactoryT<HandlerT>& factory)
{
FactoryT<HandlerT> *new_factory = new FactoryT<HandlerT>(factory);
new_factory->set_application(dynamic_cast<typename FactoryT<HandlerT>::application_type*>(this));
this->on(event, std::shared_ptr<base_factory_t>(new_factory));
}
template<class HandlerT, template<class> class FactoryT>
void
application_t::on_unregistered(const FactoryT<HandlerT>& factory) {
FactoryT<HandlerT> *new_factory = new FactoryT<HandlerT>(factory);
new_factory->set_application(dynamic_cast<typename FactoryT<HandlerT>::application_type*>(this));
this->on_unregistered(std::shared_ptr<base_factory_t>(new_factory));
}
#endif // COCAINE_GRAPE_WORKER