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 98 of file FileStore.h.

Member Typedef Documentation

◆ NumToOffset

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

Definition at line 125 of file FileStore.h.

◆ OffsetSize

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

Definition at line 123 of file FileStore.h.

Constructor & Destructor Documentation

◆ FileStore()

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

Definition at line 51 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 
86  m_msgFile = 0;

◆ ~FileStore()

FIX::FileStore::~FileStore ( )
virtual

Definition at line 88 of file FileStore.cpp.

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

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

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 240 of file FileStore.cpp.

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

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

◆ get() [2/2]

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

Definition at line 341 of file FileStore.cpp.

◆ getCreationTime()

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

Implements FIX::MessageStore.

Definition at line 287 of file FileStore.cpp.

290 {

◆ getNextSenderMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 253 of file FileStore.cpp.

253 {
255  setSeqNum();
256 }

◆ getNextTargetMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 258 of file FileStore.cpp.

259 {
261  setSeqNum();

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

◆ incrNextSenderMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 275 of file FileStore.cpp.

276 {
277  try
278  {
279  m_cache.reset();

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

◆ incrNextTargetMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 281 of file FileStore.cpp.

284  {
285  throw IOException( e.what() );

◆ open()

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

Definition at line 96 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)
141  {

Referenced by incrNextSenderMsgSeqNum(), and reset().

◆ populateCache()

void FIX::FileStore::populateCache ( )
private

Definition at line 143 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 
192 bool FileStore::set( int msgSeqNum, const std::string& msg )

◆ 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 306 of file FileStore.cpp.

314 {
315  rewind( m_sessionFile );
316  fprintf( m_sessionFile, "%s",

◆ reset()

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

Implements FIX::MessageStore.

Definition at line 292 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 {
304  rewind( m_seqNumsFile );

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 209 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 263 of file FileStore.cpp.

265 {
267  setSeqNum();

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

◆ setNextTargetMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 269 of file FileStore.cpp.

271 {
272  return m_cache.getCreationTime();
273 }

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

◆ setSeqNum()

void FIX::FileStore::setSeqNum ( )
private

Definition at line 319 of file FileStore.cpp.

326 {
327  NumToOffset::const_iterator find = m_offsets.find( msgSeqNum );
328  if ( find == m_offsets.end() ) return false;

Referenced by getNextTargetMsgSeqNum(), and setNextSenderMsgSeqNum().

◆ setSession()

void FIX::FileStore::setSession ( )
private

Definition at line 330 of file FileStore.cpp.

335  {
336  delete [] buffer;
337  throw IOException( "Unable to read from file " + m_msgFileName );
338  }
339  buffer[ offset.second ] = 0;

Member Data Documentation

◆ m_cache

MemoryStore FIX::FileStore::m_cache
private

◆ m_headerFile

FILE* FIX::FileStore::m_headerFile
private

Definition at line 144 of file FileStore.h.

◆ m_headerFileName

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

Definition at line 139 of file FileStore.h.

Referenced by ~FileStore().

◆ m_msgFile

FILE* FIX::FileStore::m_msgFile
private

Definition at line 143 of file FileStore.h.

◆ m_msgFileName

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

Definition at line 138 of file FileStore.h.

Referenced by ~FileStore().

◆ m_offsets

NumToOffset FIX::FileStore::m_offsets
private

Definition at line 136 of file FileStore.h.

◆ m_seqNumsFile

FILE* FIX::FileStore::m_seqNumsFile
private

Definition at line 145 of file FileStore.h.

◆ m_seqNumsFileName

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

Definition at line 140 of file FileStore.h.

Referenced by ~FileStore().

◆ m_sessionFile

FILE* FIX::FileStore::m_sessionFile
private

Definition at line 146 of file FileStore.h.

◆ m_sessionFileName

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

Definition at line 141 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:141
FIX::FileStore::m_msgFileName
std::string m_msgFileName
Definition: FileStore.h:138
FIX::FileStoreFactory::create
MessageStore * create(const SessionID &)
Definition: FileStore.cpp:194
FIX::FileStore::m_cache
MemoryStore m_cache
Definition: FileStore.h:135
FIX::FileStore::FileStore
FileStore(std::string, const SessionID &s)
Definition: FileStore.cpp:51
FIX::FileStore::m_msgFile
FILE * m_msgFile
Definition: FileStore.h:143
FIX::FileStore::~FileStore
virtual ~FileStore()
Definition: FileStore.cpp:88
FIX::MemoryStore::getNextSenderMsgSeqNum
int getNextSenderMsgSeqNum() const
Definition: MessageStore.h:121
FIX::MemoryStore::setNextTargetMsgSeqNum
void setNextTargetMsgSeqNum(int value)
Definition: MessageStore.h:127
FIX::MemoryStore::getNextTargetMsgSeqNum
int getNextTargetMsgSeqNum() const
Definition: MessageStore.h:123
FIX::FileStore::getNextSenderMsgSeqNum
int getNextSenderMsgSeqNum() const
Definition: FileStore.cpp:253
FIX::MemoryStore::incrNextSenderMsgSeqNum
void incrNextSenderMsgSeqNum()
Definition: MessageStore.h:129
FIX::FILE_STORE_PATH
const char FILE_STORE_PATH[]
Definition: SessionSettings.h:97
FIX::FileStore::get
void get(int, int, std::vector< std::string > &) const
Definition: FileStore.cpp:240
FIX::MemoryStore::setNextSenderMsgSeqNum
void setNextSenderMsgSeqNum(int value)
Definition: MessageStore.h:125
FIX::FileStore::m_seqNumsFile
FILE * m_seqNumsFile
Definition: FileStore.h:145
FIX::FileStore::m_headerFileName
std::string m_headerFileName
Definition: FileStore.h:139
FIX::FileStore::m_sessionFile
FILE * m_sessionFile
Definition: FileStore.h:146
FIX::UtcTimeStampConvertor::convert
static std::string convert(const UtcTimeStamp &value, int precision=0)
Definition: FieldConvertors.h:451
FIX::file_unlink
void file_unlink(const char *path)
Definition: Utility.cpp:554
FIX::MemoryStore::incrNextTargetMsgSeqNum
void incrNextTargetMsgSeqNum()
Definition: MessageStore.h:131
FIX::MemoryStore::getCreationTime
UtcTimeStamp getCreationTime() const
Definition: MessageStore.h:136
FIX::MemoryStore::reset
void reset()
Definition: MessageStore.h:139
FIX::FileStore::setSeqNum
void setSeqNum()
Definition: FileStore.cpp:319
FIX::file_fopen
FILE * file_fopen(const char *path, const char *mode)
Definition: Utility.cpp:526
FIX::FileStore::m_offsets
NumToOffset m_offsets
Definition: FileStore.h:136
FILE_FSCANF
#define FILE_FSCANF
Definition: Utility.h:215
FIX::MemoryStore::setCreationTime
void setCreationTime(const UtcTimeStamp &creationTime)
Definition: MessageStore.h:134
FIX::FileStore::open
void open(bool deleteFile)
Definition: FileStore.cpp:96
FIX::FileStore::set
bool set(int, const std::string &)
Definition: FileStore.cpp:209
FIX::FileStoreFactory::destroy
void destroy(MessageStore *)
Definition: FileStore.cpp:204
FIX::FileStore::setNextSenderMsgSeqNum
void setNextSenderMsgSeqNum(int value)
Definition: FileStore.cpp:263
FIX::FileStore::m_headerFile
FILE * m_headerFile
Definition: FileStore.h:144

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001