Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
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]

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 )
 
- Public Member Functions inherited from FIX::MessageStore
virtual ~MessageStore ()
 

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.

Constructor & Destructor Documentation

FIX::FileStore::FileStore ( std::string  path,
const SessionID s 
)

Definition at line 34 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().

35 : m_msgFile( 0 ), m_headerFile( 0 ), m_seqNumsFile( 0 ), m_sessionFile( 0 )
36 {
37  file_mkdir( path.c_str() );
38 
39  if ( path.empty() ) path = ".";
40  const std::string& begin =
41  s.getBeginString().getString();
42  const std::string& sender =
43  s.getSenderCompID().getString();
44  const std::string& target =
45  s.getTargetCompID().getString();
46  const std::string& qualifier =
47  s.getSessionQualifier();
48 
49  std::string sessionid = begin + "-" + sender + "-" + target;
50  if( qualifier.size() )
51  sessionid += "-" + qualifier;
52 
53  std::string prefix
54  = file_appendpath(path, sessionid + ".");
55 
56  m_msgFileName = prefix + "body";
57  m_headerFileName = prefix + "header";
58  m_seqNumsFileName = prefix + "seqnums";
59  m_sessionFileName = prefix + "session";
60 
61  try
62  {
63  open( false );
64  }
65  catch ( IOException & e )
66  {
67  throw ConfigError( e.what() );
68  }
69 }
std::string m_sessionFileName
Definition: FileStore.h:120
std::string m_msgFileName
Definition: FileStore.h:117
FILE * m_msgFile
Definition: FileStore.h:122
std::string file_appendpath(const std::string &path, const std::string &file)
Definition: Utility.cpp:528
void open(bool deleteFile)
Definition: FileStore.cpp:79
std::string m_headerFileName
Definition: FileStore.h:118
void file_mkdir(const char *path)
Definition: Utility.cpp:466
FILE * m_sessionFile
Definition: FileStore.h:125
FILE * m_headerFile
Definition: FileStore.h:123
FILE * m_seqNumsFile
Definition: FileStore.h:124
std::string m_seqNumsFileName
Definition: FileStore.h:119
FIX::FileStore::~FileStore ( )
virtual

Definition at line 71 of file FileStore.cpp.

References m_headerFile, m_msgFile, m_seqNumsFile, and m_sessionFile.

72 {
73  if( m_msgFile ) fclose( m_msgFile );
74  if( m_headerFile ) fclose( m_headerFile );
75  if( m_seqNumsFile ) fclose( m_seqNumsFile );
76  if( m_sessionFile ) fclose( m_sessionFile );
77 }
FILE * m_msgFile
Definition: FileStore.h:122
FILE * m_sessionFile
Definition: FileStore.h:125
FILE * m_headerFile
Definition: FileStore.h:123
FILE * m_seqNumsFile
Definition: FileStore.h:124

Member Function Documentation

void FIX::FileStore::get ( int  begin,
int  end,
std::vector< std::string > &  result 
) const
throw (IOException
)
virtual

Implements FIX::MessageStore.

Definition at line 207 of file FileStore.cpp.

210 {
211  result.clear();
212  std::string msg;
213  for ( int i = begin; i <= end; ++i )
214  {
215  if ( get( i, msg ) )
216  result.push_back( msg );
217  }
218 }
bool FIX::FileStore::get ( int  msgSeqNum,
std::string &  msg 
) const
throw (IOException
)
private

Definition at line 308 of file FileStore.cpp.

310 {
311  NumToOffset::const_iterator find = m_offsets.find( msgSeqNum );
312  if ( find == m_offsets.end() ) return false;
313  const OffsetSize& offset = find->second;
314  if ( fseek( m_msgFile, offset.first, SEEK_SET ) )
315  throw IOException( "Unable to seek in file " + m_msgFileName );
316  char* buffer = new char[ offset.second + 1 ];
317  size_t result = fread( buffer, sizeof( char ), offset.second, m_msgFile );
318  if ( ferror( m_msgFile ) || result != (size_t)offset.second )
319  {
320  delete [] buffer;
321  throw IOException( "Unable to read from file " + m_msgFileName );
322  }
323  buffer[ offset.second ] = 0;
324  msg = buffer;
325  delete [] buffer;
326  return true;
327 }
std::string m_msgFileName
Definition: FileStore.h:117
FILE * m_msgFile
Definition: FileStore.h:122
NumToOffset m_offsets
Definition: FileStore.h:115
std::pair< int, int > OffsetSize
Definition: FileStore.h:103
UtcTimeStamp FIX::FileStore::getCreationTime ( ) const
throw (IOException
)
virtual

Implements FIX::MessageStore.

Definition at line 254 of file FileStore.cpp.

References FIX::MemoryStore::getCreationTime(), and m_cache.

255 {
256  return m_cache.getCreationTime();
257 }
MemoryStore m_cache
Definition: FileStore.h:114
UtcTimeStamp getCreationTime() const
Definition: MessageStore.h:119
int FIX::FileStore::getNextSenderMsgSeqNum ( ) const
throw (IOException
)
virtual

Implements FIX::MessageStore.

Definition at line 220 of file FileStore.cpp.

References FIX::MemoryStore::getNextSenderMsgSeqNum(), and m_cache.

Referenced by open(), and setSeqNum().

221 {
223 }
int getNextSenderMsgSeqNum() const
Definition: MessageStore.h:104
MemoryStore m_cache
Definition: FileStore.h:114
int FIX::FileStore::getNextTargetMsgSeqNum ( ) const
throw (IOException
)
virtual

Implements FIX::MessageStore.

Definition at line 225 of file FileStore.cpp.

References FIX::MemoryStore::getNextTargetMsgSeqNum(), and m_cache.

Referenced by open(), and setSeqNum().

226 {
228 }
int getNextTargetMsgSeqNum() const
Definition: MessageStore.h:106
MemoryStore m_cache
Definition: FileStore.h:114
void FIX::FileStore::incrNextSenderMsgSeqNum ( )
throw (IOException
)
virtual

Implements FIX::MessageStore.

Definition at line 242 of file FileStore.cpp.

References FIX::MemoryStore::incrNextSenderMsgSeqNum(), m_cache, and setSeqNum().

243 {
245  setSeqNum();
246 }
void incrNextSenderMsgSeqNum()
Definition: MessageStore.h:112
MemoryStore m_cache
Definition: FileStore.h:114
void setSeqNum()
Definition: FileStore.cpp:286
void FIX::FileStore::incrNextTargetMsgSeqNum ( )
throw (IOException
)
virtual

Implements FIX::MessageStore.

Definition at line 248 of file FileStore.cpp.

References FIX::MemoryStore::incrNextTargetMsgSeqNum(), m_cache, and setSeqNum().

249 {
251  setSeqNum();
252 }
MemoryStore m_cache
Definition: FileStore.h:114
void incrNextTargetMsgSeqNum()
Definition: MessageStore.h:114
void setSeqNum()
Definition: FileStore.cpp:286
void FIX::FileStore::open ( bool  deleteFile)
private

Definition at line 79 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(), setNextSenderMsgSeqNum(), setNextTargetMsgSeqNum(), and setSession().

Referenced by FileStore(), refresh(), and reset().

80 {
81  if ( m_msgFile ) fclose( m_msgFile );
82  if ( m_headerFile ) fclose( m_headerFile );
83  if ( m_seqNumsFile ) fclose( m_seqNumsFile );
84  if ( m_sessionFile ) fclose( m_sessionFile );
85 
86  m_msgFile = 0;
87  m_headerFile = 0;
88  m_seqNumsFile = 0;
89  m_sessionFile = 0;
90 
91  if ( deleteFile )
92  {
93  file_unlink( m_msgFileName.c_str() );
94  file_unlink( m_headerFileName.c_str() );
95  file_unlink( m_seqNumsFileName.c_str() );
96  file_unlink( m_sessionFileName.c_str() );
97  }
98 
99  populateCache();
100  m_msgFile = file_fopen( m_msgFileName.c_str(), "r+" );
101  if ( !m_msgFile ) m_msgFile = file_fopen( m_msgFileName.c_str(), "w+" );
102  if ( !m_msgFile ) throw ConfigError( "Could not open body file: " + m_msgFileName );
103 
104  m_headerFile = file_fopen( m_headerFileName.c_str(), "r+" );
105  if ( !m_headerFile ) m_headerFile = file_fopen( m_headerFileName.c_str(), "w+" );
106  if ( !m_headerFile ) throw ConfigError( "Could not open header file: " + m_headerFileName );
107 
108  m_seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "r+" );
109  if ( !m_seqNumsFile ) m_seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "w+" );
110  if ( !m_seqNumsFile ) throw ConfigError( "Could not open seqnums file: " + m_seqNumsFileName );
111 
112  bool setCreationTime = false;
113  m_sessionFile = file_fopen( m_sessionFileName.c_str(), "r" );
114  if ( !m_sessionFile ) setCreationTime = true;
115  else fclose( m_sessionFile );
116 
117  m_sessionFile = file_fopen( m_sessionFileName.c_str(), "r+" );
118  if ( !m_sessionFile ) m_sessionFile = file_fopen( m_sessionFileName.c_str(), "w+" );
119  if ( !m_sessionFile ) throw ConfigError( "Could not open session file" );
120  if ( setCreationTime ) setSession();
121 
124 }
std::string m_sessionFileName
Definition: FileStore.h:120
std::string m_msgFileName
Definition: FileStore.h:117
void setNextTargetMsgSeqNum(int value)
Definition: FileStore.cpp:236
int getNextTargetMsgSeqNum() const
Definition: FileStore.cpp:225
FILE * m_msgFile
Definition: FileStore.h:122
int getNextSenderMsgSeqNum() const
Definition: FileStore.cpp:220
void populateCache()
Definition: FileStore.cpp:126
std::string m_headerFileName
Definition: FileStore.h:118
void setSession()
Definition: FileStore.cpp:297
void file_unlink(const char *path)
Definition: Utility.cpp:514
FILE * m_sessionFile
Definition: FileStore.h:125
void setNextSenderMsgSeqNum(int value)
Definition: FileStore.cpp:230
FILE * m_headerFile
Definition: FileStore.h:123
FILE * file_fopen(const char *path, const char *mode)
Definition: Utility.cpp:486
FILE * m_seqNumsFile
Definition: FileStore.h:124
std::string m_seqNumsFileName
Definition: FileStore.h:119
void FIX::FileStore::populateCache ( )
private

Definition at line 126 of file FileStore.cpp.

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

Referenced by open().

127 {
128  FILE* headerFile = file_fopen( m_headerFileName.c_str(), "r+" );
129  if ( headerFile )
130  {
131  int num, offset, size;
132  while ( FILE_FSCANF( headerFile, "%d,%d,%d ", &num, &offset, &size ) == 3 )
133  m_offsets[ num ] = std::make_pair( offset, size );
134  fclose( headerFile );
135  }
136 
137  FILE* seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "r+" );
138  if ( seqNumsFile )
139  {
140  int sender, target;
141  if ( FILE_FSCANF( seqNumsFile, "%d : %d", &sender, &target ) == 2 )
142  {
145  }
146  fclose( seqNumsFile );
147  }
148 
149  FILE* sessionFile = file_fopen( m_sessionFileName.c_str(), "r+" );
150  if ( sessionFile )
151  {
152  char time[ 22 ];
153 #ifdef HAVE_FSCANF_S
154  int result = FILE_FSCANF( sessionFile, "%s", time, 22 );
155 #else
156  int result = FILE_FSCANF( sessionFile, "%s", time );
157 #endif
158  if( result == 1 )
159  {
161  }
162  fclose( sessionFile );
163  }
164 }
std::string m_sessionFileName
Definition: FileStore.h:120
static std::string convert(const UtcTimeStamp &value, bool showMilliseconds=false)
#define FILE_FSCANF
Definition: Utility.h:178
void setNextSenderMsgSeqNum(int value)
Definition: MessageStore.h:108
void setNextTargetMsgSeqNum(int value)
Definition: MessageStore.h:110
MemoryStore m_cache
Definition: FileStore.h:114
std::string m_headerFileName
Definition: FileStore.h:118
FILE * file_fopen(const char *path, const char *mode)
Definition: Utility.cpp:486
NumToOffset m_offsets
Definition: FileStore.h:115
void setCreationTime(const UtcTimeStamp &creationTime)
Definition: MessageStore.h:117
std::string m_seqNumsFileName
Definition: FileStore.h:119
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 273 of file FileStore.cpp.

References m_cache, open(), and FIX::MemoryStore::reset().

274 {
275  try
276  {
277  m_cache.reset();
278  open( false );
279  }
280  catch( std::exception& e )
281  {
282  throw IOException( e.what() );
283  }
284 }
void open(bool deleteFile)
Definition: FileStore.cpp:79
MemoryStore m_cache
Definition: FileStore.h:114
void FIX::FileStore::reset ( )
throw (IOException
)
virtual

Implements FIX::MessageStore.

Definition at line 259 of file FileStore.cpp.

References m_cache, open(), FIX::MemoryStore::reset(), and setSession().

260 {
261  try
262  {
263  m_cache.reset();
264  open( true );
265  setSession();
266  }
267  catch( std::exception& e )
268  {
269  throw IOException( e.what() );
270  }
271 }
void open(bool deleteFile)
Definition: FileStore.cpp:79
MemoryStore m_cache
Definition: FileStore.h:114
void setSession()
Definition: FileStore.cpp:297
bool FIX::FileStore::set ( int  msgSeqNum,
const std::string &  msg 
)
throw (IOException
)
virtual

Implements FIX::MessageStore.

Definition at line 181 of file FileStore.cpp.

183 {
184  if ( fseek( m_msgFile, 0, SEEK_END ) )
185  throw IOException( "Cannot seek to end of " + m_msgFileName );
186  if ( fseek( m_headerFile, 0, SEEK_END ) )
187  throw IOException( "Cannot seek to end of " + m_headerFileName );
188 
189  int offset = ftell( m_msgFile );
190  if ( offset < 0 )
191  throw IOException( "Unable to get file pointer position from " + m_msgFileName );
192  int size = msg.size();
193 
194  if ( fprintf( m_headerFile, "%d,%d,%d ", msgSeqNum, offset, size ) < 0 )
195  throw IOException( "Unable to write to file " + m_headerFileName );
196  m_offsets[ msgSeqNum ] = std::make_pair( offset, size );
197  fwrite( msg.c_str(), sizeof( char ), msg.size(), m_msgFile );
198  if ( ferror( m_msgFile ) )
199  throw IOException( "Unable to write to file " + m_msgFileName );
200  if ( fflush( m_msgFile ) == EOF )
201  throw IOException( "Unable to flush file " + m_msgFileName );
202  if ( fflush( m_headerFile ) == EOF )
203  throw IOException( "Unable to flush file " + m_headerFileName );
204  return true;
205 }
std::string m_msgFileName
Definition: FileStore.h:117
FILE * m_msgFile
Definition: FileStore.h:122
std::string m_headerFileName
Definition: FileStore.h:118
FILE * m_headerFile
Definition: FileStore.h:123
NumToOffset m_offsets
Definition: FileStore.h:115
void FIX::FileStore::setNextSenderMsgSeqNum ( int  value)
throw (IOException
)
virtual

Implements FIX::MessageStore.

Definition at line 230 of file FileStore.cpp.

Referenced by open().

231 {
233  setSeqNum();
234 }
void setNextSenderMsgSeqNum(int value)
Definition: MessageStore.h:108
MemoryStore m_cache
Definition: FileStore.h:114
void setSeqNum()
Definition: FileStore.cpp:286
void FIX::FileStore::setNextTargetMsgSeqNum ( int  value)
throw (IOException
)
virtual

Implements FIX::MessageStore.

Definition at line 236 of file FileStore.cpp.

Referenced by open().

237 {
239  setSeqNum();
240 }
void setNextTargetMsgSeqNum(int value)
Definition: MessageStore.h:110
MemoryStore m_cache
Definition: FileStore.h:114
void setSeqNum()
Definition: FileStore.cpp:286
void FIX::FileStore::setSeqNum ( )
private

Definition at line 286 of file FileStore.cpp.

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

Referenced by incrNextSenderMsgSeqNum(), and incrNextTargetMsgSeqNum().

287 {
288  rewind( m_seqNumsFile );
289  fprintf( m_seqNumsFile, "%10.10d : %10.10d",
291  if ( ferror( m_seqNumsFile ) )
292  throw IOException( "Unable to write to file " + m_seqNumsFileName );
293  if ( fflush( m_seqNumsFile ) )
294  throw IOException( "Unable to flush file " + m_seqNumsFileName );
295 }
int getNextTargetMsgSeqNum() const
Definition: FileStore.cpp:225
int getNextSenderMsgSeqNum() const
Definition: FileStore.cpp:220
FILE * m_seqNumsFile
Definition: FileStore.h:124
std::string m_seqNumsFileName
Definition: FileStore.h:119
void FIX::FileStore::setSession ( )
private

Definition at line 297 of file FileStore.cpp.

References FIX::UtcTimeStampConvertor::convert(), FIX::MemoryStore::getCreationTime(), m_cache, m_sessionFile, and m_sessionFileName.

Referenced by open(), and reset().

298 {
299  rewind( m_sessionFile );
300  fprintf( m_sessionFile, "%s",
302  if ( ferror( m_sessionFile ) )
303  throw IOException( "Unable to write to file " + m_sessionFileName );
304  if ( fflush( m_sessionFile ) )
305  throw IOException( "Unable to flush file " + m_sessionFileName );
306 }
std::string m_sessionFileName
Definition: FileStore.h:120
static std::string convert(const UtcTimeStamp &value, bool showMilliseconds=false)
MemoryStore m_cache
Definition: FileStore.h:114
FILE * m_sessionFile
Definition: FileStore.h:125
UtcTimeStamp getCreationTime() const
Definition: MessageStore.h:119

Member Data Documentation

MemoryStore FIX::FileStore::m_cache
private
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 Sat Mar 29 2014 15:13:34 for QuickFIX by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2001