Server.h 3.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
/*
 * 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
52
53
	Server(const std::string& endpoint);
	~Server();

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

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

legoc's avatar
legoc committed
67
68
	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
69
70
	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
71
72
73
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;

	/**
	 * throws ConnectionTimeout
	 */
legoc's avatar
legoc committed
95
	std::unique_ptr<EventStreamSocket> openEventStream();
legoc's avatar
legoc committed
96

legoc's avatar
legoc committed
97
	/**
98
	 * Creates a connection handler with polling time.
legoc's avatar
legoc committed
99
	 */
legoc's avatar
legoc committed
100
	std::unique_ptr<ConnectionChecker> createConnectionChecker(ConnectionCheckerType handler, int pollingTimeMs = 10000);
legoc's avatar
legoc committed
101

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
	/**
	 * 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
117
private:
legoc's avatar
legoc committed
118
	std::unique_ptr<application::Instance> makeInstance();
legoc's avatar
legoc committed
119
120
	bool isAlive(int id) const;
	Response stopApplicationAsynchronously(int id, bool immediately) const;
121
	std::unique_ptr<application::Subscriber> createSubscriber(int id, const std::string& publisherName, const std::string& instanceName);
122
	int getAvailableTimeout() const;
legoc's avatar
legoc committed
123
	int getStreamPort(const std::string& name);
124
125
126

	std::mutex m_eventListenersMutex;
	std::vector<EventListener *> m_eventListeners;
127
	std::unique_ptr<EventThread> m_eventThread;
legoc's avatar
legoc committed
128
129
130
131
132
133
134
};

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

}

#endif