MFClient  1.3.0
 All Classes Functions Variables Enumerations
MFMarketDataSession.h
1 /*
2  * Copyright (C) 2015 MarketFactory, Inc. All rights reserved.
3  * Use in source and binary forms, with or without modification, is permitted
4  * provided that the following conditions are met:
5  * 1. You may only use this software for internal evaluation and testing
6  * purposes and may not use the software for engaging
7  * in live trades, unless and until you have entered into a separate agreement
8  * with MarketFactory governing the use of
9  * MarketFactory software in a production environment.
10  * 2. You may not distribute the software (in either source or binary forms) to
11  * third parties.
12  * 3. All copies of source code must retain the above copyright notice, this
13  * list of conditions and the following disclaimer.
14  * THIS SOFTWARE IS PROVIDED BY MARKETFACTORY AND ITS LICENSORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
16  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
18  * NO EVENT SHALL MARKETFACTORY OR ITS LICENSORS BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #ifndef _MF_MFMARKETDATASESSION_H_
29 #define _MF_MFMARKETDATASESSION_H_
30 
31 #define USERNAME_LENGTH 32
32 #define PASSWORD_LENGTH 32
33 #define TEXT_LENGTH 64
34 
35 #include <MFProto.h>
36 #include <MFProtoInputBuffer.h>
37 #include <MFProtoOutputBuffer.h>
38 #include <MFProtoMsgWrapper.h>
39 #include <SessionState.h>
40 
41 #include <mfmarketdata/SubscriptionRequestType.hpp>
42 #include <mfmarketdata/Logon.hpp>
43 #include <mfmarketdata/SecurityDefinitionRequest.hpp>
44 #include <mfmarketdata/MarketDataRequest.hpp>
45 #include <mfmarketdata/RegisterForBatch.hpp>
46 #include <mfmarketdata/UnregisterForBatch.hpp>
47 #include <mfmarketdata/Logout.hpp>
48 #include <mfmarketdata/LogoutResponse.hpp>
49 #include <mfmarketdata/Heartbeat.hpp>
50 
51 #include <boost/asio.hpp>
52 #include <boost/thread/mutex.hpp>
53 #include <boost/noncopyable.hpp>
54 
56 
60 class MFMarketDataSession : private boost::noncopyable {
61  friend class MFClient;
62 
63  public:
67  uint16_t getVenueID() const { return _venueID; }
68 
72  const std::string getVenueName() const { return _venueName; }
73 
77  uint64_t getSendingTime() const { return _inputBuffer.getSendingTime(); }
78 
83  void logon();
84 
91  void logout(const char* text);
92 
99  void logoutResponse();
100 
107 
115  void marketDataRequest(mfmarketdata::SubscriptionRequestType::Value subType,
116  MFProto::SecurityID securityID);
117 
121  mfmarketdata::SubscriptionRequestType::Value subType,
122  MFProto::VenueID venueID, MFProto::SecurityID securityID,
123  uint64_t startTime, uint64_t endTime);
124 
134  void registerForBatch(MFProto::VenueID venueID,
135  MFProto::BatchDefID batchDefID,
136  MFProto::SecurityID* securityIDs, int numSecurityIDs);
137 
143  void unregisterFromBatch(MFProto::BatchDefID batchDefID);
144 
148  bool isLoggedOn() const;
149 
150  private:
151  void sendLogonMessage();
152  void sendLogoutMessage(const char* text);
153 
154  void relogonIfNecessary();
155 
159  bool disconnect(bool reconnect = true);
160 
164  void connect();
165 
166  //-- Socket methods
167  template <typename T>
168  void write(MFProtoMsgWrapper<T>& msgWrapper) {
169  write(msgWrapper.buf());
170  }
171 
172  void write(MFProtoOutputBuffer& outputBuffer);
173 
174  void handle_connect(const boost::system::error_code& ec);
175  void handle_read(const boost::system::error_code& ec,
176  size_t bytes_transferred);
177 
178  void onTimer(TimeUtil::Timestamp now);
179  void checkForDeadConnection(TimeUtil::Timestamp now);
180  void sendHeartbeatIfNecessary(TimeUtil::Timestamp now);
181 
183  boost::asio::io_service& ios,
184  const std::string& venueName, MFProto::VenueID venueID,
185  const std::string& username, const std::string& password,
186  const std::string& hostname, int port,
187  bool relogonOnDisconnect, uint32_t heartBtInt);
188 
189  void onStart();
190  void onStop();
191 
192  void write_internal(MFProtoOutputBuffer& outputBuffer);
193 
194  MFMarketDataHandler& _handler;
195  const uint16_t _heartBtInt;
196  boost::asio::ip::tcp::socket _socket;
197  MFProtoInputBuffer _inputBuffer;
198  mutable boost::mutex _outputBufferLock;
199  uint32_t _outputBufferPacketSeqNum;
200  const std::string _username;
201  const std::string _password;
202  const std::string _hostname;
203  const uint16_t _port;
204  const std::string _venueName;
205  const uint16_t _venueID;
206  const bool _relogonOnDisconnect;
207  SessionState _state;
208  uint64_t _lastMessageSentTimestamp;
209  uint64_t _lastMessageRecvTimestamp;
210 };
211 
212 #endif
A wrapper to encapsulate an SBE class instance and the buffer where the message payload is actually s...
Definition: MFProtoMsgWrapper.h:17
Applications implement this interface in order to process marketdata messages from a MFMarketDataSess...
Definition: MFMarketDataHandler.h:49
uint64_t getSendingTime() const
Retrieve the sending time of the current frame.
Definition: MFMarketDataSession.h:77
void securityDefinitionRequest()
Requests information about the securities supported on this venue. See MFMarketDataHandler::on(const ...
Definition: MFMarketDataSession.cpp:131
void marketDataRequestHistoric(mfmarketdata::SubscriptionRequestType::Value subType, MFProto::VenueID venueID, MFProto::SecurityID securityID, uint64_t startTime, uint64_t endTime)
Definition: MFMarketDataSession.cpp:151
void registerForBatch(MFProto::VenueID venueID, MFProto::BatchDefID batchDefID, MFProto::SecurityID *securityIDs, int numSecurityIDs)
Register for a given list of markets on a given feed. MFProto::VenueIDMFProto::BatchDefIDArray of MFP...
Definition: MFMarketDataSession.cpp:171
void logoutResponse()
The response to a logout from the server. This should be called if a logout is received so that the c...
Definition: MFMarketDataSession.cpp:126
Central class for managing sessions with MarketFactory. Create sessions using this class...
Definition: MFClient.h:83
const std::string getVenueName() const
Get the venue name associated with this session.
Definition: MFMarketDataSession.h:72
bool isLoggedOn() const
Returns whether this session is connected.
Definition: MFMarketDataSession.cpp:382
void logon()
Sends a logon message with the supplied username and password. Should only be called in the event loo...
Definition: MFMarketDataSession.cpp:90
A session used to interact with a single venue for market data.
Definition: MFMarketDataSession.h:60
uint16_t getVenueID() const
Get the venue ID associated with this session.
Definition: MFMarketDataSession.h:67
void marketDataRequest(mfmarketdata::SubscriptionRequestType::Value subType, MFProto::SecurityID securityID)
A request to subscribe or unsubscribe to a particular security. mfmarketdata::SubscriptionRequestType...
Definition: MFMarketDataSession.cpp:140
void logout(const char *text)
Initiates logout. This should be called so a clean and dirty (one where the socket closes w/o a logou...
Definition: MFMarketDataSession.cpp:109
void unregisterFromBatch(MFProto::BatchDefID batchDefID)
Unregister market combination based on the id provided in onBatchEvent callback. MFProto::BatchDefID
Definition: MFMarketDataSession.cpp:189
Definition: SessionState.h:37
MFProtoOutputBuffer & buf()
Return a reference to the buffer where the object payload is stored.
Definition: MFProtoMsgWrapper.h:34