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/field_iterator.h>
30 #include <interface/types.h>
31 
32 #define INTERFACE_MESSAGE_TYPE_SIZE_ 32
33 
34 namespace fawkes {
35 
36 class Mutex;
37 class Interface;
38 class InterfaceFieldIterator;
39 class Time;
40 
41 class Message : public RefCount
42 {
43  friend Interface;
44 
45 public:
46  Message(const char *type);
47  Message(const Message *mesg);
48  Message(const Message &mesg);
49  virtual ~Message();
50 
51  Message &operator=(const Message &m);
52 
53  unsigned int id() const;
54  void set_id(unsigned int message_id);
55  void mark_enqueued();
56  bool enqueued() const;
57  const Time * time_enqueued() const;
58 
59  unsigned int sender_id() const;
60  const char * sender_thread_name() const;
61  Interface * interface() const;
62  const char * type() const;
63 
66 
67  unsigned int num_fields() const;
68 
69  const void * datachunk() const;
70  unsigned int datasize() const;
71 
72  unsigned int hops() const;
73  void set_hops(unsigned int hops);
74 
75  void set_from_chunk(const void *chunk);
76 
77  unsigned int recipient() const;
78 
79  virtual Message *clone() const;
80 
81  /** Check if message has desired type.
82  * @return true, if message has desired type, false otherwise
83  */
84  template <class MessageType>
85  bool is_of_type();
86 
87  /** Cast message to given type if possible.
88  * Check with is_of_type() first if the message has the requested type.
89  * @return message casted to requested type
90  * @throw TypeMismatchException thrown if the message is not of the requested type
91  */
92  template <class MessageType>
93  MessageType *as_type();
94 
95 private: // fields
96  unsigned int message_id_;
97  unsigned int hops_;
98  bool enqueued_;
99  Time * time_enqueued_;
100 
101  unsigned int recipient_interface_mem_serial;
102  unsigned int sender_interface_instance_serial;
103 
104  char * _type;
105  char * _sender_thread_name;
106  unsigned int _sender_id;
107 
108  Interface *_transmit_via_iface;
109 
110  interface_fieldinfo_t *fieldinfo_list_;
111 
112  unsigned int num_fields_;
113 
114 private: // methods
115  void set_interface(Interface *iface);
116 
117 protected:
119  const char * name,
120  size_t length,
121  void * value,
122  const char * enumtype = 0,
123  const interface_enum_map_t *enum_map = 0);
124 
125  void * data_ptr;
126  unsigned int data_size;
127 
128  /** Timestamp data, must be present and first entries for each interface
129  * data structs! This leans on timeval struct. */
130  typedef struct
131  {
132  int64_t timestamp_sec; /**< time in seconds since Unix epoch */
133  int64_t timestamp_usec; /**< additional time microseconds */
135  message_data_ts_t *data_ts; /**< data timestamp aliasing pointer */
136 };
137 
138 template <class MessageType>
139 bool
141 {
142  return (dynamic_cast<MessageType *>(this) != 0);
143 }
144 
145 template <class MessageType>
146 MessageType *
148 {
149  MessageType *m = dynamic_cast<MessageType *>(this);
150  if (m) {
151  return m;
152  } else {
153  throw fawkes::TypeMismatchException("Message is not of requested type");
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:317
const Time * time_enqueued() const
Get time when message was enqueued.
Definition: message.cpp:252
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:125
Interface * interface() const
Get transmitting interface.
Definition: message.cpp:347
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:41
unsigned int id() const
Get message ID.
Definition: message.cpp:190
void mark_enqueued()
Mark message as being enqueued.
Definition: message.cpp:224
Interface field info list.
Definition: types.h:57
Fawkes library namespace.
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:130
A class for handling time.
Definition: time.h:92
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:78
bool enqueued() const
Check is message has been enqueued.
Definition: message.cpp:239
InterfaceFieldIterator fields_end()
Invalid iterator.
Definition: message.cpp:374
const void * datachunk() const
Get pointer to data.
Definition: message.cpp:271
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:135
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:126
Message(const char *type)
Constructor.
Definition: message.cpp:62
unsigned int sender_id() const
Get ID of sender.
Definition: message.cpp:326
virtual ~Message()
Destructor.
Definition: message.cpp:172
void set_from_chunk(const void *chunk)
Set from raw data chunk.
Definition: message.cpp:291
unsigned int hops() const
Get number of hops.
Definition: message.cpp:199
int64_t timestamp_usec
additional time microseconds
Definition: message.h:133
MessageType * as_type()
Cast message to given type if possible.
Definition: message.h:147
Reference counting base class.
Definition: refcount.h:31
unsigned int recipient() const
Get recipient memory serial.
Definition: message.cpp:261
InterfaceFieldIterator fields()
Get iterator over all fields of this interface instance.
Definition: message.cpp:365
bool is_of_type()
Check if message has desired type.
Definition: message.h:140
int64_t timestamp_sec
time in seconds since Unix epoch
Definition: message.h:132
unsigned int datasize() const
Get size of data.
Definition: message.cpp:280
unsigned int num_fields() const
Get the number of fields in the message.
Definition: message.cpp:383
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:410
void set_hops(unsigned int hops)
Set number of hops.
Definition: message.cpp:217
interface_fieldtype_t
Interface field type.
Definition: types.h:36
std::map< int, std::string > interface_enum_map_t
Map of enum integer to string values.
Definition: types.h:54
const char * type() const
Get message type.
Definition: message.cpp:356
Message & operator=(const Message &m)
Assign this message to given message.
Definition: message.cpp:303
void set_id(unsigned int message_id)
Set message ID.
Definition: message.cpp:208
virtual Message * clone() const
Clone this message.
Definition: message.cpp:393