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
32namespace fawkes {
33
34#pragma pack(push, 4)
35
36/** Fawkes network message header.
37 * Header that is prepended to all following messages.
38 */
39typedef 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 */
53typedef 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 */
63typedef struct
64{
65 unsigned int size; /**< size of the following payload. */
67
69{
70public:
71 FawkesNetworkMessageTooBigException(size_t message_size);
72};
73
75
77{
78public:
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);
177
178 void pack();
179
180private:
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
188};
189
190} // end namespace fawkes
191
192#endif
Base class for exceptions in Fawkes.
Definition: exception.h:36
Fawkes network message content.
The given message size exceeds the limit.
Definition: message.h:69
FawkesNetworkMessageTooBigException(size_t message_size)
Constructor.
Definition: message.cpp:47
Representation of a message that is sent over the network.
Definition: message.h:77
const fawkes_message_t & fmsg() const
Get message reference.
Definition: message.cpp:321
unsigned short int msgid() const
Get message type ID.
Definition: message.cpp:294
unsigned short int cid() const
Get component ID.
Definition: message.cpp:285
MT * msg() const
Get correctly casted payload.
Definition: message.h:120
MT * msgge() const
Get correctly casted payload.
Definition: message.h:139
void set_message_id(unsigned short int msg_id)
Set message type ID.
Definition: message.cpp:348
unsigned int clid() const
Get client ID.
Definition: message.cpp:276
void * payload() const
Get payload buffer.
Definition: message.cpp:312
virtual ~FawkesNetworkMessage()
Destructor.
Definition: message.cpp:259
size_t payload_size() const
Get payload size.
Definition: message.cpp:303
MT * msgc() const
Get correctly parsed output.
Definition: message.h:159
void set_client_id(unsigned int clid)
Set client ID.
Definition: message.cpp:330
void set_component_id(unsigned short int cid)
Set component ID.
Definition: message.cpp:339
void pack()
Pack data for sending.
Definition: message.cpp:392
FawkesNetworkMessage()
Constructor.
Definition: message.cpp:87
void set(fawkes_message_t &msg)
Set from message.
Definition: message.cpp:372
void set_payload(void *payload, size_t payload_size)
Set payload.
Definition: message.cpp:358
void set_content(FawkesNetworkMessageContent *content)
Set complex message content.
Definition: message.cpp:381
Reference counting base class.
Definition: refcount.h:32
Fawkes library namespace.
Fawkes network message header.
Definition: message.h:40
unsigned short int cid
component id
Definition: message.h:41
unsigned short int msg_id
message id
Definition: message.h:42
unsigned int payload_size
payload size in bytes
Definition: message.h:43
Message as stored in local queues.
Definition: message.h:54
fawkes_message_header_t header
message header
Definition: message.h:55
void * payload
message payload
Definition: message.h:56
Fawkes transfer header.
Definition: message.h:64
unsigned int size
size of the following payload.
Definition: message.h:65