www.pudn.com > fanccMSNr.src.rar > FileTransferModel.hpp


#pragma once 
#include  
#include "FileTransferListener.hpp" 
 
namespace poral { 
	class ChatterModel; 
	class FileTransferModel: 
		public MessageListener { 
	public: 
		typedef enum { 
			NOT_CONNECTED, 
			ATTEMPT_TO_RECEIVE, 
			ATTEMPT_TO_SEND, 
			RECEIVING, 
			SENDING, 
		} Status; 
 
		/** 
		* Indicates how often notifies incoming/outgoing data packets transferred. 
		* Notify once per transferNotificationInterval packets. Default value is 1. 
		* Never notifies if the value is -1. 
		*/ 
		int transferNotificationInterval; 
	protected: 
		ChatterModel *chatterModel; 
		MessageDispatcher *messageDispatcher; 
 
		string filename; 
		/** Incoming file length. */ 
		size_t length; 
		/** Number of received/send bytes of transferring file. */ 
		size_t nTransferred; 
		/** Number of received/send packets of transferring file. */ 
		int nPackets; 
		string authCookie; 
		string cookie; 
		// TODO: explain. 
		ofstream *fileOut; 
		ifstream *fileIn; 
 
		Status status; 
 
		list listeners; 
 
		/** Available cookie number. */ 
		static int newCookie; 
		/** Available authCookie number. */ 
		static int newAuthCookie; 
 
		//static const int maxBytesPerPacket=2045;	// VC6 can't compile this! DAMN!!! 
#define maxBytesPerPacket (2045) 
		/** Buffer for send file. */ 
		unsigned char buffer[maxBytesPerPacket+3]; 
		/** Length of data in buffer. */ 
		int nData; 
		bool retransmit; 
	public: 
		// Constructors and destructors ///////////////////// 
		FileTransferModel(); 
		virtual ~FileTransferModel(); 
 
		// Getter and setters ///////////////////// 
		Status getStatus() const { 
			return status; 
		} 
 
		size_t getLength() const { 
			return length; 
		} 
 
		size_t getNTransferred() const { 
			return nTransferred; 
		} 
 
		// Operators ///////////////////// 
		/** Attempt to connect to accept file transfer invitation. */ 
		void connect(ChatterModel &chatterModel, const string &ip, int port, const string &authCookie, const string &filename); 
		/**  
		* Listen to incoming connection.  
		* @return Port number listening; 
		*/ 
		int listen(ChatterModel &chatterModel, int preferredPort, const string &authCookie, const string &filename); 
		void disconnect(); 
 
		 
		void addFileTransferListener(FileTransferListener &listener); 
		void removeFileTransferListener(FileTransferListener &listener); 
 
		// Implementation of MessageListener. ///////////////////// 
		// Shoud not call these mehtods. 
		virtual void connected(); 
		virtual void messageArrived(const Message &message); 
		virtual void failed(); 
		virtual void sendable(); 
 
 
	protected: 
		void fireFileTransferUpdate(FileTransferListener::Event event); 
 
		friend class ChatterModel; 
 
	}; 
}