Fawkes API  Fawkes Development Version
message_content.cpp
1 
2 /***************************************************************************
3  * message_content.cpp - Fawkes network message content
4  *
5  * Created: Fri Jun 01 13:31:18 2007
6  * Copyright 2006-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 <netcomm/fawkes/message_content.h>
25 #include <core/exceptions/software.h>
26 
27 #include <cstring>
28 
29 namespace fawkes {
30 
31 /** @class FawkesNetworkMessageContent <netcomm/fawkes/message_content.h>
32  * Fawkes network message content.
33  * Interface for complex Fawkes network messages. Use this type if you want
34  * either a nicer interface to your network message or if you need a more
35  * complex kind of message type, for example by using DynamicBuffer.
36  *
37  * Implement all accessor methods that you need and add any data you want.
38  * In the end you have to implement serialize() to create a single contiguous
39  * buffer that contains all the data that has to be sent. Make _payload point
40  * to this buffer and _payload_size contain the size of the buffer.
41  *
42  * @see DynamicBuffer
43  * @ingroup NetComm
44  * @author Tim Niemueller
45  *
46  * @fn void FawkesNetworkMessageContent::serialize() = 0
47  * Serialize message content.
48  * Generate a single contiguous buffer. Make _payload point to this buffer and
49  * _payload_size contain the size of the buffer.
50  */
51 
52 /** Constructor. */
54 {
55  _payload = NULL;
56  _payload_size = 0;
57 }
58 
59 
60 /** Virtual empty destructor. */
62 {
63 }
64 
65 
66 /** Return pointer to payload.
67  * @return pointer to payload
68  * @exception NullPointerException thrown if _payload does not point to a valid
69  * buffer or if _payload_size is zero.
70  */
71 void *
73 {
74  if ( (_payload == NULL) || (_payload_size == 0) ) {
75  throw NullPointerException("Payload in network message content may not be NULL");
76  }
77  return _payload;
78 }
79 
80 
81 
82 /** Return payload size
83  * @return payload size
84  * @exception NullPointerException thrown if _payload does not point to a valid
85  * buffer or if _payload_size is zero.
86  */
87 size_t
89 {
90  if ( (_payload == NULL) || (_payload_size == 0) ) {
91  throw NullPointerException("Payload in network message content may not be NULL");
92  }
93  return _payload_size;
94 }
95 
96 
97 /** Copy payload into payload buffer to a specified offset.
98  * This assumes that you have made sure that the buffer is big enough!
99  * @param offset offset in _payload where to copy the data to
100  * @param buf buffer to copy from
101  * @param len number of bytes to copy from buf
102  */
103 void
104 FawkesNetworkMessageContent::copy_payload(size_t offset, const void *buf, size_t len)
105 {
106  void *tmp = (void *)((size_t)_payload + offset);
107  memcpy(tmp, buf, len);
108 }
109 
110 } // end namespace fawkes
void * _payload
Pointer to payload.
Fawkes library namespace.
A NULL pointer was supplied where not allowed.
Definition: software.h:34
virtual void * payload()
Return pointer to payload.
void copy_payload(size_t offset, const void *buf, size_t len)
Copy payload into payload buffer to a specified offset.
virtual ~FawkesNetworkMessageContent()
Virtual empty destructor.
virtual size_t payload_size()
Return payload size.