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 /** @class FuseMessageContent <fvutils/net/fuse_message_content.h> 00031 * FUSE message content. 00032 * Interface for complex FUSE network messages. Use this type if you want 00033 * either a nicer interface to your network message or if you need a more 00034 * complex kind of message type, for example by using DynamicBuffer. 00035 * 00036 * Implement all accessor methods that you need and add any data you want. 00037 * In the end you have to implement serialize() to create a single contiguous 00038 * buffer that contains all the data that has to be sent. Make _payload point 00039 * to this buffer and _payload_size contain the size of the buffer. 00040 * 00041 * @see DynamicBuffer 00042 * @ingroup FUSE 00043 * @ingroup FireVisioin 00044 * @author Tim Niemueller 00045 * 00046 * @fn void FuseMessageContent::serialize() = 0 00047 * Serialize message content. 00048 * Generate a single contiguous buffer. Make _payload point to this buffer and 00049 * _payload_size contain the size of the buffer. 00050 */ 00051 00052 /** Constructor. */ 00053 FuseMessageContent::FuseMessageContent() 00054 { 00055 _payload = NULL; 00056 _payload_size = 0; 00057 } 00058 00059 00060 /** Virtual empty destructor. */ 00061 FuseMessageContent::~FuseMessageContent() 00062 { 00063 } 00064 00065 00066 /** Return pointer to payload. 00067 * @return pointer to payload 00068 * @exception NullPointerException thrown if _payload does not point to a valid 00069 * buffer or if _payload_size is zero. 00070 */ 00071 void * 00072 FuseMessageContent::payload() const 00073 { 00074 if ( (_payload == NULL) || (_payload_size == 0) ) { 00075 throw fawkes::NullPointerException("Payload in network message content may not be NULL"); 00076 } 00077 return _payload; 00078 } 00079 00080 00081 00082 /** Return payload size 00083 * @return payload size 00084 * @exception NullPointerException thrown if _payload does not point to a valid 00085 * buffer or if _payload_size is zero. 00086 */ 00087 size_t 00088 FuseMessageContent::payload_size() const 00089 { 00090 if ( (_payload == NULL) || (_payload_size == 0) ) { 00091 throw fawkes::NullPointerException("Payload in network message content may not be NULL"); 00092 } 00093 return _payload_size; 00094 } 00095 00096 00097 /** Copy payload into payload buffer to a specified offset. 00098 * This assumes that you have made sure that the buffer is big enough! 00099 * @param offset offset in _payload where to copy the data to 00100 * @param buf buffer to copy from 00101 * @param len number of bytes to copy from buf 00102 */ 00103 void 00104 FuseMessageContent::copy_payload(size_t offset, void *buf, size_t len) 00105 { 00106 void *tmp = (void *)((size_t)_payload + offset); 00107 memcpy(tmp, buf, len); 00108 } 00109 00110 00111 /** Free message payload. */ 00112 void 00113 FuseMessageContent::free_payload() 00114 { 00115 if ( _payload ) free(_payload); 00116 _payload = NULL; 00117 _payload_size = 0; 00118 }

