Fawkes API  Fawkes Development Version
message.h
1 
2 /***************************************************************************
3  * message.h - BlackBoard message
4  *
5  * Created: Sun Oct 08 00:08:10 2006
6  * Copyright 2006-2010 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 __INTERFACE_MESSAGE_H_
25 #define __INTERFACE_MESSAGE_H_
26 
27 #include <interface/field_iterator.h>
28 #include <interface/types.h>
29 #include <core/utils/refcount.h>
30 #include <core/exceptions/software.h>
31 
32 #define __INTERFACE_MESSAGE_TYPE_SIZE 32
33 
34 namespace fawkes {
35 #if 0 /* just to make Emacs auto-indent happy */
36 }
37 #endif
38 
39 class Mutex;
40 class Interface;
41 class InterfaceFieldIterator;
42 class Time;
43 
44 class Message : public RefCount
45 {
46  friend class Interface;
47  public:
48  Message(const char *type);
49  Message(const Message *mesg);
50  Message(const Message &mesg);
51  virtual ~Message();
52 
53  Message & operator= (const Message & m);
54 
55  unsigned int id() const;
56  void set_id(unsigned int message_id);
57  void mark_enqueued();
58  bool enqueued() const;
59  const Time * time_enqueued() const;
60 
61  unsigned int sender_id() const;
62  const char * sender_thread_name() const;
63  Interface * interface() const;
64  const char * type() const;
65 
68 
69  unsigned int num_fields() const;
70 
71  const void * datachunk() const;
72  unsigned int datasize() const;
73 
74  unsigned int hops() const;
75  void set_hops(unsigned int hops);
76 
77  void set_from_chunk(const void *chunk);
78 
79  unsigned int recipient() const;
80 
81  virtual Message * clone() const;
82 
83  /** Check if message has desired type.
84  * @return true, if message has desired type, false otherwise
85  */
86  template <class MessageType>
87  bool is_of_type();
88 
89  /** Cast message to given type if possible.
90  * Check with is_of_type() first if the message has the requested type.
91  * @return message casted to requested type
92  * @throw TypeMismatchException thrown if the message is not of the requested type
93  */
94  template <class MessageType>
95  MessageType * as_type();
96 
97  private: // fields
98  unsigned int __message_id;
99  unsigned int __hops;
100  bool __enqueued;
101  Time *__time_enqueued;
102 
103  unsigned int recipient_interface_mem_serial;
104  unsigned int sender_interface_instance_serial;
105 
106  char *_type;
107  char *_sender_thread_name;
108  unsigned int _sender_id;
109 
110  Interface *_transmit_via_iface;
111 
112  interface_fieldinfo_t *__fieldinfo_list;
113 
114  unsigned int __num_fields;
115 
116  private: // methods
117  void set_interface(Interface *iface);
118 
119  protected:
120  void add_fieldinfo(interface_fieldtype_t type, const char *name,
121  size_t length, void *value, const char *enumtype = 0,
122  const interface_enum_map_t *enum_map = 0);
123 
124  void *data_ptr;
125  unsigned int data_size;
126 
127  /** Timestamp data, must be present and first entries for each interface
128  * data structs! This leans on timeval struct. */
129  typedef struct {
130  int64_t timestamp_sec; /**< time in seconds since Unix epoch */
131  int64_t timestamp_usec; /**< additional time microseconds */
133  message_data_ts_t *data_ts; /**< data timestamp aliasing pointer */
134 };
135 
136 template <class MessageType>
137 bool
139 {
140  return (dynamic_cast<MessageType *>(this) != 0);
141 }
142 
143 
144 template <class MessageType>
145 MessageType *
147 {
148  MessageType *m = dynamic_cast<MessageType *>(this);
149  if (m) {
150  return m;
151  } else {
152  throw fawkes::TypeMismatchException("Message is not of requested type");
153  }
154 }
155 
156 
157 } // end namespace fawkes
158 
159 #endif
Interface field iterator.
const char * sender_thread_name() const
Get sender of message.
Definition: message.cpp:335
const Time * time_enqueued() const
Get time when message was enqueued.
Definition: message.cpp:265
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:124
Interface * interface() const
Get transmitting interface.
Definition: message.cpp:368
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
unsigned int id() const
Get message ID.
Definition: message.cpp:197
void mark_enqueued()
Mark message as being enqueued.
Definition: message.cpp:235
Interface field info list.
Definition: types.h:56
Fawkes library namespace.
Timestamp data, must be present and first entries for each interface data structs! This leans on time...
Definition: message.h:129
A class for handling time.
Definition: time.h:91
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
bool enqueued() const
Check is message has been enqueued.
Definition: message.cpp:251
InterfaceFieldIterator fields_end()
Invalid iterator.
Definition: message.cpp:398
const void * datachunk() const
Get pointer to data.
Definition: message.cpp:285
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:133
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:125
Message(const char *type)
Constructor.
Definition: message.cpp:67
unsigned int sender_id() const
Get ID of sender.
Definition: message.cpp:345
virtual ~Message()
Destructor.
Definition: message.cpp:178
void set_from_chunk(const void *chunk)
Set from raw data chunk.
Definition: message.cpp:307
unsigned int hops() const
Get number of hops.
Definition: message.cpp:207
int64_t timestamp_usec
additional time microseconds
Definition: message.h:131
MessageType * as_type()
Cast message to given type if possible.
Definition: message.h:146
Reference counting base class.
Definition: refcount.h:32
unsigned int recipient() const
Get recipient memory serial.
Definition: message.cpp:275
InterfaceFieldIterator fields()
Get iterator over all fields of this interface instance.
Definition: message.cpp:388
bool is_of_type()
Check if message has desired type.
Definition: message.h:138
int64_t timestamp_sec
time in seconds since Unix epoch
Definition: message.h:130
unsigned int datasize() const
Get size of data.
Definition: message.cpp:295
unsigned int num_fields() const
Get the number of fields in the message.
Definition: message.cpp:408
void add_fieldinfo(interface_fieldtype_t type, const char *name, size_t length, void *value, const char *enumtype=0, const interface_enum_map_t *enum_map=0)
Add an entry to the info list.
Definition: message.cpp:436
void set_hops(unsigned int hops)
Set number of hops.
Definition: message.cpp:227
interface_fieldtype_t
Interface field type.
Definition: types.h:35
std::map< int, std::string > interface_enum_map_t
Map of enum integer to string values.
Definition: types.h:53
const char * type() const
Get message type.
Definition: message.cpp:378
Message & operator=(const Message &m)
Assign this message to given message.
Definition: message.cpp:320
void set_id(unsigned int message_id)
Set message ID.
Definition: message.cpp:217
virtual Message * clone() const
Clone this message.
Definition: message.cpp:419