DiscoveryUdpMessage.cpp

Go to the documentation of this file.
00001 /** @file    DiscoveryUdpMessage.cpp
00002  *  @author  Alessandro Polo
00003  *  @version $Id: DiscoveryUdpMessage.cpp 3775 2011-01-01 16:38:17Z alex $
00004  *  @brief
00005  * File containing methods for the DiscoveryUdpMessage class.
00006  * The header for this class can be found in DiscoveryUdpMessage.h, check that file
00007  * for class description.
00008  ****************************************************************************/
00009 /* Copyright (c) 2007-2011, WOSH - Wide Open Smart Home 
00010  * by Alessandro Polo - OpenSmartHome.com
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions are met:
00015  *     * Redistributions of source code must retain the above copyright
00016  *       notice, this list of conditions and the following disclaimer.
00017  *     * Redistributions in binary form must reproduce the above copyright
00018  *       notice, this list of conditions and the following disclaimer in the
00019  *       documentation and/or other materials provided with the distribution.
00020  *     * Neither the name of the OpenSmartHome.com WOSH nor the
00021  *       names of its contributors may be used to endorse or promote products
00022  *       derived from this software without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
00025  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
00028  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00033  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  ****************************************************************************/
00035 
00036  #include "DiscoveryUdpMessage.h"
00037  #include <core/Utilities.h>
00038  #include <string.h>
00039 
00040 
00041 using namespace std;
00042 using namespace wosh;
00043 
00044 
00045 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00046 
00047 long DiscoveryUdpFormat::isValidPacket( const char* data, int maxSize ) {
00048     if ( data == NULL) return 0;
00049     static const int len = strlen(_DiscoveryUdpFormat_PacketHeader);
00050     if ( maxSize < len ) return 0;
00051     if ( strncmp(_DiscoveryUdpFormat_PacketHeader, data, len) != 0 ) return 0;
00052     data += len;
00053     data += sizeof(char)*2;
00054     int32 proto = 0;
00055     memcpy( &proto, data, sizeof(int32) );
00056     return proto;
00057  }
00058 
00059 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00060 
00061 int DiscoveryUdpFormat::writeHeader( char* out_data, int maxSize ) const {
00062     if ( out_data == NULL ) return 0;
00063     static const int len = strlen(_DiscoveryUdpFormat_PacketHeader);
00064     if ( maxSize < (int)(len+sizeof(char)*2+sizeof(int32)) ) return 0;
00065     strcpy(out_data, _DiscoveryUdpFormat_PacketHeader);
00066     out_data += len;
00067     memset( out_data, 0, sizeof(char)*2 );
00068     out_data += sizeof(char)*2;
00069     int32 proto = getProtocolVersion();
00070     memcpy( out_data, &proto, sizeof(int32) );
00071     return len + sizeof(char)*2 + sizeof(int32);
00072  }
00073 
00074 int DiscoveryUdpFormat::headerSize() const {
00075     return strlen(_DiscoveryUdpFormat_PacketHeader) + sizeof(char)*2 + sizeof(int32);
00076  }
00077 
00078 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00079 
00080 char* DiscoveryUdpFormat::putInt( long value, char* data, int maxSize ) {
00081     if ( maxSize < (int)sizeof(int32) ) return data;
00082     int32 tmpInteger = value;
00083     memcpy( data, &tmpInteger, sizeof(int32) );
00084     memset( data+sizeof(char)*2, 0, sizeof(char)*2 );
00085     return data + sizeof(char)*2 + sizeof(int32);
00086  }
00087 
00088 const char* DiscoveryUdpFormat::popInt( long& value, const char* data, int maxSize ) {
00089     if ( maxSize < (int)sizeof(int32) ) return data;
00090     int32 tmpInteger = value;
00091     memcpy( &tmpInteger, data, sizeof(int32) );
00092     value = tmpInteger;
00093     return data + sizeof(char)*2 + sizeof(int32);
00094  }
00095 
00096 char* DiscoveryUdpFormat::putString( const std::string& value, char* data, int maxSize ) {
00097     if ( maxSize < (int)value.size() ) return data;
00098     memcpy( data, value.c_str(), value.size() );
00099     memset( data+value.size()+sizeof(char), 0, sizeof(char)*2 );
00100     return data + value.size() + sizeof(char)*2;
00101  }
00102 
00103 const char* DiscoveryUdpFormat::popString( std::string& value, const char* data, int maxSize ) {
00104         if ( maxSize < (int)sizeof(char)*2 ) return data;
00105     value.assign(data);
00106     return data + value.size() + sizeof(char)*2;
00107  }
00108 
00109 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00110 
00111 bool DiscoveryUdpFormat_84::isRequest( const char* data, int maxSize ) {
00112     if ( data == NULL) return false;
00113     int offset = headerSize();
00114     if ( offset >= maxSize ) return false;
00115     if ( strcmp(data+offset, _DiscoveryUdpFormat_PacketRequest) == 0 ) return true;
00116     return false;
00117  }
00118 
00119 WRESULT DiscoveryUdpFormat_84::encode( wosh::WoshDiscoveryInfo& d_info, wosh::WoshDiscoveryData& d_data, char* out_data, int& out_size, int buffer_size ) {
00120     if ( out_data == NULL) return WRET_ERR_PARAM;
00121     char* outp = out_data + writeHeader(out_data, buffer_size);
00122 
00123     outp = putString( d_data.getKernelName(), outp, buffer_size-(outp-out_data) );
00124     outp = putInt( d_data.getKernelState(), outp, buffer_size-(outp-out_data) );
00125     outp = putInt( d_info.getNotifyFrequency(), outp, buffer_size-(outp-out_data) );
00126     outp = putString( d_info.getLocalProtocol(), outp, buffer_size-(outp-out_data) );
00127     
00128     string protocols; String::join(protocols, d_data.protocols, ";;");
00129     outp = putString( protocols, outp, buffer_size-(outp-out_data) );
00130 
00131     string neighbours; String::join(neighbours, d_data.neighbours, ";");
00132     outp = putString( neighbours, outp, buffer_size-(outp-out_data) );
00133 
00134     memset(outp, 0, sizeof(char)*2);
00135     out_size = outp - out_data + sizeof(char)*2;
00136     return WRET_OK;
00137  }
00138 
00139 WRESULT DiscoveryUdpFormat_84::decode( wosh::WoshDiscoveryInfo& d_info, wosh::WoshDiscoveryData& d_data, const char* data, int maxSize ) {
00140     if ( data == NULL) return WRET_ERR_PARAM;
00141     const char* inp = data + headerSize();
00142     string bufferStr; long bufferLong;
00143     bufferStr.clear();
00144     inp = popString(bufferStr, inp, maxSize-(inp-data) ); d_data.setKernelName(bufferStr);
00145     bufferStr.clear();
00146     inp = popInt(bufferLong, inp, maxSize-(inp-data) ); d_data.setKernelState(bufferLong);
00147     bufferStr.clear();
00148     inp = popInt(bufferLong, inp, maxSize-(inp-data) ); d_info.setNotifyFrequency(bufferLong);
00149     bufferStr.clear();
00150     inp = popString(bufferStr, inp, maxSize-(inp-data) ); d_info.setLocalProtocol(bufferStr);
00151     bufferStr.clear();
00152     d_data.protocols.clear();
00153     inp = popString(bufferStr, inp, maxSize-(inp-data) );
00154     wosh::String::split(d_data.protocols, bufferStr, ";;", _String_SPLIT_OPTION_NOT_EMPTY | _String_SPLIT_OPTION_TRIM | _String_SPLIT_OPTION_UNIQUE );
00155     bufferStr.clear();
00156     d_data.neighbours.clear();
00157     inp = popString(bufferStr, inp, maxSize-(inp-data) );
00158     wosh::String::split(d_data.neighbours, bufferStr, ";", _String_SPLIT_OPTION_NOT_EMPTY | _String_SPLIT_OPTION_TRIM | _String_SPLIT_OPTION_UNIQUE );
00159     return WRET_OK;
00160  }
00161 
00162 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00163 
00164 bool DiscoveryUdpFormat_85::isRequest( const char* data, int maxSize ) {
00165     if ( data == NULL) return false;
00166     (void)maxSize;
00167     return false;
00168  }
00169 
00170 WRESULT DiscoveryUdpFormat_85::encode( wosh::WoshDiscoveryInfo& d_info, wosh::WoshDiscoveryData& d_data, char* out_data, int& out_size, int buffer_size ) {
00171     if ( out_data == NULL) return WRET_ERR_PARAM;
00172     (void)d_info; (void)d_data; (void)out_size; (void)buffer_size;
00173     return WRET_NOT_IMPLEMENTED;
00174  }
00175 
00176 WRESULT DiscoveryUdpFormat_85::decode( wosh::WoshDiscoveryInfo& d_info, wosh::WoshDiscoveryData& d_data, const char* data, int maxSize ) {
00177     if ( data == NULL) return WRET_ERR_PARAM;
00178     (void)d_info; (void)d_data; (void)maxSize;
00179     return WRET_NOT_IMPLEMENTED;
00180  }
00181 
00182 ///////////////////////////////////////////////////////////////////////////////////////////////////// SETTERS
00183 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00184 ///////////////////////////////////////////////////////////////////////////////////////////////////// GETTERS
00185 
00186 
00187 ///////////////////////////////////////////////////////////////////////////////////////////////////// GETTERS
00188 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00189 
00190 DiscoveryUdpMessageRequest::DiscoveryUdpMessageRequest() {
00191     this->buffer_size = strlen(_DiscoveryUdpFormat_PacketHeader);
00192     this->buffer = new char[this->buffer_size+1];
00193     memset(this->buffer, 0, this->buffer_size+1);
00194     memcpy(this->buffer, _DiscoveryUdpFormat_PacketHeader, this->buffer_size);
00195  }
00196 
00197 DiscoveryUdpMessageRequest::~DiscoveryUdpMessageRequest() {
00198     delete this->buffer; this->buffer = NULL;
00199  }
00200 
00201 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00202 

Generated on Tue Feb 8 2011 09:32:51 for WOSH system 0.8.888 [wolf] by Alessandro Polo, using DoxyGen 1.7.2 hosted by WOSH Framework