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

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

Go to the documentation of this file.
00001 #ifndef __J2K__File_Factory_CPP__
00002 #define __J2K__File_Factory_CPP__
00003 
00004 #include <j2k/Net/TFTP/FileFactory.hpp>
00005 
00006 FileFactory::FileFactory()
00007  : Basic_PThread(),
00008   expectedBlockNo( 1 ), initialized( FALSE ),
00009   sz( 0 ), rc( 0 ), length( 0 ), block( 0 ), empty( TRUE )
00010 {
00011   blocksize = option.getBlockSizeValue();
00012 }
00013 
00014 void FileFactory::init()
00015 {
00016   if ( initialized == TRUE ) return;
00017   initialized = TRUE;
00018   expectedBlockNo = 1;
00019   sz     = 0;
00020   rc     = 0;
00021   length = 0;
00022   block  = 0;
00023   empty  = TRUE;
00024 
00025   printf( "FileFactory::init\n");
00026   fflush( stdout );
00027 
00028   option.openFile( "wb" );
00029   option.rwlock2_file.read();
00030   option.myFile->setBegin();
00031   option.rwlock2_file.releaseRead();
00032 }
00033 
00034 void FileFactory::checkIfEmpty()
00035 {
00036   while ( empty == TRUE ) 
00037   { 
00038     option.rwlock5_data_queue.read();
00039     empty = option.data_queue->isEmpty();
00040     option.rwlock5_data_queue.releaseRead();
00041    
00042     if ( empty == TRUE )
00043     {
00044         printf("Empty Packet\n");
00045         sched_yield();  // it's empty, yield.
00046     }
00047   }
00048 }
00049 
00050 void FileFactory::emptyPacket()
00051 {
00052     option.rwlock5_data_queue.read();
00053 
00054 #ifdef DEBUG
00055   printf( "Packet null in list, size=%d\n", option.data_queue->size() );
00056   option.data_queue->display();
00057 #endif
00058 
00059   option.rwlock5_data_queue.releaseRead();
00060   fflush( stdout );
00061   sched_yield();
00062 }
00063 
00064 void FileFactory::directWrite( TFTP_Packet* pkt )
00065 {
00066   init();
00067   packet = pkt;
00068   validPacket();
00069 }
00070 
00071 void FileFactory::validPacket()
00072 {
00073   block = packet->getBlockNo();
00074 
00075   if ( block == expectedBlockNo ) 
00076   {
00077     printf( "\nThis packet was in order: %d / %d\n", block, expectedBlockNo );
00078     fflush( stdout );
00079 
00080     option.rwlock2_file.read(); // Read filedes to write on file
00081     length = packet->getpktLength() - 4;
00082     if ( length > 0 ) 
00083     { 
00084       printf( "\nWriting to file: #%d, length %d\n", block, length );
00085       fflush( stdout );
00086 
00087 #ifdef DEBUG
00088       option.showChar( "Data",  packet->getData(), length );
00089 #endif
00090 
00091       sz = option.myFile->putData( packet->getData(), length, option.getBlockSizeValue() );
00092 
00093     } else if (length == 0)
00094     {
00095         sz = 0;
00096         printf( "\nNothing to write to file: #%d, length %d\n", block, length );
00097         fflush( stdout );
00098     } else 
00099     {   
00100       printf( "\nDid NOT write to file: #%d, length %d\n", block, length );
00101       fflush( stdout );
00102     }
00103 
00104     option.rwlock2_file.releaseRead();
00105     expectedBlockNo++; 
00106 
00107     // Last Packet;
00108     if ( sz < option.getBlockSizeValue() ) 
00109     {
00110       printf( "\nThis shall be the last packet of size %d out of %d #%d\n", \
00111               sz, option.getBlockSizeValue(), block );
00112 
00113       if ( option.myFile != NULL ) {
00114         delete option.myFile;
00115         option.myFile = NULL;
00116       }
00117       fflush( stdout );
00118 
00119       if ( option.isReading() ) {
00120         option.state = TFTP_EndofRead;
00121       } else if ( option.isWriting() ) {
00122         option.state = TFTP_EndofWrite;
00123       } else {
00124         option.state = TFTP_Finished;
00125       }
00126       return;
00127     }
00128     else
00129     {
00130         printf("This aint the last packet\n");
00131         printf("The size is %d, Blocksize is %d\n",sz,option.getBlockSizeValue());
00132     }
00133     
00134     // It's 1 NOT 0, since 0 means we started!
00135     // Loop the Window size.
00136     if ( (expectedBlockNo-1) > option.getWindowSizeValue() ) 
00137     {
00138         //No need for this
00139      // expectedBlockNo = 1;   
00140     }
00141 
00142     } else   { // Trash Packet.
00143      printf( "\nTrashing Data Packet# %d / %d\n", block, expectedBlockNo );
00144      fflush( stdout );
00145      packet = NULL;
00146      delete packet;
00147   }
00148 }
00149 
00150 void FileFactory::run() 
00151 {
00152   init(); 
00153   
00154   while ( option.isConnected() || !option.data_queue->isEmpty() )
00155   {
00156     checkIfEmpty();
00157 
00158     // Read data in Write mode (modifying structure) 
00159     option.rwlock5_data_queue.write();
00160     packet = option.data_queue->dequeue();        
00161     option.rwlock5_data_queue.releaseWrite();
00162     
00163     if ( packet != NULL ) 
00164     {
00165       validPacket();
00166 
00167     } else {  // List is empty ?!
00168 
00169       if ( option.isEnding() ) return;
00170 
00171       emptyPacket();
00172       //      sched_yield();
00173     }
00174 
00175   } // end while()
00176 }
00177 
00178 #endif
00179 
00180 
00181 
00182 
00183 
00184 
00185 
00186 

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