Server.cpp 10.4 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 "Server.h"
legoc's avatar
legoc committed
18
19
20

#include <iostream>
#include <sstream>
21
#include "Application.h"
22
#include "ConnectionChecker.h"
23
24
25
#include "impl/ServicesImpl.h"
#include "impl/SocketImpl.h"
#include "ProtoType.h"
legoc's avatar
legoc committed
26
27
28
29
30
31

using namespace std;

namespace cameo {

Server::Server(const std::string& endpoint) :
32
	Services() {
legoc's avatar
legoc committed
33
34
35
36
37
38
39

	m_impl = new ServicesImpl();
	Services::setImpl(m_impl);

	vector<string> tokens = split(endpoint);

	if (tokens.size() < 3) {
40
		throw InvalidArgumentException(endpoint + " is not a valid endpoint");
legoc's avatar
legoc committed
41
42
43
44
45
46
47
48
49
50
51
52
	}

	m_url = tokens[0] + ":" + tokens[1];
	string port = tokens[2];
	istringstream is(port);
	is >> m_port;
	m_serverEndpoint = m_url + ":" + port;
}

Server::~Server() {
}

legoc's avatar
legoc committed
53
void Server::setTimeout(int timeoutMs) {
54
	Services::setTimeout(timeoutMs);
legoc's avatar
legoc committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
}

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

const std::string& Server::getEndpoint() const {
	return Services::getEndpoint();
}

const std::string& Server::getUrl() const {
	return Services::getUrl();
}

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

bool Server::isAvailable(int timeout) const {
	return Services::isAvailable(timeout);
}

legoc's avatar
legoc committed
77
bool Server::isAvailable() const {
78
79
	return isAvailable(getAvailableTimeout());
}
legoc's avatar
legoc committed
80

81
int Server::getAvailableTimeout() const {
legoc's avatar
legoc committed
82
83
	int timeout = getTimeout();
	if (timeout > 0) {
84
85
86
87
		return timeout;
	}
	else {
		return 10000;
legoc's avatar
legoc committed
88
89
90
	}
}

legoc's avatar
legoc committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
std::auto_ptr<application::Instance> Server::makeInstance() {
	auto_ptr<EventStreamSocket> socket = Services::openEventStream();
	return auto_ptr<application::Instance>(new application::Instance(this, socket));
}

std::auto_ptr<application::Instance> Server::start(const std::string& name, Option options) {
	return start(name, vector<string>(), options);
}

std::auto_ptr<application::Instance> Server::start(const std::string& name, const std::vector<std::string> & args, Option options) {

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

	instance->setName(name);
	string strRequestType = m_impl->createRequest(PROTO_START);
	string strRequestData = m_impl->createStartRequest(name, args, application::This::getReference());

	try {
109
		auto_ptr<zmq::message_t> reply = m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, m_serverEndpoint);
legoc's avatar
legoc committed
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

		proto::RequestResponse requestResponse;
		requestResponse.ParseFromArray((*reply).data(), (*reply).size());

		if (requestResponse.value() == -1) {
			instance->setErrorMessage(requestResponse.message());
		} else {
			instance->setId(requestResponse.value());
		}

	} catch (const ConnectionTimeout& e) {
		instance->setErrorMessage(e.what());
	}

	return instance;
}

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

	string strRequestType;
	string strRequestData;

	if (immediately) {
		strRequestType = m_impl->createRequest(PROTO_KILL);
		strRequestData = m_impl->createKillRequest(id);
	} else {
		strRequestType = m_impl->createRequest(PROTO_STOP);
		strRequestData = m_impl->createStopRequest(id);
	}

140
	auto_ptr<zmq::message_t> reply = m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, m_serverEndpoint);
legoc's avatar
legoc committed
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

	proto::RequestResponse requestResponse;
	requestResponse.ParseFromArray((*reply).data(), (*reply).size());

	return Response(requestResponse.value(), requestResponse.message());
}

std::auto_ptr<application::Instance> Server::stop(int id, bool immediately) {

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

	try {
		Response response = stopApplicationAsynchronously(id, immediately);

		if (response.getValue() != -1) {
			// we get the name in the message attribute
			instance->setName(response.getMessage());
			instance->setId(id);

		} else {
			instance->setErrorMessage(response.getMessage());
		}

	} catch (const ConnectionTimeout& e) {
		instance->setErrorMessage(e.what());
	}

	return instance;
}

application::InstanceArray Server::connectAll(const std::string& name) {

	application::InstanceArray instances;

	string strRequestType = m_impl->createRequest(PROTO_CONNECT);
	string strRequestData = m_impl->createConnectRequest(name);
177
	auto_ptr<zmq::message_t> reply = m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, m_serverEndpoint);
legoc's avatar
legoc committed
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

	proto::ApplicationInfoListResponse response;
	response.ParseFromArray((*reply).data(), (*reply).size());

	// allocate the array
	instances.allocate(response.applicationinfo_size());

	int aliveInstancesCount = 0;

	for (int i = 0; i < response.applicationinfo_size(); ++i) {
		proto::ApplicationInfo info = response.applicationinfo(i);

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

		instance->setName(info.name());
		int applicationId = info.id();

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

			instance->setId(applicationId);
			instance->setInitialState(info.applicationstate());
			instance->setPastStates(info.pastapplicationstates());

			instances.m_array[i] = instance;
		}
	}

	// Copy the instances alive
	application::InstanceArray aliveInstances;
	aliveInstances.allocate(aliveInstancesCount);

	int j = 0;
	for (int i = 0; i < response.applicationinfo_size(); ++i) {

		if (instances.m_array[i].get() != 0) {
			aliveInstances[j] = instances.m_array[i];
			j++;
		}
	}

	return aliveInstances;
}

std::auto_ptr<application::Instance> Server::connect(const std::string& name) {

	application::InstanceArray instances = connectAll(name);

	if (instances.size() == 0) {
		auto_ptr<application::Instance> instance = makeInstance();
		return instance;
	}

	return instances[0];
}

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 {

	string strRequestType = m_impl->createRequest(PROTO_ISALIVE);
	string strRequestData = m_impl->createIsAliveRequest(id);
249
	auto_ptr<zmq::message_t> reply = m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, m_serverEndpoint);
legoc's avatar
legoc committed
250
251
252
253
254
255
256
257
258
259
260
261
262

	proto::IsAliveResponse isAliveResponse;
	isAliveResponse.ParseFromArray((*reply).data(), (*reply).size());

	return isAliveResponse.isalive();
}

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

	vector<application::Configuration> configVector;

	string strRequestType = m_impl->createRequest(PROTO_ALLAVAILABLE);
	string strRequestData = m_impl->createAllAvailableRequest();
263
	auto_ptr<zmq::message_t> reply = m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, m_serverEndpoint);
legoc's avatar
legoc committed
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

	proto::AllAvailableResponse allAvailableResponse;
	allAvailableResponse.ParseFromArray((*reply).data(), (*reply).size());

	for (int i = 0; i < allAvailableResponse.applicationconfig_size(); ++i) {
		proto::ApplicationConfig config = allAvailableResponse.applicationconfig(i);

		application::Configuration applicationConfig(config.name(),
				config.description(),
				config.runssingle(),
				config.restart(),
				config.startingtime(),
				config.retries(),
				config.stoppingtime());

		configVector.push_back(applicationConfig);
	}

	return configVector;
}

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

	vector<application::Info> infoVector;

	string strRequestType = m_impl->createRequest(PROTO_SHOWALL);
	string strRequestData = m_impl->createShowAllRequest();
291
	auto_ptr<zmq::message_t> reply = m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, m_serverEndpoint);
legoc's avatar
legoc committed
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

	proto::ApplicationInfoListResponse response;
	response.ParseFromArray((*reply).data(), (*reply).size());

	for (int i = 0; i < response.applicationinfo_size(); ++i) {
		proto::ApplicationInfo info = response.applicationinfo(i);

		application::Info applicationInfo(info.name(),
						info.id(),
						info.applicationstate(),
						info.pastapplicationstates(),
						info.args());

		infoVector.push_back(applicationInfo);
	}

	return infoVector;
}

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

	vector<application::Info> allInfoVector = getApplicationInfos();
	vector<application::Info> infoVector;

	for (vector<application::Info>::const_iterator i = allInfoVector.begin(); i != allInfoVector.end(); ++i) {
		application::Info const & info = *i;
		if (info.getName() == name) {
			infoVector.push_back(info);
		}
	}

	return infoVector;
}

std::auto_ptr<EventStreamSocket> Server::openEventStream() {
	return Services::openEventStream();
}

std::auto_ptr<application::Subscriber> Server::createSubscriber(int id, const std::string& publisherName, const std::string& instanceName) const {

	string strRequestType = m_impl->createRequest(PROTO_CONNECTPUBLISHER);
	string strRequestData = m_impl->createConnectPublisherRequest(id, publisherName);

335
	auto_ptr<zmq::message_t> reply = m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, m_serverEndpoint);
legoc's avatar
legoc committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
	proto::PublisherResponse requestResponse;
	requestResponse.ParseFromArray((*reply).data(), (*reply).size());

	int publisherPort = requestResponse.publisherport();
	if (publisherPort == -1) {
		throw SubscriberCreationException(requestResponse.message());
	}

	int synchronizerPort = requestResponse.synchronizerport();
	int numberOfSubscribers = requestResponse.numberofsubscribers();

	auto_ptr<application::Subscriber> subscriber(new application::Subscriber(this, getUrl(), publisherPort, synchronizerPort, publisherName, numberOfSubscribers, instanceName, id, m_serverEndpoint, m_serverStatusEndpoint));
	subscriber->init();

	return subscriber;
}

353
354
355
356
std::auto_ptr<ConnectionChecker> Server::createConnectionChecker(ConnectionCheckerType handler, int pollingTimeMs) {

	auto_ptr<ConnectionChecker> connectionChecker(new ConnectionChecker(this, handler));
	connectionChecker->startThread(getAvailableTimeout(), pollingTimeMs);
legoc's avatar
legoc committed
357

358
	return connectionChecker;
legoc's avatar
legoc committed
359
360
}

legoc's avatar
legoc committed
361
362
363
364
365
366
367
368
std::ostream& operator<<(std::ostream& os, const cameo::Server& server) {

	os << "server@" << server.m_serverEndpoint;

	return os;
}

}