YAMI4 C++
value_publisher.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_VALUE_PUBLISHER_H_INCLUDED
18 #define YAMICPP_VALUE_PUBLISHER_H_INCLUDED
19 
20 #include "incoming_message_generic_dispatcher.h"
21 #include "value_publisher_overflow_generic_dispatcher.h"
22 #include <yami4-core/dll.h>
23 
24 #include <memory>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
29 namespace yami
30 {
31 
32 class agent;
33 class incoming_message;
34 class serializable;
35 
36 namespace details
37 {
38 class value_publisher_impl;
39 }
40 
47 class DLL value_publisher
48 {
49 public:
50 
56 
63  ~value_publisher();
64 
78  template <typename functor>
79  value_publisher(functor & f)
80  {
81  user_command_handler_.reset(
82  new details::incoming_message_generic_dispatcher<functor>(f));
83  init(user_command_handler_.get());
84  }
85 
104  template <typename incoming_message_functor,
105  typename queue_overflow_functor>
106  value_publisher(incoming_message_functor & f,
107  std::size_t max_queue_length, queue_overflow_functor & qof)
108  {
109  user_command_handler_.reset(
110  new details::incoming_message_generic_dispatcher<
111  incoming_message_functor>(f));
112  user_overflow_handler_.reset(
113  new details::value_publisher_overflow_generic_dispatcher<
114  queue_overflow_functor>(qof));
115  init(user_command_handler_.get(),
116  max_queue_length, user_overflow_handler_.get());
117  }
118 
125  void register_at(agent & controlling_agent,
126  const std::string & object_name);
127 
129  void unregister();
130 
140  void subscribe(const std::string & destination_target,
141  const std::string & destination_object);
142 
146  void unsubscribe(const std::string & destination_target);
147 
156  void publish(const serializable & value, std::size_t priority = 0);
157 
159  std::size_t get_number_of_subscribers() const;
160 
166  std::vector<std::pair<std::string, std::string> >
167  get_subscribers() const;
168 
169 private:
170 
172  void operator=(const value_publisher &);
173 
174  void init(details::incoming_message_dispatcher_base * imd = NULL,
175  std::size_t max_queue_length = 1,
176  details::value_publisher_overflow_dispatcher_base * qod = NULL);
177 
178  details::value_publisher_impl * pimpl_;
179 
180  std::unique_ptr<details::incoming_message_dispatcher_base>
181  user_command_handler_;
182  std::unique_ptr<details::value_publisher_overflow_dispatcher_base>
183  user_overflow_handler_;
184 };
185 
186 } // namespace yami
187 
188 #endif // YAMICPP_VALUE_PUBLISHER_H_INCLUDED
Message broker.
Definition: agent.h:56
value_publisher(functor &f)
Constructor.
Definition: value_publisher.h:79
value_publisher(incoming_message_functor &f, std::size_t max_queue_length, queue_overflow_functor &qof)
Constructor.
Definition: value_publisher.h:106
Namespace devoted to everything related to YAMI4.
Definition: activity_statistics_monitor.cpp:27
Common interface for serializable data source.
Definition: serializable.h:32
Simple subscription publisher.
Definition: value_publisher.h:47