Server.h 4.36 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

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

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

legoc's avatar
legoc committed
57
	int getTimeout() const;
58
	const Endpoint& getEndpoint() const;
legoc's avatar
legoc committed
59
	const std::string& getUrl() const;
legoc's avatar
legoc committed
60
	std::array<int, 3> getVersion() const;
legoc's avatar
legoc committed
61
	int getPort() const;
legoc's avatar
legoc committed
62
63
64
65
66
67
	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
68

legoc's avatar
legoc committed
69
70
	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
71
72
	application::InstanceArray connectAll(const std::string& name, Option options = NONE);
	std::unique_ptr<application::Instance> connect(const std::string& name, Option options = NONE);
73
	std::unique_ptr<application::Instance> connect(int id, Option options = NONE);
legoc's avatar
legoc committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

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

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

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

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

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

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

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
	/**
	 * 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
135
private:
136
	void initServer(const Endpoint& endpoint, int timeoutMs);
legoc's avatar
legoc committed
137
	std::unique_ptr<application::Instance> makeInstance();
legoc's avatar
legoc committed
138
	bool isAlive(int id) const;
139

legoc's avatar
legoc committed
140
	Response stopApplicationAsynchronously(int id, bool immediately) const;
141
	std::unique_ptr<application::Subscriber> createSubscriber(int id, const std::string& publisherName, const std::string& instanceName);
142
	int getAvailableTimeout() const;
143

144
145
146
	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);
147

148
149
150
151
	int requestPort(int id);
	void setPortUnavailable(int id, int port);
	void releasePort(int id, int port);

152
153
	std::mutex m_eventListenersMutex;
	std::vector<EventListener *> m_eventListeners;
154
	std::unique_ptr<EventThread> m_eventThread;
legoc's avatar
legoc committed
155
156
157
158
159
160
161
};

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

}

#endif