00001 // [License] 00002 // The Ariba-Underlay Copyright 00003 // 00004 // Copyright (c) 2008-2009, Institute of Telematics, Universität Karlsruhe (TH) 00005 // 00006 // Institute of Telematics 00007 // Universität Karlsruhe (TH) 00008 // Zirkel 2, 76128 Karlsruhe 00009 // Germany 00010 // 00011 // Redistribution and use in source and binary forms, with or without 00012 // modification, are permitted provided that the following conditions are 00013 // met: 00014 // 00015 // 1. Redistributions of source code must retain the above copyright 00016 // notice, this list of conditions and the following disclaimer. 00017 // 2. 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 // 00021 // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND 00022 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00024 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR 00025 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00027 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // The views and conclusions contained in the software and documentation 00034 // are those of the authors and should not be interpreted as representing 00035 // official policies, either expressed or implied, of the Institute of 00036 // Telematics. 00037 // [License] 00038 00039 #include "EndpointDescriptor.h" 00040 #include "ariba/utility/misc/StringFormat.h" 00041 #include "ariba/utility/misc/Helper.h" 00042 00043 namespace ariba { 00044 namespace communication { 00045 00046 vsznDefault(EndpointDescriptor); 00047 00048 const EndpointDescriptor EndpointDescriptor::UNSPECIFIED; 00049 00050 EndpointDescriptor::EndpointDescriptor() : locator( NULL ), isUnspec( true ){ 00051 } 00052 00053 EndpointDescriptor::EndpointDescriptor(const EndpointDescriptor& rh){ 00054 locator = (rh.locator != NULL) ? new IPv4Locator(*rh.locator) : NULL; 00055 isUnspec = rh.isUnspec; 00056 } 00057 00058 EndpointDescriptor::EndpointDescriptor(const Locator* _locator){ 00059 if( _locator == NULL ) return; 00060 00061 locator = new IPv4Locator(*dynamic_cast<IPv4Locator*>((Locator*)_locator)); 00062 isUnspec = false; 00063 } 00064 00065 EndpointDescriptor::~EndpointDescriptor() { 00066 } 00067 00068 bool EndpointDescriptor::isUnspecified() const { 00069 return isUnspec; 00070 } 00071 00072 string EndpointDescriptor::toString() const { 00073 if( locator == NULL ) return "<undefined locator>"; 00074 std::ostringstream o; 00075 o << "ip{" << locator->getIP() << "}"; 00076 o << ","; 00077 o << "tcp(ip,{" << locator->getPort() << "})"; 00078 return o.str(); 00079 } 00080 00081 EndpointDescriptor* EndpointDescriptor::fromString( string str ) { 00082 using namespace boost::xpressive; 00083 using namespace ariba::utility::string_format; 00084 using namespace ariba::utility::Helper; 00085 using namespace std; 00086 00087 EndpointDescriptor* ep = NULL; 00088 smatch match; 00089 if (regex_search(str, match, robjects)) { 00090 regex_nav nav = match; 00091 for (int i=0; i<nav.size(); i++) { 00092 string type = nav[i][robject_id].str(); 00093 if (type=="ip") { 00094 string ip = nav[i][robject_data].str(); 00095 ip = ip.substr(1,ip.size()-2); 00096 ep = new EndpointDescriptor(); 00097 ep->locator = new IPv4Locator(); 00098 ep->locator->setIP(ip); 00099 ep->isUnspec = false; 00100 } else 00101 if (type=="tcp") { 00102 string port = nav[i][robject_data][rfields][1].str(); 00103 port = port.substr(1,port.size()-2); 00104 ep->locator->setPort(stoi(port)); 00105 } 00106 } 00107 } 00108 return ep; 00109 } 00110 00111 00112 bool EndpointDescriptor::operator==(const EndpointDescriptor& rh) const { 00113 00114 if( isUnspecified() && rh.isUnspecified() ) { 00115 00116 // both unspec bit set 00117 return true; 00118 00119 } else if( (!isUnspecified()) && (!rh.isUnspecified()) ) { 00120 00121 // 00122 // both are valid, check locators 00123 // 00124 00125 if( locator == NULL && rh.locator == NULL ){ 00126 00127 // both locators are invalid, ok true 00128 return true; 00129 00130 } else if( locator == NULL ^ rh.locator == NULL ) { 00131 00132 // one locator is invalid, the other not, false 00133 return false; 00134 00135 } else { 00136 00137 // both locators are valid, compare 00138 assert( locator != NULL && rh.locator != NULL ); 00139 return ( locator->operator==(*rh.locator) ); 00140 00141 } 00142 00143 } else { 00144 00145 // one is unspec, the other not 00146 assert( isUnspecified() ^ rh.isUnspecified() ); 00147 return false; 00148 00149 } 00150 00151 } 00152 00153 }} // namespace ariba, communication