Application.h 12.6 KB
Newer Older
legoc's avatar
legoc committed
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
/*
 * 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.
 */

#ifndef CAMEO_APPLICATION_H_
#define CAMEO_APPLICATION_H_

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <vector>
#include <set>
#include <memory>
#include <stdint.h>
#include "../cameo/ConnectionTimeout.h"
#include "../cameo/PublisherCreationException.h"
#include "../cameo/RequesterCreationException.h"
#include "../cameo/ResponderCreationException.h"
#include "../cameo/Response.h"
#include "../cameo/Services.h"
#include "../cameo/StateException.h"

namespace cameo {

enum Option {
	NONE = 0
};

class Server;
class EventStreamSocket;
class ApplicationImpl;
class PublisherImpl;
class SubscriberImpl;
class RequestImpl;
class ResponderImpl;
class RequesterImpl;
class WaitingImpl;
class SocketWaitingImpl;
class GenericWaitingImpl;
class WaitingImplSet;

namespace application {

// forward declarations
class Publisher;
class Subscriber;
class Responder;
class Requester;
class Instance;
class Waiting;

typedef int32_t State;

const State UNKNOWN = 0;
const State STARTING = 1;
const State RUNNING = 2;
const State STOPPING = 4;
const State KILLING = 8;
const State PROCESSING_ERROR = 16;
const State ERROR = 32;
const State SUCCESS = 64;
const State STOPPED = 128;
const State KILLED = 256;


class This : private Services {

	friend class cameo::application::Publisher;
	friend class cameo::application::Responder;
	friend class cameo::application::Requester;
	friend class cameo::PublisherImpl;
	friend class cameo::RequestImpl;
	friend class cameo::ResponderImpl;
	friend class cameo::RequesterImpl;
	friend class cameo::ApplicationImpl;
	friend class cameo::SocketWaitingImpl;
	friend class cameo::GenericWaitingImpl;
	friend class cameo::Server;
	friend std::ostream& operator<<(std::ostream&, const cameo::application::This&);

	typedef boost::function<void ()> StopFunctionType;

public:
	static void init(int argc, char *argv[]);
	static void terminate();

	static const std::string& getName();
	static int getId();
	static void setTimeout(int timeout);
	static int getTimeout();
102
	static const std::string& getEndpoint();
legoc's avatar
legoc committed
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
	static Server& getServer();

	/**
	 * throws StarterServerException.
	 */
	static Server& getStarterServer();
	static const std::string& getUrl();
	static bool isAvailable(int timeout = 10000);
	static bool isStopping();

	template<typename Type>
	static void handleStop(Type function) {
		m_instance->handleStopImpl(function);
	}

	static void cancelWaitings();

	/**
	 * throws StateException.
	 */
	static void setRunning();

	/**
	 * Sets the result.
	 */
	static void setBinaryResult(const std::string& data);
	static void setResult(const std::string& data);
	static void setResult(const int32_t* data, std::size_t size);
	static void setResult(const int64_t* data, std::size_t size);
	static void setResult(const float* data, std::size_t size);
	static void setResult(const double* data, std::size_t size);

	/**
	 * Connects to the starter application, i.e. the application which started this application.
	 */
	static std::auto_ptr<Instance> connectToStarter();

private:
	This(int argc, char *argv[]);
	~This();
	void init();
	static std::string getReference();
	static State parseState(const std::string& value);
	State getState(int id) const;
	bool destroyPublisher(const std::string& name) const;
	bool removePort(const std::string& name) const;
	State waitForStop();
	void handleStopImpl(StopFunctionType function);

	ApplicationImpl * m_impl;
	std::string m_name;
	int m_id;

	std::string m_starterEndpoint;
	std::string m_starterName;
	int m_starterId;

	std::auto_ptr<Server> m_server;
	std::auto_ptr<Server> m_starterServer;

	std::auto_ptr<WaitingImplSet> m_waitingSet;

	static This * m_instance;
	static const std::string RUNNING_STATE;
};


class Instance {

	friend class cameo::Server;
	friend class cameo::application::Subscriber;
	friend std::ostream& operator<<(std::ostream&, const Instance&);

public:
	~Instance();

	const std::string& getName() const;
	int getId() const;
	const std::string& getUrl() const;
	const std::string& getEndpoint() const;
	std::string getNameId() const;
	bool hasResult() const;
	bool exists() const;
	const std::string& getErrorMessage() const;
	State getInitialState() const;
	bool stop();
	bool kill();
	State waitFor(int states = 0);
	State waitFor(int states, const std::string& eventName);
	void cancelWaitFor();

	bool getBinaryResult(std::string& result);
	bool getResult(std::string& result);
	bool getResult(std::vector<int32_t>& result);
	bool getResult(std::vector<int64_t>& result);
	bool getResult(std::vector<float>& result);
	bool getResult(std::vector<double>& result);

private:
	Instance(const Server * server, std::auto_ptr<EventStreamSocket>& socket);

	void setId(int id);
	void setName(const std::string& name);
	void setErrorMessage(const std::string& message);
	void setPastStates(State pastStates);
	void setInitialState(State state);

	const Server * m_server;
	std::auto_ptr<EventStreamSocket> m_eventSocket;
	std::auto_ptr<WaitingImpl> m_waiting;
	std::string m_name;
	int m_id;
	std::string m_errorMessage;
	int m_pastStates;
	State m_initialState;
	State m_lastState;
	bool m_hasResult;
	std::string m_resultData;
};

///////////////////////////////////////////////////////////////////////////
// InstanceArray

class InstanceArray {

	friend class cameo::Server;

public:
	InstanceArray(const InstanceArray& array);
	~InstanceArray();

	std::size_t size() const;
	std::auto_ptr<Instance>& operator[](std::size_t index);

private:
	InstanceArray();
	void allocate(std::size_t size);

	std::size_t m_size;
	std::auto_ptr<Instance>* m_array;
};

///////////////////////////////////////////////////////////////////////////
// Publisher

class Publisher {

	friend class cameo::application::This;
	friend std::ostream& operator<<(std::ostream&, const Publisher&);

public:
	~Publisher();

	/**
	 * Returns the publisher with name.
	 * throws PublisherCreationException.
	 */
	static std::auto_ptr<Publisher> create(const std::string& name, int numberOfSubscribers = 0);

	const std::string& getName() const;
	const std::string& getApplicationName() const;
	int getApplicationId() const;
	const std::string& getApplicationEndpoint() const;

	/**
	 * Returns true if the wait succeeds or false if it was canceled.
	 */
	bool waitForSubscribers() const;
	void cancelWaitForSubscribers();

	void sendBinary(const std::string& data) const;
	void send(const std::string& data) const;
	void send(const int32_t* data, std::size_t size) const;
	void send(const int64_t* data, std::size_t size) const;
	void send(const float* data, std::size_t size) const;
	void send(const double* data, std::size_t size) const;
	void sendEnd() const;
	bool hasEnded() const;

private:
	Publisher(const application::This * application, int publisherPort, int synchronizerPort, const std::string& name, int numberOfSubscribers);

	std::auto_ptr<PublisherImpl> m_impl;
	std::auto_ptr<WaitingImpl> m_waiting;
};

///////////////////////////////////////////////////////////////////////////
// Subscriber

class Subscriber {

	friend class cameo::Server;
	friend class cameo::application::Instance;
	friend std::ostream& operator<<(std::ostream&, const Subscriber&);

public:
	~Subscriber();

	static std::auto_ptr<Subscriber> create(Instance & instance, const std::string& publisherName);

	const std::string& getPublisherName() const;
	const std::string& getInstanceName() const;
	int getInstanceId() const;
	const std::string& getInstanceEndpoint() const;

	bool hasEnded() const;

	/**
	 * Returns false if the stream finishes.
	 */
	bool receiveBinary(std::string& data) const;
	bool receive(std::string& data) const;
	bool receive(std::vector<int32_t>& data) const;
	bool receive(std::vector<int64_t>& data) const;
	bool receive(std::vector<float>& data) const;
	bool receive(std::vector<double>& data) const;

	void cancel();

private:
	Subscriber(const Server * server, const std::string& url, int publisherPort, int synchronizerPort, const std::string& publisherName, int numberOfSubscribers, const std::string& instanceName, int instanceId, const std::string& instanceEndpoint, const std::string& statusEndpoint);
	void init();

	std::auto_ptr<SubscriberImpl> m_impl;
	std::auto_ptr<WaitingImpl> m_waiting;
};

///////////////////////////////////////////////////////////////////////////
// Request

class Request {

	friend class cameo::application::Responder;
	friend std::ostream& operator<<(std::ostream&, const Request&);

public:
	~Request();

legoc's avatar
legoc committed
341
342
	const std::string& getBinaryData() const;
	std::string getData() const;
legoc's avatar
legoc committed
343

legoc's avatar
legoc committed
344
345
	void replyBinary(const std::string& response);
	void reply(const std::string& response);
legoc's avatar
legoc committed
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483

private:
	Request(std::auto_ptr<RequestImpl> & impl);

	std::auto_ptr<RequestImpl> m_impl;
};

///////////////////////////////////////////////////////////////////////////
// Responder

class Responder {

	friend std::ostream& operator<<(std::ostream&, const Responder&);

public:
	~Responder();

	/**
	 * Returns the responder with name.
	 * throws ResponderCreationException.
	 */
	static std::auto_ptr<Responder> create(const std::string& name);

	const std::string& getName() const;

	void cancel();
	std::auto_ptr<Request> receive();
	bool hasEnded() const;

private:
	Responder(const application::This * application, int responderPort, const std::string& name);

	std::auto_ptr<ResponderImpl> m_impl;
	std::auto_ptr<WaitingImpl> m_waiting;
};

///////////////////////////////////////////////////////////////////////////
// Requester

class Requester {

friend std::ostream& operator<<(std::ostream&, const Requester&);

public:
	~Requester();

	/**
	 * Returns the responder with name.
	 * throws RequesterCreationException.
	 */
	static std::auto_ptr<Requester> create(Instance & instance, const std::string& name);

	const std::string& getName() const;

	void sendBinary(const std::string& request);
	void send(const std::string& request);

	bool receiveBinary(std::string& response);
	bool receive(std::string& response);

	void cancel();

private:
	Requester(const application::This * application, const std::string& url, int requesterPort, int responderPort, const std::string& name);

	std::auto_ptr<RequesterImpl> m_impl;
	std::auto_ptr<WaitingImpl> m_waiting;
};

///////////////////////////////////////////////////////////////////////////
// Configuration

class Configuration {

	friend std::ostream& operator<<(std::ostream&, const Configuration&);

public:
	Configuration(const std::string& name, const std::string& description, bool singleInfo, bool restart, int startingTime, int retries, int stoppingTime);

	const std::string& getName() const;
	const std::string& getDescription() const;
	bool hasSingleInstance() const;
	bool canRestart() const;
	int getStartingTime() const;
	int getRetries() const;
	int getStoppingTime() const;

private:
	std::string m_name;
	std::string m_description;
	bool m_singleInstance;
	bool m_restart;
	int m_startingTime;
	int m_retries;
	int m_stoppingTime;
};

///////////////////////////////////////////////////////////////////////////
// Info

class Info {

	friend std::ostream& operator<<(std::ostream&, const Info&);

public:
	Info(const std::string& name, int id, State applicationState, State pastApplicationStates, const std::string& args);

	int getId() const;
	State getState() const;
	State getPastStates() const;
	const std::string& getArgs() const;
	const std::string& getName() const;

private:
	int m_id;
	State m_applicationState;
	State m_pastApplicationStates;
	std::string m_processState;
	std::string m_args;
	std::string m_name;
};

std::string toString(cameo::application::State applicationStates);
std::ostream& operator<<(std::ostream&, const cameo::application::This&);
std::ostream& operator<<(std::ostream&, const cameo::application::Instance&);
std::ostream& operator<<(std::ostream&, const cameo::application::Publisher&);
std::ostream& operator<<(std::ostream&, const cameo::application::Subscriber&);
std::ostream& operator<<(std::ostream&, const cameo::application::Request&);
std::ostream& operator<<(std::ostream&, const cameo::application::Responder&);
std::ostream& operator<<(std::ostream&, const cameo::application::Requester&);
std::ostream& operator<<(std::ostream&, const cameo::application::Configuration&);
std::ostream& operator<<(std::ostream&, const cameo::application::Info&);

}
}


#endif