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
 
FILEm_msgFile
 
FILEm_headerFile
 
FILEm_seqNumsFile
 
FILEm_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

◆ NumToOffset

Definition at line 108 of file FileStore.h.

◆ OffsetSize

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

Definition at line 106 of file FileStore.h.

Constructor & Destructor Documentation

◆ FileStore()

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

Definition at line 34 of file FileStore.cpp.

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
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}
FILE * m_headerFile
Definition FileStore.h:127
std::string m_seqNumsFileName
Definition FileStore.h:123
FILE * m_sessionFile
Definition FileStore.h:129
FILE * m_seqNumsFile
Definition FileStore.h:128
std::string m_sessionFileName
Definition FileStore.h:124
void open(bool deleteFile)
Definition FileStore.cpp:79
std::string m_headerFileName
Definition FileStore.h:122
std::string m_msgFileName
Definition FileStore.h:121
FILE * m_msgFile
Definition FileStore.h:126
bool set(int, const std::string &)
void file_mkdir(const char *path)
Definition Utility.cpp:489
std::string file_appendpath(const std::string &path, const std::string &file)
Definition Utility.cpp:551

References FIX::file_appendpath(), FIX::file_mkdir(), m_headerFileName, m_msgFileName, m_seqNumsFileName, m_sessionFileName, open(), and set().

◆ ~FileStore()

FIX::FileStore::~FileStore ( )
virtual

Definition at line 71 of file FileStore.cpp.

References m_headerFile, m_msgFile, m_seqNumsFile, m_sessionFile, and set().

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

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}
void get(int, int, std::vector< std::string > &) const

◆ get() [2/2]

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

Definition at line 324 of file FileStore.cpp.

326{
327 NumToOffset::const_iterator find = m_offsets.find( msgSeqNum );
328 if ( find == m_offsets.end() ) return false;
329 const OffsetSize& offset = find->second;
330 if ( fseek( m_msgFile, offset.first, SEEK_SET ) )
331 throw IOException( "Unable to seek in file " + m_msgFileName );
332 char* buffer = new char[ offset.second + 1 ];
333 size_t result = fread( buffer, sizeof( char ), offset.second, m_msgFile );
334 if ( ferror( m_msgFile ) || result != (size_t)offset.second )
335 {
336 delete [] buffer;
337 throw IOException( "Unable to read from file " + m_msgFileName );
338 }
339 buffer[ offset.second ] = 0;
340 msg = buffer;
341 delete [] buffer;
342 return true;
343}
std::pair< long, std::size_t > OffsetSize
Definition FileStore.h:106
NumToOffset m_offsets
Definition FileStore.h:119

◆ getCreationTime()

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

Implements FIX::MessageStore.

Definition at line 270 of file FileStore.cpp.

271{
272 return m_cache.getCreationTime();
273}
MemoryStore m_cache
Definition FileStore.h:118
UtcTimeStamp getCreationTime() const

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

◆ getNextSenderMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 236 of file FileStore.cpp.

237{
239}
int getNextSenderMsgSeqNum() const

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

Referenced by open(), and setSeqNum().

◆ getNextTargetMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 241 of file FileStore.cpp.

242{
244}
int getNextTargetMsgSeqNum() const

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

Referenced by open(), and setSeqNum().

◆ incrNextSenderMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 258 of file FileStore.cpp.

259{
261 setSeqNum();
262}
void incrNextSenderMsgSeqNum()

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

◆ incrNextTargetMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 264 of file FileStore.cpp.

265{
267 setSeqNum();
268}
void incrNextTargetMsgSeqNum()

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

◆ open()

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

Definition at line 79 of file FileStore.cpp.

80{
81 if ( m_msgFile ) fclose( m_msgFile );
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() );
97 }
98
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;
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}
void setNextSenderMsgSeqNum(int value)
int getNextSenderMsgSeqNum() const
void populateCache()
void setNextTargetMsgSeqNum(int value)
int getNextTargetMsgSeqNum() const
void file_unlink(const char *path)
Definition Utility.cpp:537
FILE * file_fopen(const char *path, const char *mode)
Definition Utility.cpp:509

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(), set(), setNextSenderMsgSeqNum(), setNextTargetMsgSeqNum(), and setSession().

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

◆ populateCache()

void FIX::FileStore::populateCache ( )
private

Definition at line 126 of file FileStore.cpp.

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 {
142 it.first->second = std::make_pair(offset, size);
143 }
144 }
146 }
147
148 FILE* seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "r+" );
149 if ( seqNumsFile )
150 {
151 int sender, target;
152 if ( FILE_FSCANF( seqNumsFile, "%d : %d", &sender, &target ) == 2 )
153 {
156 }
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 }
174 }
175}
#define FILE_FSCANF
Definition Utility.h:215
void setCreationTime(const UtcTimeStamp &creationTime)
void setNextTargetMsgSeqNum(int value)
void setNextSenderMsgSeqNum(int value)
static std::string convert(const UtcTimeStamp &value, int precision=0)

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

Referenced by open().

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

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

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

◆ reset()

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

Implements FIX::MessageStore.

Definition at line 275 of file FileStore.cpp.

276{
277 try
278 {
279 m_cache.reset();
280 open( true );
281 setSession();
282 }
283 catch( std::exception& e )
284 {
285 throw IOException( e.what() );
286 }
287}

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

◆ set()

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

Implements FIX::MessageStore.

Definition at line 192 of file FileStore.cpp.

194{
195 if ( fseek( m_msgFile, 0, SEEK_END ) )
196 throw IOException( "Cannot seek to end of " + m_msgFileName );
197 if ( fseek( m_headerFile, 0, SEEK_END ) )
198 throw IOException( "Cannot seek to end of " + m_headerFileName );
199
200 long offset = ftell( m_msgFile );
201 if ( offset < 0 )
202 throw IOException( "Unable to get file pointer position from " + m_msgFileName );
203 std::size_t size = msg.size();
204
205 if ( fprintf( m_headerFile, "%d,%ld,%lu ", msgSeqNum, offset, size ) < 0 )
206 throw IOException( "Unable to write to file " + m_headerFileName );
207 std::pair<NumToOffset::iterator, bool> it =
208 m_offsets.insert(NumToOffset::value_type(msgSeqNum, std::make_pair(offset, size)));
209 if (it.second == false)
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}

Referenced by FileStore(), open(), populateCache(), refresh(), reset(), setSeqNum(), setSession(), and ~FileStore().

◆ setNextSenderMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 246 of file FileStore.cpp.

247{
249 setSeqNum();
250}

Referenced by open().

◆ setNextTargetMsgSeqNum()

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

Implements FIX::MessageStore.

Definition at line 252 of file FileStore.cpp.

253{
255 setSeqNum();
256}

Referenced by open().

◆ setSeqNum()

void FIX::FileStore::setSeqNum ( )
private

Definition at line 302 of file FileStore.cpp.

303{
305 fprintf( m_seqNumsFile, "%10.10d : %10.10d",
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}

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

Referenced by incrNextSenderMsgSeqNum(), and incrNextTargetMsgSeqNum().

◆ setSession()

void FIX::FileStore::setSession ( )
private

Definition at line 313 of file FileStore.cpp.

314{
316 fprintf( m_sessionFile, "%s",
318 if ( ferror( m_sessionFile ) )
319 throw IOException( "Unable to write to file " + m_sessionFileName );
320 if ( fflush( m_sessionFile ) )
321 throw IOException( "Unable to flush file " + m_sessionFileName );
322}

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

Referenced by open(), and reset().

Member Data Documentation

◆ m_cache

MemoryStore FIX::FileStore::m_cache
private

◆ m_headerFile

FILE* FIX::FileStore::m_headerFile
private

Definition at line 127 of file FileStore.h.

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

◆ m_headerFileName

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

Definition at line 122 of file FileStore.h.

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

◆ m_msgFile

FILE* FIX::FileStore::m_msgFile
private

Definition at line 126 of file FileStore.h.

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

◆ m_msgFileName

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

Definition at line 121 of file FileStore.h.

Referenced by FileStore(), and open().

◆ m_offsets

NumToOffset FIX::FileStore::m_offsets
private

Definition at line 119 of file FileStore.h.

Referenced by populateCache().

◆ m_seqNumsFile

FILE* FIX::FileStore::m_seqNumsFile
private

Definition at line 128 of file FileStore.h.

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

◆ m_seqNumsFileName

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

Definition at line 123 of file FileStore.h.

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

◆ m_sessionFile

FILE* FIX::FileStore::m_sessionFile
private

Definition at line 129 of file FileStore.h.

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

◆ m_sessionFileName

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

Definition at line 124 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 Mar 4 2024 21:10:02 for QuickFIX by doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2001