00001 00002 /*************************************************************************** 00003 * message_content.cpp - Fawkes network message content 00004 * 00005 * Created: Fri Jun 01 13:31:18 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 <netcomm/fawkes/message_content.h> 00025 #include <core/exceptions/software.h> 00026 00027 #include <cstring> 00028 00029 namespace fawkes { 00030 00031 /** @class FawkesNetworkMessageContent <netcomm/fawkes/message_content.h> 00032 * Fawkes network message content. 00033 * Interface for complex Fawkes network messages. Use this type if you want 00034 * either a nicer interface to your network message or if you need a more 00035 * complex kind of message type, for example by using DynamicBuffer. 00036 * 00037 * Implement all accessor methods that you need and add any data you want. 00038 * In the end you have to implement serialize() to create a single contiguous 00039 * buffer that contains all the data that has to be sent. Make _payload point 00040 * to this buffer and _payload_size contain the size of the buffer. 00041 * 00042 * @see DynamicBuffer 00043 * @ingroup NetComm 00044 * @author Tim Niemueller 00045 * 00046 * @fn void FawkesNetworkMessageContent::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 FawkesNetworkMessageContent::FawkesNetworkMessageContent() 00054 { 00055 _payload = NULL; 00056 _payload_size = 0; 00057 } 00058 00059 00060 /** Virtual empty destructor. */ 00061 FawkesNetworkMessageContent::~FawkesNetworkMessageContent() 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 FawkesNetworkMessageContent::payload() 00073 { 00074 if ( (_payload == NULL) || (_payload_size == 0) ) { 00075 throw 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 FawkesNetworkMessageContent::payload_size() 00089 { 00090 if ( (_payload == NULL) || (_payload_size == 0) ) { 00091 throw 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 FawkesNetworkMessageContent::copy_payload(size_t offset, const void *buf, size_t len) 00105 { 00106 void *tmp = (void *)((size_t)_payload + offset); 00107 memcpy(tmp, buf, len); 00108 } 00109 00110 } // end namespace fawkes