![]() |
File based implementation of MessageStore. More...
#include <FileStore.h>
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, OffsetSize > | NumToOffset |
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 |
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.
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.
FIX::FileStore::FileStore | ( | std::string | path, | |
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 if( m_msgFile ) fclose( m_msgFile ); 00075 if( m_headerFile ) fclose( m_headerFile ); 00076 if( m_seqNumsFile ) fclose( m_seqNumsFile ); 00077 if( m_sessionFile ) fclose( m_sessionFile ); 00078 }
bool FIX::FileStore::get | ( | int | msgSeqNum, | |
std::string & | msg | |||
) | const throw ( IOException ) [private] |
Definition at line 336 of file FileStore.cpp.
References get(), QF_STACK_POP, and QF_STACK_PUSH.
00338 { QF_STACK_PUSH(FileStore::get) 00339 00340 NumToOffset::const_iterator find = m_offsets.find( msgSeqNum ); 00341 if ( find == m_offsets.end() ) return false; 00342 const OffsetSize& offset = find->second; 00343 if ( fseek( m_msgFile, offset.first, SEEK_SET ) ) 00344 throw IOException( "Unable to seek in file " + m_msgFileName ); 00345 char* buffer = new char[ offset.second + 1 ]; 00346 fread( buffer, sizeof( char ), offset.second, m_msgFile ); 00347 if ( ferror( m_msgFile ) ) 00348 throw IOException( "Unable to read from file " + m_msgFileName ); 00349 buffer[ offset.second ] = 0; 00350 msg = buffer; 00351 delete [] buffer; 00352 return true; 00353 00354 QF_STACK_POP 00355 }
void FIX::FileStore::get | ( | int | begin, | |
int | end, | |||
std::vector< std::string > & | result | |||
) | const throw ( IOException ) [virtual] |
Implements FIX::MessageStore.
Definition at line 227 of file FileStore.cpp.
References QF_STACK_POP, and QF_STACK_PUSH.
Referenced by get().
00230 { QF_STACK_PUSH(FileStore::get) 00231 00232 result.clear(); 00233 std::string msg; 00234 for ( int i = begin; i <= end; ++i ) 00235 { 00236 if ( get( i, msg ) ) 00237 result.push_back( msg ); 00238 } 00239 00240 QF_STACK_POP 00241 }
UtcTimeStamp FIX::FileStore::getCreationTime | ( | ) | const throw ( IOException ) [virtual] |
Implements FIX::MessageStore.
Definition at line 283 of file FileStore.cpp.
References FIX::MemoryStore::getCreationTime(), m_cache, QF_STACK_POP, and QF_STACK_PUSH.
00284 { QF_STACK_PUSH(FileStore::getCreationTime) 00285 return m_cache.getCreationTime(); 00286 QF_STACK_POP 00287 }
int FIX::FileStore::getNextSenderMsgSeqNum | ( | ) | const throw ( IOException ) [virtual] |
Implements FIX::MessageStore.
Definition at line 243 of file FileStore.cpp.
References FIX::MemoryStore::getNextSenderMsgSeqNum(), m_cache, QF_STACK_POP, and QF_STACK_PUSH.
Referenced by open(), and setSeqNum().
00244 { QF_STACK_PUSH(FileStore::getNextSenderMsgSeqNum) 00245 return m_cache.getNextSenderMsgSeqNum(); 00246 QF_STACK_POP 00247 }
int FIX::FileStore::getNextTargetMsgSeqNum | ( | ) | const throw ( IOException ) [virtual] |
Implements FIX::MessageStore.
Definition at line 249 of file FileStore.cpp.
References FIX::MemoryStore::getNextTargetMsgSeqNum(), m_cache, QF_STACK_POP, and QF_STACK_PUSH.
Referenced by open(), and setSeqNum().
00250 { QF_STACK_PUSH(FileStore::getNextTargetMsgSeqNum) 00251 return m_cache.getNextTargetMsgSeqNum(); 00252 QF_STACK_POP 00253 }
void FIX::FileStore::incrNextSenderMsgSeqNum | ( | ) | throw ( IOException ) [virtual] |
Implements FIX::MessageStore.
Definition at line 269 of file FileStore.cpp.
References FIX::MemoryStore::incrNextSenderMsgSeqNum(), m_cache, QF_STACK_POP, QF_STACK_PUSH, and setSeqNum().
00270 { QF_STACK_PUSH(FileStore::incrNextSenderMsgSeqNum) 00271 m_cache.incrNextSenderMsgSeqNum(); 00272 setSeqNum(); 00273 QF_STACK_POP 00274 }
void FIX::FileStore::incrNextTargetMsgSeqNum | ( | ) | throw ( IOException ) [virtual] |
Implements FIX::MessageStore.
Definition at line 276 of file FileStore.cpp.
References FIX::MemoryStore::incrNextTargetMsgSeqNum(), m_cache, QF_STACK_POP, QF_STACK_PUSH, and setSeqNum().
00277 { QF_STACK_PUSH(FileStore::incrNextTargetMsgSeqNum) 00278 m_cache.incrNextTargetMsgSeqNum(); 00279 setSeqNum(); 00280 QF_STACK_POP 00281 }
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(), refresh(), and reset().
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 m_msgFile = 0; 00089 m_headerFile = 0; 00090 m_seqNumsFile = 0; 00091 m_sessionFile = 0; 00092 00093 if ( deleteFile ) 00094 { 00095 file_unlink( m_msgFileName.c_str() ); 00096 file_unlink( m_headerFileName.c_str() ); 00097 file_unlink( m_seqNumsFileName.c_str() ); 00098 file_unlink( m_sessionFileName.c_str() ); 00099 } 00100 00101 populateCache(); 00102 m_msgFile = file_fopen( m_msgFileName.c_str(), "r+" ); 00103 if ( !m_msgFile ) m_msgFile = file_fopen( m_msgFileName.c_str(), "w+" ); 00104 if ( !m_msgFile ) throw ConfigError( "Could not open body file: " + m_msgFileName ); 00105 00106 m_headerFile = file_fopen( m_headerFileName.c_str(), "r+" ); 00107 if ( !m_headerFile ) m_headerFile = file_fopen( m_headerFileName.c_str(), "w+" ); 00108 if ( !m_headerFile ) throw ConfigError( "Could not open header file: " + m_headerFileName ); 00109 00110 m_seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "r+" ); 00111 if ( !m_seqNumsFile ) m_seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "w+" ); 00112 if ( !m_seqNumsFile ) throw ConfigError( "Could not open seqnums file: " + m_seqNumsFileName ); 00113 00114 bool setCreationTime = false; 00115 m_sessionFile = file_fopen( m_sessionFileName.c_str(), "r" ); 00116 if ( !m_sessionFile ) setCreationTime = true; 00117 else fclose( m_sessionFile ); 00118 00119 m_sessionFile = file_fopen( m_sessionFileName.c_str(), "r+" ); 00120 if ( !m_sessionFile ) m_sessionFile = file_fopen( m_sessionFileName.c_str(), "w+" ); 00121 if ( !m_sessionFile ) throw ConfigError( "Could not open session file" ); 00122 if ( setCreationTime ) setSession(); 00123 00124 setNextSenderMsgSeqNum( getNextSenderMsgSeqNum() ); 00125 setNextTargetMsgSeqNum( getNextTargetMsgSeqNum() ); 00126 00127 QF_STACK_POP 00128 }
void FIX::FileStore::populateCache | ( | ) | [private] |
Definition at line 130 of file FileStore.cpp.
References FIX::UtcTimeStampConvertor::convert(), 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().
00131 { QF_STACK_PUSH(FileStore::populateCache) 00132 00133 std::string msg; 00134 Message message; 00135 00136 FILE* headerFile; 00137 headerFile = file_fopen( m_headerFileName.c_str(), "r+" ); 00138 if ( headerFile ) 00139 { 00140 int num, offset, size; 00141 while ( FILE_FSCANF( headerFile, "%d,%d,%d ", &num, &offset, &size ) == 3 ) 00142 m_offsets[ num ] = std::make_pair( offset, size ); 00143 fclose( headerFile ); 00144 } 00145 00146 FILE* seqNumsFile; 00147 seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "r+" ); 00148 if ( seqNumsFile ) 00149 { 00150 int sender, target; 00151 if ( FILE_FSCANF( seqNumsFile, "%d : %d", &sender, &target ) == 2 ) 00152 { 00153 m_cache.setNextSenderMsgSeqNum( sender ); 00154 m_cache.setNextTargetMsgSeqNum( target ); 00155 } 00156 fclose( seqNumsFile ); 00157 } 00158 00159 FILE* sessionFile; 00160 sessionFile = file_fopen( m_sessionFileName.c_str(), "r+" ); 00161 if ( sessionFile ) 00162 { 00163 char time[ 22 ]; 00164 #ifdef HAVE_FSCANF_S 00165 int result = FILE_FSCANF( sessionFile, "%s", time, 22 ); 00166 #else 00167 int result = FILE_FSCANF( sessionFile, "%s", time ); 00168 #endif 00169 if( result == 1 ) 00170 { 00171 m_cache.setCreationTime( UtcTimeStampConvertor::convert( time, true ) ); 00172 } 00173 fclose( sessionFile ); 00174 } 00175 00176 QF_STACK_POP 00177 }
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 299 of file FileStore.cpp.
References m_cache, open(), QF_STACK_POP, QF_STACK_PUSH, and FIX::MemoryStore::reset().
00300 { QF_STACK_PUSH(FileStore::refresh) 00301 00302 m_cache.reset(); 00303 open( false ); 00304 00305 QF_STACK_POP 00306 }
void FIX::FileStore::reset | ( | ) | throw ( IOException ) [virtual] |
Implements FIX::MessageStore.
Definition at line 289 of file FileStore.cpp.
References m_cache, open(), QF_STACK_POP, QF_STACK_PUSH, FIX::MemoryStore::reset(), and setSession().
00290 { QF_STACK_PUSH(FileStore::reset) 00291 00292 m_cache.reset(); 00293 open( true ); 00294 setSession(); 00295 00296 QF_STACK_POP 00297 }
bool FIX::FileStore::set | ( | int | msgSeqNum, | |
const std::string & | msg | |||
) | throw ( IOException ) [virtual] |
Implements FIX::MessageStore.
Definition at line 198 of file FileStore.cpp.
References QF_STACK_POP, and QF_STACK_PUSH.
00200 { QF_STACK_PUSH(FileStore::set) 00201 00202 if ( fseek( m_msgFile, 0, SEEK_END ) ) 00203 throw IOException( "Cannot seek to end of " + m_msgFileName ); 00204 if ( fseek( m_headerFile, 0, SEEK_END ) ) 00205 throw IOException( "Cannot seek to end of " + m_headerFileName ); 00206 00207 int offset = ftell( m_msgFile ); 00208 if ( offset < 0 ) 00209 throw IOException( "Unable to get file pointer position from " + m_msgFileName ); 00210 int size = msg.size(); 00211 00212 if ( fprintf( m_headerFile, "%d,%d,%d ", msgSeqNum, offset, size ) < 0 ) 00213 throw IOException( "Unable to write to file " + m_headerFileName ); 00214 m_offsets[ msgSeqNum ] = std::make_pair( offset, size ); 00215 fwrite( msg.c_str(), sizeof( char ), msg.size(), m_msgFile ); 00216 if ( ferror( m_msgFile ) ) 00217 throw IOException( "Unable to write to file " + m_msgFileName ); 00218 if ( fflush( m_msgFile ) == EOF ) 00219 throw IOException( "Unable to flush file " + m_msgFileName ); 00220 if ( fflush( m_headerFile ) == EOF ) 00221 throw IOException( "Unable to flush file " + m_headerFileName ); 00222 return true; 00223 00224 QF_STACK_POP 00225 }
void FIX::FileStore::setNextSenderMsgSeqNum | ( | int | value | ) | throw ( IOException ) [virtual] |
Implements FIX::MessageStore.
Definition at line 255 of file FileStore.cpp.
References QF_STACK_POP, and QF_STACK_PUSH.
Referenced by open().
00256 { QF_STACK_PUSH(FileStore::setNextSenderMsgSeqNum) 00257 m_cache.setNextSenderMsgSeqNum( value ); 00258 setSeqNum(); 00259 QF_STACK_POP 00260 }
void FIX::FileStore::setNextTargetMsgSeqNum | ( | int | value | ) | throw ( IOException ) [virtual] |
Implements FIX::MessageStore.
Definition at line 262 of file FileStore.cpp.
References QF_STACK_POP, and QF_STACK_PUSH.
Referenced by open().
00263 { QF_STACK_PUSH(FileStore::setNextTargetMsgSeqNum) 00264 m_cache.setNextTargetMsgSeqNum( value ); 00265 setSeqNum(); 00266 QF_STACK_POP 00267 }
void FIX::FileStore::setSeqNum | ( | ) | [private] |
Definition at line 308 of file FileStore.cpp.
References getNextSenderMsgSeqNum(), getNextTargetMsgSeqNum(), m_seqNumsFile, m_seqNumsFileName, QF_STACK_POP, and QF_STACK_PUSH.
Referenced by incrNextSenderMsgSeqNum(), and incrNextTargetMsgSeqNum().
00309 { QF_STACK_PUSH(FileStore::setSeqNum) 00310 00311 rewind( m_seqNumsFile ); 00312 fprintf( m_seqNumsFile, "%10.10d : %10.10d", 00313 getNextSenderMsgSeqNum(), getNextTargetMsgSeqNum() ); 00314 if ( ferror( m_seqNumsFile ) ) 00315 throw IOException( "Unable to write to file " + m_seqNumsFileName ); 00316 if ( fflush( m_seqNumsFile ) ) 00317 throw IOException( "Unable to flush file " + m_seqNumsFileName ); 00318 00319 QF_STACK_POP 00320 }
void FIX::FileStore::setSession | ( | ) | [private] |
Definition at line 322 of file FileStore.cpp.
References FIX::UtcTimeStampConvertor::convert(), FIX::MemoryStore::getCreationTime(), m_cache, m_sessionFile, m_sessionFileName, QF_STACK_POP, and QF_STACK_PUSH.
Referenced by open(), and reset().
00323 { QF_STACK_PUSH(FileStore::setSession) 00324 00325 rewind( m_sessionFile ); 00326 fprintf( m_sessionFile, "%s", 00327 UtcTimeStampConvertor::convert( m_cache.getCreationTime() ).c_str() ); 00328 if ( ferror( m_sessionFile ) ) 00329 throw IOException( "Unable to write to file " + m_sessionFileName ); 00330 if ( fflush( m_sessionFile ) ) 00331 throw IOException( "Unable to flush file " + m_sessionFileName ); 00332 00333 QF_STACK_POP 00334 }
MemoryStore FIX::FileStore::m_cache [private] |
Definition at line 114 of file FileStore.h.
Referenced by getCreationTime(), getNextSenderMsgSeqNum(), getNextTargetMsgSeqNum(), incrNextSenderMsgSeqNum(), incrNextTargetMsgSeqNum(), populateCache(), refresh(), reset(), 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().