Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Net/TFTP/TFTP.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__TFTP_CPP__
00002 #define __J2K__TFTP_CPP__
00003 
00004 #include <j2k/Net/TFTP/TFTP.hpp>
00005 #include <j2k/Fred/JFile.cpp>
00006 #include <j2k/Fred/Error/JError.cpp>
00007 #include <j2k/Net/TFTP/TFTP_Project.cpp>
00008 
00009 TFTP::TFTP( int argc, char* argv[] )
00010 {
00011   parser = NULL;
00012   option.ArgParser( argc, argv );
00013   option.createSocket();
00014 
00015    add_TFTP_ErrorHandlers();
00016   parser = new TFTP_Parser();
00017   option.sendFactory       = new TFTP_Sender();
00018   option.dataFactory       = new DataFactory();
00019   option.fileFactory       = new FileFactory();
00020   option.data_queue        = new DList_pPacket();
00021   option.waitingAck_queue  = new DList_pPacket();
00022   option.timeoutThread     = new TFTP_Timeout();   
00023 
00024   switch( option.pgmType )
00025   {
00026      case TFTP_Server:  server(); break;
00027 
00028      case TFTP_Client:  // Shell or prompt a R/W request
00029         if ( option.RW_command == 1 ) { read();   break; }
00030         if ( option.RW_command == 2 ) { write();  break; }
00031         option.RW_command = 0;
00032 
00033 #ifndef SHELL
00034         char rw;
00035         char f[161];
00036         cout << "\n\nDo you want to (R)ead or (W)rite a file ? ";
00037         cin >> rw;
00038         cout << "\n\nEnter the name of the file: ";
00039         cin >> f;
00040         cout << "\n\nInitializing...\n";
00041         option.setFileName( f );
00042 
00043         if ( rw == 'r' || rw == 'R' || rw == 'd' || rw == 'D' ) {
00044           read(); break;
00045         } else {
00046           write(); break;
00047         }
00048 #else
00049         TFTP_client_shell();        
00050 #endif
00051 
00052        break;
00053        
00054      case TFTP_ClientRead:  read();  TFTP_client_shell(); break;
00055      case TFTP_ClientWrite: write(); TFTP_client_shell(); break;
00056 
00057      default:
00058         cout << "\n\nError: Client/Server not specified. \n";
00059         exit( 1 );
00060   }
00061 }
00062 
00063 inline TFTP::~TFTP() {
00064   if ( parser != NULL )  delete parser;
00065 }
00066 
00067 void TFTP::read() {
00068     printf("Reading\n");
00069     option.state = TFTP_Connected;  
00070     option.setMode( "octet" );
00071     option.build_OptionPacket();
00072     option.build_RRQ_WRQ_TemplatePacket();
00073     option.openFile( "wb" );
00074 
00075     option.showOptions();
00076 
00077     option.timeoutThread->start();
00078     TFTP_Packet pkt;
00079     //I'm just going to try to send a read request and send an ack back. 
00080     pkt.sendRRQ();
00081     option.state = TFTP_Reading;
00082     option.showOptions();
00083 }
00084 
00085 void TFTP::write() {
00086     printf("Writing\n");
00087     option.state = TFTP_Connected;
00088     option.build_OptionPacket();
00089     option.build_RRQ_WRQ_TemplatePacket();
00090     option.openFile( "rb" );
00091     option.showOptions();
00092 
00093     option.timeoutThread->start();
00094     TFTP_Packet pkt;
00095     //I'm just going to try to send a read request and send an ack back. 
00096     pkt.sendWRQ();
00097     option.state = TFTP_Writing;
00098     option.showOptions();
00099 }
00100 
00101 void TFTP::server() {
00102   printf("Server\n");
00103   option.state = TFTP_Connected;
00104   option.build_OptionPacket();
00105   
00106     option.showOptions();
00107 
00108     option.timeoutThread->start();
00109 
00110   //First receive request.
00111   //Send Ack
00112 }
00113 
00114 int main( int argc, char* argv[] )
00115 {
00116   pthread_attr_t attr;
00117   sched_param    param;
00118 
00119   memset( &attr,  0, sizeof( attr  ) );
00120   memset( &param, 0, sizeof( param ) );
00121 
00122   pthread_attr_init( &attr );
00123   pthread_attr_setscope(        &attr, PTHREAD_SCOPE_SYSTEM );
00124   pthread_attr_setdetachstate(  &attr, PTHREAD_CREATE_JOINABLE );
00125   pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
00126   pthread_attr_setschedparam(   &attr, &param );
00127  
00128   param.sched_priority = 15; 
00129   pthread_setschedparam( pthread_self(), SCHED_RR, &param );
00130 
00131   size_t q = 0;
00132   TFTP( argc, argv );
00133   while( option.state != TFTP_Finished && !option.isEnding() )
00134   {
00135 //    q++;
00136 //    if ( q > 100 ) {
00137 //      printf("."); 
00138 //      q = 0;
00139 //    }
00140     sched_yield();  // I'm lazy, let bright children run please.
00141   }
00142   return 0;
00143 }
00144 #endif
00145 
00146 
00147 

Generated on Sun Oct 14 18:46:34 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001