YAMI4 C++
agent.h
1 // Copyright Maciej Sobczak 2008-2019.
2 // This file is part of YAMI4.
3 //
4 // YAMI4 is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // YAMI4 is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with YAMI4. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef YAMICPP_AGENT_H_INCLUDED
18 #define YAMICPP_AGENT_H_INCLUDED
19 
20 #include "agent_impl_base.h"
21 #include "connection_event_generic_dispatcher.h"
22 #include "event_callback.h"
23 #include "incoming_message_generic_dispatcher.h"
24 #include "io_error_generic_dispatcher.h"
25 #include "outgoing_message.h"
26 #include "outgoing_message_generic_dispatcher.h"
27 #include "parameters.h"
28 #include <yami4-core/dll.h>
29 #include <memory>
30 #include <string>
31 
32 namespace yami
33 {
34 
35 class value_publisher;
36 
37 namespace details
38 {
39 class agent_impl;
40 } // namespace details
41 
56 class DLL agent
57 {
58 public:
59 
61  typedef long long outgoing_message_id;
62 
67  agent(const parameters & options = parameters());
68 
73  agent(event_callback & event_listener,
74  const parameters & options = parameters());
75 
85  ~agent();
86 
106  std::string add_listener(const std::string & listener);
107 
115  void remove_listener(const std::string & listener);
116 
127  template <typename functor>
128  void register_object(const std::string & object_name, functor & f)
129  {
130  std::unique_ptr<details::incoming_message_dispatcher_base> object(
131  new details::incoming_message_generic_dispatcher<functor>(f));
132 
133  pimpl_base_->register_object(object_name, object);
134  }
135 
136  void register_raw_object(const std::string & object_name,
137  void (* callback)(incoming_message & im, void * hint), void * hint);
138 
145  void register_value_publisher(const std::string & object_name,
146  value_publisher & publisher);
147 
168  void unregister_object(const std::string & object_name);
169 
180  void open_connection(const std::string & target);
181 
195  void open_connection(const std::string & target,
196  const parameters & options);
197 
222  std::unique_ptr<outgoing_message> send(
223  const std::string & target,
224  const std::string & object_name,
225  const std::string & message_name,
226  const serializable & content = parameters(),
227  std::size_t priority = 0,
228  bool auto_connect = true);
229 
237  void send(
238  outgoing_message & message,
239  const std::string & target,
240  const std::string & object_name,
241  const std::string & message_name,
242  const serializable & content = parameters(),
243  std::size_t priority = 0,
244  bool auto_connect = true);
245 
256  template <typename functor>
258  functor & f,
259  const std::string & target,
260  const std::string & object_name,
261  const std::string & message_name,
262  const serializable & content = parameters(),
263  std::size_t priority = 0,
264  bool auto_connect = true)
265  {
266  std::unique_ptr<details::outgoing_message_dispatcher_base>
267  outgoing_message_callback(
268  new details::outgoing_message_generic_dispatcher<functor>(f));
269 
270  return pimpl_base_->send(outgoing_message_callback,
271  target, object_name, message_name,
272  content, priority, auto_connect);
273  }
274 
282  bool clean_outgoing_message_callback(outgoing_message_id id);
283 
290  void send_one_way(const std::string & target,
291  const std::string & object_name,
292  const std::string & message_name,
293  const serializable & content = parameters(),
294  std::size_t priority = 0,
295  bool auto_connect = true);
296 
314  void close_connection(const std::string & target,
315  std::size_t priority = 0);
316 
326  void hard_close_connection(const std::string & target);
327 
340  template <typename functor>
342  {
343  std::unique_ptr<details::connection_event_dispatcher_base> monitor(
344  new details::connection_event_generic_dispatcher<functor>(f));
345 
346  pimpl_base_->register_connection_event_monitor(monitor);
347  }
348 
361  template <typename functor>
362  void register_io_error_logger(functor & f)
363  {
364  std::unique_ptr<details::io_error_dispatcher_base> logger(
365  new details::io_error_generic_dispatcher<functor>(f));
366 
367  pimpl_base_->register_io_error_logger(logger);
368  }
369 
381  void get_outgoing_flow_state(std::size_t & current_level,
382  std::size_t & high_water_mark, std::size_t & low_water_mark) const;
383 
393  void get_channel_usage(int & max_allowed, int & used);
394 
399  std::size_t get_pending_outgoing_bytes(const std::string & target);
400 
401 private:
402  agent(const agent &);
403  void operator=(const agent &);
404 
405  details::agent_impl * pimpl_;
406  details::agent_impl_base * pimpl_base_;
407 };
408 
409 } // namespace yami
410 
411 #endif // YAMICPP_AGENT_H_INCLUDED
Incoming message.
Definition: incoming_message.h:48
Message broker.
Definition: agent.h:56
Event notification callback interface.
Definition: event_callback.h:37
long long outgoing_message_id
Outgoing message identifier type.
Definition: agent.h:61
void register_object(const std::string &object_name, functor &f)
Registers the new logical destination object.
Definition: agent.h:128
Collection of message parameters.
Definition: parameters.h:71
Namespace devoted to everything related to YAMI4.
Definition: activity_statistics_monitor.cpp:27
Common interface for serializable data source.
Definition: serializable.h:32
Outgoing message.
Definition: outgoing_message.h:44
void register_connection_event_monitor(functor &f)
Registers the monitor for connection-related events.
Definition: agent.h:341
void register_io_error_logger(functor &f)
Registers the logger for I/O errors.
Definition: agent.h:362
outgoing_message_id send(functor &f, const std::string &target, const std::string &object_name, const std::string &message_name, const serializable &content=parameters(), std::size_t priority=0, bool auto_connect=true)
Sends the outgoing message.
Definition: agent.h:257
Simple subscription publisher.
Definition: value_publisher.h:47