Server.cpp 19.2 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
18
#include "Server.h"
#include "Application.h"
19
#include "ConnectionChecker.h"
20
#include "impl/ServicesImpl.h"
21
#include "EventThread.h"
22
#include "impl/StreamSocketImpl.h"
legoc's avatar
legoc committed
23
#include "impl/RequestSocketImpl.h"
24
#include "message/Message.h"
25
26
#include "UndefinedApplicationException.h"
#include "UndefinedKeyException.h"
27
28
#include <iostream>
#include <sstream>
29
#include "JSON.h"
legoc's avatar
legoc committed
30
31
32
33

using namespace std;

namespace cameo {
34
35
constexpr int defaultTimeout = 10000;
	
36
void Server::initServer(const Endpoint& endpoint, int timeoutMs) {
legoc's avatar
legoc committed
37

38
	Services::init();
legoc's avatar
legoc committed
39

40
	m_serverEndpoint = endpoint;
41

legoc's avatar
legoc committed
42
43
44
	// Set the timeout.
	Services::setTimeout(timeoutMs);

45
46
47
	// Create the request socket. The server endpoint has been defined.
	Services::initRequestSocket();

legoc's avatar
legoc committed
48
49
	// Manage the ConnectionTimeout exception that can occur.
	try {
50
51
52
		// Retrieve the server version.
		Services::retrieveServerVersion();

legoc's avatar
legoc committed
53
54
55
56
57
58
59
60
		// Start the event thread.
		unique_ptr<EventStreamSocket> socket = openEventStream();
		m_eventThread.reset(new EventThread(this, socket));
		m_eventThread->start();
	}
	catch (...) {
		// ...
	}
legoc's avatar
legoc committed
61
62
}

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Server::Server(const Endpoint& endpoint, int timeoutMs) :
	Services() {

	Services::init();

	initServer(endpoint, timeoutMs);
}

Server::Server(const std::string& endpoint, int timeoutMs) :
	Services() {

	Services::init();

	try {
		initServer(Endpoint::parse(endpoint), timeoutMs);
	}
	catch (...) {
		throw InvalidArgumentException(endpoint + " is not a valid endpoint");
	}
}

legoc's avatar
legoc committed
84
Server::~Server() {
85
86
87
88
	// Stop the event thread.
	if (m_eventThread.get() != nullptr) {
		m_eventThread->cancel();
	}
legoc's avatar
legoc committed
89
90
}

legoc's avatar
legoc committed
91
92
void Server::setTimeout(int value) {
	Services::setTimeout(value);
legoc's avatar
legoc committed
93
94
95
96
97
98
}

int Server::getTimeout() const {
	return Services::getTimeout();
}

99
const Endpoint& Server::getEndpoint() const {
legoc's avatar
legoc committed
100
101
102
	return Services::getEndpoint();
}

legoc's avatar
legoc committed
103
104
105
106
std::array<int, 3> Server::getVersion() const {
	return Services::getVersion();
}

legoc's avatar
legoc committed
107
108
109
110
bool Server::isAvailable(int timeout) const {
	return Services::isAvailable(timeout);
}

legoc's avatar
legoc committed
111
bool Server::isAvailable() const {
112
113
	return isAvailable(getAvailableTimeout());
}
legoc's avatar
legoc committed
114

115
int Server::getAvailableTimeout() const {
legoc's avatar
legoc committed
116
117
	int timeout = getTimeout();
	if (timeout > 0) {
118
119
120
		return timeout;
	}
	else {
121
		return defaultTimeout;
legoc's avatar
legoc committed
122
123
124
	}
}

legoc's avatar
legoc committed
125
std::unique_ptr<application::Instance> Server::makeInstance() {
126
	return unique_ptr<application::Instance>(new application::Instance(this));
legoc's avatar
legoc committed
127
128
}

129
std::unique_ptr<application::Instance> Server::start(const std::string& name, int options) {
legoc's avatar
legoc committed
130
131
132
	return start(name, vector<string>(), options);
}

133
std::unique_ptr<application::Instance> Server::start(const std::string& name, const std::vector<std::string> & args, int options) {
legoc's avatar
legoc committed
134

legoc's avatar
legoc committed
135
136
	bool outputStream = ((options & OUTPUTSTREAM) != 0);

legoc's avatar
legoc committed
137
	unique_ptr<application::Instance> instance = makeInstance();
legoc's avatar
legoc committed
138

139
	// Set the name and register the instance as event listener.
legoc's avatar
legoc committed
140
	instance->setName(name);
141
	registerEventListener(instance.get());
legoc's avatar
legoc committed
142
143

	try {
144
145
		unique_ptr<OutputStreamSocket> streamSocket;

legoc's avatar
legoc committed
146
		if (outputStream) {
legoc's avatar
legoc committed
147
148
			// Connect to the stream port. A sync is made to ensure that the subscriber is connected.
			streamSocket = createOutputStreamSocket(name);
legoc's avatar
legoc committed
149
150
		}

legoc's avatar
legoc committed
151
		unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createStartRequest(name, args, application::This::getName(), application::This::getId(), application::This::getEndpoint().toString()));
legoc's avatar
legoc committed
152

153
154
155
		// Get the JSON response.
		json::Object response;
		json::parse(response, reply.get());
legoc's avatar
legoc committed
156

157
158
159
		int value = response[message::RequestResponse::VALUE].GetInt();
		if (value == -1) {
			instance->setErrorMessage(response[message::RequestResponse::MESSAGE].GetString());
legoc's avatar
legoc committed
160
		}
161
162
		else {
			instance->setId(value);
163
164
165
166

			if (outputStream) {
				instance->setOutputStreamSocket(streamSocket);
			}
167
168
169
		}
	}
	catch (const ConnectionTimeout& e) {
legoc's avatar
legoc committed
170
171
172
173
174
175
176
177
		instance->setErrorMessage(e.what());
	}

	return instance;
}

Response Server::stopApplicationAsynchronously(int id, bool immediately) const {

178
	string request;
legoc's avatar
legoc committed
179
180

	if (immediately) {
181
182
183
184
		request = m_impl->createKillRequest(id);
	}
	else {
		request = m_impl->createStopRequest(id);
legoc's avatar
legoc committed
185
186
	}

187
	unique_ptr<zmq::message_t> reply = m_requestSocket->request(request);
legoc's avatar
legoc committed
188

189
190
191
	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());
legoc's avatar
legoc committed
192

193
194
195
196
	int value = response[message::RequestResponse::VALUE].GetInt();
	string message = response[message::RequestResponse::MESSAGE].GetString();

	return Response(value, message);
legoc's avatar
legoc committed
197
198
}

199
application::InstanceArray Server::connectAll(const std::string& name, int options) {
legoc's avatar
legoc committed
200
201

	bool outputStream = ((options & OUTPUTSTREAM) != 0);
legoc's avatar
legoc committed
202

203
204
205
206
207
	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createConnectRequest(name));

	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());
legoc's avatar
legoc committed
208

209
210
	application::InstanceArray instances;

211
212
213
	json::Value& applicationInfo = response[message::ApplicationInfoListResponse::APPLICATION_INFO];
	json::Value::Array array = applicationInfo.GetArray();
	size_t size = array.Size();
legoc's avatar
legoc committed
214

215
	// Allocate the array.
216
	instances.reserve(size);
legoc's avatar
legoc committed
217
218
219

	int aliveInstancesCount = 0;

220
221
	for (int i = 0; i < size; ++i) {
		json::Value::Object info = array[i].GetObject();
legoc's avatar
legoc committed
222

legoc's avatar
legoc committed
223
		unique_ptr<application::Instance> instance = makeInstance();
legoc's avatar
legoc committed
224

225
		// Set the name and register the instance as event listener.
226
227
		string name = info[message::ApplicationInfo::NAME].GetString();
		instance->setName(name);
228
229
		registerEventListener(instance.get());

230
		int applicationId = info[message::ApplicationInfo::ID].GetInt();
legoc's avatar
legoc committed
231
232
233
234
235
236

		// test if the application is still alive otherwise we could have missed a status message
		if (isAlive(applicationId)) {
			aliveInstancesCount++;

			instance->setId(applicationId);
237
238
			instance->setInitialState(info[message::ApplicationInfo::APPLICATION_STATE].GetInt());
			instance->setPastStates(info[message::ApplicationInfo::PAST_APPLICATION_STATES].GetInt());
legoc's avatar
legoc committed
239

240
			if (outputStream) {
legoc's avatar
legoc committed
241
				unique_ptr<OutputStreamSocket> streamSocket = createOutputStreamSocket(name);
242
243
244
				instance->setOutputStreamSocket(streamSocket);
			}

245
			instances.push_back(std::move(instance));
legoc's avatar
legoc committed
246
247
248
		}
	}

249
	// Copy the alive instances.
legoc's avatar
legoc committed
250
	application::InstanceArray aliveInstances;
251
	aliveInstances.reserve(aliveInstancesCount);
legoc's avatar
legoc committed
252
253

	int j = 0;
254
	for (int i = 0; i < size; ++i) {
legoc's avatar
legoc committed
255

256
257
		if (instances[i].get() != nullptr) {
			aliveInstances.push_back(std::move(instances[i]));
legoc's avatar
legoc committed
258
259
260
261
262
263
264
			j++;
		}
	}

	return aliveInstances;
}

265
std::unique_ptr<application::Instance> Server::connect(const std::string& name, int options) {
legoc's avatar
legoc committed
266

legoc's avatar
legoc committed
267
	application::InstanceArray instances = connectAll(name, options);
legoc's avatar
legoc committed
268
269

	if (instances.size() == 0) {
legoc's avatar
legoc committed
270
		unique_ptr<application::Instance> instance = makeInstance();
legoc's avatar
legoc committed
271
272
273
		return instance;
	}

legoc's avatar
legoc committed
274
	return std::move(instances[0]);
legoc's avatar
legoc committed
275
276
}

277
std::unique_ptr<application::Instance> Server::connect(int id, int options) {
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

	bool outputStream = ((options & OUTPUTSTREAM) != 0);

	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createConnectWithIdRequest(id));

	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());

	json::Value& applicationInfo = response[message::ApplicationInfoListResponse::APPLICATION_INFO];
	json::Value::Array array = applicationInfo.GetArray();
	size_t size = array.Size();

	if (size > 0) {
		json::Value::Object info = array[0].GetObject();

		unique_ptr<application::Instance> instance = makeInstance();

		// Set the name and register the instance as event listener.
		string name = info[message::ApplicationInfo::NAME].GetString();
		instance->setName(name);
		registerEventListener(instance.get());

		int applicationId = info[message::ApplicationInfo::ID].GetInt();

		// test if the application is still alive otherwise we could have missed a status message
		if (isAlive(applicationId)) {

			instance->setId(applicationId);
			instance->setInitialState(info[message::ApplicationInfo::APPLICATION_STATE].GetInt());
			instance->setPastStates(info[message::ApplicationInfo::PAST_APPLICATION_STATES].GetInt());

			if (outputStream) {
legoc's avatar
legoc committed
311
				unique_ptr<OutputStreamSocket> streamSocket = createOutputStreamSocket(name);
312
313
314
315
316
317
318
319
320
321
				instance->setOutputStreamSocket(streamSocket);
			}

			return instance;
		}
	}

	return makeInstance();
}

legoc's avatar
legoc committed
322
323
324
325
326
327
328
329
330
331
332
333
void Server::killAllAndWaitFor(const std::string& name) {

	application::InstanceArray instances = connectAll(name);

	for (int i = 0; i < instances.size(); ++i) {
		instances[i]->kill();
		instances[i]->waitFor();
	}
}

bool Server::isAlive(int id) const {

334
	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createIsAliveRequest(id));
legoc's avatar
legoc committed
335

336
337
338
	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());
legoc's avatar
legoc committed
339

340
	return response[message::IsAliveResponse::IS_ALIVE].GetBool();
legoc's avatar
legoc committed
341
342
343
344
}

std::vector<application::Configuration> Server::getApplicationConfigurations() const {

345
346
	vector<application::Configuration> configs;

347
	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createListRequest());
legoc's avatar
legoc committed
348

349
350
351
	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());
legoc's avatar
legoc committed
352

353
	json::Value& applicationConfigs = response[message::ApplicationConfigListResponse::APPLICATION_CONFIG];
354
355
	json::Value::Array array = applicationConfigs.GetArray();
	size_t size = array.Size();
legoc's avatar
legoc committed
356

357
358
	for (int i = 0; i < size; ++i) {
		json::Value::Object config = array[i].GetObject();
legoc's avatar
legoc committed
359

360
361
362
363
364
365
		string name = config[message::ApplicationConfig::NAME].GetString();
		string description = config[message::ApplicationConfig::DESCRIPTION].GetString();
		bool runsSingle = config[message::ApplicationConfig::RUNS_SINGLE].GetBool();
		bool restart = config[message::ApplicationConfig::RESTART].GetBool();
		int startingTime = config[message::ApplicationConfig::STARTING_TIME].GetInt();
		int stoppingTime = config[message::ApplicationConfig::STOPPING_TIME].GetInt();
legoc's avatar
legoc committed
366

367
368
369
370
371
372
373
374
		application::Configuration applicationConfig(name,
				description,
				runsSingle,
				restart,
				startingTime,
				stoppingTime);

		configs.push_back(applicationConfig);
legoc's avatar
legoc committed
375
376
	}

377
	return configs;
legoc's avatar
legoc committed
378
379
380
381
}

std::vector<application::Info> Server::getApplicationInfos() const {

legoc's avatar
legoc committed
382
	vector<application::Info> infos;
legoc's avatar
legoc committed
383

legoc's avatar
legoc committed
384
	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createAppsRequest());
385
386
387
388
389

	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());

390
391
	json::Value& applicationInfos = response[message::ApplicationInfoListResponse::APPLICATION_INFO];
	json::Value::Array array = applicationInfos.GetArray();
392
	size_t size = array.Size();
legoc's avatar
legoc committed
393

394
395
	for (int i = 0; i < size; ++i) {
		json::Value::Object info = array[i].GetObject();
legoc's avatar
legoc committed
396

397
398
399
400
401
402
		string name = info[message::ApplicationInfo::NAME].GetString();
		int id = info[message::ApplicationInfo::ID].GetInt();
		int pid = info[message::ApplicationInfo::PID].GetInt();
		application::State state = info[message::ApplicationInfo::APPLICATION_STATE].GetInt();
		application::State pastStates = info[message::ApplicationInfo::PAST_APPLICATION_STATES].GetInt();
		string args = info[message::ApplicationInfo::ARGS].GetString();
legoc's avatar
legoc committed
403

404
405
406
407
408
409
		application::Info applicationInfo(name,
						id,
						pid,
						state,
						pastStates,
						args);
legoc's avatar
legoc committed
410

legoc's avatar
legoc committed
411
		infos.push_back(applicationInfo);
legoc's avatar
legoc committed
412
413
	}

legoc's avatar
legoc committed
414
	return infos;
legoc's avatar
legoc committed
415
416
417
418
}

std::vector<application::Info> Server::getApplicationInfos(const std::string& name) const {

419
420
	vector<application::Info> allInfos = getApplicationInfos();
	vector<application::Info> infos;
legoc's avatar
legoc committed
421

422
	for (vector<application::Info>::const_iterator i = allInfos.begin(); i != allInfos.end(); ++i) {
legoc's avatar
legoc committed
423
424
		application::Info const & info = *i;
		if (info.getName() == name) {
425
			infos.push_back(info);
legoc's avatar
legoc committed
426
427
428
		}
	}

429
	return infos;
legoc's avatar
legoc committed
430
431
}

legoc's avatar
legoc committed
432
std::vector<application::Port> Server::getPorts() const {
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450

	vector<application::Port> ports;

	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createPortsRequest());

	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());

	json::Value& portInfos = response[message::PortInfoListResponse::PORT_INFO];
	json::Value::Array array = portInfos.GetArray();
	size_t size = array.Size();

	for (int i = 0; i < size; ++i) {
		json::Value::Object info = array[i].GetObject();

		int port = info[message::PortInfo::PORT].GetInt();
		string status = info[message::PortInfo::STATUS].GetString();
legoc's avatar
legoc committed
451
		string owner = info[message::PortInfo::OWNER].GetString();
452

legoc's avatar
legoc committed
453
		application::Port portInfo(port, status, owner);
454
455
456
457
458
459
460

		ports.push_back(portInfo);
	}

	return ports;
}

461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
application::State Server::getActualState(int id) const {

	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createGetStatusRequest(id));

	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());

	return response[message::StatusEvent::APPLICATION_STATE].GetInt();
}

std::set<application::State> Server::getPastStates(int id) const {

	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createGetStatusRequest(id));

	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());

	application::State applicationStates = response[message::StatusEvent::PAST_APPLICATION_STATES].GetInt();

	set<application::State> result;

	if ((applicationStates & application::STARTING) != 0) {
		result.insert(application::STARTING);
	}

	if ((applicationStates & application::RUNNING) != 0) {
		result.insert(application::RUNNING);
	}

	if ((applicationStates & application::STOPPING) != 0) {
		result.insert(application::STOPPING);
	}

	if ((applicationStates & application::KILLING) != 0) {
		result.insert(application::KILLING);
	}

	if ((applicationStates & application::PROCESSING_ERROR) != 0) {
		result.insert(application::PROCESSING_ERROR);
	}

	if ((applicationStates & application::FAILURE) != 0) {
		result.insert(application::FAILURE);
	}

	if ((applicationStates & application::SUCCESS) != 0) {
		result.insert(application::SUCCESS);
	}

	if ((applicationStates & application::STOPPED) != 0) {
		result.insert(application::STOPPED);
	}

	if ((applicationStates & application::KILLED) != 0) {
		result.insert(application::KILLED);
	}

	return result;
}

legoc's avatar
legoc committed
523
std::unique_ptr<EventStreamSocket> Server::openEventStream() {
legoc's avatar
legoc committed
524
525
526
	return Services::openEventStream();
}

527
std::unique_ptr<application::Subscriber> Server::createSubscriber(int id, const std::string& publisherName, const std::string& instanceName) {
legoc's avatar
legoc committed
528

529
	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createConnectPublisherRequest(id, publisherName));
legoc's avatar
legoc committed
530

531
532
533
	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());
legoc's avatar
legoc committed
534

535
	int publisherPort = response[message::PublisherResponse::PUBLISHER_PORT].GetInt();
legoc's avatar
legoc committed
536
	if (publisherPort == -1) {
537
		throw SubscriberCreationException(response[message::PublisherResponse::MESSAGE].GetString());
legoc's avatar
legoc committed
538
539
	}

540
541
	int synchronizerPort = response[message::PublisherResponse::SYNCHRONIZER_PORT].GetInt();
	int numberOfSubscribers = response[message::PublisherResponse::NUMBER_OF_SUBSCRIBERS].GetInt();
legoc's avatar
legoc committed
542

legoc's avatar
legoc committed
543
	// TODO simplify the use of some variables: e.g. m_serverEndpoint accessible from this.
544
	unique_ptr<application::Subscriber> subscriber(new application::Subscriber(this, publisherPort, synchronizerPort, publisherName, numberOfSubscribers, instanceName, id, m_serverEndpoint.toString(), m_serverEndpoint.withPort(m_statusPort).toString()));
legoc's avatar
legoc committed
545
546
547
548
549
	subscriber->init();

	return subscriber;
}

legoc's avatar
legoc committed
550
std::unique_ptr<ConnectionChecker> Server::createConnectionChecker(ConnectionCheckerType handler, int pollingTimeMs) {
551

legoc's avatar
legoc committed
552
	unique_ptr<ConnectionChecker> connectionChecker(new ConnectionChecker(this, handler));
553
	connectionChecker->startThread(getAvailableTimeout(), pollingTimeMs);
legoc's avatar
legoc committed
554

555
	return connectionChecker;
legoc's avatar
legoc committed
556
557
}

558
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
594
595
596
597
598
599
600
601
602
603
604
605
void Server::storeKeyValue(int id, const std::string& key, const std::string& value) {

	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createStoreKeyValueRequest(id, key, value));

	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());
}

std::string Server::getKeyValue(int id, const std::string& key) {

	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createGetKeyValueRequest(id, key));

	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());

	int value = response[message::RequestResponse::VALUE].GetInt();
	if (value == 0) {
		return response[message::RequestResponse::MESSAGE].GetString();
	}
	else if (value == -1) {
		throw UndefinedApplicationException(response[message::RequestResponse::MESSAGE].GetString());
	}
	else if (value == -2) {
		throw UndefinedKeyException(response[message::RequestResponse::MESSAGE].GetString());
	}

	return "";
}

void Server::removeKey(int id, const std::string& key) {

	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createRemoveKeyRequest(id, key));

	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());

	int value = response[message::RequestResponse::VALUE].GetInt();
	if (value == -1) {
		throw UndefinedApplicationException(response[message::RequestResponse::MESSAGE].GetString());
	}
	else if (value == -2) {
		throw UndefinedKeyException(response[message::RequestResponse::MESSAGE].GetString());
	}
}

606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
int Server::requestPort(int id) {

	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createRequestPortRequest(id));

	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());

	int value = response[message::RequestResponse::VALUE].GetInt();
	if (value == -1) {
		throw UndefinedApplicationException(response[message::RequestResponse::MESSAGE].GetString());
	}

	return value;
}

void Server::setPortUnavailable(int id, int port) {

	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createPortUnavailableRequest(id, port));

	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());

	int value = response[message::RequestResponse::VALUE].GetInt();
	if (value == -1) {
		throw UndefinedApplicationException(response[message::RequestResponse::MESSAGE].GetString());
	}
}

void Server::releasePort(int id, int port) {

	unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createReleasePortRequest(id, port));

	// Get the JSON response.
	json::Object response;
	json::parse(response, reply.get());

	int value = response[message::RequestResponse::VALUE].GetInt();
	if (value == -1) {
		throw UndefinedApplicationException(response[message::RequestResponse::MESSAGE].GetString());
	}
}

650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
std::vector<EventListener *> Server::getEventListeners() {
	std::unique_lock<std::mutex> lock(m_eventListenersMutex);
	return m_eventListeners;
}

void Server::registerEventListener(EventListener * listener) {
	std::unique_lock<std::mutex> lock(m_eventListenersMutex);
	m_eventListeners.push_back(listener);
}

void Server::unregisterEventListener(EventListener * listener) {
	std::unique_lock<std::mutex> lock(m_eventListenersMutex);

	// Iterate to find the listener.
	for (auto it = m_eventListeners.begin(); it != m_eventListeners.end(); ++it) {
		if (*it == listener) {
			m_eventListeners.erase(it);
			break;
		}
	}
}

legoc's avatar
legoc committed
672
673
std::ostream& operator<<(std::ostream& os, const cameo::Server& server) {

674
	os << "server@" << server.m_serverEndpoint.toString();
legoc's avatar
legoc committed
675
676
677
678
679

	return os;
}

}