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/utils/refcount.h>
28 #include <core/exceptions/software.h>
29 #include <fvutils/net/fuse.h>
30 #include <sys/types.h>
31 #include <cstdlib>
32 #include <cstring>
33 
34 namespace firevision {
35 #if 0 /* just to make Emacs auto-indent happy */
36 }
37 #endif
38 
39 class FuseMessageContent;
40 
42 {
43  public:
46  FuseNetworkMessage(FUSE_message_type_t type, void *payload, 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) ) {
70  throw fawkes::TypeMismatchException("FawkesNetworkMessage: message has incorrect size for this type");
71  }
72  return (MT *)(_msg.payload);
73  }
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) ) {
88  throw fawkes::TypeMismatchException("FawkesNetworkMessage: message has incorrect size for this type");
89  }
90  void *tmp = malloc(sizeof(MT));
91  memcpy(tmp, _msg.payload, sizeof(MT));
92  return (MT *)tmp;
93  }
94 
95  /** Get correctly parsed output.
96  * Use this method to cast the payload to a specific complex type. You can use this
97  * routine to parse complex messages that are derived from FuseComplexMessageContent.
98  * Note that the class must provide a constructor that takes three parameters: The
99  * message type, a pointer to the payload and the payload size. From this
100  * the class shall parse the output and throw an exception if that for whatever
101  * reason fails.
102  * @return casted message
103  * @exception TypeMismatchException payload size does not match requested type
104  */
105  template <typename MT>
106  MT *
107  msgc() const
108  {
109  try {
110  MT *m = new MT(type(), _msg.payload, payload_size());
111  return m;
112  } catch (fawkes::Exception &e) {
113  throw;
114  } catch (...) {
115  throw fawkes::Exception("Unknown exception caught while parsing complex network message");
116  }
117  }
118 
119  void pack();
120 
121  void set_payload(void *payload, size_t payload_size);
122  void set(FUSE_message_t &msg);
123  //void set_content(FuseComplexMessageContent *content);
124 
125  protected:
126  /** Internal message. Fill in derivatives. */
128 
129  private:
130  FuseMessageContent *__content;
131 };
132 
133 } // end namespace firevision
134 
135 #endif
void * payload
payload
Definition: fuse.h:94
FUSE_message_t _msg
Internal message.
Definition: fuse_message.h:127
MT * msg_copy() const
Get copy of correctly casted payload.
Definition: fuse_message.h:85
void set_payload(void *payload, size_t payload_size)
Set payload.
void * payload() const
Get pointer to payload.
MT * msgc() const
Get correctly parsed output.
Definition: fuse_message.h:107
FUSE Network Message.
Definition: fuse_message.h:41
uint32_t type() const
Get message type.
void pack()
Pack data for sending.
Base class for exceptions in Fawkes.
Definition: exception.h:36
Reference counting base class.
Definition: refcount.h:32
FUSE message.
Definition: fuse.h:92
MT * msg() const
Get correctly casted payload.
Definition: fuse_message.h:67
const FUSE_message_t & fmsg() const
Get plain message.
size_t payload_size() const
Get payload size.