Server.h 5 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_SERVER_H_
#define CAMEO_SERVER_H_

20
#include "Application.h"
21
#include "ConnectionChecker.h"
22
23
#include "ConnectionTimeout.h"
#include "Response.h"
24
25
26
27
28
29
30
#include "Strings.h"
#include "EventStreamSocket.h"
#include "OutputStreamSocket.h"
#include <vector>
#include <memory>
#include <mutex>
#include <array>
legoc's avatar
legoc committed
31
32
33
34
35
36
37

namespace cameo {

namespace application {
	class This;
}

38
class EventListener;
39
class EventThread;
legoc's avatar
legoc committed
40
class ContextZmq;
41
class RequestSocket;
42

43
class Server {
legoc's avatar
legoc committed
44
45
46
47
48
49

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

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

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

legoc's avatar
legoc committed
56
	void setTimeout(int value);
legoc's avatar
legoc committed
57

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

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

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

	/**
	 * 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;

legoc's avatar
legoc committed
94
95
96
	/**
	 * throws ConnectionTimeout
	 */
legoc's avatar
legoc committed
97
	std::vector<application::Port> getPorts() const;
legoc's avatar
legoc committed
98

99
100
101
102
103
104
105
106
107
108
	/**
	 * throws ConnectionTimeout
	 */
	application::State getActualState(int id) const;

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

legoc's avatar
legoc committed
109
110
111
	/**
	 * throws ConnectionTimeout
	 */
legoc's avatar
legoc committed
112
	std::unique_ptr<EventStreamSocket> openEventStream();
legoc's avatar
legoc committed
113

legoc's avatar
legoc committed
114
	/**
115
	 * Creates a connection handler with polling time.
legoc's avatar
legoc committed
116
	 */
legoc's avatar
legoc committed
117
	std::unique_ptr<ConnectionChecker> createConnectionChecker(ConnectionCheckerType handler, int pollingTimeMs = 10000);
legoc's avatar
legoc committed
118

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
	/**
	 * 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
134
135
136
137
138
139

	Context * getContext();

	Endpoint getStatusEndpoint() const;

	void sendSync();
legoc's avatar
legoc committed
140
141
142
	void sendSyncStream(const std::string& name);

	int getStreamPort(const std::string& name);
legoc's avatar
legoc committed
143

legoc's avatar
legoc committed
144
private:
145
	void initServer(const Endpoint& endpoint, int timeoutMs);
legoc's avatar
legoc committed
146
	std::unique_ptr<application::Instance> makeInstance();
legoc's avatar
legoc committed
147
	bool isAlive(int id) const;
148

legoc's avatar
legoc committed
149
	Response stopApplicationAsynchronously(int id, bool immediately) const;
150
	int getAvailableTimeout() const;
legoc's avatar
legoc committed
151

legoc's avatar
legoc committed
152
153
154
	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);
155

legoc's avatar
legoc committed
156
157
158
159
	int requestPort(int id);
	void setPortUnavailable(int id, int port);
	void releasePort(int id, int port);

160
161
	json::Object requestJSON(const std::string& request, int overrideTimeout = -1);
	json::Object requestJSON(const std::string& requestPart1, const std::string& requestPart2, int overrideTimeout = -1);
162

163
164
165
166
167
168
	void initContext();
	void initRequestSocket();

	void retrieveServerVersion();
	void initStatus();
	std::unique_ptr<OutputStreamSocket> createOutputStreamSocket(const std::string& name);
169
170
	std::unique_ptr<RequestSocket> createRequestSocket(const std::string& endpoint);
	std::unique_ptr<RequestSocket> createRequestSocket(const std::string& endpoint, int timeout);
171
172
173
174
175


	Endpoint m_serverEndpoint;
	std::array<int, 3> m_serverVersion;
	int m_statusPort;
legoc's avatar
legoc committed
176
	std::unique_ptr<ContextZmq> m_contextImpl;
177
	std::unique_ptr<RequestSocket> m_requestSocket;
178

179
180
	std::mutex m_eventListenersMutex;
	std::vector<EventListener *> m_eventListeners;
181
	std::unique_ptr<EventThread> m_eventThread;
legoc's avatar
legoc committed
182
183
184
185
186
187
188
};

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

}

#endif