ServicesImpl.cpp 14.9 KB
Newer Older
legoc's avatar
legoc committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * Copyright 2015 Institut Laue-Langevin
 *
 * Licensed under the EUPL, Version 1.1 only (the "License");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://joinup.ec.europa.eu/software/page/eupl
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 */

17
#include "ServicesImpl.h"
18
19
20
#include "SocketException.h"
#include "ConnectionTimeout.h"
#include "message/Message.h"
21
#include "RequestSocketImpl.h"
22
23
#include <iostream>
#include <sstream>
24
#include "JSON.h"
legoc's avatar
legoc committed
25

26
27
28
29
// Using Visual Studio preprocessor.
// It must be improved in case of other compilers.
#ifdef _WIN32
	#define GET_PROCESS_PID() GetCurrentProcessId()
30
31
32
33
34
35
36
#else
	#include <unistd.h>

	#define GET_PROCESS_PID() ::getpid()
#endif


legoc's avatar
legoc committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using namespace std;

namespace cameo {

ServicesImpl::ServicesImpl() :
	m_context(1), m_timeout(0) {
}

ServicesImpl::~ServicesImpl() {
}

void ServicesImpl::setTimeout(int timeout) {
	m_timeout = timeout;
}

int ServicesImpl::getTimeout() const {
	return m_timeout;
}

56
57
58
59
60
std::string ServicesImpl::createSyncRequest() const {

	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::SYNC);
61

62
	return request.toString();
63
64
}

legoc's avatar
legoc committed
65
66
67
68
69
70
71
72
73
74
75
76
std::string ServicesImpl::createSyncStreamRequest(const std::string& name) const {

	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::SYNC_STREAM);

	request.pushKey(message::SyncStreamRequest::NAME);
	request.pushString(name);

	return request.toString();
}

77
78
79
80
81
82
83
84
85
std::string ServicesImpl::createVersionRequest() const {

	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::IMPL_VERSION);

	return request.toString();
}

legoc's avatar
legoc committed
86
87
std::string ServicesImpl::createIsAliveRequest(int id) const {

88
89
90
	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::IS_ALIVE);
legoc's avatar
legoc committed
91

92
93
	request.pushKey(message::IsAliveRequest::ID);
	request.pushInt(id);
legoc's avatar
legoc committed
94

95
	return request.toString();
legoc's avatar
legoc committed
96
97
98
}

std::string ServicesImpl::createStartRequest(const std::string& name, const std::vector<std::string> & args, const std::string& instanceReference) const {
99
100
101
102
103
104
105
106
107
108
109
110

	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::START);

	request.pushKey(message::StartRequest::NAME);
	request.pushString(name);

	request.pushKey(message::StartRequest::ARGS);
	request.startArray();
	for (auto& a : args) {
		request.pushString(a);
legoc's avatar
legoc committed
111
	}
112
113
114
115
116
117
	request.endArray();

	request.pushKey(message::StartRequest::INSTANCE_REFERENCE);
	request.pushString(instanceReference);

	return request.toString();
legoc's avatar
legoc committed
118
119
120
121
}

std::string ServicesImpl::createStopRequest(int id) const {

122
123
124
	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::STOP);
legoc's avatar
legoc committed
125

126
127
	request.pushKey(message::StopRequest::ID);
	request.pushInt(id);
legoc's avatar
legoc committed
128

129
	return request.toString();
legoc's avatar
legoc committed
130
131
}

132
std::string ServicesImpl::createKillRequest(int id) const {
legoc's avatar
legoc committed
133

134
135
136
	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::KILL);
legoc's avatar
legoc committed
137

138
139
	request.pushKey(message::KillRequest::ID);
	request.pushInt(id);
legoc's avatar
legoc committed
140

141
	return request.toString();
142
}
legoc's avatar
legoc committed
143

144
std::string ServicesImpl::createConnectRequest(const std::string& name) const {
legoc's avatar
legoc committed
145

146
147
148
	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::CONNECT);
legoc's avatar
legoc committed
149

150
151
	request.pushKey(message::ConnectRequest::NAME);
	request.pushString(name);
legoc's avatar
legoc committed
152

153
	return request.toString();
legoc's avatar
legoc committed
154
155
}

156
157
158
159
160
161
162
163
164
165
166
167
std::string ServicesImpl::createConnectWithIdRequest(int id) const {

	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::CONNECT_WITH_ID);

	request.pushKey(message::ConnectWithIdRequest::ID);
	request.pushInt(id);

	return request.toString();
}

168
std::string ServicesImpl::createListRequest() const {
legoc's avatar
legoc committed
169

170
171
	json::StringObject request;
	request.pushKey(message::TYPE);
172
	request.pushInt(message::LIST);
legoc's avatar
legoc committed
173

174
	return request.toString();
175
}
legoc's avatar
legoc committed
176

legoc's avatar
legoc committed
177
std::string ServicesImpl::createAppsRequest() const {
legoc's avatar
legoc committed
178

179
180
	json::StringObject request;
	request.pushKey(message::TYPE);
legoc's avatar
legoc committed
181
	request.pushInt(message::APPS);
legoc's avatar
legoc committed
182

183
	return request.toString();
legoc's avatar
legoc committed
184
185
}

186
std::string ServicesImpl::createStreamStatusRequest() const {
legoc's avatar
legoc committed
187

188
189
190
	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::STATUS);
legoc's avatar
legoc committed
191

192
	return request.toString();
legoc's avatar
legoc committed
193
194
}

195
std::string ServicesImpl::createOutputPortWithIdRequest(int id) const {
196

197
198
	json::StringObject request;
	request.pushKey(message::TYPE);
199
	request.pushInt(message::OUTPUT_PORT_WITH_ID);
200

201
	request.pushKey(message::OutputPortWithIdRequest::ID);
202
	request.pushInt(id);
203

204
	return request.toString();
legoc's avatar
legoc committed
205
206
}

207
208
209
210
211
212
213
214
215
216
217
218
std::string ServicesImpl::createOutputPortRequest(const std::string& name) const {

	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::OUTPUT_PORT);

	request.pushKey(message::OutputPortRequest::NAME);
	request.pushString(name);

	return request.toString();
}

legoc's avatar
legoc committed
219
220
std::string ServicesImpl::createSetStatusRequest(int id, int32_t state) const {

221
222
223
	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::SET_STATUS);
224

225
226
	request.pushKey(message::SetStatusRequest::ID);
	request.pushInt(id);
227

228
229
	request.pushKey(message::SetStatusRequest::APPLICATION_STATE);
	request.pushInt(state);
230

231
	return request.toString();
legoc's avatar
legoc committed
232
233
234
235
}

std::string ServicesImpl::createGetStatusRequest(int id) const {

236
237
238
	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::GET_STATUS);
239

240
241
242
243
	request.pushKey(message::GetStatusRequest::ID);
	request.pushInt(id);

	return request.toString();
legoc's avatar
legoc committed
244
245
}

246
std::string ServicesImpl::createSetResultRequest(int id) const {
247

248
249
250
	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::SET_RESULT);
251

252
253
	request.pushKey(message::SetResultRequest::ID);
	request.pushInt(id);
legoc's avatar
legoc committed
254

255
	return request.toString();
legoc's avatar
legoc committed
256
257
258
259
}

std::string ServicesImpl::createSubscribePublisherRequest() const {

260
261
	json::StringObject request;
	request.pushKey(message::TYPE);
262
	request.pushInt(message::SUBSCRIBE_PUBLISHER_v0);
263
264

	return request.toString();
legoc's avatar
legoc committed
265
266
267
268
}

std::string ServicesImpl::createCreatePublisherRequest(int id, const std::string& name, int numberOfSubscribers) const {

269
270
	json::StringObject request;
	request.pushKey(message::TYPE);
271
	request.pushInt(message::CREATE_PUBLISHER_v0);
272
273
274
275
276
277
278
279
280
281
282

	request.pushKey(message::CreatePublisherRequest::ID);
	request.pushInt(id);

	request.pushKey(message::CreatePublisherRequest::NAME);
	request.pushString(name);

	request.pushKey(message::CreatePublisherRequest::NUMBER_OF_SUBSCRIBERS);
	request.pushInt(numberOfSubscribers);

	return request.toString();
legoc's avatar
legoc committed
283
284
285
286
}

std::string ServicesImpl::createConnectPublisherRequest(int id, const std::string& publisherName) const {

287
288
	json::StringObject request;
	request.pushKey(message::TYPE);
289
	request.pushInt(message::CONNECT_PUBLISHER_v0);
290
291
292
293
294
295
296
297

	request.pushKey(message::ConnectPublisherRequest::APPLICATION_ID);
	request.pushInt(id);

	request.pushKey(message::ConnectPublisherRequest::PUBLISHER_NAME);
	request.pushString(publisherName);

	return request.toString();
legoc's avatar
legoc committed
298
299
300
301
}

std::string ServicesImpl::createTerminatePublisherRequest(int id, const std::string& name) const {

302
303
	json::StringObject request;
	request.pushKey(message::TYPE);
304
	request.pushInt(message::TERMINATE_PUBLISHER_v0);
305
306
307
308
309
310
311
312

	request.pushKey(message::TerminatePublisherRequest::ID);
	request.pushInt(id);

	request.pushKey(message::TerminatePublisherRequest::NAME);
	request.pushString(name);

	return request.toString();
legoc's avatar
legoc committed
313
314
}

315
std::string ServicesImpl::createRequestPortV0Request(int id, const std::string& name) const {
legoc's avatar
legoc committed
316

317
318
	json::StringObject request;
	request.pushKey(message::TYPE);
319
	request.pushInt(message::REQUEST_PORT_v0);
320

321
	request.pushKey(message::RequestPortV0Request::ID);
322
323
	request.pushInt(id);

324
	request.pushKey(message::RequestPortV0Request::NAME);
325
326
327
	request.pushString(name);

	return request.toString();
legoc's avatar
legoc committed
328
329
}

330
std::string ServicesImpl::createConnectPortV0Request(int id, const std::string& name) const {
legoc's avatar
legoc committed
331

332
333
	json::StringObject request;
	request.pushKey(message::TYPE);
334
	request.pushInt(message::CONNECT_PORT_v0);
335

336
	request.pushKey(message::ConnectPortV0Request::ID);
337
338
	request.pushInt(id);

339
	request.pushKey(message::ConnectPortV0Request::NAME);
340
341
342
	request.pushString(name);

	return request.toString();
legoc's avatar
legoc committed
343
344
}

345
std::string ServicesImpl::createRemovePortV0Request(int id, const std::string& name) const {
legoc's avatar
legoc committed
346

347
348
	json::StringObject request;
	request.pushKey(message::TYPE);
349
	request.pushInt(message::REMOVE_PORT_v0);
350

351
	request.pushKey(message::RemovePortV0Request::ID);
352
353
	request.pushInt(id);

354
	request.pushKey(message::RemovePortV0Request::NAME);
355
356
357
	request.pushString(name);

	return request.toString();
legoc's avatar
legoc committed
358
359
}

360
std::string ServicesImpl::createAttachUnmanagedRequest(const std::string& name) const {
361
362
363
364

	// Get the pid.
	long pid = GET_PROCESS_PID();

365
366
	json::StringObject request;
	request.pushKey(message::TYPE);
367
	request.pushInt(message::ATTACH_UNMANAGED);
legoc's avatar
legoc committed
368

369
	request.pushKey(message::AttachUnmanagedRequest::NAME);
370
371
	request.pushString(name);

372
	request.pushKey(message::AttachUnmanagedRequest::PID);
373
374
375
	request.pushInt64(pid);

	return request.toString();
legoc's avatar
legoc committed
376
377
}

378
std::string ServicesImpl::createDetachUnmanagedRequest(int id) const {
legoc's avatar
legoc committed
379

380
381
	json::StringObject request;
	request.pushKey(message::TYPE);
382
	request.pushInt(message::DETACH_UNMANAGED);
383

384
	request.pushKey(message::DetachUnmanagedRequest::ID);
385
386
387
	request.pushInt(id);

	return request.toString();
legoc's avatar
legoc committed
388
389
}

390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
std::string ServicesImpl::createRequestResponse(int64_t value) const {

	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::RESPONSE);

	request.pushKey(message::RequestResponse::VALUE);
	request.pushInt64(value);

	request.pushKey(message::RequestResponse::MESSAGE);
	request.pushString("");

	return request.toString();
}

std::string ServicesImpl::createRequestResponse(int64_t value, const std::string& message) const {

	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::RESPONSE);

	request.pushKey(message::RequestResponse::VALUE);
	request.pushInt64(value);

	request.pushKey(message::RequestResponse::MESSAGE);
	request.pushString(message);

	return request.toString();
}

420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468

std::string ServicesImpl::createStoreKeyValueRequest(int id, const std::string& key, const std::string& value) {

	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::STORE_KEY_VALUE);

	request.pushKey(message::StoreKeyValueRequest::ID);
	request.pushInt(id);

	request.pushKey(message::StoreKeyValueRequest::KEY);
	request.pushString(key);

	request.pushKey(message::StoreKeyValueRequest::VALUE);
	request.pushString(value);

	return request.toString();
}

std::string ServicesImpl::createGetKeyValueRequest(int id, const std::string& key) {

	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::GET_KEY_VALUE);

	request.pushKey(message::GetKeyValueRequest::ID);
	request.pushInt(id);

	request.pushKey(message::GetKeyValueRequest::KEY);
	request.pushString(key);

	return request.toString();
}

std::string ServicesImpl::createRemoveKeyRequest(int id, const std::string& key) {

	json::StringObject request;
	request.pushKey(message::TYPE);
	request.pushInt(message::REMOVE_KEY);

	request.pushKey(message::RemoveKeyRequest::ID);
	request.pushInt(id);

	request.pushKey(message::RemoveKeyRequest::KEY);
	request.pushString(key);

	return request.toString();
}

469
470
471
472
473
zmq::socket_t * ServicesImpl::createEventSubscriber(const std::string& endpoint, const std::string& cancelEndpoint) {

	zmq::socket_t * subscriber = new zmq::socket_t(m_context, ZMQ_SUB);

	vector<string> streamList;
474
475
476
477
	streamList.push_back(message::Event::STATUS);
	streamList.push_back(message::Event::RESULT);
	streamList.push_back(message::Event::PUBLISHER);
	streamList.push_back(message::Event::PORT);
478
	streamList.push_back(message::Event::KEYVALUE);
479
	streamList.push_back(message::Event::CANCEL);
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495

	for (vector<string>::const_iterator s = streamList.begin(); s != streamList.end(); ++s) {
		subscriber->setsockopt(ZMQ_SUBSCRIBE, s->c_str(), s->length());
	}

	subscriber->connect(endpoint.c_str());
	subscriber->connect(cancelEndpoint.c_str());

	return subscriber;
}

zmq::socket_t * ServicesImpl::createOutputStreamSubscriber(const std::string& endpoint, const std::string& cancelEndpoint) {

	zmq::socket_t * subscriber = new zmq::socket_t(m_context, ZMQ_SUB);

	vector<string> streamList;
legoc's avatar
legoc committed
496
	streamList.push_back(message::Event::SYNCSTREAM);
497
498
499
	streamList.push_back(message::Event::STREAM);
	streamList.push_back(message::Event::ENDSTREAM);
	streamList.push_back(message::Event::CANCEL);
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535

	for (vector<string>::const_iterator s = streamList.begin(); s != streamList.end(); ++s) {
		subscriber->setsockopt(ZMQ_SUBSCRIBE, s->c_str(), s->length());
	}

	subscriber->connect(endpoint.c_str());
	subscriber->connect(cancelEndpoint.c_str());

	return subscriber;
}

zmq::socket_t * ServicesImpl::createCancelPublisher(const std::string& endpoint) {

	zmq::socket_t * publisher = new zmq::socket_t(m_context, ZMQ_PUB);
	publisher->bind(endpoint.c_str());

	return publisher;
}

zmq::socket_t * ServicesImpl::createRequestSocket(const std::string& endpoint) {

	zmq::socket_t* socket = new zmq::socket_t(m_context, ZMQ_REQ);

	try {
		// Set the linger value to 0 to ensure that pending requests are destroyed in case of timeout.
		int value = 0;
		socket->setsockopt(ZMQ_LINGER, &value, sizeof(int));

		// Connect to the endpoint.
		socket->connect(endpoint.c_str());
	}
	catch (exception const & e) {
		throw SocketException(e.what());
	}

	return socket;
legoc's avatar
legoc committed
536
537
}

538
539
bool ServicesImpl::isAvailable(RequestSocketImpl * socket, int timeout) {

540
	string request = createSyncRequest();
541
542

	try {
543
		unique_ptr<zmq::message_t> reply = socket->request(request, timeout);
544
545
546
547

		if (reply.get() != nullptr) {
			return true;
		}
548
549
	}
	catch (const ConnectionTimeout&) {
550
		// The server is not accessible.
551
552
553
	}
	catch (...) {
		// Should not happen.
554
555
556
557
558
	}

	return false;
}

legoc's avatar
legoc committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
void ServicesImpl::sendSyncStream(RequestSocketImpl * socket, const std::string& name) {

	string request = createSyncStreamRequest(name);

	try {
		unique_ptr<zmq::message_t> reply = socket->request(request);
	}
	catch (const ConnectionTimeout&) {
		// The server is not accessible.
	}
	catch (...) {
		// Should not happen.
	}
}

void ServicesImpl::waitForStreamSubscriber(zmq::socket_t * subscriber, RequestSocketImpl * socket, const std::string& name) {

	// Poll subscriber.
	zmq_pollitem_t items[1];
	items[0].socket = static_cast<void *>(*subscriber);
	items[0].fd = 0;
	items[0].events = ZMQ_POLLIN;
	items[0].revents = 0;

	while (true) {
		sendSyncStream(socket, name);

		// Wait for 100ms.
		int rc = zmq::poll(items, 1, 100);
		if (rc != 0) {
			break;
		}
	}
}

legoc's avatar
legoc committed
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
void ServicesImpl::waitForSubscriber(zmq::socket_t * subscriber, RequestSocketImpl * socket) {

	// Poll subscriber.
	zmq_pollitem_t items[1];
	items[0].socket = static_cast<void *>(*subscriber);
	items[0].fd = 0;
	items[0].events = ZMQ_POLLIN;
	items[0].revents = 0;

	while (true) {
		isAvailable(socket, 100);

		// Wait for 100ms.
		int rc = zmq::poll(items, 1, 100);
		if (rc != 0) {
			break;
		}
	}
}

legoc's avatar
legoc committed
614
}