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 <core/exceptions/software.h>
28#include <core/utils/refcount.h>
29#include <interface/change_field.h>
30#include <interface/field_iterator.h>
31#include <interface/types.h>
32#include <utils/uuid.h>
33
34#define INTERFACE_MESSAGE_TYPE_SIZE_ 64
35
36namespace fawkes {
37
38class Mutex;
39class Interface;
40class InterfaceFieldIterator;
41class Time;
42
43class Message : public RefCount
44{
45 friend Interface;
46
47public:
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 Uuid sender_id() const;
62 Uuid source_id() const;
63 void set_sender_id(const Uuid &id);
64 void set_source_id(const Uuid &id);
65 const char *sender_thread_name() const;
66 Interface * interface() const;
67 const char *type() const;
68
71
72 unsigned int num_fields() const;
73
74 const void * datachunk() const;
75 unsigned int datasize() const;
76
77 unsigned int hops() const;
78 void set_hops(unsigned int hops);
79
80 void set_from_chunk(const void *chunk);
81
82 unsigned int recipient() const;
83
84 virtual Message *clone() const;
85
86 /** Check if message has desired type.
87 * @return true, if message has desired type, false otherwise
88 */
89 template <class MessageType>
90 bool is_of_type();
91
92 /** Cast message to given type if possible.
93 * Check with is_of_type() first if the message has the requested type.
94 * @return message casted to requested type
95 * @throw TypeMismatchException thrown if the message is not of the requested type
96 */
97 template <class MessageType>
98 MessageType *as_type();
99
100private: // fields
101 unsigned int message_id_;
102 unsigned int hops_;
103 bool enqueued_;
104 Time * time_enqueued_;
105
106 unsigned int recipient_interface_mem_serial;
107 unsigned int sender_interface_instance_serial;
108
109 char *_type;
110 char *_sender_thread_name;
111 Uuid _sender_id;
112 Uuid _source_id;
113
114 Interface *_transmit_via_iface;
115
116 interface_fieldinfo_t *fieldinfo_list_;
117
118 unsigned int num_fields_;
119
120private: // methods
121 void set_interface(Interface *iface, bool proxy = false);
122
123protected:
125 const char * name,
126 size_t length,
127 void * value,
128 const char * enumtype = 0,
129 const interface_enum_map_t *enum_map = 0);
130
131 /** Set a field
132 * @param field Reference to the field
133 * @param data New field value
134 */
135 template <class FieldT, class DataT>
136 void set_field(FieldT &field, DataT &data);
137
138 /** Set an array field at a given index
139 * @param field Reference to the array field
140 * @param index Index into the array field
141 * @param data New value to put at given index
142 */
143 template <class FieldT, class DataT>
144 void set_field(FieldT &field, unsigned int index, DataT &data);
145
146 void * data_ptr;
147 unsigned int data_size;
148
149 /** Timestamp data, must be present and first entries for each interface
150 * data structs! This leans on timeval struct. */
151 typedef struct
152 {
153 int64_t timestamp_sec; /**< time in seconds since Unix epoch */
154 int64_t timestamp_usec; /**< additional time microseconds */
156 message_data_ts_t *data_ts; /**< data timestamp aliasing pointer */
157};
158
159template <class MessageType>
160bool
162{
163 return (dynamic_cast<MessageType *>(this) != 0);
164}
165
166template <class MessageType>
167MessageType *
169{
170 MessageType *m = dynamic_cast<MessageType *>(this);
171 if (m) {
172 return m;
173 } else {
174 throw fawkes::TypeMismatchException("Message is not of requested type");
175 }
176}
177
178template <class FieldT, class DataT>
179void
180Message::set_field(FieldT &field, DataT &data)
181{
182 change_field(field, data);
183}
184
185template <class FieldT, class DataT>
186void
187Message::set_field(FieldT &field, unsigned int index, DataT &data)
188{
189 change_field(field, index, data);
190}
191
192} // end namespace fawkes
193
194#endif
Interface field iterator.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
InterfaceFieldIterator fields()
Get iterator over all fields of this interface instance.
Definition: message.cpp:390
unsigned int recipient() const
Get recipient memory serial.
Definition: message.cpp:271
void set_field(FieldT &field, DataT &data)
Set a field.
Definition: message.h:180
virtual ~Message()
Destructor.
Definition: message.cpp:163
const char * type() const
Get message type.
Definition: message.cpp:381
const void * datachunk() const
Get pointer to data.
Definition: message.cpp:281
void mark_enqueued()
Mark message as being enqueued.
Definition: message.cpp:234
unsigned int datasize() const
Get size of data.
Definition: message.cpp:290
Message(const char *type)
Constructor.
Definition: message.cpp:62
bool enqueued() const
Check is message has been enqueued.
Definition: message.cpp:249
Uuid source_id() const
Get ID of the original source of the message.
Definition: message.cpp:346
MessageType * as_type()
Cast message to given type if possible.
Definition: message.h:168
bool is_of_type()
Check if message has desired type.
Definition: message.h:161
Interface * interface() const
Get transmitting interface.
Definition: message.cpp:372
void set_source_id(const Uuid &id)
Set source ID.
Definition: message.cpp:218
void set_from_chunk(const void *chunk)
Set from raw data chunk.
Definition: message.cpp:301
void set_id(unsigned int message_id)
Set message ID.
Definition: message.cpp:199
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:435
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:146
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:156
const Time * time_enqueued() const
Get time when message was enqueued.
Definition: message.cpp:262
unsigned int num_fields() const
Get the number of fields in the message.
Definition: message.cpp:408
Uuid sender_id() const
Get ID of the immediate sender, not necessarily the creator of the message.
Definition: message.cpp:336
void set_hops(unsigned int hops)
Set number of hops.
Definition: message.cpp:227
Message & operator=(const Message &m)
Assign this message to given message.
Definition: message.cpp:313
virtual Message * clone() const
Clone this message.
Definition: message.cpp:418
InterfaceFieldIterator fields_end()
Invalid iterator.
Definition: message.cpp:399
const char * sender_thread_name() const
Get sender of message.
Definition: message.cpp:327
unsigned int id() const
Get message ID.
Definition: message.cpp:181
unsigned int hops() const
Get number of hops.
Definition: message.cpp:190
void set_sender_id(const Uuid &id)
Set sender ID.
Definition: message.cpp:208
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:147
Reference counting base class.
Definition: refcount.h:32
A class for handling time.
Definition: time.h:93
A convenience class for universally unique identifiers (UUIDs).
Definition: uuid.h:29
Fawkes library namespace.
std::map< int, std::string > interface_enum_map_t
Map of enum integer to string values.
Definition: types.h:54
interface_fieldtype_t
Interface field type.
Definition: types.h:36
bool change_field(FieldT &field, const DataT &value)
Set a field and return whether it changed.
Definition: change_field.h:37
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:152
int64_t timestamp_sec
time in seconds since Unix epoch
Definition: message.h:153
int64_t timestamp_usec
additional time microseconds
Definition: message.h:154
Interface field info list.
Definition: types.h:58