Fawkes API  Fawkes Development Version
net_handler.h
1 
2 /***************************************************************************
3  * net_handler.h - Fawkes configuration network handler
4  *
5  * Created: Sat Jan 06 22:53:23 2007
6  * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef _CONFIG_NET_HANDLER_H_
25 #define _CONFIG_NET_HANDLER_H_
26 
27 #include <config/change_handler.h>
28 #include <config/config.h>
29 #include <config/net_messages.h>
30 #include <core/threading/thread.h>
31 #include <core/utils/lock_list.h>
32 #include <core/utils/lock_queue.h>
33 #include <netcomm/fawkes/handler.h>
34 
35 #include <cstdlib>
36 #include <cstring>
37 #include <map>
38 #include <string>
39 #include <utility>
40 
41 namespace fawkes {
42 
43 class FawkesNetworkHub;
44 
46  public FawkesNetworkHandler,
48 {
49 public:
52 
53  /* from FawkesNetworkHandler interface */
55  virtual void client_connected(unsigned int clid);
56  virtual void client_disconnected(unsigned int clid);
57  virtual void loop();
58 
59  /* from ConfigurationChangeHandler interface */
60  virtual void config_tag_changed(const char *new_location);
63  virtual void config_value_erased(const char *path);
64 
65  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
66 protected:
67  virtual void
68  run()
69  {
70  Thread::run();
71  }
72 
73 private:
74  void send_value(unsigned int clid, const Configuration::ValueIterator *i);
75  void send_inv_value(unsigned int clid, const char *path);
76 
77  template <typename T>
78  T *
79  prepare_msg(const char *path, bool is_default)
80  {
81  T *m = (T *)calloc(1, sizeof(T));
82  snprintf(m->cp.path, CONFIG_MSG_PATH_LENGTH, "%s", path);
83  m->cp.is_default = is_default;
84  return m;
85  }
86 
87  template <typename T>
88  void *
89  prepare_value_msg(const char *path,
90  bool is_default,
91  bool is_list,
92  uint16_t num_values,
93  size_t & data_size,
94  void *__attribute__((__may_alias__)) * data)
95  {
96  data_size = sizeof(config_descriptor_t) + sizeof(T) * (is_list ? num_values : 1);
97  void * m = calloc(1, data_size);
98  config_descriptor_t *cd = (config_descriptor_t *)m;
99  snprintf(cd->path, CONFIG_MSG_PATH_LENGTH, "%s", path);
100  cd->is_default = is_default;
101  cd->num_values = is_list ? num_values : 0;
102  *data = (void *)((char *)m + sizeof(config_descriptor_t));
103  return m;
104  }
105 
106  Configuration * config_;
107  FawkesNetworkHub * hub_;
108  LockQueue<FawkesNetworkMessage *> inbound_queue_;
109 
110  LockList<unsigned int> subscribers_;
111  LockList<unsigned int>::iterator sit_;
112 };
113 
114 } // end namespace fawkes
115 
116 #endif
fawkes::ConfigNetworkHandler::config_value_changed
virtual void config_value_changed(const Configuration::ValueIterator *v)
Called whenever a watched value has changed.
Definition: net_handler.cpp:562
fawkes::ConfigNetworkHandler::config_comment_changed
virtual void config_comment_changed(const Configuration::ValueIterator *v)
Called whenever a comment of a watched value has changed.
Definition: net_handler.cpp:583
fawkes::ConfigNetworkHandler::config_value_erased
virtual void config_value_erased(const char *path)
Called whenever a value has been erased from the config.
Definition: net_handler.cpp:588
fawkes::ConfigNetworkHandler::run
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: net_handler.h:68
fawkes::ConfigurationChangeHandler
Interface for configuration change handling.
Definition: change_handler.h:32
fawkes::FawkesNetworkHandler
Network handler abstract base class.
Definition: handler.h:32
fawkes::ConfigNetworkHandler::handle_network_message
virtual void handle_network_message(FawkesNetworkMessage *msg)
Handle network message.
Definition: net_handler.cpp:518
fawkes::Configuration::ValueIterator
Iterator interface to iterate over config values.
Definition: config.h:72
fawkes::Configuration
Interface for configuration handling.
Definition: config.h:65
fawkes::ConfigNetworkHandler::~ConfigNetworkHandler
~ConfigNetworkHandler()
Destructor.
Definition: net_handler.cpp:66
fawkes
Fawkes library namespace.
fawkes::ConfigNetworkHandler::ConfigNetworkHandler
ConfigNetworkHandler(Configuration *config, FawkesNetworkHub *hub)
Constructor.
Definition: net_handler.cpp:51
fawkes::ConfigNetworkHandler
Fawkes Configuration Network Handler.
Definition: net_handler.h:48
fawkes::ConfigNetworkHandler::client_disconnected
virtual void client_disconnected(unsigned int clid)
Client disconnected.
Definition: net_handler.cpp:539
fawkes::ConfigNetworkHandler::loop
virtual void loop()
Process all network messages that have been received.
Definition: net_handler.cpp:235
fawkes::ConfigNetworkHandler::config_tag_changed
virtual void config_tag_changed(const char *new_location)
Tag changed.
Definition: net_handler.cpp:557
fawkes::Thread::run
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:918
fawkes::Thread
Thread class encapsulation of pthreads.
Definition: thread.h:46
fawkes::FawkesNetworkMessage
Representation of a message that is sent over the network.
Definition: message.h:77
fawkes::FawkesNetworkHub
Fawkes Network Hub.
Definition: hub.h:34
fawkes::ConfigNetworkHandler::client_connected
virtual void client_connected(unsigned int clid)
Client connected.
Definition: net_handler.cpp:530