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 <core/threading/thread.h>
28 #include <netcomm/fawkes/handler.h>
29 #include <core/utils/lock_queue.h>
30 #include <core/utils/lock_list.h>
31 
32 #include <config/net_messages.h>
33 #include <config/config.h>
34 #include <config/change_handler.h>
35 
36 #include <cstdlib>
37 #include <map>
38 #include <string>
39 #include <utility>
40 #include <cstring>
41 
42 namespace fawkes {
43 
44 class FawkesNetworkHub;
45 
46 
48 : public Thread,
49  public FawkesNetworkHandler,
51 {
52  public:
55 
56  /* from FawkesNetworkHandler interface */
58  virtual void client_connected(unsigned int clid);
59  virtual void client_disconnected(unsigned int clid);
60  virtual void loop();
61 
62  /* from ConfigurationChangeHandler interface */
63  virtual void config_tag_changed(const char *new_location);
66  virtual void config_value_erased(const char *path);
67 
68  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
69  protected: virtual void run() { Thread::run(); }
70 
71  private:
72  void send_value(unsigned int clid, const Configuration::ValueIterator *i);
73  void send_inv_value(unsigned int clid, const char *path);
74 
75  template <typename T>
76  T * prepare_msg(const char *path, bool is_default)
77  {
78  T * m = (T *)calloc(1, sizeof(T));
79  strncpy(m->cp.path, path, CONFIG_MSG_PATH_LENGTH);
80  m->cp.is_default = is_default;
81  return m;
82  }
83 
84  template <typename T>
85  void *
86  prepare_value_msg(const char *path, bool is_default, bool is_list,
87  uint16_t num_values, size_t &data_size, void * __attribute__((__may_alias__)) * data)
88  {
89  data_size = sizeof(config_descriptor_t) + sizeof(T) * (is_list ? num_values : 1);
90  void* m = calloc(1, data_size);
92  strncpy(cd->path, path, CONFIG_MSG_PATH_LENGTH);
93  cd->is_default = is_default;
94  cd->num_values = is_list ? num_values : 0;
95  *data = (void *)((char *)m + sizeof(config_descriptor_t));
96  return m;
97  }
98 
99  Configuration *__config;
100  FawkesNetworkHub *__hub;
101  LockQueue< FawkesNetworkMessage * > __inbound_queue;
102 
103  LockList< unsigned int > __subscribers;
105 };
106 
107 } // end namespace fawkes
108 
109 #endif
Fawkes library namespace.
virtual void config_value_changed(const Configuration::ValueIterator *v)
Called whenever a watched value has changed.
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:939
~ConfigNetworkHandler()
Destructor.
Definition: net_handler.cpp:69
Interface for configuration change handling.
char path[CONFIG_MSG_PATH_LENGTH]
path to config value.
Definition: net_messages.h:93
virtual void handle_network_message(FawkesNetworkMessage *msg)
Handle network message.
virtual void config_value_erased(const char *path)
Called whenever a value has been erased from the config.
Representation of a message that is sent over the network.
Definition: message.h:75
Thread class encapsulation of pthreads.
Definition: thread.h:42
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: net_handler.h:69
virtual void loop()
Process all network messages that have been received.
Fawkes Network Hub.
Definition: hub.h:33
ConfigNetworkHandler(Configuration *config, FawkesNetworkHub *hub)
Constructor.
Definition: net_handler.cpp:52
Fawkes Configuration Network Handler.
Definition: net_handler.h:47
uint16_t is_default
1 if value is a default value, 0 otherwise, only for get response
Definition: net_messages.h:94
Basic config descriptor.
Definition: net_messages.h:92
Network handler abstract base class.
Definition: handler.h:31
uint16_t num_values
Number of valus in list.
Definition: net_messages.h:97
virtual void client_disconnected(unsigned int clid)
Client disconnected.
virtual void client_connected(unsigned int clid)
Client connected.
Iterator interface to iterate over config values.
Definition: config.h:72
virtual void config_comment_changed(const Configuration::ValueIterator *v)
Called whenever a comment of a watched value has changed.
Interface for configuration handling.
Definition: config.h:67
virtual void config_tag_changed(const char *new_location)
Tag changed.