FileStore.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* -*- C++ -*- */
  2. /****************************************************************************
  3. ** Copyright (c) 2001-2014
  4. **
  5. ** This file is part of the QuickFIX FIX Engine
  6. **
  7. ** This file may be distributed under the terms of the quickfixengine.org
  8. ** license as defined by quickfixengine.org and appearing in the file
  9. ** LICENSE included in the packaging of this file.
  10. **
  11. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  12. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. **
  14. ** See http://www.quickfixengine.org/LICENSE for licensing information.
  15. **
  16. ** Contact ask@quickfixengine.org if any conditions of this licensing are
  17. ** not clear to you.
  18. **
  19. ****************************************************************************/
  20. #ifndef FIX_FILESTORE_H
  21. #define FIX_FILESTORE_H
  22. #ifdef _MSC_VER
  23. #pragma warning( disable : 4503 4355 4786 4290 )
  24. #endif
  25. #include "MessageStore.h"
  26. #include "SessionSettings.h"
  27. #include <fstream>
  28. #include <string>
  29. namespace FIX
  30. {
  31. class Session;
  32. /// Creates a file based implementation of MessageStore.
  33. class FileStoreFactory : public MessageStoreFactory
  34. {
  35. public:
  36. FileStoreFactory( const SessionSettings& settings )
  37. : m_settings( settings ) {};
  38. FileStoreFactory( const std::string& path )
  39. : m_path( path ) {};
  40. MessageStore* create( const SessionID& );
  41. void destroy( MessageStore* );
  42. private:
  43. std::string m_path;
  44. SessionSettings m_settings;
  45. };
  46. /*! @} */
  47. /**
  48. * File based implementation of MessageStore.
  49. *
  50. * Four files are created by this implementation. One for storing outgoing
  51. * messages, one for indexing message locations, one for storing sequence numbers,
  52. * and one for storing the session creation time.
  53. *
  54. * The formats of the files are:<br>
  55. * &nbsp;&nbsp;
  56. * [path]+[BeginString]-[SenderCompID]-[TargetCompID].body<br>
  57. * &nbsp;&nbsp;
  58. * [path]+[BeginString]-[SenderCompID]-[TargetCompID].header<br>
  59. * &nbsp;&nbsp;
  60. * [path]+[BeginString]-[SenderCompID]-[TargetCompID].seqnums<br>
  61. * &nbsp;&nbsp;
  62. * [path]+[BeginString]-[SenderCompID]-[TargetCompID].session<br>
  63. *
  64. *
  65. * The messages file is a pure stream of %FIX messages.<br><br>
  66. * The sequence number file is in the format of<br>
  67. * &nbsp;&nbsp;
  68. * [SenderMsgSeqNum] : [TargetMsgSeqNum]<br><br>
  69. * The session file is a UTC timestamp in the format of<br>
  70. * &nbsp;&nbsp;
  71. * YYYYMMDD-HH:MM:SS
  72. */
  73. class FileStore : public MessageStore
  74. {
  75. public:
  76. FileStore( std::string, const SessionID& s );
  77. virtual ~FileStore();
  78. bool set( int, const std::string& ) throw ( IOException );
  79. void get( int, int, std::vector < std::string > & ) const throw ( IOException );
  80. int getNextSenderMsgSeqNum() const throw ( IOException );
  81. int getNextTargetMsgSeqNum() const throw ( IOException );
  82. void setNextSenderMsgSeqNum( int value ) throw ( IOException );
  83. void setNextTargetMsgSeqNum( int value ) throw ( IOException );
  84. void incrNextSenderMsgSeqNum() throw ( IOException );
  85. void incrNextTargetMsgSeqNum() throw ( IOException );
  86. UtcTimeStamp getCreationTime() const throw ( IOException );
  87. void reset() throw ( IOException );
  88. void refresh() throw ( IOException );
  89. private:
  90. typedef std::pair < int, int > OffsetSize;
  91. typedef std::map < int, OffsetSize > NumToOffset;
  92. void open( bool deleteFile );
  93. void populateCache();
  94. bool readFromFile( int offset, int size, std::string& msg );
  95. void setSeqNum();
  96. void setSession();
  97. bool get( int, std::string& ) const throw ( IOException );
  98. MemoryStore m_cache;
  99. NumToOffset m_offsets;
  100. std::string m_msgFileName;
  101. std::string m_headerFileName;
  102. std::string m_seqNumsFileName;
  103. std::string m_sessionFileName;
  104. FILE* m_msgFile;
  105. FILE* m_headerFile;
  106. FILE* m_seqNumsFile;
  107. FILE* m_sessionFile;
  108. };
  109. }
  110. #endif //FIX_FILESTORE_H