Goby v2
application.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef APPLICATION20100908H
24 #define APPLICATION20100908H
25 
26 #include "goby/common/core_helpers.h"
27 #include "goby/common/exception.h"
28 #include "goby/common/logger.h"
29 
30 #include "goby/pb/protobuf/config.pb.h"
31 #include "goby/pb/protobuf/header.pb.h"
32 
33 #include "goby/common/zeromq_application_base.h"
34 #include "protobuf_pubsub_node_wrapper.h"
35 
36 namespace google
37 {
38 namespace protobuf
39 {
40 class Message;
41 }
42 } // namespace google
43 namespace goby
44 {
46 namespace pb
47 {
50 {
51  protected:
52  // make these more accessible
54 
56 
59  virtual ~Application();
61 
63 
64 
65  void publish(const google::protobuf::Message& msg, const std::string& group = "");
66 
68 
72  template <typename ProtoBufMessage>
73  void subscribe(boost::function<void(const ProtoBufMessage&)> handler =
74  boost::function<void(const ProtoBufMessage&)>(),
75  const std::string& group = "")
76  {
77  if (pubsub_node_)
78  pubsub_node_->subscribe<ProtoBufMessage>(handler, group);
79  }
80 
85  template <typename ProtoBufMessage, class C>
86  void subscribe(void (C::*mem_func)(const ProtoBufMessage&), C* obj,
87  const std::string& group = "")
88  {
89  if (pubsub_node_)
90  pubsub_node_->subscribe<ProtoBufMessage>(boost::bind(mem_func, obj, _1), group);
91  }
92 
94 
95  /* template<typename ProtoBufMessage> */
99  /* const ProtoBufMessage& newest() */
100  /* { */
101  /* if(pubsub_node_) */
102  /* return pubsub_node_->newest<ProtoBufMessage>(); */
103  /* else */
104  /* throw(goby::Exception("not using pubsub, can't call newest")); */
105  /* } */
106 
108 
109  common::ZeroMQService& zeromq_service() { return zeromq_service_; }
110 
111  boost::shared_ptr<StaticProtobufPubSubNodeWrapper> pubsub_node() { return pubsub_node_; }
112 
113  private:
114  Application(const Application&);
115  Application& operator=(const Application&);
116 
117  void __set_up_sockets();
118 
119  private:
120  common::ZeroMQService zeromq_service_;
121  boost::shared_ptr<StaticProtobufNode> protobuf_node_;
122  boost::shared_ptr<StaticProtobufPubSubNodeWrapper> pubsub_node_;
123 };
124 } // namespace pb
125 } // namespace goby
126 
127 #endif
Base class provided for users to generate applications that participate in the Goby publish/subscribe...
Definition: application.h:49
void subscribe(void(C::*mem_func)(const ProtoBufMessage &), C *obj, const std::string &group="")
Subscribe for a type using a class member function as the handler.
Definition: application.h:86
void subscribe(boost::function< void(const ProtoBufMessage &)> handler=boost::function< void(const ProtoBufMessage &)>(), const std::string &group="")
Subscribe to a message (of any type derived from google::protobuf::Message)
Definition: application.h:73
The global namespace for the Goby project.
Represents the eight available terminal colors (and bold variants)
Definition: term_color.h:108
common::ZeroMQService & zeromq_service()
Fetchs the newest received message of this type.
Definition: application.h:109