Fawkes API  Fawkes Development Version
fuse_message.cpp
1 
2 /***************************************************************************
3  * fuse_message.cpp - FireVision Remote Control Protocol Message Type
4  *
5  * Created: Wed Nov 07 13:01:20 2007
6  * Copyright 2005-2007 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 #include <core/exceptions/software.h>
25 #include <fvutils/net/fuse_message.h>
26 #include <fvutils/net/fuse_message_content.h>
27 
28 #include <cstdio>
29 #include <cstring>
30 #include <cstdlib>
31 #include <netinet/in.h>
32 
33 namespace firevision {
34 #if 0 /* just to make Emacs auto-indent happy */
35 }
36 #endif
37 
38 /** @class FuseNetworkMessage <fvutils/net/fuse_message.h>
39  * FUSE Network Message.
40  * This is the basic entity for messages that are sent over the network. Either
41  * just use this message to send arbitrary payload or derive this class for more
42  * complex behavior or nice encapsulations of messages.
43  *
44  * @ingroup FUSE
45  * @ingroup FireVision
46  * @author Tim Niemueller
47  */
48 
49 /** Constructor. */
51 {
52  memset(&_msg, 0, sizeof(_msg));
53  __content = NULL;
54 }
55 
56 
57 /** Constructor.
58  * @param msg message information to copy
59  */
61 {
62  memcpy(&_msg, msg, sizeof(FUSE_message_t));
63  __content = NULL;
64 }
65 
66 
67 /** Constructor.
68  * @param type message type
69  * @param payload payload
70  * @param payload_size size of payload
71  * @param copy_payload if true payload is copied, otherwise payload is referenced
72  * and ownership of payload is claimed.
73  */
75  void *payload, size_t payload_size,
76  bool copy_payload)
77 {
78  __content = NULL;
79  _msg.header.message_type = htonl(type);
80  _msg.header.payload_size = htonl(payload_size);
81 
82  if ( copy_payload ) {
83  _msg.payload = malloc(payload_size);
84  memcpy(_msg.payload, payload, payload_size);
85  } else {
87  }
88 }
89 
90 
91 /** Constructor without payload.
92  * Constructs message without payload.
93  * @param type FUSE message type
94  */
96 {
97  __content = NULL;
98  _msg.header.message_type = htonl(type);
99  _msg.header.payload_size = htonl(0);
100  _msg.payload = NULL;
101 }
102 
103 
104 /** Content constructor.
105  * Construct a message with complex message content.
106  * @param type FUSE message type
107  * @param content complex message content.
108  */
110 {
111  __content = content;
112  _msg.header.message_type = htonl(type);
113  _msg.header.payload_size = htonl(0);
114  _msg.payload = NULL;
115 }
116 
117 /** Destructor. */
119 {
120  if ( __content == NULL ) {
121  if ( _msg.payload != NULL ) {
122  free(_msg.payload);
123  _msg.payload = NULL;
124  }
125  } else {
126  __content->free_payload();
127  delete __content;
128  }
129 }
130 
131 /** Get message type.
132  * @return message type
133  */
134 uint32_t
136 {
137  return ntohl(_msg.header.message_type);
138 }
139 
140 
141 /** Get payload size.
142  * @return payload size
143  */
144 size_t
146 {
147  return ntohl(_msg.header.payload_size);
148 }
149 
150 
151 /** Get pointer to payload.
152  * @return pointer to payload.
153  */
154 void *
156 {
157  return _msg.payload;
158 }
159 
160 
161 /** Get plain message.
162  * @return plain message
163  */
164 const FUSE_message_t &
166 {
167  return _msg;
168 }
169 
170 
171 /** Set payload.
172  * Payload is referenced and ownership claimed.
173  * @param payload payload
174  * @param payload_size size of payload
175  */
176 void
178 {
179  if ( payload_size > 0xFFFFFFFF ) {
180  // cannot carry that many bytes
181  throw fawkes::OutOfBoundsException("Payload too big", payload_size, 0, 0xFFFFFFFF);
182  }
183  _msg.payload = payload;
184  _msg.header.payload_size = htonl(payload_size);
185 }
186 
187 
188 /** Set from message.
189  * @param msg reference to message. Content is deep-copied.
190  */
191 void
193 {
194  memcpy(&_msg, &msg, sizeof(FUSE_message_t));
195 }
196 
197 /** Pack data for sending.
198  * Use this if any additional packing is needed before sending the data (for
199  * example if using a DynamicBuffer).
200  */
201 void
203 {
204  if ( __content != NULL ) {
205  __content->serialize();
206  _msg.payload = __content->payload();
207  _msg.header.payload_size = htonl(__content->payload_size());
208  }
209 }
210 
211 } // end namespace firevision
void * payload
payload
Definition: fuse.h:94
FUSE_message_t _msg
Internal message.
Definition: fuse_message.h:127
void set_payload(void *payload, size_t payload_size)
Set payload.
virtual void * payload() const
Return pointer to payload.
void * payload() const
Get pointer to payload.
void free_payload()
Free message payload.
uint32_t type() const
Get message type.
void pack()
Pack data for sending.
void set(FUSE_message_t &msg)
Set from message.
virtual size_t payload_size() const
Return payload size.
FUSE message.
Definition: fuse.h:92
virtual void serialize()=0
Serialize message content.
uint32_t payload_size
payload size
Definition: fuse.h:88
uint32_t message_type
packet type from FUSE_message_type_t
Definition: fuse.h:87
Index out of bounds.
Definition: software.h:88
MT * msg() const
Get correctly casted payload.
Definition: fuse_message.h:67
const FUSE_message_t & fmsg() const
Get plain message.
FUSE_header_t header
header
Definition: fuse.h:93
size_t payload_size() const
Get payload size.