00001 /* 00002 00003 Copyright (C) 2006-2007 by Peter Dimov. 00004 00005 This file is part of Calitko (http://www.calitko.org). 00006 00007 Calitko is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or 00010 (at your option) any later version. 00011 00012 Calitko is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with Calitko; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00020 00021 */ 00022 00023 #ifndef PROTOCOLS__BIT_TORRENT__TRANSFERS__SESSION_H 00024 #define PROTOCOLS__BIT_TORRENT__TRANSFERS__SESSION_H 00025 00026 #include "Imports.h" 00027 00028 namespace Protocols { 00029 namespace BitTorrent { 00030 namespace Transfers { 00031 00032 // \todo 00033 struct PeerInfo 00034 { 00035 NodeAddress nodeAddress; 00036 QByteArray id; 00037 }; 00038 00039 // \todo 00040 typedef QByteArray FileId; 00041 00042 class SessionStatus; 00043 00045 00048 class Session : public TransportStatus 00049 { 00050 CALITKO_TESTABLE 00051 00052 public: 00053 Session (Transport &, SessionStatus &, 00054 const PeerInfo &localPeerInfo); 00055 ~Session(); 00056 00057 void establish (const PeerInfo &remotePeerInfo, const FileId &fileId); 00058 void waitRequest(); 00059 void accept(); 00060 00061 private: 00062 void connected(); 00063 void readyRead(); 00064 void readyWrite(); 00065 void disconnected(); 00066 00067 private: 00068 enum State 00069 { 00070 DisconnectedTransport, 00071 EstablishingSession, 00072 AwaitingRequest, 00073 AwaitingAccept, 00074 ExchangingPackets 00075 }; 00076 00077 bool readHandshake(); 00078 void writeHandshake(); 00079 void readPacket(); 00080 void writePacket(); 00081 00082 Transport &transport; 00083 SessionStatus &status; 00084 PeerInfo localPeerInfo; 00085 PeerInfo remotePeerInfo; 00086 FileId fileId; 00087 State state; 00088 }; 00089 00090 class SessionStatus 00091 { 00092 CALITKO_MOCKABLE 00093 00094 public: 00095 virtual ~SessionStatus() {} 00096 00097 virtual void establishingSession() = 0; 00098 //virtual void negotiationError() = 0; 00099 virtual void sessionRequestedFor (const FileId &fileId) = 0; 00100 virtual void sessionEstablished() = 0; 00101 //virtual void sessionClosing() = 0; 00102 virtual void sessionClosed() = 0; 00103 00104 virtual void sessionChoked() = 0; 00105 virtual void sessionUnchoked() = 0; 00106 virtual void sessionInterested() = 0; 00107 virtual void sessionNotInterested() = 0; 00108 }; 00109 00110 } // namespace Transfers 00111 } // namespace BitTorrent 00112 } // namespace Protocols 00113 00114 #endif // PROTOCOLS__BIT_TORRENT__TRANSFERS__SESSION_H