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 
35 namespace firevision {
36 
37 class FuseMessageContent;
38 
40 {
41 public:
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 
126 protected:
127  /** Internal message. Fill in derivatives. */
129 
130 private:
131  FuseMessageContent *content_;
132 };
133 
134 } // end namespace firevision
135 
136 #endif
firevision::FuseNetworkMessage::msgc
MT * msgc() const
Get correctly parsed output.
Definition: fuse_message.h:108
firevision::FuseNetworkMessage::_msg
FUSE_message_t _msg
Internal message.
Definition: fuse_message.h:128
firevision::FuseNetworkMessage::~FuseNetworkMessage
~FuseNetworkMessage()
Destructor.
Definition: fuse_message.cpp:112
firevision::FuseMessageContent
FUSE message content.
Definition: fuse_message_content.h:32
fawkes::TypeMismatchException
Type mismatch.
Definition: software.h:44
firevision::FuseNetworkMessage::pack
void pack()
Pack data for sending.
Definition: fuse_message.cpp:191
firevision::FUSE_message_t
FUSE message.
Definition: fuse.h:91
firevision::FUSE_message_t::payload
void * payload
payload
Definition: fuse.h:93
firevision::FuseNetworkMessage::type
uint32_t type() const
Get message type.
Definition: fuse_message.cpp:129
firevision::FuseNetworkMessage::set
void set(FUSE_message_t &msg)
Set from message.
Definition: fuse_message.cpp:181
firevision::FuseNetworkMessage::fmsg
const FUSE_message_t & fmsg() const
Get plain message.
Definition: fuse_message.cpp:156
fawkes::RefCount
Reference counting base class.
Definition: refcount.h:32
firevision::FuseNetworkMessage::FuseNetworkMessage
FuseNetworkMessage()
Constructor.
Definition: fuse_message.cpp:47
firevision::FuseNetworkMessage::msg
MT * msg() const
Get correctly casted payload.
Definition: fuse_message.h:67
firevision::FuseNetworkMessage::payload_size
size_t payload_size() const
Get payload size.
Definition: fuse_message.cpp:138
firevision::FuseNetworkMessage::payload
void * payload() const
Get pointer to payload.
Definition: fuse_message.cpp:147
firevision::FuseNetworkMessage::msg_copy
MT * msg_copy() const
Get copy of correctly casted payload.
Definition: fuse_message.h:85
firevision::FuseNetworkMessage::set_payload
void set_payload(void *payload, size_t payload_size)
Set payload.
Definition: fuse_message.cpp:167
firevision::FuseNetworkMessage
FUSE Network Message.
Definition: fuse_message.h:40
fawkes::Exception
Base class for exceptions in Fawkes.
Definition: exception.h:36