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_ParserStatic.hpp

Go to the documentation of this file.
00001 #ifndef __J2K__TFTP_ParserStatic_HPP__
00002 #define __J2K__TFTP_ParserStatic_HPP__
00003 
00004 // [  2 |     n+1     |   n+1   |      n+1      | ... ]
00005 // [ 01 | Filename \0 | Mode \0 | extensions \0 | ... ]
00006 void RRQ_driver( char* rawPacket, int sz )
00007 {
00008   // %0000,0011  && Server
00009   // We are the Server and the client wants to read a file from the server.
00010   if (  option.isRRQ_Valid()  ) 
00011   {
00012     option.state = TFTP_Reading;
00013 
00014     // Save default, for future request from other clients.
00015     savedOptions.save();
00016 
00017     // Parse RRQ args
00018     parseOptions( rawPacket, sz, 2 );
00019 
00020     option.showOptions();
00021     fflush( stdout );
00022 
00023     option.openFile( "rb" );
00024     fflush( stdout );
00025 
00026     if ( option.dataFactory == NULL ) {
00027       option.dataFactory = new DataFactory();
00028     }
00029 
00030     // Cheap scheduling without threads
00031     option.dataFactory->run();
00032    
00033     // Let Data Factory the time to produce ! =)
00034     //    sched_yield();
00035 
00036     //option.build_RRQ_WRQ_TemplatePacket();
00037     option.build_OptionPacket();
00038 
00039     printf("back\n");
00040     option.showOptions();
00041     fflush( stdout );
00042 
00043     OACK_arg = FALSE;         
00044     // Send Data/OACK...
00045     if ( OACK_arg == TRUE ) 
00046     {
00047         TFTP_Packet* pkt = new TFTP_Packet();
00048         pkt->sendOAck();
00049 
00050         // Ack#0 will start sendFactory then...
00051 
00052     } else {
00053 
00054         if ( option.sendFactory == NULL ) {
00055           option.sendFactory = new TFTP_Sender();
00056         }
00057 
00058         option.sendFactory->start();
00059 
00060         // Let Send Factory the time to Send ! =)
00061         sched_yield(); 
00062 
00063         // Send data #1
00064         // Start the DataFactory & SendFactory
00065         // which will create data packet and send them.
00066     }
00067 
00068   } else {
00069  
00070     option.state = TFTP_Error;
00071 
00072     // We are in a situation where a RRQ packet is not expected,
00073     // in any given way and therefore makes no sense.
00074 
00075     // Send Error #4 packet.
00076     SendIllegalOpError( "RRQ Rejected: Your Read Request makes no sense. \n" );
00077   }
00078 }
00079 
00080 // [  2 |     n+1     |   n+1   |      n+1      | ... ]
00081 // [ 02 | Filename \0 | Mode \0 | extensions \0 | ... ]
00082 void WRQ_driver( char* rawPacket, int sz )
00083 {
00084   // %0000,0011  && Server
00085   // We are the Server and the client wants to write
00086   // a file on the server side.
00087   if (  option.isWRQ_Valid()  )
00088   {
00089     option.state = TFTP_Writing;
00090 
00091     // Save default, for future request from other clients.
00092     savedOptions.save();
00093 
00094     // Parse WRQ args
00095     parseOptions( rawPacket, sz, 2 );
00096     option.showOptions();
00097     option.openFile( "wb" );
00098     
00099     if ( option.fileFactory != NULL ) {
00100       option.fileFactory = new FileFactory();
00101     }
00102 
00103 
00104 
00105 
00106 //    option.fileFactory->start(28);
00107 
00108     option.build_RRQ_WRQ_TemplatePacket();
00109     option.build_OptionPacket();
00110 
00111     // Send Data/OACK...
00112     if ( OACK_arg == TRUE ) 
00113     {
00114         // Send OAck
00115         TFTP_Packet* pkt = new TFTP_Packet();
00116         pkt->sendOAck();
00117 
00118     } else {
00119 
00120         TFTP_Packet* pkt = new TFTP_Packet();
00121         pkt->sendAck( 0 );
00122         // Send Ack Block#0
00123     }
00124   } else {
00125  
00126     option.state = TFTP_Error;
00127 
00128     // We are in a situation where a WRQ packet is not expected,
00129     // in any given way and therefore makes no sense.
00130 
00131     // Send Error #4 packet.
00132     SendIllegalOpError( "WRQ Rejected: Your Write Request makes no sense. \n" );
00133   }
00134 }
00135 
00136 int TestTimeout( short blockNo )
00137 {
00138 return 1;
00139 
00140     if ( blockNo == 2 ) 
00141     {
00142         if ( option.debug == 0 ) 
00143         {
00144             option.debug = option.currentTime + 3;
00145         } else 
00146         {
00147             if ( option.debug > option.currentTime ) {
00148          printf( "\nLet NOT TIMEOUT !\n");
00149               fflush( stdout );
00150               return 1;
00151             }
00152         }
00153         printf( "\nTIMEOUT TEST!\n");
00154         fflush( stdout );
00155         return 0;
00156     }
00157     
00158     return 1;    
00159 }
00160 
00161 // Might act as an ack for poor type TFTP
00162 
00163 // Data #1  as ack for RRQ on the client side
00164 // Data #1  after OACK received for RRQ on the client side
00165 // Data > 1 for reading on the client side
00166 // Data received on the server side for WRQ/Writing
00167 
00168 // [  2 |    2     |    n     ]
00169 // [ 03 | Block No |   Data   ]
00170 void DATA_driver( char* rawPacket, int sz )
00171 {
00172   if ( option.isDATA_Valid()  )
00173   {
00174     TFTP_Packet* pktData = new TFTP_Packet();
00175     pktData->createData( rawPacket, sz );
00176 
00177 //    option.rwlock5_data_queue.write();
00178 //    option.data_queue->enqueue( pktData );
00179 //    option.rwlock5_data_queue.releaseWrite();
00180 
00181     TFTP_Packet* pktAck = new TFTP_Packet();
00182     short blockNo = 0;
00183     memcpy( (char*)&blockNo, rawPacket + 2, 2 );
00184 
00185 //    option.fileFactory->start(28);
00186 
00187     option.fileFactory->directWrite( pktData );
00188 
00189     sched_yield();
00190 
00191     if ( TestTimeout( blockNo ) )
00192     {
00193         printf("Sending Ack for block number %d\n",blockNo);
00194         fflush(stdout);
00195         pktAck->sendAck( blockNo );  // Auto-Respond.
00196     }
00197     
00198     if (option.isEnding())
00199     {
00200         exit( 0);
00201     }    
00202   } else {
00203  
00204     option.state = TFTP_Error;
00205 
00206     // Send Error 4.
00207     SendIllegalOpError( "DATA Rejected: Your NOT allowed yet to send data. \n" );
00208   }
00209 }
00210 
00211 // When do we receive ack ?
00212 // [  2 |    2     ]
00213 // [ 04 | Block No ]
00214 void ACK_driver( char* rawPacket, int sz )
00215 {           
00216   if ( option.isACK_Valid() )
00217   {
00218     short blockNo = 0;
00219     memcpy( (char*)&blockNo, rawPacket+2, 2 );
00220 
00221     // Scan waitingForAck SList and remove packets from Timeout Queue.
00222     if (  option.isClient() && option.state == TFTP_Writing ) 
00223     {
00224       if ( option.dataFactory == NULL ) {
00225         option.dataFactory = new DataFactory();
00226      }
00227 
00228       // Cheap scheduling without threads
00229       option.dataFactory->run();  
00230     }
00231         
00232     if ( blockNo == 0 && \
00233        ( (  option.isServer() && option.state == TFTP_Reading ) || \
00234          (  option.isClient() && option.state == TFTP_Writing ) ) )
00235     {
00236         if ( option.sendFactory != NULL ) {
00237           option.sendFactory = new TFTP_Sender();
00238         }
00239 
00240         option.sendFactory->start();
00241         // Let Send Factory the time to Send ! =)
00242         // sched_yield(); 
00243 
00244         // Send data #1
00245         // Start the DataFactory & TFTP_DataSender
00246         // which will create data packet and send them.
00247     }
00248     
00249     
00250     
00251     if (blockNo == option.LastData && blockNo != 0 )
00252     {
00253         printf("Got the ACK of the last packet. \n");
00254         option.state= TFTP_Finished;
00255         //exit(0);
00256     }
00257     
00258     if ( blockNo > 0 ) {
00259         TFTP_DataAck clean( blockNo );  
00260         clean.run();
00261         option.windowsizeLeft++;
00262         printf("I received an acknowledgment, for block number %d\n",blockNo);
00263 
00264         // printf("The windowsizeLeft is now %d\n",option.windowsizeLeft);
00265 
00266       // Should be auto-destroyed after this if block
00267     }
00268     
00269     
00270     if ( option.isClient() && option.state == TFTP_WritingNotAck ) {
00271       // The server refused all extension and used default TFTP.
00272 
00273     }
00274 
00275     if ( option.isServer() && option.isReading() ) {
00276       // The client approved server OACK and replied.
00277     }
00278 
00279    }else {
00280        option.state = TFTP_Error;
00281     // Send Error 4.
00282        SendIllegalOpError( "ACK Rejected ?! \n" );
00283   }
00284 }
00285 
00286 // [  2 |    2     |     n+1      ]
00287 // [ 05 | Error No | Error Msg \0 ]
00288 void ERROR_driver( char* rawPacket, int sz )
00289 {
00290   // Allow DList to complete ?
00291   option.state = TFTP_Finished;
00292   exit( 0 );
00293 }
00294 
00295 // When do we receive OAck ?
00296 // [  2 |      n+1      | ... ]
00297 // [ 06 | extensions \0 | ... ]
00298 void OACK_driver( char* rawPacket, int sz )
00299 {
00300   if (  ( option.state == TFTP_ReadingNotAck ||  \
00301           option.state == TFTP_WritingNotAck ) && \
00302           option.isClient()  )
00303   {
00304      // Okay.
00305 
00306     // Save default, for future request to other servers.
00307     savedOptions.save();
00308 
00309     // Parse OACK args
00310     parseOptions( rawPacket, sz, 2 );
00311 
00312     if ( option.state == TFTP_ReadingNotAck ) {
00313       option.state = TFTP_Reading;
00314       // option.openFile( "rb" );      
00315       TFTP_Packet* pkt = new TFTP_Packet();
00316       pkt->sendAck( 0 );
00317     }
00318 
00319     if ( option.state == TFTP_WritingNotAck ) { 
00320       option.state = TFTP_Writing;
00321         // option.openFile( "wb" );
00322         // pkt.sendData( 1 );
00323 
00324         if ( option.dataFactory != NULL ) {
00325           option.dataFactory = new DataFactory();
00326         }
00327 
00328         option.dataFactory->run();
00329 
00330         // Let Data Factory the time to produce ! =)
00331         sched_yield();
00332 
00333         if ( option.sendFactory != NULL ) {
00334           option.sendFactory = new TFTP_Sender();
00335         }
00336 
00337         option.sendFactory->run();
00338 
00339         // Let Send Factory the time to Send ! =)
00340         // sched_yield(); 
00341 
00342         // Send data #1
00343         // Start the DataFactory & TFTP_DataSender
00344         // which will create data packet and send them.
00345     }
00346 
00347   } else {
00348  
00349     option.state = TFTP_Error;
00350     // Send Error 4.
00351     SendIllegalOpError( "OACK Rejected. \n" );
00352   }
00353 }
00354 
00355 void opcodeDispatcher( char* rawPacket, int sz )
00356 {
00357     //printf("\nPDU = {");
00358     //for( int i = 0; i < sz; i++ )
00359     //    printf("[%d]%c)", rawPacket[i], rawPacket[i] );
00360 
00361     //printf("};\n");
00362     //option.showOptions();
00363     //fflush( stdout );
00364     short op;
00365     
00366     memcpy( (char*)&op, rawPacket, 2 );
00367 
00368     if ( op >= 1 && op <= 6 ) 
00369     {
00370       char* rawCopy = new char[ sz + 1 ];
00371       memcpy( rawCopy, rawPacket, sz );
00372 
00373       // opTable start at 0, not 1
00374       switch( op ) 
00375       {
00376         case 1: RRQ_driver(   rawCopy, sz ); break;
00377         case 2: WRQ_driver(   rawCopy, sz ); break;
00378         case 3: DATA_driver(  rawCopy, sz ); break;
00379         case 4: ACK_driver(   rawCopy, sz ); break;
00380         case 5: ERROR_driver( rawCopy, sz ); break;
00381         case 6: OACK_driver(  rawCopy, sz ); break;
00382       }
00383       
00384       //(this->*( opTable[ op-1 ] ))( rawCopy, sz ); 
00385 
00386     } else {
00387 
00388         char buf[80];
00389         sprintf( buf, "Invalid Opcode: %d\n", op );
00390         SendIllegalOpError( buf );        
00391     }    
00392 }
00393 
00394 #endif

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