ed2kfile.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 ed2kfile.h Interface for ED2KFile class */
00020 
00021 #ifndef __ED2KFILE_H__
00022 #define __ED2KFILE_H__
00023 
00024 #include <hn/metadata.h>           // for FileType
00025 #include <hn/hash.h>               // for Hash<ED2KHash>
00026 
00027 class SharedFile;
00028 
00029 /**
00030  * ED2KFile class represents a file as is known to ED2K network. ED2K network
00031  * puts some restrictions on the files it can support - namely, the file must
00032  * have a valid ED2KHash present. Additional fields ED2KFile is required to
00033  * include are size, type, and completeness.
00034  *
00035  * Note that file size is 32-bit value in eDonkey2000 network - max ED2KFile
00036  * size is 2^32 bytes. While HydraNode supports up to 2^64 byte files
00037  * internally, eDonkey2000 network doesn't - thus sharing files with size > 4gb
00038  * is not possible on ed2k.
00039  *
00040  * File type is used in eDonkey2000 network as strings (refer to opcodes.h for
00041  * the actual strings definitions), and not all file types that HydraNode
00042  * supports are also supported by eDonkey2000 network. In any case, we provide
00043  * convenience accessors and constructors for using HydraNode FileType enum
00044  * values here, and handle the details internally as needed.
00045  */
00046 class ED2KFile {
00047 public:
00048         /**
00049          * Construct using hash, name and type.
00050          *
00051          * @param h            ED2KHash of the file
00052          * @param name         File name (excluding path)
00053          * @param type         Type of file, as specified in hn/metadata.h
00054          * @param flags        Bitfield of Flags
00055          */
00056         ED2KFile(
00057                 Hash<ED2KHash> h, const std::string &name, uint32_t size,
00058                 FileType type, uint8_t flags
00059         );
00060 
00061         /**
00062          * Construct using hash, name and type.
00063          *
00064          * @param h            ED2KHash of the file
00065          * @param name         File name (excluding path)
00066          * @param type         Type of file, as used in ED2K network (strings)
00067          * @param flags        Bitfield of Flags
00068          */
00069         ED2KFile(
00070                 Hash<ED2KHash> h, const std::string &name, uint32_t size,
00071                 const std::string &type, uint8_t flags
00072         );
00073 
00074         /**
00075          * Construct an ED2KFile object which has bare minimum of information,
00076          * plus client id and client port. This omit type and flags data,
00077          * however adds clientid/port, which comes in handy when handling
00078          * search results for example. The type and flags may be added later
00079          * using accessor methods.
00080          *
00081          * @param h         ED2KHash of the file
00082          * @param name      File name
00083          * @param size      File size
00084          * @param id        ClientId of the client sharing this file
00085          * @param port      ClientPort of the client sharing this file
00086          */
00087         ED2KFile(
00088                 Hash<ED2KHash> h, const std::string &name, uint32_t size,
00089                 uint32_t id, uint16_t port
00090         );
00091 
00092         //! @name Accessors
00093         //@{
00094         Hash<ED2KHash> getHash() const { return m_hash;        }
00095         std::string    getName() const { return m_name;        }
00096         uint32_t       getSize() const { return m_size;        }
00097         FileType     getHNType() const { return m_hnType;      }
00098         std::string getStrType() const { return m_ed2kType;    }
00099         uint32_t         getId() const { return m_id;          }
00100         uint16_t       getPort() const { return m_port;        }
00101         //@}
00102 
00103         //! Output operator to streams for usage in ed2k protocol
00104         friend std::ostream& operator<<(std::ostream &o, const ED2KFile &f);
00105 
00106         //! Flags usable at ED2KFile constructor arguments
00107         enum Flags {
00108                 FL_COMPLETE        = 0x01, //!< If this file is complete
00109                 FL_USECOMPLETEINFO = 0x02  //!< Whether to send complete info
00110         };
00111 
00112         //! File ratings
00113         enum Rating {
00114                 FR_NORATING = 0x00,
00115                 FR_INVALID,
00116                 FR_POOR,
00117                 FR_GOOD,
00118                 FR_FAIR,
00119                 FR_EXCELLENT
00120         };
00121 
00122         //! Converts rating value to rating string.
00123         static std::string ratingToString(const Rating &r);
00124 
00125         //! @name Static utility functions for file type conversions
00126         //@{
00127         //! Converts HydraNode filetype into ED2K filetype
00128         static std::string HNType2ED2KType(FileType type);
00129         //! Converts ED2K file type to HydraNode file type
00130         static FileType    ED2KType2HNType(const std::string &type);
00131         //@}
00132 private:
00133         const Hash<ED2KHash> m_hash;        //!< File hash
00134         const std::string    m_name;        //!< File name
00135         uint32_t             m_size;        //!< File size
00136         FileType             m_hnType;      //!< File type (HN-compatible)
00137         std::string          m_ed2kType;    //!< File type (ed2k-compatible)
00138         uint8_t              m_flags;       //!< Various useful flags
00139         uint32_t             m_id;          //!< ClientId sharing this file
00140         uint16_t             m_port;        //!< ClientPort of the client
00141 
00142         /**
00143          * Convenience method for constructing complex ED2KFile object from
00144          * pre-given data.
00145          *
00146          * @param sf               SharedFile to refer to
00147          * @param md               MetaData corresponding to the SharedFile
00148          * @param hs               ED2KHashSet corresponding to this file
00149          * @param useCompletEInfo  Whether to include "complete" information
00150          * @return                 Newly allocated ED2KFile object
00151          */
00152         friend boost::shared_ptr<ED2KFile> makeED2KFile(
00153                 SharedFile *sf, MetaData *md, HashSetBase *hs,
00154                 bool useCompleteInfo
00155         );
00156 };
00157 
00158 #endif