Sirikata
libcore/include/sirikata/core/network/Address.hpp
Go to the documentation of this file.
00001 /*  Sirikata Network Utilities
00002  *  Address.hpp
00003  *
00004  *  Copyright (c) 2009, Daniel Reiter Horn
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 #ifndef _NETWORK_ADDRESS_HPP_
00033 #define _NETWORK_ADDRESS_HPP_
00034 namespace Sirikata { namespace Network {
00039 class Address {
00040     String mName;
00041     String mService;
00042     String mProtocol;
00043 public:
00044     static Any lexical_cast(const std::string&value) {
00045         std::string::size_type where=value.rfind(':');
00046         if (where==std::string::npos) {
00047             throw std::invalid_argument("Address does not contain a port");
00048         }
00049         std::string::size_type protowhere;
00050         const char *separator="://";
00051         if ((protowhere=value.find(separator))!=std::string::npos) {
00052             
00053             Address retval(value.substr(protowhere+strlen(separator),where-protowhere-strlen(separator)),value.substr(where+1));
00054             retval.setProtocol(value.substr(0,protowhere));
00055             return retval;
00056         }else {
00057             Address retval(value.substr(0,where),
00058                            value.substr(where+1));
00059             return retval;
00060         }
00061     }
00062     String toString()const{
00063         return getHostName()+':'+getService();
00064     }
00065     void setProtocol(const String&str) {
00066         mProtocol=str;
00067     }
00068     
00069     const String &getProtocol()const {
00070         return mProtocol;
00071     }
00072     const String &getHostName()const {
00073         return mName;
00074     }
00075     const String &getService()const {
00076         return mService;
00077     }
00078     static Address null() {
00079         return Address(String(),String());
00080     }
00081     bool operator==(const Address&other) const{
00082         return other.mName==mName&&other.mService==mService&&other.mProtocol==mProtocol;
00083     }
00084     bool operator<(const Address&other) const{
00085         return other.mName==mName?(mService==other.mService?mProtocol<other.mProtocol:mService<other.mService):mName<other.mName;
00086     }
00087     Address(const String& name, 
00088             const String& service) {
00089         mName=name;
00090         mService=service;
00091     }
00092     Address(const String& name, 
00093             const String& service,
00094             const String&protocol) {
00095         mName=name;
00096         mService=service;
00097         mProtocol=protocol;
00098     }
00099     class Hasher {public:
00100         size_t operator()(const Address&addy)const {
00101             return std::tr1::hash<std::string>()(addy.mName)^std::tr1::hash<std::string>()(addy.mService)^std::tr1::hash<std::string>()(addy.mProtocol);
00102         }
00103     };
00104 };
00105 } }
00106 #endif