YAMI4 C++
agent.h
1 // Copyright Maciej Sobczak 2008-2020.
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 #ifdef YAMI4_WITH_NETX
33 #include <nx_api.h>
34 #endif // YAMI4_WITH_NETX
35 
36 namespace yami
37 {
38 
39 class value_publisher;
40 
41 namespace details
42 {
43 class agent_impl;
44 } // namespace details
45 
60 class DLL agent
61 {
62 public:
63 
65  typedef long long outgoing_message_id;
66 
71  agent(const parameters & options = parameters());
72 
77  agent(event_callback & event_listener,
78  const parameters & options = parameters());
79 
80 #ifdef YAMI4_WITH_NETX
81  void set_netx_context(NX_IP * ip, NX_PACKET_POOL * packet_pool);
86 #endif // YAMI4_WITH_NETX
87 
97  ~agent();
98 
118  std::string add_listener(const std::string & listener);
119 
127  void remove_listener(const std::string & listener);
128 
139  template <typename functor>
140  void register_object(const std::string & object_name, functor & f)
141  {
142  std::unique_ptr<details::incoming_message_dispatcher_base> object(
143  new details::incoming_message_generic_dispatcher<functor>(f));
144 
145  pimpl_base_->register_object(object_name, object);
146  }
147 
148  void register_raw_object(const std::string & object_name,
149  void (* callback)(incoming_message & im, void * hint), void * hint);
150 
157  void register_value_publisher(const std::string & object_name,
158  value_publisher & publisher);
159 
180  void unregister_object(const std::string & object_name);
181 
192  void open_connection(const std::string & target);
193 
207  void open_connection(const std::string & target,
208  const parameters & options);
209 
234  std::unique_ptr<outgoing_message> send(
235  const std::string & target,
236  const std::string & object_name,
237  const std::string & message_name,
238  const serializable & content = parameters(),
239  std::size_t priority = 0,
240  bool auto_connect = true);
241 
249  void send(
250  outgoing_message & message,
251  const std::string & target,
252  const std::string & object_name,
253  const std::string & message_name,
254  const serializable & content = parameters(),
255  std::size_t priority = 0,
256  bool auto_connect = true);
257 
268  template <typename functor>
270  functor & f,
271  const std::string & target,
272  const std::string & object_name,
273  const std::string & message_name,
274  const serializable & content = parameters(),
275  std::size_t priority = 0,
276  bool auto_connect = true)
277  {
278  std::unique_ptr<details::outgoing_message_dispatcher_base>
279  outgoing_message_callback(
280  new details::outgoing_message_generic_dispatcher<functor>(f));
281 
282  return pimpl_base_->send(outgoing_message_callback,
283  target, object_name, message_name,
284  content, priority, auto_connect);
285  }
286 
294  bool clean_outgoing_message_callback(outgoing_message_id id);
295 
302  void send_one_way(const std::string & target,
303  const std::string & object_name,
304  const std::string & message_name,
305  const serializable & content = parameters(),
306  std::size_t priority = 0,
307  bool auto_connect = true);
308 
326  void close_connection(const std::string & target,
327  std::size_t priority = 0);
328 
338  void hard_close_connection(const std::string & target);
339 
352  template <typename functor>
354  {
355  std::unique_ptr<details::connection_event_dispatcher_base> monitor(
356  new details::connection_event_generic_dispatcher<functor>(f));
357 
358  pimpl_base_->register_connection_event_monitor(monitor);
359  }
360 
373  template <typename functor>
374  void register_io_error_logger(functor & f)
375  {
376  std::unique_ptr<details::io_error_dispatcher_base> logger(
377  new details::io_error_generic_dispatcher<functor>(f));
378 
379  pimpl_base_->register_io_error_logger(logger);
380  }
381 
393  void get_outgoing_flow_state(std::size_t & current_level,
394  std::size_t & high_water_mark, std::size_t & low_water_mark) const;
395 
405  void get_channel_usage(std::size_t & max_allowed, std::size_t & used);
406 
411  std::size_t get_pending_outgoing_bytes(const std::string & target);
412 
421  void run_worker();
422 
436  void run_dispatcher(std::size_t dispatcher_index);
437 
438 private:
439  agent(const agent &);
440  void operator=(const agent &);
441 
442  details::agent_impl * pimpl_;
443  details::agent_impl_base * pimpl_base_;
444 };
445 
446 } // namespace yami
447 
448 #endif // YAMICPP_AGENT_H_INCLUDED
Incoming message.
Definition: incoming_message.h:48
Message broker.
Definition: agent.h:60
Event notification callback interface.
Definition: event_callback.h:37
long long outgoing_message_id
Outgoing message identifier type.
Definition: agent.h:65
void register_object(const std::string &object_name, functor &f)
Registers the new logical destination object.
Definition: agent.h:140
Collection of message parameters.
Definition: parameters.h:71
Namespace devoted to everything related to YAMI4.
Definition: activity_statistics_monitor.h:23
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:353
void register_io_error_logger(functor &f)
Registers the logger for I/O errors.
Definition: agent.h:374
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:269
Simple subscription publisher.
Definition: value_publisher.h:47