server.h

Go to the documentation of this file.
00001 /**
00002  *  Copyright (C) 2004-2005 Alo Sarv <madcat_@users.sourceforge.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 /** \file server.h Interface for Detail::Server class */
00020 
00021 #ifndef __SERVER_H__
00022 #define __SERVER_H__
00023 
00024 #include <hn/hnfwd.h>
00025 #include <hn/event.h>
00026 #include <hn/ipv4addr.h>
00027 #include <hn/object.h>
00028 #include "ed2kfwd.h"
00029 
00030 namespace Detail {
00031 
00032 // Server class
00033 // ------------
00034 /**
00035  * Server represents one server object. A server in eDonkey2000 network
00036  * has a set of properties, however internally only the address of
00037  * the server is used - the remainder of properties are used only for
00038  * showing to user.
00039  *
00040  * Server object is owned by ServerList object, the destructor has been
00041  * protected to enforce that relationship.
00042  *
00043  * Note: TCP flags are not stored in server.met according to eMule.
00044  */
00045 class Server : public Object {
00046 public:
00047         DECLARE_EVENT_TABLE(Server*, int);
00048 
00049         //! Construct using IP and port
00050         Server(IPV4Address addr);
00051 
00052         //! Construct by reading data from stream
00053         Server(std::istream &i);
00054 
00055         //! Write the object to stream
00056         friend std::ostream& operator<<(
00057                 std::ostream &o, const Server &s
00058         );
00059 
00060         //! @name Getters
00061         //@{
00062         IPV4Address getAddr()               const { return m_addr;           }
00063         std::string getName()               const { return m_name;           }
00064         std::string getDesc()               const { return m_desc;           }
00065         std::string getDynIp()              const { return m_dynip;          }
00066         std::string getVersion()            const { return m_version;        }
00067         uint32_t    getFailedCount()        const { return m_failedCount;    }
00068         uint32_t    getPreference()         const { return m_preference;     }
00069         uint32_t    getPing()               const { return m_ping;           }
00070         uint32_t    getLastPing()           const { return m_lastPing;       }
00071         uint32_t    getMaxUsers()           const { return m_maxUsers;       }
00072         uint32_t    getSoftLimit()          const { return m_softLimit;      }
00073         uint32_t    getHardLimit()          const { return m_hardLimit;      }
00074         uint32_t    getUdpFlags()           const { return m_udpFlags;       }
00075         uint32_t    getTcpFlags()           const { return m_tcpFlags;       }
00076         uint32_t    getUsers()              const { return m_users;          }
00077         uint32_t    getFiles()              const { return m_files;          }
00078         uint32_t    getLowIdUsers()         const { return m_lowIdUsers;     }
00079         std::vector<uint16_t> getAuxPorts() const { return m_auxPorts;       }
00080         uint64_t    getLastUdpQuery()       const { return m_lastUdpQuery;   }
00081         bool        pingInProgress()        const { return m_pingInProgress; }
00082         uint32_t    getChallenge()          const { return m_challenge;      }
00083 
00084         //! ED2K Server UDP port is always TCP + 4
00085         uint16_t    getUdpPort()  const { return m_addr.getPort() + 4;       }
00086         bool supportsGetSources() const { return m_udpFlags & FL_GETSOURCES; }
00087         bool supportsGetFiles()   const { return m_udpFlags & FL_GETFILES;   }
00088         //@}
00089 
00090         //! @name Setters
00091         //@{
00092         void setAddr(IPV4Address addr) { m_addr = addr; notify(); }
00093         void setName(const std::string &name) {
00094                 Object::setName(name);
00095                 m_name = name;
00096         }
00097         void setDesc(const std::string &desc)    { m_desc = desc;    notify(); }
00098         void setDynIp(const std::string &dyndip) { m_dynip = dyndip; notify(); }
00099         void setVersion(uint32_t ver)            { m_version = ver;  notify(); }
00100         void setFailedCount(uint32_t count) { m_failedCount = count; notify(); }
00101         void addFailedCount()                 { ++m_failedCount;     notify(); }
00102         void setPing(uint32_t ping)           { m_ping = ping;       notify(); }
00103         void setLastPing(uint32_t ping)       { m_lastPing = ping;   notify(); }
00104         void setMaxUsers(uint32_t users)      { m_maxUsers = users;  notify(); }
00105         void setSoftLimit(uint32_t limit)     { m_softLimit = limit; notify(); }
00106         void setHardLimit(uint32_t limit)     { m_hardLimit = limit; notify(); }
00107         void setUdpFlags(uint32_t flags)      { m_udpFlags = flags;  notify(); }
00108         void setTcpFlags(uint32_t flags)      { m_tcpFlags = flags;  notify(); }
00109         void addAuxPort(uint16_t port) { m_auxPorts.push_back(port); notify(); }
00110         void setUsers(uint32_t users)         { m_users = users;     notify(); }
00111         void setFiles(uint32_t files)         { m_files = files;     notify(); }
00112         void setLowIdUsers(uint32_t num)      { m_lowIdUsers = num;  notify(); }
00113 
00114         void setLastUdpQuery(uint64_t time)   { m_lastUdpQuery = time; }
00115         void setPingInProgress(bool v)        { m_pingInProgress = v; }
00116         void setChallenge(uint32_t c)         { m_challenge = c; }
00117         //@}
00118 private:
00119         friend class ::ServerList;
00120         friend bool operator<(const Server &x, const Server &y) {
00121                 return x.m_addr < y.m_addr;
00122         }
00123 
00124         //! Values found in m_udpFlags bitfield
00125         enum ED2K_ServerUdpFlags {
00126                 FL_GETSOURCES  = 0x01, //!< Supports UDP GetSources
00127                 FL_GETFILES    = 0x02, //!< Supports UDP GetFiles/Search
00128                 FL_NEWTAGS     = 0x08, //!< Supports NewTags
00129                 FL_UNICODE     = 0x10, //!< Supports Unicode
00130                 FL_GETSOURCES2 = 0x20  //!< Supports extended GetSources
00131         };
00132 
00133         Server();                    //!< Forbidden
00134         ~Server();                   //!< Allowed by ServerList
00135 
00136         //! @name Data
00137         //@{
00138         IPV4Address m_addr;          //!< Address of the server
00139         std::string m_name;          //!< Name of the server
00140         std::string m_desc;          //!< Description
00141         std::string m_dynip;         //!< Dynamic ip address
00142         std::string m_version;       //!< Version number
00143         uint32_t m_failedCount;      //!< Number of times it has failed
00144         uint32_t m_preference;       //!< Priority
00145         uint32_t m_ping;             //!< Ping
00146         uint32_t m_lastPing;         //!< Last ping
00147         uint32_t m_maxUsers;         //!< Max allowed users
00148         uint32_t m_softLimit;        //!< Soft files limit
00149         uint32_t m_hardLimit;        //!< Hard files limit
00150         uint32_t m_udpFlags;         //!< UDP flags
00151         uint32_t m_tcpFlags;         //!< TCP flags
00152         uint32_t m_users;            //!< Current online users
00153         uint32_t m_files;            //!< Current online files
00154         uint32_t m_lowIdUsers;       //!< Current online lowId users
00155         std::vector<uint16_t> m_auxPorts; //!< Auxiliary ports
00156 
00157         uint64_t m_lastUdpQuery;     //!< Time of last UDP query
00158         bool m_pingInProgress;       //!< UDP Ping is in progress
00159         uint32_t m_challenge;        //!< Last UDP query challenge
00160         //@}
00161 
00162         //! Parses comma-separated list of ports and adds them
00163         void addAuxPorts(const std::string &ports);
00164 };
00165 
00166 }
00167 
00168 #endif