6 #ifndef CPPIPC_SERVER_COMM_SERVER_HPP 7 #define CPPIPC_SERVER_COMM_SERVER_HPP 9 #include <unordered_map> 11 #include <unordered_set> 17 #include <core/parallel/mutex.hpp> 18 #include <boost/thread/mutex.hpp> 19 #include <boost/thread/lock_guard.hpp> 20 #include <core/system/nanosockets/socket_errors.hpp> 21 #include <core/system/nanosockets/async_reply_socket.hpp> 22 #include <core/system/nanosockets/publish_socket.hpp> 23 #include <core/system/cppipc/common/status_types.hpp> 24 #include <core/system/cppipc/server/dispatch.hpp> 25 #include <core/system/cppipc/server/cancel_ops.hpp> 34 class object_factory_impl;
36 template <
typename RetType,
typename T,
typename MemFn,
typename... Args>
157 bool callback(nanosockets::zmq_msg_vector& recv, nanosockets::zmq_msg_vector& reply);
159 std::map<std::string, dispatch*> dispatch_map;
160 boost::mutex registered_object_lock;
161 std::map<size_t, std::shared_ptr<void>> registered_objects;
162 std::map<void*, size_t> inv_registered_objects;
173 bool comm_server_debug_mode =
false;
180 void register_constructor(std::string type_name,
181 std::function<std::shared_ptr<void>()> constructor_call);
186 size_t get_next_object_id();
193 inline size_t register_object(std::shared_ptr<void>
object) {
194 boost::lock_guard<boost::mutex> guard(registered_object_lock);
195 if (inv_registered_objects.count(
object.get())) {
196 return inv_registered_objects.at(
object.
get());
198 size_t id = get_next_object_id();
199 registered_objects.insert({id,
object});
200 inv_registered_objects.insert({
object.get(),
id});
204 struct object_map_key_cmp {
205 bool operator()(
size_t i,
206 const std::pair<
size_t, std::shared_ptr<void>>& map_elem)
const {
207 return i < map_elem.first;
210 bool operator()(
const std::pair<
size_t,std::shared_ptr<void>>& map_elem,
212 return map_elem.first < i;
238 std::string alternate_bind_address=
"",
239 std::string alternate_control_address=
"",
240 std::string alternate_publish_address=
"",
241 std::string secret_key=
"");
260 std::string get_bound_address();
265 std::string get_control_address();
270 std::string get_status_address();
275 void* get_zmq_context();
288 void report_status(std::string status_type, std::string message);
294 boost::lock_guard<boost::mutex> guard(registered_object_lock);
295 if(registered_objects.count(objectid) != 1) {
298 inv_registered_objects.erase(registered_objects[objectid].
get());
300 registered_objects.erase(objectid);
303 inline size_t num_registered_objects() {
304 boost::lock_guard<boost::mutex> guard(registered_object_lock);
305 return registered_objects.size();
313 template <
typename T>
315 T::__register__(*
this);
316 register_constructor(T::__get_type_name__(),
317 [=]()->std::shared_ptr<void> {
318 return std::static_pointer_cast<
void>(std::shared_ptr<T>(constructor_call()));
329 template <
typename T>
331 T::__register__(*
this);
332 register_constructor(T::__get_type_name__(),
333 [=]()->std::shared_ptr<void> {
334 return std::static_pointer_cast<
void>(constructor_call());
345 template <
typename T>
347 boost::lock_guard<boost::mutex> guard(registered_object_lock);
348 if (inv_registered_objects.count(
object.get())) {
349 return inv_registered_objects.at(
object.
get());
351 size_t id = get_next_object_id();
353 registered_objects.insert({id, std::static_pointer_cast<
void>(object)});
354 inv_registered_objects.insert({
object.get(),
id});
363 boost::lock_guard<boost::mutex> guard(registered_object_lock);
364 if (inv_registered_objects.count((
void*)
object)) {
365 return inv_registered_objects.at((
void*)
object);
376 boost::lock_guard<boost::mutex> guard(registered_object_lock);
377 if (registered_objects.count(objectid) == 1) {
378 return registered_objects[objectid];
384 void delete_unused_objects(std::vector<size_t> object_ids,
393 template <
typename MemFn>
394 void register_function(MemFn fn, std::string function_name);
397 template <
typename RetType,
typename T,
typename MemFn,
typename... Args>
404 #include <core/system/cppipc/server/dispatch_impl.hpp> 407 template <
typename MemFn>
409 if (dispatch_map.count(function_name) == 0) {
void register_type(std::function< std::shared_ptr< T >()> constructor_call)
std::shared_ptr< void > get_object(size_t objectid)
dispatch * create_dispatch(MemFn memfn)
void delete_object(size_t objectid)
size_t find_object(void *object)
size_t register_object(std::shared_ptr< T > object)
void register_type(std::function< T *()> constructor_call)
void register_function(MemFn fn, std::string function_name)