Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9321 in orxonox.OLD for branches/proxy/src/lib/network


Ignore:
Timestamp:
Jul 18, 2006, 11:30:12 AM (18 years ago)
Author:
bensch
Message:

more elaborate IP

Location:
branches/proxy/src/lib/network
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/proxy/src/lib/network/ip.cc

    r9311 r9321  
    11/**
    22 * @file ip.cc
    3  * @brief A IP class, that handles all about time.
     3 * @brief A IP class, that handles IP names and resolution.
    44 *
    55 * code taken from audiere.
     
    77
    88#include "ip.h"
     9
     10#include <iostream>
     11
    912#include "multi_type.h"
    1013#include "substring.h"
     
    1619{
    1720  this->_ip = 0;
    18   this->_port = 0;
     21  this->_port = IP::_defaultPort;
    1922}
    2023
     
    2932{
    3033  this->_ip = ip;
    31   this->_port = port;
    32 }
    33 
     34  if (port != -1)
     35    this->_port = port;
     36  else
     37    this->_port = IP::_defaultPort;
     38}
     39
     40
     41/**
     42 * @brief constructor from a String
     43 * @param ip the IP as a String.
     44 * @param resolve if true, the IP is resolved via DNS,
     45 * @return self
     46 */
     47IP::IP(const std::string& ip, bool resolve)
     48{
     49  *this = IP::stringToIP(ip, IP::_defaultPort, resolve);
     50}
    3451
    3552/**
     
    6582IP::IP(const IP& ip)
    6683{
    67   this->_ip = ip.ip();
    68   this->_port = ip.port();
     84  *this = ip;
    6985}
    7086
     
    88104bool IP::operator==(const IP& ip)
    89105{
    90   return (this->_ip == ip.ip() && this->_port == ip.port());
     106  return (this->_ip == ip.ip() &&
     107          this->_port == ip.port());
    91108}
    92109
     
    160177std::string IP::ipString() const
    161178{
    162   return IP::ipToString(this->_ip);
     179  return IP::ipToString(this->_ip, this->_port);
    163180}
    164181
     
    172189{
    173190  int ip = SDLNet_Read32 (ipaddr.host);
    174   return ipToString(ip, ipaddr.port);
     191  return IP::ipToString(ip, ipaddr.port);
    175192}
    176193
     
    193210
    194211  if (port != -1)
    195     addr += MultiType(port).getString();
     212    addr += ":" + MultiType(port).getString();
    196213  return addr;
    197214}
    198215
     216
     217/// default port definition.
     218short IP::_defaultPort = 80;
     219
     220/**
     221 * @brief if no IP is supplied this port is used, so that IP can be resolved usefully.
     222 * @param defaultPort The default port.
     223 */
     224void IP::setDefaultPort(short defaultPort)
     225{
     226  IP::_defaultPort = defaultPort;
     227}
     228
     229
     230/**
     231 * @brief print out the IP in a nice fashion
     232 */
     233void IP::debug() const
     234{
     235  std::cout << "IP is " << this->ipString() << std::endl;
     236}
  • branches/proxy/src/lib/network/ip.h

    r9311 r9321  
    2222    /// CONSTRUCTORS
    2323    IP();
     24    IP(int ip, int port);
     25    IP(const std::string& ip, bool resolve = true);
    2426    IP(const std::string& ip, int port = -1, bool resolve = true);
    25     IP(int ip, int port);
    2627    IP(const IPaddress& ip);
    2728    IP(const IP& ip);
     
    3132    bool operator==(const IP& ip);
    3233
    33     /// RETRIEAL
     34    /// RETRIVEAL
    3435    /** @returns the IP */
    3536    int ip() const { return this->_ip; };
     
    4041    std::string ipString() const;
    4142
     43    void debug() const;
     44
    4245  public:
    43 
    4446    /// SETUP
    4547    static IP stringToIP(const std::string& ip, int port = -1, bool resolve = true);
    46 
    4748
    4849    static std::string ipToString(const IPaddress& ipaddr);
    4950    static std::string ipToString(int ip, int port = -1);
    5051
     52
     53    static void setDefaultPort(short defaultPort);
     54    static short defaultPort(short defaultPort) { return IP::_defaultPort; };
     55
    5156  private:
    52     int           _ip;    //!< The IP in int form.
    53     short         _port;  //!< The Port number of the IP
     57    int           _ip;           //!< The IP in int form.
     58    short         _port;         //!< The Port number of the IP
    5459
     60
     61    static short  _defaultPort;  //!< Default Port
    5562};
    5663
Note: See TracChangeset for help on using the changeset viewer.