Application.h 15 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
/*
 * 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_

legoc's avatar
legoc committed
20
#include <functional>
legoc's avatar
legoc committed
21
22
23
#include <vector>
#include <set>
#include <memory>
24
#include "InvalidArgumentException.h"
25
#include "UnmanagedApplicationException.h"
26
#include "SocketException.h"
27
28
29
30
#include "ConnectionTimeout.h"
#include "PublisherCreationException.h"
#include "RequesterCreationException.h"
#include "ResponderCreationException.h"
31
32
#include "UndefinedApplicationException.h"
#include "UndefinedKeyException.h"
33
#include "Response.h"
34
#include "Serializer.h"
35
#include "Services.h"
legoc's avatar
legoc committed
36
#include "TimeCondition.h"
37
#include "EventListener.h"
38
#include "KeyValue.h"
legoc's avatar
legoc committed
39
40
41
42

namespace cameo {

enum Option {
legoc's avatar
legoc committed
43
44
	NONE = 0,
	OUTPUTSTREAM = 1
legoc's avatar
legoc committed
45
46
47
48
};

class Server;
class EventStreamSocket;
legoc's avatar
legoc committed
49
class OutputStreamSocket;
legoc's avatar
legoc committed
50
51
52
53
54
55
56
57
58
class PublisherImpl;
class SubscriberImpl;
class RequestImpl;
class ResponderImpl;
class RequesterImpl;
class WaitingImpl;
class SocketWaitingImpl;
class GenericWaitingImpl;
class WaitingImplSet;
59
class HandlerImpl;
legoc's avatar
legoc committed
60
61
62
63
64
65
66
67
68
69
70
71
72

namespace application {

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

typedef int32_t State;

legoc's avatar
legoc committed
73
74
#undef ERROR

legoc's avatar
legoc committed
75
76
77
78
79
80
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;
legoc's avatar
legoc committed
81
const State FAILURE = 32;
legoc's avatar
legoc committed
82
83
84
85
86
const State SUCCESS = 64;
const State STOPPED = 128;
const State KILLED = 256;


87
class This : private Services, private EventListener {
legoc's avatar
legoc committed
88
89
90
91
92
93
94
95
96
97
98
99
100

	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::SocketWaitingImpl;
	friend class cameo::GenericWaitingImpl;
	friend class cameo::Server;
	friend std::ostream& operator<<(std::ostream&, const cameo::application::This&);

legoc's avatar
legoc committed
101
	typedef std::function<void ()> StopFunctionType;
legoc's avatar
legoc committed
102
103

public:
104
105
106
107
108
109
110
111
112
113
114
115
	/**
	 * Class defining the Communication Operations Manager (COM).
	 */
	class Com {

		friend class This;

	public:
		void storeKeyValue(const std::string& key, const std::string& value) const;
		std::string getKeyValue(const std::string& key) const;
		void removeKey(const std::string& key) const;

116
117
118
119
		int requestPort() const;
		void setPortUnavailable(int port) const;
		void releasePort(int port) const;

120
121
122
123
124
125
126
	private:
		Com(Server * server, int applicationId);

		Server * m_server;
		int m_applicationId;
	};

127
128
129
	This();
	~This();

legoc's avatar
legoc committed
130
	static void init(int argc, char *argv[]);
131
	static void init(const std::string& name, const std::string& endpoint);
132
133
134
135

	/**
	 * The terminate call is not necessary unless the static instance of This is not destroyed automatically.
	 */
legoc's avatar
legoc committed
136
137
138
139
140
141
	static void terminate();

	static const std::string& getName();
	static int getId();
	static void setTimeout(int timeout);
	static int getTimeout();
142
	static const Endpoint& getEndpoint();
legoc's avatar
legoc committed
143
	static Server& getServer();
144
	static const Com& getCom();
legoc's avatar
legoc committed
145
146
147
148
149
150
151
152
153
154

	/**
	 * throws StarterServerException.
	 */
	static Server& getStarterServer();
	static bool isAvailable(int timeout = 10000);
	static bool isStopping();

	template<typename Type>
	static void handleStop(Type function) {
155
		m_instance.handleStopImpl(function);
legoc's avatar
legoc committed
156
157
158
159
	}

	static void cancelWaitings();

160
	static bool setRunning();
legoc's avatar
legoc committed
161
162
163
164
165
166
167
168
169
170

	/**
	 * Sets the result.
	 */
	static void setBinaryResult(const std::string& data);
	static void setResult(const std::string& data);

	/**
	 * Connects to the starter application, i.e. the application which started this application.
	 */
legoc's avatar
legoc committed
171
	static std::unique_ptr<Instance> connectToStarter();
legoc's avatar
legoc committed
172
173

private:
174
	void initApplication(int argc, char *argv[]);
175
	void initApplication(const std::string& name, const std::string& endpoint);
legoc's avatar
legoc committed
176

legoc's avatar
legoc committed
177
178
	static State parseState(const std::string& value);
	State getState(int id) const;
legoc's avatar
legoc committed
179
180
181
182

	int initUnmanagedApplication();
	void terminateUnmanagedApplication();

legoc's avatar
legoc committed
183
184
185
	bool destroyPublisher(const std::string& name) const;
	bool removePort(const std::string& name) const;
	State waitForStop();
186
187

	void stoppingFunction(StopFunctionType stop);
legoc's avatar
legoc committed
188
189
190
191
	void handleStopImpl(StopFunctionType function);

	std::string m_name;
	int m_id;
legoc's avatar
legoc committed
192
	bool m_managed;
legoc's avatar
legoc committed
193

legoc's avatar
legoc committed
194
	Endpoint m_starterEndpoint;
legoc's avatar
legoc committed
195
196
197
	std::string m_starterName;
	int m_starterId;

legoc's avatar
legoc committed
198
199
	std::unique_ptr<Server> m_server;
	std::unique_ptr<Server> m_starterServer;
200
	std::unique_ptr<Com> m_com;
legoc's avatar
legoc committed
201

legoc's avatar
legoc committed
202
	std::unique_ptr<WaitingImplSet> m_waitingSet;
203
	std::unique_ptr<HandlerImpl> m_stopHandler;
legoc's avatar
legoc committed
204

205
	static This m_instance;
legoc's avatar
legoc committed
206
207
208
209
	static const std::string RUNNING_STATE;
};


210
class Instance : private EventListener {
legoc's avatar
legoc committed
211
212
213
214
215
216

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

public:
legoc's avatar
legoc committed
217
	typedef std::function<void (State)> StateHandlerType;
legoc's avatar
legoc committed
218

219
220
221
222
223
224
225
226
227
228
229
230
231
232
	class Com {

		friend class Instance;

	public:
		std::string getKeyValue(const std::string& key) const;

	private:
		Com(Server * server);

		Server * m_server;
		int m_applicationId;
	};

legoc's avatar
legoc committed
233
234
	~Instance();

legoc's avatar
legoc committed
235
	const std::string& getName() const;
legoc's avatar
legoc committed
236
	int getId() const;
237
	const Endpoint& getEndpoint() const;
legoc's avatar
legoc committed
238
	std::string getNameId() const;
239
240
	const Com& getCom() const;

legoc's avatar
legoc committed
241
242
243
244
245
	bool hasResult() const;
	bool exists() const;
	const std::string& getErrorMessage() const;
	bool stop();
	bool kill();
legoc's avatar
legoc committed
246

legoc's avatar
legoc committed
247
248
	State waitFor(StateHandlerType handler = nullptr);
	State waitFor(int states, StateHandlerType handler = nullptr);
249
	State waitFor(const std::string& eventName);
250
	State waitFor(KeyValue& keyValue);
legoc's avatar
legoc committed
251

legoc's avatar
legoc committed
252
253
	void cancelWaitFor();

254
255
256
257
	/**
	 * Deprecated.
	 * TODO remove in next version.
	 */
258
259
	State now();

260
261
262
263
264
265
266
267
268
269
	/**
	 * Gets the last state.
	 */
	State getLastState();

	/**
	 * Returns the actual state and UNKNOWN if the instance does not exist anymore.
	 */
	State getActualState() const;

270
271
272
273
274
	/**
	 * Returns the past states.
	 */
	std::set<State> getPastStates() const;

275
276
277
278
279
	/**
	 * Returns the exit code.
	 */
	int getExitCode() const;

legoc's avatar
legoc committed
280
281
282
	bool getBinaryResult(std::string& result);
	bool getResult(std::string& result);

legoc's avatar
legoc committed
283
284
	std::shared_ptr<OutputStreamSocket> getOutputStreamSocket();

legoc's avatar
legoc committed
285
private:
286
	Instance(Server * server);
legoc's avatar
legoc committed
287
288
289

	void setId(int id);
	void setErrorMessage(const std::string& message);
legoc's avatar
legoc committed
290
	void setOutputStreamSocket(std::unique_ptr<OutputStreamSocket>& socket);
legoc's avatar
legoc committed
291
292
	void setPastStates(State pastStates);
	void setInitialState(State state);
293
	State waitFor(int states, const std::string& eventName, KeyValue& keyValue, StateHandlerType handler, bool blocking);
legoc's avatar
legoc committed
294

295
	Server * m_server;
legoc's avatar
legoc committed
296
	std::shared_ptr<OutputStreamSocket> m_outputStreamSocket;
legoc's avatar
legoc committed
297
298
	int m_id;
	std::string m_errorMessage;
299
300
	Com m_com;

legoc's avatar
legoc committed
301
302
303
304
305
	int m_pastStates;
	State m_initialState;
	State m_lastState;
	bool m_hasResult;
	std::string m_resultData;
306
	int m_exitCode;
307
	std::unique_ptr<WaitingImpl> m_waiting;
legoc's avatar
legoc committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
};

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

class InstanceArray {

	friend class cameo::Server;

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

	std::size_t size() const;
legoc's avatar
legoc committed
322
	std::unique_ptr<Instance>& operator[](std::size_t index);
legoc's avatar
legoc committed
323
324
325
326
327
328

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

	std::size_t m_size;
legoc's avatar
legoc committed
329
	std::unique_ptr<Instance>* m_array;
legoc's avatar
legoc committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
};

///////////////////////////////////////////////////////////////////////////
// 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.
	 */
legoc's avatar
legoc committed
347
	static std::unique_ptr<Publisher> create(const std::string& name, int numberOfSubscribers = 0);
legoc's avatar
legoc committed
348
349
350
351

	const std::string& getName() const;
	const std::string& getApplicationName() const;
	int getApplicationId() const;
352
	std::string getApplicationEndpoint() const;
legoc's avatar
legoc committed
353
354
355
356
357
358
359
360
361

	/**
	 * 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;
362
	void sendTwoBinaryParts(const std::string& data1, const std::string& data2) const;
legoc's avatar
legoc committed
363
	void sendEnd() const;
364
365
366
367
368

	/**
	 * Deprecated.
	 * TODO remove in next version.
	 */
legoc's avatar
legoc committed
369
370
	bool hasEnded() const;

371
372
	bool isEnded() const;

legoc's avatar
legoc committed
373
private:
legoc's avatar
legoc committed
374
	Publisher(application::This * application, int publisherPort, int synchronizerPort, const std::string& name, int numberOfSubscribers);
legoc's avatar
legoc committed
375

legoc's avatar
legoc committed
376
377
	std::unique_ptr<PublisherImpl> m_impl;
	std::unique_ptr<WaitingImpl> m_waiting;
legoc's avatar
legoc committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
};

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

class Subscriber {

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

public:
	~Subscriber();

legoc's avatar
legoc committed
392
	static std::unique_ptr<Subscriber> create(Instance & instance, const std::string& publisherName);
legoc's avatar
legoc committed
393
394
395
396
397
398

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

399
400
401
402
	/**
	 * Deprecated.
	 * TODO remove in next version.
	 */
legoc's avatar
legoc committed
403
404
	bool hasEnded() const;

405
406
407
	bool isEnded() const;
	bool isCanceled() const;

legoc's avatar
legoc committed
408
409
410
411
412
	/**
	 * Returns false if the stream finishes.
	 */
	bool receiveBinary(std::string& data) const;
	bool receive(std::string& data) const;
413
	bool receiveTwoBinaryParts(std::string& data1, std::string& data2) const;
legoc's avatar
legoc committed
414
415
416
417

	void cancel();

private:
418
	Subscriber(Server * server, int publisherPort, int synchronizerPort, const std::string& publisherName, int numberOfSubscribers, const std::string& instanceName, int instanceId, const std::string& instanceEndpoint, const std::string& statusEndpoint);
legoc's avatar
legoc committed
419
420
	void init();

legoc's avatar
legoc committed
421
422
	std::unique_ptr<SubscriberImpl> m_impl;
	std::unique_ptr<WaitingImpl> m_waiting;
legoc's avatar
legoc committed
423
424
425
426
427
428
429
430
431
432
433
434
435
};

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

class Request {

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

public:
	~Request();

legoc's avatar
legoc committed
436
	std::string getObjectId() const;
437
	std::string getRequesterEndpoint() const;
legoc's avatar
legoc committed
438

439
440
441
	const std::string& getBinary() const;
	std::string get() const;
	const std::string& getSecondBinaryPart() const;
legoc's avatar
legoc committed
442

legoc's avatar
legoc committed
443
	void setTimeout(int value);
444

legoc's avatar
legoc committed
445
446
	bool replyBinary(const std::string& response);
	bool reply(const std::string& response);
legoc's avatar
legoc committed
447

legoc's avatar
legoc committed
448
	std::unique_ptr<Instance> connectToRequester();
449

legoc's avatar
legoc committed
450
451
452
	/**
	 * Transfers the ownership of the requester server.
	 */
legoc's avatar
legoc committed
453
	std::unique_ptr<Server> getServer();
legoc's avatar
legoc committed
454

legoc's avatar
legoc committed
455
private:
legoc's avatar
legoc committed
456
	Request(std::unique_ptr<RequestImpl> & impl);
legoc's avatar
legoc committed
457

legoc's avatar
legoc committed
458
459
	std::unique_ptr<RequestImpl> m_impl;
	std::unique_ptr<Server> m_requesterServer;
legoc's avatar
legoc committed
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
};

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

class Responder {

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

public:
	~Responder();

	/**
	 * Returns the responder with name.
	 * throws ResponderCreationException.
	 */
legoc's avatar
legoc committed
476
	static std::unique_ptr<Responder> create(const std::string& name);
legoc's avatar
legoc committed
477
478
479
480

	const std::string& getName() const;

	void cancel();
legoc's avatar
legoc committed
481
	std::unique_ptr<Request> receive();
482
483

	bool isCanceled() const;
legoc's avatar
legoc committed
484
485

private:
legoc's avatar
legoc committed
486
	Responder(application::This * application, int responderPort, const std::string& name);
legoc's avatar
legoc committed
487

legoc's avatar
legoc committed
488
489
	std::unique_ptr<ResponderImpl> m_impl;
	std::unique_ptr<WaitingImpl> m_waiting;
legoc's avatar
legoc committed
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
};

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

class Requester {

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

public:
	~Requester();

	/**
	 * Returns the responder with name.
	 * throws RequesterCreationException.
	 */
legoc's avatar
legoc committed
506
	static std::unique_ptr<Requester> create(Instance & instance, const std::string& name);
legoc's avatar
legoc committed
507
508
509
510
511

	const std::string& getName() const;

	void sendBinary(const std::string& request);
	void send(const std::string& request);
512
	void sendTwoBinaryParts(const std::string& request1, const std::string& request2);
legoc's avatar
legoc committed
513
514
515
516
517
518

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

	void cancel();

519
520
	bool isCanceled() const;

legoc's avatar
legoc committed
521
private:
legoc's avatar
legoc committed
522
	Requester(application::This * application, const std::string& url, int requesterPort, int responderPort, const std::string& name, int responderId, int requesterId);
legoc's avatar
legoc committed
523

legoc's avatar
legoc committed
524
525
	std::unique_ptr<RequesterImpl> m_impl;
	std::unique_ptr<WaitingImpl> m_waiting;
legoc's avatar
legoc committed
526
527
528
529
530
531
532
533
534
535
};

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

class Configuration {

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

public:
536
	Configuration(const std::string& name, const std::string& description, bool singleInfo, bool restart, int startingTime, int stoppingTime);
legoc's avatar
legoc committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561

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

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

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

class Info {

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

public:
legoc's avatar
legoc committed
562
	Info(const std::string& name, int id, int pid, State applicationState, State pastApplicationStates, const std::string& args);
legoc's avatar
legoc committed
563
564
565
566
567
568

	int getId() const;
	State getState() const;
	State getPastStates() const;
	const std::string& getArgs() const;
	const std::string& getName() const;
legoc's avatar
legoc committed
569
	int getPid() const;
legoc's avatar
legoc committed
570
571
572

private:
	int m_id;
legoc's avatar
legoc committed
573
	int m_pid;
legoc's avatar
legoc committed
574
575
576
577
578
579
580
	State m_applicationState;
	State m_pastApplicationStates;
	std::string m_processState;
	std::string m_args;
	std::string m_name;
};

581
582
583
584
585
586
587
588
///////////////////////////////////////////////////////////////////////////
// Port

class Port {

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

public:
legoc's avatar
legoc committed
589
	Port(int port, const std::string& status, const std::string& owner);
590
591
592

	int getPort() const;
	const std::string& getStatus() const;
legoc's avatar
legoc committed
593
	const std::string& getOwner() const;
594
595
596
597

private:
	int m_port;
	std::string m_status;
legoc's avatar
legoc committed
598
	std::string m_owner;
599
600
};

legoc's avatar
legoc committed
601
602
603
604
605
606
607
608
609
610
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&);
611
std::ostream& operator<<(std::ostream&, const cameo::application::Port&);
legoc's avatar
legoc committed
612
613
614
615
616
617

}
}


#endif