Server.h 4.02 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
/*
 * 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_SERVER_H_
#define CAMEO_SERVER_H_

#include <vector>
#include <memory>
22
#include <mutex>
23
#include "Application.h"
24
#include "ConnectionChecker.h"
25
26
27
28
#include "ConnectionTimeout.h"
#include "Response.h"
#include "Services.h"
#include "SubscriberCreationException.h"
legoc's avatar
legoc committed
29
30
31
32
33
34
35

namespace cameo {

namespace application {
	class This;
}

36
class EventListener;
37
class EventThread;
38

legoc's avatar
legoc committed
39
40
41
42
43
44
45
46
47
48
class Server : private Services {

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

public:
legoc's avatar
legoc committed
49
	typedef std::function<void (bool)> ConnectionCheckerType;
legoc's avatar
legoc committed
50

legoc's avatar
legoc committed
51
	Server(const std::string& endpoint, int timeoutMs = 0);
legoc's avatar
legoc committed
52
53
	~Server();

legoc's avatar
legoc committed
54
55
	void setTimeout(int timeoutMs);

legoc's avatar
legoc committed
56
57
58
	int getTimeout() const;
	const std::string& getEndpoint() const;
	const std::string& getUrl() const;
legoc's avatar
legoc committed
59
	std::array<int, 3> getVersion() const;
legoc's avatar
legoc committed
60
	int getPort() const;
legoc's avatar
legoc committed
61
62
63
64
65
66
	bool isAvailable(int timeoutMs) const;

	/**
	 * Returns true if is available. Uses the timeout if set or 10000ms.
	 */
	bool isAvailable() const;
legoc's avatar
legoc committed
67

legoc's avatar
legoc committed
68
69
	std::unique_ptr<application::Instance> start(const std::string& name, const std::vector<std::string> &args, Option options = NONE);
	std::unique_ptr<application::Instance> start(const std::string& name, Option options = NONE);
legoc's avatar
legoc committed
70
71
	application::InstanceArray connectAll(const std::string& name, Option options = NONE);
	std::unique_ptr<application::Instance> connect(const std::string& name, Option options = NONE);
legoc's avatar
legoc committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

	/**
	 * throws ConnectionTimeout
	 */
	void killAllAndWaitFor(const std::string& name);

	/**
	 * throws ConnectionTimeout
	 */
	std::vector<application::Configuration> getApplicationConfigurations() const;

	/**
	 * throws ConnectionTimeout
	 */
	std::vector<application::Info> getApplicationInfos() const;

	/**
	 * throws ConnectionTimeout
	 */
	std::vector<application::Info> getApplicationInfos(const std::string& name) const;

93
94
95
96
97
98
99
100
101
102
	/**
	 * throws ConnectionTimeout
	 */
	application::State getActualState(int id) const;

	/**
	 * throws ConnectionTimeout
	 */
	std::set<application::State> getPastStates(int id) const;

legoc's avatar
legoc committed
103
104
105
	/**
	 * throws ConnectionTimeout
	 */
legoc's avatar
legoc committed
106
	std::unique_ptr<EventStreamSocket> openEventStream();
legoc's avatar
legoc committed
107

legoc's avatar
legoc committed
108
	/**
109
	 * Creates a connection handler with polling time.
legoc's avatar
legoc committed
110
	 */
legoc's avatar
legoc committed
111
	std::unique_ptr<ConnectionChecker> createConnectionChecker(ConnectionCheckerType handler, int pollingTimeMs = 10000);
legoc's avatar
legoc committed
112

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	/**
	 * Gets the event listeners. Copies the list.
	 */
	std::vector<EventListener *> getEventListeners();

	/**
	 * Registers an event listener.
	 */
	void registerEventListener(EventListener * listener);

	/**
	 * Unregisters an event listener.
	 */
	void unregisterEventListener(EventListener * listener);

legoc's avatar
legoc committed
128
private:
legoc's avatar
legoc committed
129
	std::unique_ptr<application::Instance> makeInstance();
legoc's avatar
legoc committed
130
131
	bool isAlive(int id) const;
	Response stopApplicationAsynchronously(int id, bool immediately) const;
132
	std::unique_ptr<application::Subscriber> createSubscriber(int id, const std::string& publisherName, const std::string& instanceName);
133
	int getAvailableTimeout() const;
legoc's avatar
legoc committed
134
	int getStreamPort(const std::string& name);
135
136
137
	void storeKeyValue(int id, const std::string& key, const std::string& value);
	std::string getKeyValue(int id, const std::string& key);
	void removeKey(int id, const std::string& key);
138
139
140

	std::mutex m_eventListenersMutex;
	std::vector<EventListener *> m_eventListeners;
141
	std::unique_ptr<EventThread> m_eventThread;
legoc's avatar
legoc committed
142
143
144
145
146
147
148
};

std::ostream& operator<<(std::ostream&, const Server&);

}

#endif