Line data Source code
1 :
2 : /* Copyright (c) 2017, Human Brain Project
3 : * Stefan.Eilemann@epfl.ch
4 : */
5 :
6 : #pragma once
7 :
8 : #include <zeroeq/api.h>
9 : #include <zeroeq/receiver.h> // base class
10 : #include <zeroeq/sender.h> // base class
11 : #include <zeroeq/types.h>
12 :
13 : namespace zeroeq
14 : {
15 : /**
16 : * Serves request from one or more Client(s).
17 : *
18 : * The session is tied to ZeroConf announcement and can be disabled by passing
19 : * zeroeq::NULL_SESSION as the session name.
20 : *
21 : * Example: @include tests/reqRep.cpp
22 : */
23 0 : class Server : public Receiver, public Sender
24 : {
25 : public:
26 : /**
27 : * Create a default server.
28 : *
29 : * Postconditions:
30 : * - bound to all network interfaces
31 : * - runs on a random port
32 : * - announces itself on the _zeroeq_rep._tcp ZeroConf service as host:port
33 : * - announces session \<username\> or ZEROEQ_SERVER_SESSION from
34 : * environment
35 : *
36 : * @throw std::runtime_error if session is empty or socket setup fails
37 : */
38 : ZEROEQ_API Server();
39 :
40 : /**
41 : * Create a server which announces itself using the specified session.
42 : *
43 : * Postconditions:
44 : * - bound to all network interfaces
45 : * - runs on a random port
46 : * - announces itself on the _zeroeq_rep._tcp ZeroConf service as host:port
47 : * - announces given session
48 : *
49 : * @param session session name used for announcement
50 : * @throw std::runtime_error if session is empty or socket setup fails
51 : */
52 : ZEROEQ_API explicit Server(const std::string& session);
53 :
54 : /**
55 : * Create a server which runs on the specified URI.
56 : *
57 : * Postconditions:
58 : * - bound to the host and/or port from the given URI
59 : * - announces itself on the _zeroeq_rep._tcp ZeroConf service as host:port
60 : * - announces session \<username\> or ZEROEQ_SERVER_SESSION from
61 : * environment
62 : *
63 : * @param uri publishing URI in the format [*|host|IP|IF][:port]
64 : * @throw std::runtime_error if session is empty or socket setup fails
65 : */
66 : ZEROEQ_API explicit Server(const URI& uri);
67 :
68 : /**
69 : * Create a server which runs on the specified URI and announces the
70 : * specified session.
71 : *
72 : * Postconditions:
73 : * - bound to the host and/or port from the given URI
74 : * - announces itself on the _zeroeq_rep._tcp ZeroConf service as host:port
75 : * - announces given session
76 : *
77 : * @param session session name used for announcement
78 : * @param uri publishing URI in the format [*|host|IP|IF][:port]
79 : * @throw std::runtime_error if session is empty or socket setup fails
80 : */
81 : ZEROEQ_API Server(const URI& uri, const std::string& session);
82 :
83 : /**
84 : * Create a default server.
85 : *
86 : * Postconditions:
87 : * - bound to all network interfaces
88 : * - runs on a random port
89 : * - announces itself on the _zeroeq_rep._tcp ZeroConf service as host:port
90 : * - announces session \<username\> or ZEROEQ_SERVER_SESSION from
91 : * environment
92 : *
93 : * @param shared another receiver to share data reception with
94 : * @throw std::runtime_error if session is empty or socket setup fails
95 : */
96 : ZEROEQ_API explicit Server(Receiver& shared);
97 :
98 : /**
99 : * Create a server which announces itself using the specified session.
100 : *
101 : * Postconditions:
102 : * - bound to all network interfaces
103 : * - runs on a random port
104 : * - announces itself on the _zeroeq_rep._tcp ZeroConf service as host:port
105 : * - announces given session
106 : *
107 : * @param session session name used for announcement
108 : * @param shared another receiver to share data reception with
109 : * @throw std::runtime_error if session is empty or socket setup fails
110 : */
111 : ZEROEQ_API Server(const std::string& session, Receiver& shared);
112 :
113 : /**
114 : * Create a server which runs on the specified URI.
115 : *
116 : * Postconditions:
117 : * - bound to the host and/or port from the given URI
118 : * - announces itself on the _zeroeq_rep._tcp ZeroConf service as host:port
119 : * - announces session \<username\> or ZEROEQ_SERVER_SESSION from
120 : * environment
121 : *
122 : * @param uri publishing URI in the format [*|host|IP|IF][:port]
123 : * @param shared another receiver to share data reception with
124 : * @throw std::runtime_error if session is empty or socket setup fails
125 : */
126 : ZEROEQ_API Server(const URI& uri, Receiver& shared);
127 :
128 : /**
129 : * Create a server which runs on the specified URI and announces the
130 : * specified session.
131 : *
132 : * Postconditions:
133 : * - bound to the host and/or port from the given URI
134 : * - announces itself on the _zeroeq_rep._tcp ZeroConf service as host:port
135 : * - announces given session
136 : *
137 : * @param session session name used for announcement
138 : * @param uri publishing URI in the format [*|host|IP|IF][:port]
139 : * @param shared another receiver to share data reception with
140 : * @throw std::runtime_error if session is empty or socket setup fails
141 : */
142 : ZEROEQ_API Server(const URI& uri, const std::string& session,
143 : Receiver& shared);
144 :
145 : /** Destroy this server. */
146 : ZEROEQ_API ~Server();
147 :
148 : ZEROEQ_API Server(Server&&);
149 : ZEROEQ_API Server& operator=(Server&&);
150 :
151 : /**
152 : * Register a request handler.
153 : *
154 : * Exceptions in a request handler are considered an error (0 is returned to
155 : * client).
156 : *
157 : * @param request the request to handle
158 : * @param func the function to call on receive() of a Client::request()
159 : * @return true if subscription was successful, false otherwise
160 : */
161 : ZEROEQ_API bool handle(const uint128_t& request, const HandleFunc& func);
162 :
163 : /**
164 : * Remove a registered request handler.
165 : *
166 : * @return true if the handler was removed, false if it was not registered.
167 : */
168 : ZEROEQ_API bool remove(const uint128_t& request);
169 :
170 : /**
171 : * Get the server URI.
172 : *
173 : * Contains the used hostname and port, if none where given in the
174 : * constructor uri.
175 : *
176 : * @return the server URI.
177 : */
178 : ZEROEQ_API const URI& getURI() const;
179 :
180 : /** @return the session name that is announced */
181 : ZEROEQ_API const std::string& getSession() const;
182 :
183 : private:
184 : class Impl;
185 : std::unique_ptr<Impl> _impl;
186 :
187 : Server(const Server&) = delete;
188 : Server& operator=(const Server&) = delete;
189 :
190 : // Receiver API
191 : void addSockets(std::vector<detail::Socket>& entries) final;
192 : bool process(detail::Socket& socket) final;
193 : void addConnection(const std::string& uri) final;
194 :
195 : // Sender API
196 : ZEROEQ_API zmq::SocketPtr getSocket() final;
197 : };
198 : }
|