Fawkes API  Fawkes Development Version
protobuf_adapter.h
1 
2 /***************************************************************************
3  * protobuf_adapter.h - PLEXIL adapter for the protobuf_comm
4  *
5  * Created: Thu Aug 16 11:05:36 2018
6  * Copyright 2006-2018 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #ifndef __PLUGINS_PLEXIL_PROTOBUF_ADAPTER_H_
23 #define __PLUGINS_PLEXIL_PROTOBUF_ADAPTER_H_
24 
25 #include <config/config.h>
26 #include <logging/logger.h>
27 #include <protobuf_comm/peer.h>
28 #include <utils/time/clock.h>
29 #include <utils/time/time.h>
30 
31 #include <InterfaceAdapter.hh>
32 #include <Value.hh>
33 #include <memory>
34 #include <mutex>
35 
36 /** Interface adapter to provide logging facilities. */
37 class ProtobufCommPlexilAdapter : public PLEXIL::InterfaceAdapter
38 {
39 public:
40  ProtobufCommPlexilAdapter(PLEXIL::AdapterExecInterface &execInterface);
41  ProtobufCommPlexilAdapter(PLEXIL::AdapterExecInterface &execInterface, pugi::xml_node const xml);
42 
43  /// @cond DELETED
44  ProtobufCommPlexilAdapter() = delete;
46  ProtobufCommPlexilAdapter &operator=(const ProtobufCommPlexilAdapter &) = delete;
47  /// @endcond
48 
50 
51  virtual bool initialize();
52  virtual bool start();
53  virtual bool stop();
54  virtual bool reset();
55  virtual bool shutdown();
56 
57  void executeCommand(PLEXIL::Command *cmd);
58  void invokeAbort(PLEXIL::Command *cmd);
59 
60 private:
61  /// @cond INTERNAL
62  struct queue_entry
63  {
64  std::vector<std::string> messages;
65  std::vector<PLEXIL::Command *> recipients;
66  };
67 
68  struct message_meta
69  {
70  fawkes::Time time_received;
71  std::string from_host;
72  int from_port;
73  std::shared_ptr<google::protobuf::Message> message;
74  };
75  /// @endcond
76 
77  void proc_receive_command(PLEXIL::Command *cmd);
78  void proc_get_param_command(PLEXIL::Command *cmd);
79  void proc_send_rv_command(PLEXIL::Command *cmd);
80 
81  void add_recipient(const std::string &msg_type, PLEXIL::Command *cmd);
82  void remove_recipient(const std::string &msg_type, PLEXIL::Command *cmd);
83 
84  std::string gen_msgid(const std::string &msg_type);
85  void add_message(const std::string &msg_type, message_meta &&msg);
86  void release_message(const std::string &msg_id);
87  std::shared_ptr<google::protobuf::Message> get_message(const std::string &msg_id);
88 
89  void proc_queue(const std::string &msg_type);
90  queue_entry &get_queue(const std::string &msg_type);
91 
92  void pb_create(PLEXIL::Command *cmd);
93  void pb_destroy(PLEXIL::Command *cmd);
94  void pb_set_value(PLEXIL::Command *cmd);
95  void pb_get_value(PLEXIL::Command *cmd, PLEXIL::ValueType value_type);
96  void pb_get_length(PLEXIL::Command *cmd);
97  void pb_has_field(PLEXIL::Command *cmd);
98  void pb_tostring(PLEXIL::Command *cmd);
99  void pb_broadcast(PLEXIL::Command *cmd);
100 
101  void pb_peer_create(PLEXIL::Command *cmd);
102  void pb_peer_create_local(PLEXIL::Command *cmd);
103  void pb_peer_create_crypto(PLEXIL::Command *cmd);
104  void pb_peer_create_local_crypto(PLEXIL::Command * cmd,
105  const std::vector<PLEXIL::Value> *args = nullptr);
106  void pb_peer_destroy(PLEXIL::Command *cmd);
107  void pb_peer_setup_crypto(PLEXIL::Command *cmd);
108 
109  void handle_peer_msg(int peer_id,
110  boost::asio::ip::udp::endpoint & endpoint,
111  uint16_t component_id,
112  uint16_t msg_type,
113  std::shared_ptr<google::protobuf::Message> msg);
114  void
115  handle_peer_recv_error(int peer_id, boost::asio::ip::udp::endpoint &endpoint, std::string msg);
116  void handle_peer_send_error(int peer_id, std::string msg);
117 
118 private:
119  fawkes::Configuration *config_;
120  fawkes::Logger * logger_;
121  fawkes::Clock * clock_;
122 
123  std::mutex queue_mutex_;
124  std::map<std::string, queue_entry> queue_;
125  std::map<std::string, message_meta> messages_;
126  unsigned long int msg_counter_;
127 
128  typedef std::map<int, std::shared_ptr<protobuf_comm::ProtobufBroadcastPeer>> PeerMap;
129 
130  int next_client_id_;
131  std::mutex map_mutex_;
132  std::shared_ptr<protobuf_comm::MessageRegister> message_register_;
133  PeerMap peers_;
134 
135  std::map<std::string, std::function<void(PLEXIL::Command *)>> commands_;
136 };
137 
138 extern "C" {
139 void initProtobufCommAdapter();
140 }
141 
142 #endif
Interface adapter to provide logging facilities.
virtual bool initialize()
Initialize adapter.
virtual bool start()
Start adapter.
void executeCommand(PLEXIL::Command *cmd)
Perform given command.
void invokeAbort(PLEXIL::Command *cmd)
Abort currently running execution.
virtual bool shutdown()
Shut adapter down.
virtual ~ProtobufCommPlexilAdapter()
Destructor.
virtual bool reset()
Reset adapter.
ProtobufCommPlexilAdapter(PLEXIL::AdapterExecInterface &execInterface)
Constructor.
virtual bool stop()
Stop adapter.
This is supposed to be the central clock in Fawkes.
Definition: clock.h:35
Interface for configuration handling.
Definition: config.h:68
Interface for logging.
Definition: logger.h:42
A class for handling time.
Definition: time.h:93