Fawkes API Fawkes Development Version
fuse_message.h
1
2/***************************************************************************
3 * fuse_message.h - FireVision Remote Control Protocol Message Type
4 *
5 * Created: Wed Nov 07 12:56:18 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#ifndef _FIREVISION_FVUTILS_NET_FUSE_MESSAGE_H_
25#define _FIREVISION_FVUTILS_NET_FUSE_MESSAGE_H_
26
27#include <core/exceptions/software.h>
28#include <core/utils/refcount.h>
29#include <fvutils/net/fuse.h>
30#include <sys/types.h>
31
32#include <cstdlib>
33#include <cstring>
34
35namespace firevision {
36
37class FuseMessageContent;
38
40{
41public:
44 FuseNetworkMessage(FUSE_message_type_t type,
45 void * payload,
46 size_t payload_size,
47 bool copy_payload = false);
48 FuseNetworkMessage(FUSE_message_type_t type, FuseMessageContent *content);
49 FuseNetworkMessage(FUSE_message_type_t type);
51
52 uint32_t type() const;
53 size_t payload_size() const;
54 void * payload() const;
55
56 const FUSE_message_t &fmsg() const;
57
58 /** Get correctly casted payload.
59 * Use this method to cast the payload to a specific type. The size is
60 * check as a sanity check and a TypeMismatchException is thrown if the
61 * size does not match.
62 * @return casted message
63 * @exception TypeMismatchException payload size does not match requested type
64 */
65 template <typename MT>
66 MT *
67 msg() const
68 {
69 if (payload_size() != sizeof(MT)) {
71 "FawkesNetworkMessage: message has incorrect size for this type");
72 }
73 return (MT *)(_msg.payload);
74 }
75
76 /** Get copy of correctly casted payload.
77 * Use this method to cast the payload to a specific type. The size is
78 * check as a sanity check and a TypeMismatchException is thrown if the
79 * size does not match.
80 * @return copy of casted message
81 * @exception TypeMismatchException payload size does not match requested type
82 */
83 template <typename MT>
84 MT *
85 msg_copy() const
86 {
87 if (payload_size() != sizeof(MT)) {
89 "FawkesNetworkMessage: message has incorrect size for this type");
90 }
91 void *tmp = malloc(sizeof(MT));
92 memcpy(tmp, _msg.payload, sizeof(MT));
93 return (MT *)tmp;
94 }
95
96 /** Get correctly parsed output.
97 * Use this method to cast the payload to a specific complex type. You can use this
98 * routine to parse complex messages that are derived from FuseComplexMessageContent.
99 * Note that the class must provide a constructor that takes three parameters: The
100 * message type, a pointer to the payload and the payload size. From this
101 * the class shall parse the output and throw an exception if that for whatever
102 * reason fails.
103 * @return casted message
104 * @exception TypeMismatchException payload size does not match requested type
105 */
106 template <typename MT>
107 MT *
108 msgc() const
109 {
110 try {
111 MT *m = new MT(type(), _msg.payload, payload_size());
112 return m;
113 } catch (fawkes::Exception &e) {
114 throw;
115 } catch (...) {
116 throw fawkes::Exception("Unknown exception caught while parsing complex network message");
117 }
118 }
119
120 void pack();
121
122 void set_payload(void *payload, size_t payload_size);
123 void set(FUSE_message_t &msg);
124 //void set_content(FuseComplexMessageContent *content);
125
126protected:
127 /** Internal message. Fill in derivatives. */
129
130private:
131 FuseMessageContent *content_;
132};
133
134} // end namespace firevision
135
136#endif
Base class for exceptions in Fawkes.
Definition: exception.h:36
Reference counting base class.
Definition: refcount.h:32
FUSE Network Message.
Definition: fuse_message.h:40
void * payload() const
Get pointer to payload.
MT * msg() const
Get correctly casted payload.
Definition: fuse_message.h:67
void set_payload(void *payload, size_t payload_size)
Set payload.
MT * msgc() const
Get correctly parsed output.
Definition: fuse_message.h:108
FUSE_message_t _msg
Internal message.
Definition: fuse_message.h:128
MT * msg_copy() const
Get copy of correctly casted payload.
Definition: fuse_message.h:85
size_t payload_size() const
Get payload size.
const FUSE_message_t & fmsg() const
Get plain message.
void pack()
Pack data for sending.
uint32_t type() const
Get message type.
void set(FUSE_message_t &msg)
Set from message.
FUSE message.
Definition: fuse.h:91
void * payload
payload
Definition: fuse.h:93