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< long, std::size_t > 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 96 of file FileStore.h.

Member Typedef Documentation

◆ NumToOffset

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

Definition at line 123 of file FileStore.h.

◆ OffsetSize

typedef std::pair< long, std::size_t > FIX::FileStore::OffsetSize
private

Definition at line 121 of file FileStore.h.

Constructor & Destructor Documentation

◆ FileStore()

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

Definition at line 50 of file FileStore.cpp.

62  {
63  open( false );
64  }
65  catch ( IOException & e )
66  {
67  throw ConfigError( e.what() );
68  }
69 }
70 
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 }
78 
79 void FileStore::open( bool deleteFile )
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 

◆ ~FileStore()

FIX::FileStore::~FileStore ( )
virtual

Definition at line 87 of file FileStore.cpp.

92  {
93  file_unlink( m_msgFileName.c_str() );

References FIX::file_unlink(), m_headerFileName, and m_msgFileName.

Member Function Documentation

◆ get() [1/2]

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

Implements FIX::MessageStore.

Definition at line 239 of file FileStore.cpp.

242 {
244 }
245 
246 void FileStore::setNextSenderMsgSeqNum( int value ) throw ( IOException )
247 {
249  setSeqNum();
250 }

◆ get() [2/2]

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

Definition at line 340 of file FileStore.cpp.

◆ getCreationTime()

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

Implements FIX::MessageStore.

Definition at line 286 of file FileStore.cpp.

290 {

◆ getNextSenderMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 252 of file FileStore.cpp.

253 {
255  setSeqNum();

◆ getNextTargetMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 257 of file FileStore.cpp.

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

◆ incrNextSenderMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 274 of file FileStore.cpp.

276 {
277  try
278  {

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

◆ incrNextTargetMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 280 of file FileStore.cpp.

284  {

◆ open()

void FIX::FileStore::open ( bool  deleteFile)
private

Definition at line 95 of file FileStore.cpp.

102  : " + 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 
122  setNextSenderMsgSeqNum( getNextSenderMsgSeqNum() );
123  setNextTargetMsgSeqNum( getNextTargetMsgSeqNum() );
124 }
125 
126 void FileStore::populateCache()
127 {
128  FILE* headerFile = file_fopen( m_headerFileName.c_str(), "r+" );
129  if ( headerFile )
130  {
131  int num;
132  long offset;
133  std::size_t size;
134 
135  while (FILE_FSCANF(headerFile, "%d,%ld,%lu ", &num, &offset, &size) == 3)
136  {
137  std::pair<NumToOffset::iterator, bool> it =
138  m_offsets.insert(NumToOffset::value_type(num, std::make_pair(offset, size)));
139  //std::cout << it.first->second.first << " --- " << it.first->second.second << '\n';
140  if (it.second == false)

Referenced by reset().

◆ populateCache()

void FIX::FileStore::populateCache ( )
private

Definition at line 142 of file FileStore.cpp.

150  {
151  int sender, target;
152  if ( FILE_FSCANF( seqNumsFile, "%d : %d", &sender, &target ) == 2 )
153  {
156  }
157  fclose( seqNumsFile );
158  }
159 
160  FILE* sessionFile = file_fopen( m_sessionFileName.c_str(), "r+" );
161  if ( sessionFile )
162  {
163  char time[ 22 ];
164 #ifdef HAVE_FSCANF_S
165  int result = FILE_FSCANF( sessionFile, "%s", time, 22 );
166 #else
167  int result = FILE_FSCANF( sessionFile, "%s", time );
168 #endif
169  if( result == 1 )
170  {
172  }
173  fclose( sessionFile );
174  }
175 }
176 
177 MessageStore* FileStoreFactory::create( const SessionID& s )
178 {
179  if ( m_path.size() ) return new FileStore( m_path, s );
180 
181  std::string path;
182  Dictionary settings = m_settings.get( s );
183  path = settings.getString( FILE_STORE_PATH );
184  return new FileStore( path, s );
185 }
186 
187 void FileStoreFactory::destroy( MessageStore* pStore )
188 {
189  delete pStore;
190 }
191 

◆ readFromFile()

bool FIX::FileStore::readFromFile ( int  offset,
int  size,
std::string &  msg 
)
private

◆ refresh()

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

Implements FIX::MessageStore.

Definition at line 305 of file FileStore.cpp.

305  : %10.10d",
306  getNextSenderMsgSeqNum(), getNextTargetMsgSeqNum() );
307  if ( ferror( m_seqNumsFile ) )
308  throw IOException( "Unable to write to file " + m_seqNumsFileName );
309  if ( fflush( m_seqNumsFile ) )
310  throw IOException( "Unable to flush file " + m_seqNumsFileName );
311 }
312 
313 void FileStore::setSession()
314 {
315  rewind( m_sessionFile );
316  fprintf( m_sessionFile, "%s",

◆ reset()

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

Implements FIX::MessageStore.

Definition at line 291 of file FileStore.cpp.

292  {
293  m_cache.reset();
294  open( false );
295  }
296  catch( std::exception& e )
297  {
298  throw IOException( e.what() );
299  }
300 }
301 
303 {

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

◆ set()

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

Implements FIX::MessageStore.

Definition at line 208 of file FileStore.cpp.

210  {
211  it.first->second = std::make_pair(offset, size);
212  }
213  fwrite( msg.c_str(), sizeof( char ), msg.size(), m_msgFile );
214  if ( ferror( m_msgFile ) )
215  throw IOException( "Unable to write to file " + m_msgFileName );
216  if ( fflush( m_msgFile ) == EOF )
217  throw IOException( "Unable to flush file " + m_msgFileName );
218  if ( fflush( m_headerFile ) == EOF )
219  throw IOException( "Unable to flush file " + m_headerFileName );
220  return true;
221 }
222 
223 void FileStore::get( int begin, int end,
224  std::vector < std::string > & result ) const
225 throw ( IOException )
226 {
227  result.clear();
228  std::string msg;
229  for ( int i = begin; i <= end; ++i )
230  {
231  if ( get( i, msg ) )
232  result.push_back( msg );
233  }
234 }
235 
236 int FileStore::getNextSenderMsgSeqNum() const throw ( IOException )
237 {

◆ setNextSenderMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 262 of file FileStore.cpp.

◆ setNextTargetMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 268 of file FileStore.cpp.

271 {
272  return m_cache.getCreationTime();

◆ setSeqNum()

void FIX::FileStore::setSeqNum ( )
private

Definition at line 318 of file FileStore.cpp.

326 {
327  NumToOffset::const_iterator find = m_offsets.find( msgSeqNum );

Referenced by getNextTargetMsgSeqNum().

◆ setSession()

void FIX::FileStore::setSession ( )
private

Definition at line 329 of file FileStore.cpp.

335  {
336  delete [] buffer;
337  throw IOException( "Unable to read from file " + m_msgFileName );
338  }

Member Data Documentation

◆ m_cache

MemoryStore FIX::FileStore::m_cache
private

Definition at line 133 of file FileStore.h.

Referenced by getNextTargetMsgSeqNum(), incrNextSenderMsgSeqNum(), and reset().

◆ m_headerFile

FILE* FIX::FileStore::m_headerFile
private

Definition at line 142 of file FileStore.h.

◆ m_headerFileName

std::string FIX::FileStore::m_headerFileName
private

Definition at line 137 of file FileStore.h.

Referenced by ~FileStore().

◆ m_msgFile

FILE* FIX::FileStore::m_msgFile
private

Definition at line 141 of file FileStore.h.

◆ m_msgFileName

std::string FIX::FileStore::m_msgFileName
private

Definition at line 136 of file FileStore.h.

Referenced by ~FileStore().

◆ m_offsets

NumToOffset FIX::FileStore::m_offsets
private

Definition at line 134 of file FileStore.h.

◆ m_seqNumsFile

FILE* FIX::FileStore::m_seqNumsFile
private

Definition at line 143 of file FileStore.h.

◆ m_seqNumsFileName

std::string FIX::FileStore::m_seqNumsFileName
private

Definition at line 138 of file FileStore.h.

◆ m_sessionFile

FILE* FIX::FileStore::m_sessionFile
private

Definition at line 144 of file FileStore.h.

◆ m_sessionFileName

std::string FIX::FileStore::m_sessionFileName
private

Definition at line 139 of file FileStore.h.


The documentation for this class was generated from the following files:
FIX::FileStore::m_sessionFileName
std::string m_sessionFileName
Definition: FileStore.h:139
FIX::FileStore::m_msgFileName
std::string m_msgFileName
Definition: FileStore.h:136
FIX::FileStoreFactory::create
MessageStore * create(const SessionID &)
Definition: FileStore.cpp:193
FIX::FileStore::m_cache
MemoryStore m_cache
Definition: FileStore.h:133
FIX::FileStore::FileStore
FileStore(std::string, const SessionID &s)
Definition: FileStore.cpp:50
FIX::FileStore::m_msgFile
FILE * m_msgFile
Definition: FileStore.h:141
FIX::FileStore::~FileStore
virtual ~FileStore()
Definition: FileStore.cpp:87
FIX::MemoryStore::setNextTargetMsgSeqNum
void setNextTargetMsgSeqNum(int value)
Definition: MessageStore.h:125
FIX::MemoryStore::getNextTargetMsgSeqNum
int getNextTargetMsgSeqNum() const
Definition: MessageStore.h:121
FIX::FileStore::getNextSenderMsgSeqNum
int getNextSenderMsgSeqNum() const
Definition: FileStore.cpp:252
FIX::MemoryStore::incrNextSenderMsgSeqNum
void incrNextSenderMsgSeqNum()
Definition: MessageStore.h:127
FIX::FILE_STORE_PATH
const char FILE_STORE_PATH[]
Definition: SessionSettings.h:95
FIX::FileStore::get
void get(int, int, std::vector< std::string > &) const
Definition: FileStore.cpp:239
FIX::MemoryStore::setNextSenderMsgSeqNum
void setNextSenderMsgSeqNum(int value)
Definition: MessageStore.h:123
FIX::FileStore::m_seqNumsFile
FILE * m_seqNumsFile
Definition: FileStore.h:143
FIX::FileStore::m_headerFileName
std::string m_headerFileName
Definition: FileStore.h:137
FIX::FileStore::m_sessionFile
FILE * m_sessionFile
Definition: FileStore.h:144
FIX::UtcTimeStampConvertor::convert
static std::string convert(const UtcTimeStamp &value, int precision=0)
Definition: FieldConvertors.h:449
FIX::file_unlink
void file_unlink(const char *path)
Definition: Utility.cpp:552
FIX::MemoryStore::incrNextTargetMsgSeqNum
void incrNextTargetMsgSeqNum()
Definition: MessageStore.h:129
FIX::MemoryStore::getCreationTime
UtcTimeStamp getCreationTime() const
Definition: MessageStore.h:134
FIX::MemoryStore::reset
void reset()
Definition: MessageStore.h:137
FIX::FileStore::setSeqNum
void setSeqNum()
Definition: FileStore.cpp:318
FIX::file_fopen
FILE * file_fopen(const char *path, const char *mode)
Definition: Utility.cpp:524
FIX::FileStore::m_offsets
NumToOffset m_offsets
Definition: FileStore.h:134
FILE_FSCANF
#define FILE_FSCANF
Definition: Utility.h:214
FIX::MemoryStore::setCreationTime
void setCreationTime(const UtcTimeStamp &creationTime)
Definition: MessageStore.h:132
FIX::FileStore::open
void open(bool deleteFile)
Definition: FileStore.cpp:95
FIX::FileStoreFactory::destroy
void destroy(MessageStore *)
Definition: FileStore.cpp:203
FIX::FileStore::setNextSenderMsgSeqNum
void setNextSenderMsgSeqNum(int value)
Definition: FileStore.cpp:262
FIX::FileStore::m_headerFile
FILE * m_headerFile
Definition: FileStore.h:142

Generated on Thu Apr 23 2020 04:32:03 for QuickFIX by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2001