/** ** File ......... Session.h ** Published .... 2005-06-14 ** Author ....... grymse@alhem.net **/ /* Copyright (C) 2005 Anders Hedstrom This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SESSION_H #define _SESSION_H #include #ifndef _WIN32 #include #else typedef __int64 int64_t; #include #endif #include #include #include "bitmap_t.h" class Peer; class Piece; class Request; class FileManager; class pSocket; class TcpSocket; typedef std::list piece_v; //! File information from metainfo file struct file_t { file_t(const std::string& n,int64_t l) : name(n),offset(0),length(l) {} std::string name; int64_t offset; // file begin int64_t length; }; typedef std::list file_v; //! Information about one metainfo file / transfer session class Session { typedef std::list peer_v; public: Session(SocketHandler&,const std::string& info_hash); virtual ~Session(); const std::string& GetInfoHash() { return m_info_hash; } SocketHandler& Handler() const { return m_handler; } void SetHash(unsigned char *p) { memcpy(m_hashptr, p, 20); } unsigned char *GetHashptr() { return m_hashptr; } unsigned char *GetPeerId() { return m_peer_id; } /** metainfo file field: announce */ void SetAnnounce(const std::string& a) { m_announce = a; } const std::string& GetAnnounce() { return m_announce; } /** metainfo file field: info.name */ void SetName(const std::string& a) { m_name = a; } const std::string& GetName() { return m_name; } /** metainfo file field: info.piece length */ void SetPieceLength(int64_t x); size_t GetPieceLength() { return m_piece_length; } /** add one file */ void AddFile(int64_t); /** add many files */ void AddFile(const std::string& path,int64_t); /** restore session */ void Load(); /** save session */ void Save(); /** session save filename */ std::string GetBitmapFilename(); /** tracker announce interval */ void SetInterval(int x) { m_interval = x; } int GetInterval() { return m_interval; } void AddPeer(Peer *p) { m_peers.push_back(p); } Peer *GetPeer(const std::string& ip); void SetTimeTracker() { m_t_tracker = time(NULL); } time_t GetTimeTracker() { return m_t_tracker; } std::string GetAnnounceUrl(const std::string& event); // started, stopped size_t GetNumberOfPieces() { return m_number_of_pieces; } void SetPieces(const std::string& ); /** length of last piece (0 - full piece) */ size_t GetLastLength() { return m_last_length; } void Status(TcpSocket *p); void Update(); piece_v& Complete() { return m_complete; } piece_v& Incomplete() { return m_incomplete; } void AddConnect(); bool GetRandomNotRequested(Peer *,size_t& piece,size_t& offset,size_t& length); Request *AddRequest(Peer *,size_t piece,size_t offset,size_t length); void RemoveRequest(Peer *,size_t piece,size_t offset,size_t length); void SaveSlice(size_t piece,size_t offset,size_t length,unsigned char *); /** info.length value */ int64_t GetLength() { return m_length_one; } file_v& GetFiles() { return m_files; } void CreateFileManager(); void Verify(); const std::string& GetPieces() { return m_pieces; } Piece *GetIncomplete(size_t piece); bool SliceSent(size_t piece,size_t offset); void SendSlice(pSocket *,size_t piece,size_t offset,size_t length); Piece *GetComplete(size_t piece); void save_piece_v(FILE *,piece_v&); void load_piece_v(FILE *,piece_v&); bool GenerateRequest(Peer *peer); bool PieceUnique(size_t piece); void PeerStatus(); // size_t GetInstances(size_t piece); void SetCheckComplete() { m_b_check_complete = true; } void SetUpdateInterested() { m_b_update_interested = true; } bool IsDemon() { return m_demon; } // size_t Available(int piece); void RequestAvailable(Peer *); void RemoveRequests(Peer *); void CheckRequests(); private: Session(const Session& s) : m_handler(s.Handler()) {} // copy constructor Session& operator=(const Session& ) { return *this; } // assignment operator unsigned char m_peer_id[20]; // random peer id SocketHandler& m_handler; std::string m_info_hash; // metainfo file std::string m_announce; std::string m_name; int64_t m_piece_length; int64_t m_length; // total file_v m_files; size_t m_number_of_pieces; std::string m_pieces; size_t m_last_length; /** tracker reconnect interval */ int m_interval; peer_v m_peers; time_t m_t_tracker; // bitmap_t *m_bitmap; int64_t m_prev_offset; int64_t m_prev_length; unsigned char m_hashptr[20]; piece_v m_complete; piece_v m_incomplete; int64_t m_length_one; FileManager *m_filemanager; bool m_b_check_complete; bool m_b_update_interested; bool m_demon; }; #endif // _SESSION_H