ed2k.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 
00020 #ifndef __ED2K_H__
00021 #define __ED2K_H__
00022 
00023 /**
00024  * \mainpage HydraNode ED2K Module
00025  * \date October 2004
00026  * \author Alo Sarv <madcat (at) hydranode (dot) com>
00027  *
00028  * ED2K Module performs communication with eDonkey2000 P2P network. The network
00029  * protocol implemented within this module is documented in \ref ed2kproto
00030  */
00031 
00032 #include <hn/modules.h>
00033 #include <hn/config.h>
00034 #include <hn/hash.h>
00035 #include <boost/filesystem/path.hpp>
00036 #include <boost/format.hpp>
00037 #include <stdexcept>
00038 #include "ed2ktypes.h"
00039 
00040 class ED2KConfig : public Config, public Object {
00041 public:
00042         static ED2KConfig& instance() {
00043                 static ED2KConfig *s_c = new ED2KConfig();
00044                 return *s_c;
00045         }
00046 private:
00047         ED2KConfig();
00048         ~ED2KConfig();
00049 };
00050 
00051 class ED2K : public ModuleBase {
00052         DECLARE_MODULE(ED2K, "ed2k");
00053 public:
00054         virtual bool onInit();
00055         virtual int onExit();
00056         virtual std::string getDesc() {
00057                 return "eDonkey2000 P2P Network Module.";
00058         }
00059 
00060         //! @name Accessors for various commonly-used variables in this module
00061         //@{
00062         uint32_t      getId()      const { return m_id;       }
00063         uint16_t      getTcpPort() const { return m_tcpPort;  }
00064         uint16_t      getUdpPort() const { return m_udpPort;  }
00065         Hash<MD4Hash> getHash()    const { return m_userHash; }
00066         std::string   getNick()    const { return m_nick;     }
00067         bool          isLowId()    const { return ::isLowId(m_id); }
00068         bool          isHighId()   const { return ::isHighId(m_id); }
00069         boost::filesystem::path getConfigDir() const { return m_configDir; }
00070 
00071         void setId(uint32_t id) { m_id = id; } //!< Used by ServerList
00072         void setNick(const std::string  &nick) {
00073                 ED2KConfig::instance().write("Nick", nick);
00074                 m_nick = nick;
00075         }
00076         void setTcpPort(uint16_t port) { m_tcpPort = port; }
00077         void setUdpPort(uint16_t port) { m_udpPort = port; }
00078         //@}
00079 private:
00080         //! Generates new userhash
00081         std::string createUserHash() const;
00082 
00083         //! Configuration directory
00084         boost::filesystem::path m_configDir;
00085 
00086         uint32_t    m_id;       //!< Own ID in ed2k network
00087         uint16_t    m_tcpPort;  //!< Where we have our TCP listener
00088         uint16_t    m_udpPort;  //!< Where we have our UDP listener
00089         std::string m_nick;     //!< Our nickname on ed2k net
00090 
00091         //! own userhash on ed2k network. This just a bunch of randomness
00092         //! really, but - its a hash afterall. Thus the object type.
00093         Hash<MD4Hash> m_userHash;
00094 
00095         //! Handler for string-based links for starting a download
00096         bool linkHandler(const std::string &link);
00097 
00098 };
00099 
00100 // ParseError exception class
00101 // --------------------------
00102 class ParseError : public std::runtime_error {
00103 public:
00104         ParseError() : std::runtime_error("") {}
00105         ParseError(const std::string &msg) : std::runtime_error(msg) {}
00106         ParseError(boost::format fmt) : std::runtime_error(fmt.str()) {}
00107         ~ParseError() throw() {}
00108 };
00109 
00110 
00111 #endif