Logo
~Apps~
~Projects~
~Contact~


Session.h

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2005  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 #ifndef _SESSION_H
00024 #define _SESSION_H
00025 
00026 #include <string>
00027 #ifndef _WIN32
00028 #include <stdint.h>
00029 #else
00030 typedef __int64 int64_t;
00031 #include <time.h>
00032 #endif
00033 #include <list>
00034 #include <ISocketHandler.h>
00035 #include "bitmap_t.h"
00036 
00037 
00038 class Peer;
00039 class Piece;
00040 class Request;
00041 class FileManager;
00042 class pSocket;
00043 class TcpSocket;
00044 
00045         typedef std::list<Piece *> piece_v;
00047         struct file_t {
00048                 file_t(const std::string& n,int64_t l) : name(n),offset(0),length(l) {}
00049                 std::string name;
00050                 int64_t offset; // file begin
00051                 int64_t length;
00052         };
00053         typedef std::list<file_t *> file_v;
00054 
00055 
00057 
00058 class Session
00059 {
00060         typedef std::list<Peer *> peer_v;
00061 public:
00062         Session(ISocketHandler&,const std::string& info_hash);
00063         virtual ~Session();
00064 
00065         const std::string& GetInfoHash() { return m_info_hash; }
00066         ISocketHandler& Handler() const { return m_handler; }
00067         void SetHash(unsigned char *p) { memcpy(m_hashptr, p, 20); }
00068         unsigned char *GetHashptr() { return m_hashptr; }
00069         unsigned char *GetPeerId() { return m_peer_id; }
00070 
00072         void SetAnnounce(const std::string& a) { m_announce = a; }
00073         const std::string& GetAnnounce() { return m_announce; }
00075         void SetName(const std::string& a) { m_name = a; }
00076         const std::string& GetName() { return m_name; }
00078         void SetPieceLength(int64_t x);
00079         size_t GetPieceLength() { return m_piece_length; }
00081         void AddFile(int64_t);
00083         void AddFile(const std::string& path,int64_t);
00084 
00086         void Load();
00088         void Save();
00090         std::string GetBitmapFilename();
00091 
00093         void SetInterval(int x) { m_interval = x; }
00094         int GetInterval() { return m_interval; }
00095 
00096         void AddPeer(Peer *p) { m_peers.push_back(p); }
00097         Peer *GetPeer(const std::string& ip);
00098 
00099         void SetTimeTracker() { m_t_tracker = time(NULL); }
00100         time_t GetTimeTracker() { return m_t_tracker; }
00101 
00102         std::string GetAnnounceUrl(const std::string& event); // started, stopped
00103         size_t GetNumberOfPieces() { return m_number_of_pieces; }
00104 
00105         void SetPieces(const std::string& );
00107         size_t GetLastLength() { return m_last_length; }
00108 
00109         void Status(TcpSocket *p);
00110 
00111         void Update();
00112 
00113         piece_v& Complete() { return m_complete; }
00114         piece_v& Incomplete() { return m_incomplete; }
00115 
00116         void AddConnect();
00117         bool GetRandomNotRequested(Peer *,size_t& piece,size_t& offset,size_t& length);
00118 
00119         Request *AddRequest(Peer *,size_t piece,size_t offset,size_t length);
00120         void RemoveRequest(Peer *,size_t piece,size_t offset,size_t length);
00121 
00122         void SaveSlice(size_t piece,size_t offset,size_t length,unsigned char *);
00123 
00125         int64_t GetLength() { return m_length_one; }
00126         file_v& GetFiles() { return m_files; }
00127 
00128         void CreateFileManager();
00129         void Verify();
00130 
00131         const std::string& GetPieces() { return m_pieces; }
00132         Piece *GetIncomplete(size_t piece);
00133 
00134         bool SliceSent(size_t piece,size_t offset);
00135         void SendSlice(pSocket *,size_t piece,size_t offset,size_t length);
00136         Piece *GetComplete(size_t piece);
00137 
00138         void save_piece_v(FILE *,piece_v&);
00139         void load_piece_v(FILE *,piece_v&);
00140 
00141         bool GenerateRequest(Peer *peer);
00142         bool PieceUnique(size_t piece);
00143 
00144         void PeerStatus();
00145 //      size_t GetInstances(size_t piece);
00146 
00147         void SetCheckComplete() { m_b_check_complete = true; }
00148         void SetUpdateInterested() { m_b_update_interested = true; }
00149 
00150         bool IsDemon() { return m_demon; }
00151 
00152         //
00153         size_t Available(int piece);
00154         void RequestAvailable(Peer *);
00155         void RemoveRequests(Peer *);
00156         void CheckRequests();
00157 
00158 private:
00159         Session(const Session& s) : m_handler(s.Handler()) {} // copy constructor
00160         Session& operator=(const Session& ) { return *this; } // assignment operator
00161 
00162         unsigned char m_peer_id[20]; // random peer id
00163         ISocketHandler& m_handler;
00164         std::string m_info_hash;
00165         // metainfo file
00166         std::string m_announce;
00167         std::string m_name;
00168         int64_t m_piece_length;
00169         int64_t m_length; // total
00170         file_v m_files;
00171         size_t m_number_of_pieces;
00172         std::string m_pieces;
00173         size_t m_last_length;
00175         int m_interval;
00176         peer_v m_peers;
00177         time_t m_t_tracker;
00178 //      bitmap_t *m_bitmap;
00179         int64_t m_prev_offset;
00180         int64_t m_prev_length;
00181         unsigned char m_hashptr[20];
00182         piece_v m_complete;
00183         piece_v m_incomplete;
00184         int64_t m_length_one;
00185         FileManager *m_filemanager;
00186         bool m_b_check_complete;
00187         bool m_b_update_interested;
00188         bool m_demon;
00189 };
00190 
00191 
00192 #endif // _SESSION_H
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4