Index  Source Files  Annotated Class List  Alphabetical Class List  Class Hierarchy  Graphical Class Hierarchy   
 

FIX::FileStore Class Reference

File based implementation of MessageStore. More...

#include <FileStore.h>

Inheritance diagram for FIX::FileStore:

Inheritance graph
[legend]
Collaboration diagram for FIX::FileStore:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 FileStore (std::string, const SessionID &s)
virtual ~FileStore ()
bool set (int, const std::string &) throw ( IOException )
void get (int, int, std::vector< std::string > &) const throw ( IOException )
int getNextSenderMsgSeqNum () const throw ( IOException )
int getNextTargetMsgSeqNum () const throw ( IOException )
void setNextSenderMsgSeqNum (int value) throw ( IOException )
void setNextTargetMsgSeqNum (int value) throw ( IOException )
void incrNextSenderMsgSeqNum () throw ( IOException )
void incrNextTargetMsgSeqNum () throw ( IOException )
UtcTimeStamp getCreationTime () const throw ( IOException )
void reset () throw ( IOException )
void refresh () throw ( IOException )

Private Types

typedef std::pair< int, int > OffsetSize
typedef std::map< int, OffsetSizeNumToOffset

Private Member Functions

void open (bool deleteFile)
void populateCache ()
bool readFromFile (int offset, int size, std::string &msg)
void setSeqNum ()
void setSession ()
bool get (int, std::string &) const throw ( IOException )

Private Attributes

MemoryStore m_cache
NumToOffset m_offsets
std::string m_msgFileName
std::string m_headerFileName
std::string m_seqNumsFileName
std::string m_sessionFileName
FILE * m_msgFile
FILE * m_headerFile
FILE * m_seqNumsFile
FILE * m_sessionFile

Detailed Description

File based implementation of MessageStore.

Four files are created by this implementation. One for storing outgoing messages, one for indexing message locations, one for storing sequence numbers, and one for storing the session creation time.

The formats of the files are:
   [path]+[BeginString]-[SenderCompID]-[TargetCompID].body
   [path]+[BeginString]-[SenderCompID]-[TargetCompID].header
   [path]+[BeginString]-[SenderCompID]-[TargetCompID].seqnums
   [path]+[BeginString]-[SenderCompID]-[TargetCompID].session

The messages file is a pure stream of FIX messages.

The sequence number file is in the format of
   [SenderMsgSeqNum] : [TargetMsgSeqNum]

The session file is a UTC timestamp in the format of
   YYYYMMDD-HH:MM:SS

Definition at line 81 of file FileStore.h.


Member Typedef Documentation

typedef std::map< int, OffsetSize > FIX::FileStore::NumToOffset [private]
 

Definition at line 104 of file FileStore.h.

typedef std::pair< int, int > FIX::FileStore::OffsetSize [private]
 

Definition at line 103 of file FileStore.h.

Referenced by get().


Constructor & Destructor Documentation

FIX::FileStore::FileStore std::string  ,
const SessionID s
 

Definition at line 35 of file FileStore.cpp.

References FIX::file_appendpath(), FIX::file_mkdir(), FIX::SessionID::getBeginString(), FIX::SessionID::getSenderCompID(), FIX::SessionID::getSessionQualifier(), FIX::SessionID::getTargetCompID(), m_headerFileName, m_msgFileName, m_seqNumsFileName, m_sessionFileName, and open().

00036 : m_msgFile( 0 ), m_headerFile( 0 ), m_seqNumsFile( 0 ), m_sessionFile( 0 )
00037 {
00038   file_mkdir( path.c_str() );
00039 
00040   if ( path.empty() ) path = ".";
00041   const std::string& begin =
00042     s.getBeginString().getString();
00043   const std::string& sender =
00044     s.getSenderCompID().getString();
00045   const std::string& target =
00046     s.getTargetCompID().getString();
00047   const std::string& qualifier =
00048     s.getSessionQualifier();
00049 
00050   std::string sessionid = begin + "-" + sender + "-" + target;
00051   if( qualifier.size() )
00052     sessionid += "-" + qualifier;
00053 
00054   std::string prefix
00055     = file_appendpath(path, sessionid + ".");
00056 
00057   m_msgFileName = prefix + "body";
00058   m_headerFileName = prefix + "header";
00059   m_seqNumsFileName = prefix + "seqnums";
00060   m_sessionFileName = prefix + "session";
00061 
00062   try
00063   {
00064     open( false );
00065   }
00066   catch ( IOException & e )
00067   {
00068     throw ConfigError( e.what() );
00069   }
00070 }

FIX::FileStore::~FileStore  )  [virtual]
 

Definition at line 72 of file FileStore.cpp.

References m_headerFile, m_msgFile, m_seqNumsFile, and m_sessionFile.

00073 {
00074   fclose( m_msgFile );
00075   fclose( m_headerFile );
00076   fclose( m_seqNumsFile );
00077   fclose( m_sessionFile );
00078 }


Member Function Documentation

bool FIX::FileStore::get int  ,
std::string & 
const throw ( IOException ) [private]
 

Definition at line 332 of file FileStore.cpp.

References OffsetSize, QF_STACK_POP, and QF_STACK_PUSH.

00334 { QF_STACK_PUSH(FileStore::get)
00335 
00336   NumToOffset::const_iterator find = m_offsets.find( msgSeqNum );
00337   if ( find == m_offsets.end() ) return false;
00338   const OffsetSize& offset = find->second;
00339   if ( fseek( m_msgFile, offset.first, SEEK_SET ) ) 
00340     throw IOException( "Unable to seek in file " + m_msgFileName );
00341   char* buffer = new char[ offset.second + 1 ];
00342   fread( buffer, sizeof( char ), offset.second, m_msgFile );
00343   if ( ferror( m_msgFile ) ) 
00344     throw IOException( "Unable to read from file " + m_msgFileName );
00345   buffer[ offset.second ] = 0;
00346   msg = buffer;
00347   delete [] buffer;
00348   return true;
00349 
00350   QF_STACK_POP
00351 }

void FIX::FileStore::get int  ,
int  ,
std::vector< std::string > & 
const throw ( IOException ) [virtual]
 

Implements FIX::MessageStore.

Definition at line 223 of file FileStore.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

00226 { QF_STACK_PUSH(FileStore::get)
00227 
00228   result.clear();
00229   std::string msg;
00230   for ( int i = begin; i <= end; ++i )
00231   {
00232     if ( get( i, msg ) )
00233       result.push_back( msg );
00234   }
00235 
00236   QF_STACK_POP
00237 }

UtcTimeStamp FIX::FileStore::getCreationTime  )  const throw ( IOException ) [virtual]
 

Implements FIX::MessageStore.

Definition at line 279 of file FileStore.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

00280 { QF_STACK_PUSH(FileStore::getCreationTime)
00281   return m_cache.getCreationTime();
00282   QF_STACK_POP
00283 }

int FIX::FileStore::getNextSenderMsgSeqNum  )  const throw ( IOException ) [virtual]
 

Implements FIX::MessageStore.

Definition at line 239 of file FileStore.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

Referenced by open(), and setSeqNum().

00240 { QF_STACK_PUSH(FileStore::getNextSenderMsgSeqNum)
00241   return m_cache.getNextSenderMsgSeqNum();
00242   QF_STACK_POP
00243 }

int FIX::FileStore::getNextTargetMsgSeqNum  )  const throw ( IOException ) [virtual]
 

Implements FIX::MessageStore.

Definition at line 245 of file FileStore.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

Referenced by open(), and setSeqNum().

00246 { QF_STACK_PUSH(FileStore::getNextTargetMsgSeqNum)
00247   return m_cache.getNextTargetMsgSeqNum();
00248   QF_STACK_POP
00249 }

void FIX::FileStore::incrNextSenderMsgSeqNum  )  throw ( IOException ) [virtual]
 

Implements FIX::MessageStore.

Definition at line 265 of file FileStore.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

00266 { QF_STACK_PUSH(FileStore::incrNextSenderMsgSeqNum)
00267   m_cache.incrNextSenderMsgSeqNum();
00268   setSeqNum();
00269   QF_STACK_POP
00270 }

void FIX::FileStore::incrNextTargetMsgSeqNum  )  throw ( IOException ) [virtual]
 

Implements FIX::MessageStore.

Definition at line 272 of file FileStore.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

00273 { QF_STACK_PUSH(FileStore::incrNextTargetMsgSeqNum)
00274   m_cache.incrNextTargetMsgSeqNum();
00275   setSeqNum();
00276   QF_STACK_POP
00277 }

void FIX::FileStore::open bool  deleteFile  )  [private]
 

Definition at line 80 of file FileStore.cpp.

References FIX::file_fopen(), FIX::file_unlink(), getNextSenderMsgSeqNum(), getNextTargetMsgSeqNum(), m_headerFile, m_headerFileName, m_msgFile, m_msgFileName, m_seqNumsFile, m_seqNumsFileName, m_sessionFile, m_sessionFileName, populateCache(), QF_STACK_POP, QF_STACK_PUSH, setNextSenderMsgSeqNum(), setNextTargetMsgSeqNum(), and setSession().

Referenced by FileStore().

00081 { QF_STACK_PUSH(FileStore::open)
00082 
00083   if ( m_msgFile ) fclose( m_msgFile );
00084   if ( m_headerFile ) fclose( m_headerFile );
00085   if ( m_seqNumsFile ) fclose( m_seqNumsFile );
00086   if ( m_sessionFile ) fclose( m_sessionFile );
00087 
00088   if ( deleteFile )
00089   {
00090     file_unlink( m_msgFileName.c_str() );
00091     file_unlink( m_headerFileName.c_str() );
00092     file_unlink( m_seqNumsFileName.c_str() );
00093     file_unlink( m_sessionFileName.c_str() );
00094   }
00095 
00096   populateCache();
00097 
00098   m_msgFile = file_fopen( m_msgFileName.c_str(), "r+" );
00099   if ( !m_msgFile ) m_msgFile = file_fopen( m_msgFileName.c_str(), "w+" );
00100   if ( !m_msgFile ) throw ConfigError( "Could not open body file" );
00101 
00102   m_headerFile = file_fopen( m_headerFileName.c_str(), "r+" );
00103   if ( !m_headerFile ) m_headerFile = file_fopen( m_headerFileName.c_str(), "w+" );
00104   if ( !m_headerFile ) throw ConfigError( "Could not open header file" );
00105 
00106   m_seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "r+" );
00107   if ( !m_seqNumsFile ) m_seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "w+" );
00108   if ( !m_seqNumsFile ) throw ConfigError( "Could not open seqnums file" );
00109 
00110   bool setCreationTime = false;
00111   m_sessionFile = file_fopen( m_sessionFileName.c_str(), "r" );
00112   if ( !m_sessionFile ) setCreationTime = true;
00113   else fclose( m_sessionFile );
00114 
00115   m_sessionFile = file_fopen( m_sessionFileName.c_str(), "r+" );
00116   if ( !m_sessionFile ) m_sessionFile = file_fopen( m_sessionFileName.c_str(), "w+" );
00117   if ( !m_sessionFile ) throw ConfigError( "Could not open session file" );
00118   if ( setCreationTime ) setSession();
00119 
00120   setNextSenderMsgSeqNum( getNextSenderMsgSeqNum() );
00121   setNextTargetMsgSeqNum( getNextTargetMsgSeqNum() );
00122 
00123   QF_STACK_POP
00124 }

void FIX::FileStore::populateCache  )  [private]
 

Definition at line 126 of file FileStore.cpp.

References FIX::file_fopen(), FILE_FSCANF, m_cache, m_headerFileName, m_offsets, m_seqNumsFileName, m_sessionFileName, QF_STACK_POP, QF_STACK_PUSH, FIX::MemoryStore::setCreationTime(), FIX::MemoryStore::setNextSenderMsgSeqNum(), and FIX::MemoryStore::setNextTargetMsgSeqNum().

Referenced by open().

00127 { QF_STACK_PUSH(FileStore::populateCache)
00128 
00129   std::string msg;
00130   Message message;
00131 
00132   FILE* headerFile;
00133   headerFile = file_fopen( m_headerFileName.c_str(), "r+" );
00134   if ( headerFile )
00135   {
00136     int num, offset, size;
00137     while ( FILE_FSCANF( headerFile, "%d,%d,%d ", &num, &offset, &size ) == 3 )
00138       m_offsets[ num ] = std::make_pair( offset, size );
00139     fclose( headerFile );
00140   }
00141 
00142   FILE* seqNumsFile;
00143   seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "r+" );
00144   if ( seqNumsFile )
00145   {
00146     int sender, target;
00147     if ( FILE_FSCANF( seqNumsFile, "%d : %d", &sender, &target ) == 2 )
00148     {
00149       m_cache.setNextSenderMsgSeqNum( sender );
00150       m_cache.setNextTargetMsgSeqNum( target );
00151     }
00152     fclose( seqNumsFile );
00153   }
00154 
00155   FILE* sessionFile;
00156   sessionFile = file_fopen( m_sessionFileName.c_str(), "r+" );
00157   if ( sessionFile )
00158   {
00159     char time[ 22 ];
00160 #ifdef HAVE_FSCANF_S
00161     int result = FILE_FSCANF( sessionFile, "%s", time, 22 );
00162 #else
00163     int result = FILE_FSCANF( sessionFile, "%s", time );
00164 #endif
00165     if( result == 1 )
00166     {
00167       m_cache.setCreationTime( UtcTimeStampConvertor::convert( time, true ) );
00168     }
00169     fclose( sessionFile );
00170   }
00171 
00172   QF_STACK_POP
00173 }

bool FIX::FileStore::readFromFile int  offset,
int  size,
std::string &  msg
[private]
 

void FIX::FileStore::refresh  )  throw ( IOException ) [virtual]
 

Implements FIX::MessageStore.

Definition at line 295 of file FileStore.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

00296 { QF_STACK_PUSH(FileStore::refresh)
00297 
00298   m_cache.reset();
00299   open( false );
00300 
00301   QF_STACK_POP
00302 }

void FIX::FileStore::reset  )  throw ( IOException ) [virtual]
 

Implements FIX::MessageStore.

Definition at line 285 of file FileStore.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

00286 { QF_STACK_PUSH(FileStore::reset)
00287 
00288   m_cache.reset();
00289   open( true );
00290   setSession();
00291 
00292   QF_STACK_POP
00293 }

bool FIX::FileStore::set int  ,
const std::string & 
throw ( IOException ) [virtual]
 

Implements FIX::MessageStore.

Definition at line 194 of file FileStore.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

00196 { QF_STACK_PUSH(FileStore::set)
00197 
00198   if ( fseek( m_msgFile, 0, SEEK_END ) ) 
00199     throw IOException( "Cannot seek to end of " + m_msgFileName );
00200   if ( fseek( m_headerFile, 0, SEEK_END ) ) 
00201     throw IOException( "Cannot seek to end of " + m_headerFileName );
00202 
00203   int offset = ftell( m_msgFile );
00204   if ( offset < 0 ) 
00205     throw IOException( "Unable to get file pointer position from " + m_msgFileName );
00206   int size = msg.size();
00207 
00208   if ( fprintf( m_headerFile, "%d,%d,%d ", msgSeqNum, offset, size ) < 0 )
00209     throw IOException( "Unable to write to file " + m_headerFileName );
00210   m_offsets[ msgSeqNum ] = std::make_pair( offset, size );
00211   fwrite( msg.c_str(), sizeof( char ), msg.size(), m_msgFile );
00212   if ( ferror( m_msgFile ) ) 
00213     throw IOException( "Unable to write to file " + m_msgFileName );
00214   if ( fflush( m_msgFile ) == EOF ) 
00215     throw IOException( "Unable to flush file " + m_msgFileName );
00216   if ( fflush( m_headerFile ) == EOF ) 
00217     throw IOException( "Unable to flush file " + m_headerFileName );
00218   return true;
00219 
00220   QF_STACK_POP
00221 }

void FIX::FileStore::setNextSenderMsgSeqNum int  value  )  throw ( IOException ) [virtual]
 

Implements FIX::MessageStore.

Definition at line 251 of file FileStore.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

Referenced by open().

00252 { QF_STACK_PUSH(FileStore::setNextSenderMsgSeqNum)
00253   m_cache.setNextSenderMsgSeqNum( value );
00254   setSeqNum();
00255   QF_STACK_POP
00256 }

void FIX::FileStore::setNextTargetMsgSeqNum int  value  )  throw ( IOException ) [virtual]
 

Implements FIX::MessageStore.

Definition at line 258 of file FileStore.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

Referenced by open().

00259 { QF_STACK_PUSH(FileStore::setNextTargetMsgSeqNum)
00260   m_cache.setNextTargetMsgSeqNum( value );
00261   setSeqNum();
00262   QF_STACK_POP
00263 }

void FIX::FileStore::setSeqNum  )  [private]
 

Definition at line 304 of file FileStore.cpp.

References getNextSenderMsgSeqNum(), getNextTargetMsgSeqNum(), m_seqNumsFile, m_seqNumsFileName, QF_STACK_POP, and QF_STACK_PUSH.

00305 { QF_STACK_PUSH(FileStore::setSeqNum)
00306 
00307   rewind( m_seqNumsFile );
00308   fprintf( m_seqNumsFile, "%10.10d : %10.10d",
00309            getNextSenderMsgSeqNum(), getNextTargetMsgSeqNum() );
00310   if ( ferror( m_seqNumsFile ) ) 
00311     throw IOException( "Unable to write to file " + m_seqNumsFileName );
00312   if ( fflush( m_seqNumsFile ) ) 
00313     throw IOException( "Unable to flush file " + m_seqNumsFileName );
00314 
00315   QF_STACK_POP
00316 }

void FIX::FileStore::setSession  )  [private]
 

Definition at line 318 of file FileStore.cpp.

References FIX::MemoryStore::getCreationTime(), m_cache, m_sessionFile, m_sessionFileName, QF_STACK_POP, and QF_STACK_PUSH.

Referenced by open().

00319 { QF_STACK_PUSH(FileStore::setSession)
00320 
00321   rewind( m_sessionFile );
00322   fprintf( m_sessionFile, "%s",
00323            UtcTimeStampConvertor::convert( m_cache.getCreationTime() ).c_str() );
00324   if ( ferror( m_sessionFile ) ) 
00325     throw IOException( "Unable to write to file " + m_sessionFileName );
00326   if ( fflush( m_sessionFile ) ) 
00327     throw IOException( "Unable to flush file " + m_sessionFileName );
00328 
00329   QF_STACK_POP
00330 }


Member Data Documentation

MemoryStore FIX::FileStore::m_cache [private]
 

Definition at line 114 of file FileStore.h.

Referenced by populateCache(), and setSession().

FILE* FIX::FileStore::m_headerFile [private]
 

Definition at line 123 of file FileStore.h.

Referenced by open(), and ~FileStore().

std::string FIX::FileStore::m_headerFileName [private]
 

Definition at line 118 of file FileStore.h.

Referenced by FileStore(), open(), and populateCache().

FILE* FIX::FileStore::m_msgFile [private]
 

Definition at line 122 of file FileStore.h.

Referenced by open(), and ~FileStore().

std::string FIX::FileStore::m_msgFileName [private]
 

Definition at line 117 of file FileStore.h.

Referenced by FileStore(), and open().

NumToOffset FIX::FileStore::m_offsets [private]
 

Definition at line 115 of file FileStore.h.

Referenced by populateCache().

FILE* FIX::FileStore::m_seqNumsFile [private]
 

Definition at line 124 of file FileStore.h.

Referenced by open(), setSeqNum(), and ~FileStore().

std::string FIX::FileStore::m_seqNumsFileName [private]
 

Definition at line 119 of file FileStore.h.

Referenced by FileStore(), open(), populateCache(), and setSeqNum().

FILE* FIX::FileStore::m_sessionFile [private]
 

Definition at line 125 of file FileStore.h.

Referenced by open(), setSession(), and ~FileStore().

std::string FIX::FileStore::m_sessionFileName [private]
 

Definition at line 120 of file FileStore.h.

Referenced by FileStore(), open(), populateCache(), and setSession().


The documentation for this class was generated from the following files:
Generated on Mon Jul 24 19:36:44 2006 for QuickFIX by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2001