00001 #ifndef __J2K__TFTP_Option_HPP__ 00002 #define __J2K__TFTP_Option_HPP__ 00003 00004 #include <j2k/Fred/Standard.hpp> 00005 #include <j2k/Fred/JFile.hpp> 00006 #include <j2k/Net/TFTP/TFTP_header.hpp> 00007 #include <j2k/Posix/RWLock.hpp> 00008 #include <j2k/Net/TFTP/DataFactory.hpp> 00009 #include <j2k/Net/TFTP/FileFactory.hpp> 00010 00011 // Needed for str* functions not included with g++ <string.h> 00012 #include <j2k/Fred/String/Str-fn.hpp> 00013 00014 #define DEFAULT_WINDOWSIZE "1" 00015 #define DEFAULT_BLOCKSIZE "512" 00016 #define DEFAULT_TIMEOUT "5" 00017 00018 //#define DEFAULT_WINDOWSIZE "1" 00019 //#define DEFAULT_BLOCKSIZE "512" 00020 //#define DEFAULT_TIMEOUT "5" 00021 00022 #define DEFAULT_MODE "octet" 00023 #define DEFAULT_HOSTNAME "fbi.ece.concordia.ca" 00024 #define DEFAULT_PORT 2000 00025 00026 #define FILENAME_LENGTH 161 00027 #define FILEPATH_LENGTH 161 00028 #define HOSTNAME_LENGTH 161 00029 00030 #define FILESIZE_LENGTH 15 00031 #define BLOCKSIZE_LENGTH 6 00032 #define TIMEOUT_LENGTH 4 00033 #define MODE_LENGTH 9 00034 #define WINDOWSIZE_LENGTH 12 00035 00036 #define RRQ_WRQ_TEMPLATE_LENGTH (FILENAME_LENGTH + MODE_LENGTH + 2) 00037 #define OPTION_TEMPLATE_LENGTH 2 00038 00039 class TFTP_Option 00040 { 00041 public: 00042 TFTP_Option(); 00043 00044 virtual ~TFTP_Option(); 00045 00046 // Parse the arguments passed on the command line. 00047 void ArgParser( int argc, char* argv[] ); 00048 00049 // Detect what kind of argument it is 00050 // and initialize variables in consequence to that argument. 00051 void ArgDetect( const char* s, char* o ); 00052 00053 // Config hostname and port for client using string option 00054 void ConfigClient( char* opt ); 00055 00056 // Config port for server 00057 void TFTP_Option::ConfigServer( char* opt ); 00058 00059 // Build a template char[] that will be used by RRQ and WRQ 00060 // in Packet class. 00061 void build_RRQ_WRQ_TemplatePacket(); 00062 00063 void build_OptionPacket(); 00064 00065 // Set the filename 00066 inline void setFileName( const char* fname ); 00067 00068 // Get the filename 00069 const char* getFileName(); 00070 00071 // Set the blocksize 00072 inline void setBlockSize( size_t bsize ); 00073 00074 // Requested blocksize 00075 inline void requestedBlockSize( size_t bsize ); 00076 00077 // Get the blocksize 00078 inline size_t getBlockSize(); 00079 00080 00081 // Set Port Number 00082 inline void setPort( size_t portNo ); 00083 00084 // Set the HostName 00085 inline void setHostName( const char* host ); 00086 00087 // Set the windowsize 00088 inline void setWindowSize( size_t wsize ); 00089 00090 // Requested windowsize 00091 inline void requestedWindowSize( size_t wsize ); 00092 00093 // Get the windowsize 00094 inline size_t getWindowSize(); 00095 00096 // Set the filesize 00097 inline void setFileSize( size_t fsize ); 00098 00099 // Requested windowsize 00100 inline void requestedFileSize( size_t fsize ); 00101 00102 // Get the filesize 00103 inline const char* getFileSize(); 00104 00105 // Set the mode 00106 inline void setMode( char* m ); 00107 00108 // Requested mode 00109 inline void requestedMode( const char* m ); 00110 00111 // Get the mode 00112 inline const char* getMode(); 00113 00114 // Set the timeout 00115 inline void setTimeOut( size_t t ); 00116 00117 // Requested timeout 00118 inline void requestedTimeOut( size_t t ); 00119 00120 // Get the timeout 00121 inline const char* getTimeOut(); 00122 00123 // Get the timeout value 00124 inline ULONG getTimeOutValue(); 00125 00126 // Get the blocksize value 00127 inline ULONG getBlockSizeValue(); 00128 00129 // Get the windowsize value 00130 inline ULONG getWindowSizeValue(); 00131 00132 // "rb" for read, "wb" for write 00133 void openFile( char* mode = "rb" ); 00134 00135 // Ask user to select Server or client. 00136 void ServiceSelection(); 00137 00138 /************************************************************************** 00139 *** Create a socket with the given parsed parameters *** 00140 **************************************************************************/ 00141 void createSocket(); 00142 00143 void showStr( const char* n, const char* s ); 00144 void showVal( const char* n, size_t s ); 00145 void showChar( const char*n, const char* s, size_t sz ); 00146 00147 void showOptions(); 00148 00149 /* 00150 bit 0 Init 00151 bit 1 Connected 00152 bit 2 Ack for RRQ/WRQ 00153 bit 3 Reading 00154 bit 4 Writing 00155 bit 5 End of Read/Write 00156 bit 6 Error 00157 bit 7 Finished, program exit signal 00158 00159 Disconnect means that we are not connected and all data 00160 in TFTP_Option are initialized. 00161 */ 00162 00163 inline int isInitialized(); 00164 inline int isConnected(); 00165 inline int isWaitingForAck(); 00166 inline int isReading(); 00167 inline int isWriting(); 00168 inline int isEnding(); 00169 inline int hasError(); 00170 inline int isKilling(); 00171 00172 inline int isRRQ_Valid(); 00173 inline int isWRQ_Valid(); 00174 inline int isDATA_Valid(); 00175 inline int isACK_Valid(); 00176 inline int isOACK_Valid(); 00177 00178 inline int isServer(); 00179 inline int isClient(); 00180 00181 private: 00182 char* fpath; // Full path "/home/fred/test.txt" 00183 char* filename; // Only the filename "test.txt" 00184 char* filesize; 00185 char* timeout; 00186 char* blocksize; 00187 char* mode; 00188 char* windowsize; 00189 00190 USHORT port; 00191 char* hostname; 00192 size_t hostnameLength; 00193 00194 size_t fpathLength; 00195 size_t filenameLength; 00196 size_t filesizeLength; 00197 size_t blocksizeLength; 00198 size_t windowsizeLength; 00199 size_t timeoutLength; 00200 size_t modeLength; 00201 00202 char* openedFile; 00203 00204 // Shared variables... 00205 public: 00206 JFile* myFile; 00207 UDP_Socket* sender; 00208 DataFactory* dataFactory; 00209 FileFactory* fileFactory; 00210 00211 char* RRQ_WRQ_template; 00212 size_t RRQ_WRQ_templateLength; 00213 00214 char* Option_template; 00215 size_t Option_templateLength; 00216 00217 RWLock rwlock1_global; 00218 RWLock rwlock2_file; 00219 RWLock rwlock3_templates; 00220 00221 enum TFTP_Status_t state; 00222 enum TFTP_Type_t pgmType; 00223 00224 int RW_command; 00225 BOOL OACK_arg; 00226 00227 RWLock rwlock5_data_queue; 00228 DList_pPacket* data_queue; 00229 00230 RWLock rwlock6_wait_queue; 00231 DList_pPacket* waitingAck_queue; 00232 00233 BOOL Done; 00234 size_t LastData; 00235 00236 double currentTime; 00237 size_t windowsizeLeft; 00238 00239 size_t TopAck; 00240 00241 TFTP_Timeout* timeoutThread; 00242 TFTP_Sender* sendFactory; 00243 00244 double debug; 00245 00246 friend TFTP_OptionSaved; 00247 }; 00248 00249 #include <j2k/Net/TFTP/TFTP_Sender.hpp> 00250 00251 #endif