Fawkes API  Fawkes Development Version
message.h
1 
2 /***************************************************************************
3  * message.h - Fawkes network message
4  *
5  * Created: Mon Nov 20 18:00:09 2006
6  * Copyright 2006 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 _NETCOMM_FAWKES_MESSAGE_H_
25 #define _NETCOMM_FAWKES_MESSAGE_H_
26 
27 #include <core/exceptions/software.h>
28 #include <core/utils/refcount.h>
29 
30 #include <cstddef>
31 
32 namespace fawkes {
33 
34 #pragma pack(push, 4)
35 
36 /** Fawkes network message header.
37  * Header that is prepended to all following messages.
38  */
39 typedef struct
40 {
41  unsigned short int cid; /**< component id */
42  unsigned short int msg_id; /**< message id */
43  unsigned int payload_size; /**< payload size in bytes */
45 
46 #pragma pack(pop)
47 
48 /** Message as stored in local queues.
49  * A message takes a header and a pointer to the data that
50  * has the size mentioned in header.payload_size that is to be
51  * sent over the network.
52  */
53 typedef struct
54 {
55  fawkes_message_header_t header; /**< message header */
56  void * payload; /**< message payload */
58 
59 /** Fawkes transfer header.
60  * This header is prepended to a collection of messages that is sent
61  * at once.
62  */
63 typedef struct
64 {
65  unsigned int size; /**< size of the following payload. */
67 
69 {
70 public:
71  FawkesNetworkMessageTooBigException(size_t message_size);
72 };
73 
75 
76 class FawkesNetworkMessage : public RefCount
77 {
78 public:
81  FawkesNetworkMessage(unsigned int clid,
82  unsigned short int cid,
83  unsigned short int msg_id,
84  void * payload,
85  size_t payload_size);
86  FawkesNetworkMessage(unsigned int clid, unsigned short int cid, unsigned short int msg_id);
87  FawkesNetworkMessage(unsigned short int cid,
88  unsigned short int msg_id,
89  void * payload,
90  size_t payload_size);
91  FawkesNetworkMessage(unsigned int clid,
92  unsigned short int cid,
93  unsigned short int msg_id,
95  FawkesNetworkMessage(unsigned short int cid,
96  unsigned short int msg_id,
98  FawkesNetworkMessage(unsigned short int cid, unsigned short int msg_id, size_t payload_size);
99  FawkesNetworkMessage(unsigned short int cid, unsigned short int msg_id);
101 
102  virtual ~FawkesNetworkMessage();
103 
104  unsigned int clid() const;
105  unsigned short int cid() const;
106  unsigned short int msgid() const;
107  size_t payload_size() const;
108  void * payload() const;
109  const fawkes_message_t &fmsg() const;
110 
111  /** Get correctly casted payload.
112  * Use this method to cast the payload to a specific type. The size is
113  * check as a sanity check and a TypeMismatchException is thrown if the
114  * size does not match.
115  * @return casted message
116  * @exception TypeMismatchException payload size does not match requested type
117  */
118  template <typename MT>
119  MT *
120  msg() const
121  {
122  if (payload_size() != sizeof(MT)) {
123  throw TypeMismatchException("FawkesNetworkMessage: message has incorrect size for this type");
124  }
125  return (MT *)(_msg.payload);
126  }
127 
128  /** Get correctly casted payload.
129  * Use this method to cast the payload to a specific type. The size is
130  * check as a sanity check and a TypeMismatchException is thrown if the
131  * size does not match. The size of the received message must be greater or
132  * equal to the size of the message type. Useful if message contains a variable
133  * length string.
134  * @return casted message
135  * @exception TypeMismatchException payload size does not match requested type
136  */
137  template <typename MT>
138  MT *
139  msgge() const
140  {
141  if (payload_size() < sizeof(MT)) {
142  throw TypeMismatchException("FawkesNetworkMessage: message has incorrect size for this type");
143  }
144  return (MT *)(_msg.payload);
145  }
146 
147  /** Get correctly parsed output.
148  * Use this method to cast the payload to a specific complex type. You can use this
149  * routine to parse complex messages that are derived from FawkesNetworkMessageContent.
150  * Note that the class must provide a constructor that takes four parameters: The
151  * component ID, message ID, a pointer to the payload and the payload size. From this
152  * the class shall parse the output and throw an exception if that for whatever
153  * reason fails.
154  * @return casted message
155  * @exception TypeMismatchException payload size does not match requested type
156  */
157  template <typename MT>
158  MT *
159  msgc() const
160  {
161  try {
162  MT *m = new MT(cid(), msgid(), _msg.payload, payload_size());
163  return m;
164  } catch (Exception &e) {
165  throw;
166  } catch (...) {
167  throw Exception("Unknown exception caught while parsing complex network message");
168  }
169  }
170 
171  void set_client_id(unsigned int clid);
172  void set_component_id(unsigned short int cid);
173  void set_message_id(unsigned short int msg_id);
174  void set_payload(void *payload, size_t payload_size);
175  void set(fawkes_message_t &msg);
176  void set_content(FawkesNetworkMessageContent *content);
177 
178  void pack();
179 
180 private:
181  void init_cid_msgid(unsigned short int cid, unsigned short int msg_id);
182  void init_payload(size_t payload_size);
183 
184  unsigned int _clid;
185  fawkes_message_t _msg;
186 
187  FawkesNetworkMessageContent *_content;
188 };
189 
190 } // end namespace fawkes
191 
192 #endif
fawkes::FawkesNetworkMessage::msgge
MT * msgge() const
Get correctly casted payload.
Definition: message.h:143
fawkes::FawkesNetworkMessageTooBigException
Definition: message.h:72
fawkes::FawkesNetworkMessageTooBigException::FawkesNetworkMessageTooBigException
FawkesNetworkMessageTooBigException(size_t message_size)
Constructor.
Definition: message.cpp:51
fawkes::FawkesNetworkMessage::cid
unsigned short int cid() const
Get component ID.
Definition: message.cpp:289
fawkes::FawkesNetworkMessage::pack
void pack()
Pack data for sending.
Definition: message.cpp:396
fawkes::FawkesNetworkMessage::msgc
MT * msgc() const
Get correctly parsed output.
Definition: message.h:163
fawkes::FawkesNetworkMessage::set_component_id
void set_component_id(unsigned short int cid)
Set component ID.
Definition: message.cpp:343
fawkes::FawkesNetworkMessage::set_payload
void set_payload(void *payload, size_t payload_size)
Set payload.
Definition: message.cpp:362
fawkes::FawkesNetworkMessage::set_message_id
void set_message_id(unsigned short int msg_id)
Set message type ID.
Definition: message.cpp:352
fawkes::FawkesNetworkMessage::msg
MT * msg() const
Get correctly casted payload.
Definition: message.h:124
fawkes::TypeMismatchException
Definition: software.h:47
fawkes
fawkes::fawkes_transfer_header_t
Fawkes transfer header.
Definition: message.h:67
fawkes::FawkesNetworkMessage::payload_size
size_t payload_size() const
Get payload size.
Definition: message.cpp:307
fawkes::fawkes_message_t::payload
void * payload
message payload
Definition: message.h:60
fawkes::FawkesNetworkMessage::payload
void * payload() const
Get payload buffer.
Definition: message.cpp:316
fawkes::FawkesNetworkMessage::~FawkesNetworkMessage
virtual ~FawkesNetworkMessage()
Destructor.
Definition: message.cpp:263
fawkes::FawkesNetworkMessage::FawkesNetworkMessage
FawkesNetworkMessage()
Constructor.
Definition: message.cpp:91
fawkes::FawkesNetworkMessage::fmsg
const fawkes_message_t & fmsg() const
Get message reference.
Definition: message.cpp:325
fawkes::fawkes_message_header_t
Fawkes network message header.
Definition: message.h:43
fawkes::RefCount
Definition: refcount.h:35
fawkes::FawkesNetworkMessage::set
void set(fawkes_message_t &msg)
Set from message.
Definition: message.cpp:376
fawkes::FawkesNetworkMessage
Definition: message.h:80
fawkes::fawkes_message_t
Message as stored in local queues.
Definition: message.h:57
fawkes::FawkesNetworkMessage::set_client_id
void set_client_id(unsigned int clid)
Set client ID.
Definition: message.cpp:334
fawkes::FawkesNetworkMessage::clid
unsigned int clid() const
Get client ID.
Definition: message.cpp:280
fawkes::FawkesNetworkMessage::set_content
void set_content(FawkesNetworkMessageContent *content)
Set complex message content.
Definition: message.cpp:385
fawkes::FawkesNetworkMessage::msgid
unsigned short int msgid() const
Get message type ID.
Definition: message.cpp:298
fawkes::FawkesNetworkMessageContent
Definition: message_content.h:37
fawkes::Exception
Definition: exception.h:39