这个问题是针对我们这些真正面临这个问题的人。我对真正的、有效的、快速的解决方案感兴趣。
描述:
我正在使用 API,它允许我通过符合 Posix 的套接字通过 tcp/ip 进行通信以获取实时流数据。这些是金融工具的价格和其他统计数据,都是数字。我有一个通过套接字管理此连接的 Posix 客户端,这是 Qt 应用程序。每个表单 (GUI) 都可以请求数据流并显示传入的报价。
问题:
每个传入的流都是通过套接字中的回调接收的(现在我只是打印信息)
void PosixClient::tickPrice( TickerId tickerId, TickType field, double price, int canAutoExecute) {
printf("tradingclient_1: tickPrice: \n");}
通过这个 API 引入的模型概念是流被识别为tickerId
字段,所以当新数据出现在套接字上时,该tickPrice
方法被触发,我应该分配/通知等感兴趣的对象 - 即将数据发送到适当的 GUI 表单区分它们tickerId
.
问题:
我应该如何实现数据交换、订阅模型以将数据发送到正确的对象? 我的第一个想法是在 Posix 客户端中使用
std::vector<int,my_function> v;
它可以映射tickerId
到请求数据的对象的回调。类似于观察者模式。
所以目前我有一个观察者模式的实现。这是它如何工作的主要思想:
可观察的:
#include <cstdlib>
#include <ql/patterns/../patterns/observable.hpp>
#include <iostream>
/*
*
*/
class DataRepository : public QuantLib::Observable{
public:
void run();
int getData();
private:
int data;
};
void DataRepository::run(){
for(int i=0;i<10;++i){
data=i;
notifyObservers();
}
}
int DataRepository::getData(){
return data;
}
观察员:
typedef boost::shared_ptr<DataRepository> pMyObservable;
class myObserver : public QuantLib::Observer{
public:
myObserver(pMyObservable obs, std::string n)
: observable(obs), name(n){
this->registerWith(observable);
}
myObserver(const myObserver &observer)
: Observer(observer),
observable(observer.observable),
name("Copy of "+observer.name){
}
void update(){
data=observable->getData();
std::cout<<"new data: "<<data<<std::endl;
}
private:
int data;
pMyObservable observable;
std::string name;
};
例子:
int main(int argc, char** argv) {
pMyObservable d(new DataRepository);
myObserver obs(d,"count_to_10_data");
d->run();
return 0;
}
结果:
新数据:0
新数据:1
新数据:2
新数据:3
新数据:4
新数据:5
新数据:6
新数据:7
新数据:8
新数据:9
运行成功(总时间:93ms)
这是我此刻的真实代码:
#include <ql/patterns/observable.hpp>
#include "Contract.h"
#include <boost/function.hpp>
#include "IB_events.h"
#include <list>
typedef boost::shared_ptr<IB::Record> rec_ptr;
typedef boost::shared_ptr<IB::TickPriceRecord> tickPriceRec_ptr;
typedef boost::shared_ptr<IB::TickSizeRecord> tickSizeRec_ptr;
typedef boost::shared_ptr<IB::TickStringRecord> tickStringRec_ptr;
class MarketData : public QuantLib::Observable {
public:
MarketData();
MarketData(IB::Event processedEvent, int tickerId, IB::Contract contractDescription):
processedEvent(processedEvent), tickerId(tickerId), contractDescription(contractDescription) {}
virtual ~MarketData();
int getTickerId(){ return tickerId; }
void putRecord(boost::shared_ptr<IB::Record> record){
record_=record;
}
boost::shared_ptr<IB::Record> getRecord(){
return record_;
}
IB::Event getEvent(){
return processedEvent;
}
private:
MarketData(const MarketData& orig);
boost::shared_ptr<IB::Record> record_;
// this MarketData object can handle these events
// any observer can subscribe to one of those events
IB::Event processedEvent;
int tickerId;
IB::Contract contractDescription;
};
更远:
typedef boost::shared_ptr<MarketData> pMyObservable;
typedef boost::function<void (int tickerId, boost::shared_ptr<IB::Record> record)> f_action_ptr;
// one MarketDataObserver may observe one tickerId and for one event
// if you want to be notified when many events happened (i.e. TickSize and TickPrice)
// you can subscribe many MarketDataObservers to one and the same MarketData instance
class MarketDataObserver : public QuantLib::Observer{
public:
MarketDataObserver(pMyObservable obs, IB::Event observedEvent, f_action_ptr ptr)
: observable(obs), observedEvent_(observedEvent), f_ptr(ptr){
this->registerWith(observable);
}
MarketDataObserver(const MarketDataObserver &observer)
: Observer(observer),
observable(observer.observable){ // faction_ptr is not copied!
}
// object which subscribed to data stream using this MarketDataObserver
// will be notified about incoming IB::Record
void update() {
if (observable->getEvent() == observedEvent_) { // just to be 100% sure
boost::shared_ptr<IB::Record> data = observable->getRecord();
// here appropriate function is called: myTickPriceUpdate,
// myTickSizeUpdate or myTickStringUpdate depending on what
// subscribing object specified in f_action_ptr ptr
// in MarketDataObserver constructor
f_ptr(observable->getTickerId(), data);
}
}
private:
pMyObservable observable;
f_action_ptr f_ptr;
IB::Event observedEvent_; // the single event in which observer is interested
};
typedef boost::shared_ptr<MarketData> mktData_ptr;
但这有很多缺点。有没有更好/更便宜/更快的方法?我可以改进什么?