#ifndef __MarketDataProxyClient_h_ #define __MarketDataProxyClient_h_ #include #include #include "DataTypes.h" #include "../shared/ContainerThread.h" #include "../shared/ServerConnection64.h" class MarketDataProxyClient : private ForeverThreadUser { public: typedef std::function< void(DataTypes::Internal /* type */, std::string const & /* symbol */, std::string const & /* data */) > Callback; typedef std::function< void(std::string const &) > StatusCallback; private: typedef ServerConnection64::Message Message; class ServerConnection : public ServerConnection64 { private: struct RequestInfo : public IMessageListener { DataTypes::Internal type; std::string symbol; MessageId messageId; ServerConnection *owner; virtual void onMessage(std::string bytes, int64_t clientId, MessageId messageId); virtual void onAbort(int64_t clientId, MessageId messageId); }; DataTypes::Container< std::map< std::string, RequestInfo > > _requests; static Message const &createSubscribeMessage(DataTypes::Internal type, std::string const &symbol); static Message const &createUnSubscribeMessage(DataTypes::Internal type, std::string const &symbol); void onMessage(RequestInfo const &request, std::string bytes); void onAbort(RequestInfo &request); void sendSubscribe(RequestInfo &request); MarketDataProxyClient *_owner; public: void cancel(DataTypes::Internal, std::string const &symbol); void subscribe(DataTypes::Internal, std::string const &symbol); void requestStatus(IMessageListener *listener, int64_t clientId); ServerConnection(std::string const &name, MarketDataProxyClient *owner); }; // TODO Eventually there will be N of these. We can listen to multiple // servers at once. The main program won't be aware of this detail. ServerConnection _serverConnection; Callback _callback; //DataTypes::Container< std::unordered_set< std::string > > _requests; void subscribeImpl(DataTypes::Internal type, std::string const &symbol); void unsubscribeImpl(DataTypes::Internal type, std::string const &symbol); MarketDataProxyClient() : ForeverThreadUser(IContainerThread::create("MarketDataProxyClient")), _serverConnection("MarketDataProxyClient", this) { } public: static MarketDataProxyClient *instance(); void subscribe(DataTypes::Internal type, std::string symbol); void unsubscribe(DataTypes::Internal type, std::string symbol); // This is aimed at development & debugging. The result might vary over // time. Among other things, this will send the request to the one server // connection. I don't know what might change when we add multiple server // connections. // // The result comes from a standard callback. You should eventually get a // successful message or an abort. // // There is no way to cancel this request. The listener must be ready until // it gets one of those two messages. // // clientId means anything you want. You will get it back in the listener. void requestStatus(ServerConnection64::IMessageListener *listener, int64_t clientId = 0); void setCallback(Callback callback); void clearCallback() { setCallback(Callback()); } }; #endif