Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9306 in orxonox.OLD


Ignore:
Timestamp:
Jul 17, 2006, 2:21:42 PM (18 years ago)
Author:
bensch
Message:

orxonox/proxy: ip class ok now

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

Legend:

Unmodified
Added
Removed
  • branches/proxy/src/lib/network/Makefile.am

    r9296 r9306  
    4040                      synchronizeable_var/synchronizeable_bool.cc \
    4141                      synchronizeable_var/synchronizeable_uint.cc \
    42                       synchronizeable_var/synchronizeable_ip.cc
     42                      synchronizeable_var/synchronizeable_ip.cc \
     43                      \
     44                      ip.cc
    4345
    4446
     
    8284                 synchronizeable_var/synchronizeable_bool.h \
    8385                 synchronizeable_var/synchronizeable_uint.h \
    84                  synchronizeable_var/synchronizeable_ip.h
     86                 synchronizeable_var/synchronizeable_ip.h \
     87                 \
     88                 ip.h
    8589
    8690
  • branches/proxy/src/lib/network/ip.cc

    r9305 r9306  
    88#include "ip.h"
    99#include "multi_type.h"
     10#include "substring.h"
    1011
    1112IP::IP()
    12 {}
     13{
     14  this->_ip = 0;
     15  this->_port = 0;
     16}
    1317
    1418
    15 IP::IP(int ip)
     19IP::IP(int ip, int port)
    1620{
    1721  this->_ip = ip;
    18 }
    19 
    20 IP::IP(const std::string& ip)
    21 {
    22   this->_ip = IP::stringToIP(ip);
     22  this->_port = port;
    2323}
    2424
    2525
    2626
     27IP::IP(const std::string& ip)
     28{
     29  *this = IP::stringToIP(ip);
     30}
    2731
    28 int IP::stringToIP(const std::string& ip)
     32
     33IP::IP(const IPaddress& ip)
    2934{
     35  this->_ip = ip.host;
     36  this->_port = ip.port;
     37}
     38
     39
     40IP::IP(const IP& ip)
     41{
     42  this->_ip = ip.ip();
     43  this->_port = ip.port();
     44}
     45
     46
     47
     48const IP& IP::operator=(const IP& ip)
     49{
     50  this->_ip = ip.ip();
     51  this->_port = ip.port();
     52  return *this;
     53}
     54
     55
     56
     57bool IP::operator==(const IP& ip)
     58{
     59  return (this->_ip == ip.ip() && this->_port == ip.port());
     60}
     61
     62
     63/**
     64 * @returns The retrieved IP
     65 *
     66 */
     67IP IP::stringToIP(const std::string& ip)
     68{
     69  IPaddress ipaddr;
     70
     71  SDLNet_ResolveHost(&ipaddr, NULL, 2000);
     72
     73  return IP(ipaddr);
     74
     75  /*
     76  SubString addr(ip, '.');
     77  if(ip.size() != 4 )
     78    return -1;
     79
     80  MultiType part0(ip[0]);
     81  MultiType part1(ip[1]);
     82  MultiType part2(ip[2]);
     83  MultiType part3(ip[3]);
     84  */
     85}
     86
     87int IP::ipPart(unsigned int part) const
     88{
     89  switch (part)
     90  {
     91    case 0:
     92      return  (_ip & 0xFF000000) >> 24;
     93    case 1:
     94      return  (_ip & 0x00FF0000) >> 16;
     95    case 2:
     96      return  (_ip & 0x0000FF00) >> 8;
     97    case 3:
     98      return  (_ip & 0x000000FF);
     99    default:
     100      return -1;
     101  }
    30102
    31103}
     104
     105
     106std::string IP::ipString() const
     107{
     108  return IP::ipToString(this->_ip);
     109}
     110
    32111
    33112std::string IP::ipToString(const IPaddress& ipaddr)
     
    38117
    39118
     119
     120
     121
    40122std::string IP::ipToString(int ip)
    41123{
    42   MultiType part0( (ip & 0xFF000000) >> 24);
    43   MultiType part1( (ip & 0x00FF0000) >> 16);
    44   MultiType part2( (ip & 0x0000FF00) >>  8);
    45   MultiType part3( (ip & 0x000000FF) );
     124  MultiType part0((int) (ip & 0xFF000000) >> 24);
     125  MultiType part1((int) (ip & 0x00FF0000) >> 16);
     126  MultiType part2((int) (ip & 0x0000FF00) >>  8);
     127  MultiType part3((int) (ip & 0x000000FF) );
    46128
    47129
    48130  return part0.getString() + "." + part1.getString() + "." +
    49            part2.getString() + "." + part3.getString();
     131         part2.getString() + "." + part3.getString();
    50132}
    51133
  • branches/proxy/src/lib/network/ip.h

    r9305 r9306  
    22/*!
    33 * @file ip.h
    4  * @brief Definition of Time Class.
    54 *
    6  * These are mainly Classes, that are used for wrapping around SDL_thread
     5 * @brief the ip class is used to transform strings to ip's and backwards
     6 * it can also be used to
     7 *
    78 */
    89
     
    2122    IP();
    2223    IP(const std::string& ip);
    23     IP(int ip);
     24    IP(int ip, int port);
     25    IP(const IPaddress& ip);
     26    IP(const IP& ip);
     27
     28    const IP& operator=(const IP& ip);
     29    bool operator==(const IP& ip);
    2430
    2531
     
    2733    short port() const { return this->_port; };
    2834
    29     int ip(unsigned int part) const;
    30     std::string& ipString() const;
     35    int ipPart(unsigned int part) const;
     36    std::string ipString() const;
    3137
    3238
    3339
    3440  public:
    35     static int stringToIP(const std::string& ip);
     41    static IP stringToIP(const std::string& ip);
     42
     43
    3644    static std::string ipToString(const IPaddress& ipaddr);
    3745    static std::string ipToString(int ip);
    3846
    3947  private:
    40     int           _ip;  //!< the IP in int form.
    41     short         _port;
     48    int           _ip;    //!< The IP in int form.
     49    short         _port;  //!< The Port number of the IP
    4250
    4351};
Note: See TracChangeset for help on using the changeset viewer.