fuse_message_content.cpp

00001 
00002 /***************************************************************************
00003  *  fuse_message_content.cpp - FUSE network message content
00004  *
00005  *  Created: Thu Nov 22 17:23:20 2007
00006  *  Copyright  2006-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <fvutils/net/fuse_message_content.h>
00025 #include <core/exceptions/software.h>
00026 
00027 #include <cstring>
00028 #include <cstdlib>
00029 
00030 namespace firevision {
00031 #if 0 /* just to make Emacs auto-indent happy */
00032 }
00033 #endif
00034 
00035 /** @class FuseMessageContent <fvutils/net/fuse_message_content.h>
00036  * FUSE message content.
00037  * Interface for complex FUSE network messages. Use this type if you want
00038  * either a nicer interface to your network message or if you need a more
00039  * complex kind of message type, for example by using DynamicBuffer.
00040  *
00041  * Implement all accessor methods that you need and add any data you want.
00042  * In the end you have to implement serialize() to create a single contiguous
00043  * buffer that contains all the data that has to be sent. Make _payload point
00044  * to this buffer and _payload_size contain the size of the buffer.
00045  *
00046  * @see DynamicBuffer
00047  * @ingroup FUSE
00048  * @ingroup FireVisioin
00049  * @author Tim Niemueller
00050  *
00051  * @fn void FuseMessageContent::serialize() = 0
00052  * Serialize message content.
00053  * Generate a single contiguous buffer. Make _payload point to this buffer and
00054  * _payload_size contain the size of the buffer.
00055  */
00056 
00057 /** Constructor. */
00058 FuseMessageContent::FuseMessageContent()
00059 {
00060   _payload = NULL;
00061   _payload_size = 0;
00062 }
00063 
00064 
00065 /** Virtual empty destructor. */
00066 FuseMessageContent::~FuseMessageContent()
00067 {
00068 }
00069 
00070 
00071 /** Return pointer to payload.
00072  * @return pointer to payload
00073  * @exception NullPointerException thrown if _payload does not point to a valid
00074  * buffer or if _payload_size is zero.
00075  */
00076 void *
00077 FuseMessageContent::payload() const
00078 {
00079   if ( (_payload == NULL) || (_payload_size == 0) ) {
00080     throw fawkes::NullPointerException("Payload in network message content may not be NULL");
00081   }
00082   return _payload;
00083 }
00084 
00085 
00086 
00087 /** Return payload size
00088  * @return payload size
00089  * @exception NullPointerException thrown if _payload does not point to a valid
00090  * buffer or if _payload_size is zero.
00091  */
00092 size_t
00093 FuseMessageContent::payload_size() const
00094 {
00095   if ( (_payload == NULL) || (_payload_size == 0) ) {
00096     throw fawkes::NullPointerException("Payload in network message content may not be NULL");
00097   }
00098   return _payload_size;
00099 }
00100 
00101 
00102 /** Copy payload into payload buffer to a specified offset.
00103  * This assumes that you have made sure that the buffer is big enough!
00104  * @param offset offset in _payload where to copy the data to
00105  * @param buf buffer to copy from
00106  * @param len number of bytes to copy from buf
00107  */
00108 void
00109 FuseMessageContent::copy_payload(size_t offset, void *buf, size_t len)
00110 {
00111   void *tmp = (void *)((size_t)_payload + offset);
00112   memcpy(tmp, buf, len);
00113 }
00114 
00115 
00116 /** Free message payload. */
00117 void
00118 FuseMessageContent::free_payload()
00119 {
00120   if ( _payload )  free(_payload);
00121   _payload = NULL;
00122   _payload_size = 0;
00123 }
00124 
00125 } // end namespace firevision