this question is aimed to those of us who really faced with this problem. I am interested in real, working, fast solutions.
Description:
I am using API which allows me to communicate over tcp/ip via Posix conformed socket to get live streaming data. These are prices of financial instruments and other their statistics, all being numbers. I have one Posix client that manages this connection via socket, this is Qt application. Each form (GUI) can request data stream and display incoming quotes.
Problem:
every incoming stream is received via a callback in socket (now I just print info)
void PosixClient::tickPrice( TickerId tickerId, TickType field, double price, int canAutoExecute) {
printf("tradingclient_1: tickPrice: \n");}
The model concept introduced via this API is that streams are recognized due to tickerId field, so when new data appears on the socket the tickPrice method is fired and I should assign/notify etc interested objects about it - i.e. send data to appropriate GUI form differentiate them by tickerId.
Question:
how should I implement data exchange, subscription model to send data to correct objects?
my first thought is to use in Posix client
std::vector<int,my_function> v;
which can map tickerId to callback from object which requested data. Something like Observer Pattern.
So at the moment I have an Observer Pattern implementation. This is main idea how it works:
Observable:
#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;
}
Observer:
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;
};
example:
int main(int argc, char** argv) {
pMyObservable d(new DataRepository);
myObserver obs(d,"count_to_10_data");
d->run();
return 0;
}
result:
new data: 0
new data: 1
new data: 2
new data: 3
new data: 4
new data: 5
new data: 6
new data: 7
new data: 8
new data: 9
RUN SUCCESSFUL (total time: 93ms)
and here is my real code for this moment:
#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;
};
further:
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;
But this has many disadvantages. Is there any better/cheaper/faster approach? What can I improve?
Yes, it's basically Observer pattern, but you need a neater subscription model.
Let's say each form implements an Observer interface containing a single method dataChanged(TickerId) which is called when a data for this tickerId gets updated.
If number of GUI forms is not very large, and it's acceptable to notify them all at every change, I'd suggest the following simple solution: the "subject" (PosixClient) maintains a list (maybe std::vector) of subscribed forms. When a callback from socket is fired, the client notifies ALL the forms, and those forms which are interested in the TickerId update their data. Yes, that's not optimal as most of the forms do not want the notification, but the overhead is usually not noticeable.
If there are too many GUI forms to notify them all at once (although I cannot imagine such a GUI), the PosixClient can maintain something like a std::multimap with TickerId as key and Observer* as value, so it can notify only those who subscribed to changes about given tickerId.
And I'd suggest to store all data (prices etc.) in some common object which can be queried by GUI forms rather than sending updated values along with dataChanged call.
Related
I have an Event system in which a Manager class contains a queue of Events. The Event class is currently an abstract base class from which all specific events are derived. The idea is to allow the Manager to process() specific events that are specialized classes constructed with their own, unique parameters elsewhere in the code where the context of creation informs which event to create and how (sometimes using an Abstract Factory pattern). These Events are consumed asynchronously by Manager in a loop that continuously empties the queue, processing each event, and then destroying it as it is popped.
I designed this system to alleviate Manager from doing all of its own data manipulation. Instead, other parts of the code can queue an Event object, and that object can perform a task via its process() member, with the Manager not needing to know about it in detail.
Without an encapsulated Event system, I would be making endless additions to Manager's members: one for each task, and calling those tasks in case statements somewhere, which is terrible. I thought it would be better to use the Event system I have in the example here. This leaves Manager unchanged while various derived, specific events are defined elsewhere. This minimal example works fine, but the problem is that all derived Events need access to Manager's private data for each one's process() to have full capability as intended. If friend inheritance was allowed, simply making Event a friend class of Manager would solve the problem. Since it's not, the only way for this to work is to make all of Manager's data public (or, technically, add every new derived Event class I make as a friend to Manager).
This works, but feels wrong, and makes me think this is not the correct design. To be clear, the Manager contains a good deal of centralized information (necessary for thread sync, etc) that is more extensive than the Example would indicate. It manages network connections that will spawn a variety of different, often arbitrary events. I'd like to elegantly react to such an environment without inflating Manager with endless additional member functions every time I want to create a new type of event.
Is there a better way to achieve this code separation pattern and still retain the freedom I want for the separated code? Should I just publicize all of Manager's data after all?
The minimal Example (includes pseudocode):
class Event;
class Manager
{
public:
Manager() {}
// Event queue insertion and processing functions omitted
// private: // Commented out on purpose to allow compilation
int dataInt;
double dataReal;
std::string dataStr;
std::queue<Event *> events;
};
// Abstract Event base class with process() member
class Event
{
public:
Event(Manager * m) : manager(m)
{
process = std::bind(&Event::processKernel, this);
}
// Process the event
std::function<void(void)> process;
protected:
// Actual processing code: derived classes must define this function
virtual void processKernel() = 0;
private:
Manager * m;
};
// One example of a specialized (derived) Event
class SpecificEvent: public Event
{
public:
SpecificEvent(Manager * m, int p) : Event(m), param(p) { }
void processKernel()
{
// Intention: modify parent manager data
manager->dataInt = param;
}
private:
int param;
};
// Another specialized (derived) Event
class OtherEvent: public Event
{
public:
OtherEvent(Manager * m, double p) : Event(m), param(p) { }
void processKernel()
{
// Intention: modify parent manager data
manager->dataReal = param;
}
private:
double param;
};
// Example usage: could be inside a Manager member, or anywhere else
int main()
{
Manager manager;
// Make a SpecificEvent, pass it the manager, and its own specific parameter(s)
SpecificEvent e(&manager, 10);
//<Not shown> Add to manager's queue
// Manager processes this event at some point later with Event::process()
}
Since
If friend inheritance was allowed, simply making Event a friend class
of Manager would solve the problem
you can just get away with
struct ManagerFields { // all Manager's fields; just for the further convenience
int dataInt;
double dataReal;
};
class Manager: private ManagerFields { // only Manager and its friends know these Fields
friend class Event;
// here Manager can use dataInt, dataReal etc. just like before
};
class Event {
public:
Event(Manager* m) : manager{m} {}
virtual void process() = 0;
protected: // "friend inheritance" setup below
ManagerFields& fields() { return *manager; } // "the further convenience"
private:
Manager* manager;
};
class SpecificEvent: public Event {
public:
SpecificEvent(Manager* m, int p) : Event{m}, param{p} {}
void process() override { fields().dataInt = param; } // "friend inheritance" usage
private:
int param;
};
See the comments in the code.
In my application, I have several modules that don't fit an 'is-a' or 'has-a' relationship, but still need to communicate and pass data to each other. To try and loosely couple these modules, I've implemented an Event Bus class that handles message passing from 'event posters' to 'event listeners'.
Classes can implement IEventListener if they wish to register to receive certain events. Likewise, classes can call EventBus::postEvent() if they need to push an event out to the bus. When EventBus::update() is called EventBus processes the queue of scheduled messages and routes them to registered listeners.
EventBus.h
#pragma once
#include <queue>
#include <map>
#include <set>
#include <memory>
class IEvent
{
public:
static enum EventType
{
EV_ENEMY_DIED,
EV_ENEMY_SPAWNED,
EV_GAME_OVER
};
virtual ~IEvent() {};
virtual EventType getType() const = 0;
};
class IEventListener
{
public:
virtual void handleEvent(IEvent * const e) = 0;
};
class EventBus
{
public:
EventBus() {};
~EventBus() {};
void update();
void postEvent(std::unique_ptr<IEvent> &e);
void registerListener(IEvent::EventType t, IEventListener *l);
void removeListener(IEvent::EventType t, IEventListener *l);
private:
std::queue<std::unique_ptr<IEvent>> m_eventBus;
std::map<IEvent::EventType, std::set<IEventListener *>> m_routingTable;
};
EventBus.cpp
#include "EventBus.h"
using namespace std;
/**
* Gives the EventBus a chance to dispatch and route events
* Listener callbacks will be called from here
*/
void EventBus::update()
{
while (!m_eventBus.empty())
{
// Get the next event (e_local now owns the on-heap event object)
unique_ptr<IEvent> e_local(move(m_eventBus.front()));
m_eventBus.pop();
IEvent::EventType t = e_local->getType();
auto it = m_routingTable.find(t);
if (it != m_routingTable.end())
{
for (auto l : ((*it).second))
{
l->handleEvent(e_local.get());
}
}
}
}
/**
* Posts an event to the bus, for processing and dispatch later on
* NB: The event bus will takes ownership of the on-heap event here
*/
void EventBus::postEvent(unique_ptr<IEvent> &e)
{
// The EventBus now owns the object pointed to by e
m_eventBus.push(unique_ptr<IEvent>(move(e)));
}
/**
* Registers a listener against an event type
*/
void EventBus::registerListener(IEvent::EventType t, IEventListener *l)
{
// Add this listener entry
// If the routing table doesn't have an entry for t, std::map.operator[] will add one
// If the listener is alredy registered std::set.insert() won't do anything
m_routingTable[t].insert(l);
}
/**
* Removes a listener from the event routing table
*/
void EventBus::removeListener(IEvent::EventType t, IEventListener *l)
{
// Check if an entry for event t exists
auto keyIterator = m_routingTable.find(t);
if (keyIterator != m_routingTable.end())
{
// Remove the given listener if it exists in the set
m_routingTable[t].erase(l);
}
}
As you can see, in my current implementation, I create concrete IEvent implementations for every type of event I want to pass around. I did this so that each event can have custom data attached to it (a requirement for my situation). Unfortunately, this means my EventBus system has to know about all the users of the system, increasing the coupling between my EventBus class and the users of the class. Additionally, the IEvent interface needs to hold a list of all event types as an enum, which has the same problem (increased coupling).
Is there a way to modify this implementation so that EventBus can be totally generic (doesn't need to know about the users of the EventBus), and yet still allow me to pass custom data with each event? I looked into C++11 variadic template functions but couldn't figure out how to use them in this case.
As a side-question, am I using std::unique_ptr correctly here?
Question 1 "Is there a way to modify this implementation so that EventBus can be totally generic":
Short answer, yes.
Longer answer: There are many ways of accomplishing this. One is described here:
Both the producer and the consumer of the event needs to agree on the type/data but the EventBus itself does not need to know. One way of accomplishing this could be to use boost::signals2::signal<T> as the event type. This will give you a proven, flexible and type safe signal/slot implementation. What it will not provide, however, is the possibility to queue up slot callbacks and process them from the EventBus::update()-function.
But, that can also be remedied. By making the event type EventBus::postEvent() takes as a parameter be std::function<void()> and calling postEvent() like this:
boost::signals2::signal<int> signal;
...
eventbus.postEvent(boost::bind(signal, 42));
// note: we need to use boost::bind (not std::bind) for boost::signals to be happy
The EventBus will see a std::function<void()> and dispatch to the slot. The data (42 in this example) will be kept by the result of boost::bind and be used as the parameter when the slot is called.
Question 2 "Am I using std::unique_ptr correctly":
Almost. I would drop the reference of EventBus::postEvent making it:
void EventBus::postEvent(std::unique_ptr<IEvent> e);
By doing this, you force the caller to actively move the std::unique_ptr<IEvent> into the EventBus. This will make the user aware the EventBus takes ownership and also making it obvious to people reading the code what the intent is and how ownership is transferred.
CppCoreGuidelines R.32:
"Take a unique_ptr parameter to express that a function assumes ownership of a widget"
I am developing GUI form in Qt and I wonder how to implement ObserverPattern. Form can subscribe to many data streams distinguished by tickerId, when data stream arrives (new quote is available) my PosixClient (socket wrapper) fires notifyObservers() method what results in update() method of observer being executed.
BUT:
this update() method is void update() and I need to take incoming data records and plot them, count something, just use them. So how can I achieve this, how pass data records to observer?
Data is available to Observable (MarketData object derived from Observable). When data arrives I push it into this Observable and notify observers.
void PosixClient::tickPrice( TickerId tickerId, TickType field, double price, int canAutoExecute) {
printf("tradingclient_1: tickPrice: \n");
for(std::vector<boost::shared_ptr<MarketData> >::iterator it=dataRepository.begin();
it!=dataRepository.end(); it++){
if((*it)->tickerId==tickerId){
(*it)->tickPriceData.push_back(tickSizeRecord(field,price,canAutoExecute));
(*it)->notifyObservers();
//TODO: start thread to store incoming data in repository
}
}
}
Their void update() methods are then called. in order to retrieve a data from this function I decided to pass a function pointer boost::function<> to it as a callback and Observer calls this function pointer that points to my GUI object with incoming data from observable as argument. Is this right approach?
struct MarketData : public QuantLib::Observable {
//public:
MarketData();
MarketData(IB::Contract c, int tickerId):c(c),tickerId(tickerId){}
MarketData(const MarketData& orig);
virtual ~MarketData();
std::vector<IB::Record> tickPriceData; //market data fed in tickPrice
//private:
IB::Contract c;
int tickerId;
};
typedef boost::shared_ptr<MarketData> pMyObservable;
typedef boost::function<void (int tickerId, IB::Record record)> f_action_ptr;
class MarketDataObserver : public QuantLib::Observer{
public:
MarketDataObserver(pMyObservable obs, f_action_ptr ptr)
: observable(obs), f_ptr(ptr){
this->registerWith(observable);
}
MarketDataObserver(const MarketDataObserver &observer)
: Observer(observer),
observable(observer.observable){ // faction_ptr is not copied!
}
void update(){
data=observable->tickPriceData.back();
//printf("new data: %l\n",data.price);
f_ptr(observable->tickerId, data);
}
private:
IB::Record data;
pMyObservable observable;
f_action_ptr f_ptr;
};
PLEASE NOTE:
I am aware of Qt signal/slot mechanism, but in my opinion Qt signal/slot is not at all solution here, when I need dynamically subscribe to data, plot them, show on Qt Form, then delete subscription when Form is canceled. But maybe I am wrong. Am I? I ask for real, working examples, from life, not theoretical dispute.
The usual Qt idiom for the observer pattern are indeed signals and slots. Have the source of the data emit signals and pass the data as an argument of the signal. That's how this is done within Qt -- signals are not used just for the GUI events.
I have a problem where I'm having to instantiate instances of objects
earlier than I would like to do so because I need to connect signal
slots through some deep ownership, and I'd like to come up with a way of
storing and forwarding the slots so that I can construct objects closer
to their use site, instead of doing so as member variables.
My basic problem is that I have a process that will download an update
file on a separate thread and send a progress signal to anyone who is
interested. The signal is essentially:
typedef boost::signals2::signal<void (double)> DownloadProgress;
Assume that the implementation of the progress function mentioned
below conforms to this; the nature of the signal itself isn't very
important (although I am using functors for the most part).
The signal is set and the code is called something like this:
Updater updater;
updater.onDownloadProgress(&progress);
updater.runDownloadTask();
When you call updater.runDownloadTask(), it will start the
UpdaterDownloadTask, which starts an HTTPRequest and returns an
HTTPResponse. The HTTPResponse is the piece which interacts with the
network layer and receives the data and contains the DownloadProgress
signal. With this, my implementation looks a bit like (bottom-up from
HTTPResponse, heavily abbreviated to elide methods that aren't
particularly illustrative):
class HTTPResponse
{
public:
// this will be called for every "chunk" the underlying HTTP
// library receives
void processData(const char* data, size_t size)
{
// process the data and then send the progress signal
// assume that currentSize_ and totalSize_ are properly set
progressSignal_(currentSize_ * 100.0 / totalSize_);
}
void onDownloadProgress(const DownloadProgress::slot_type& slot)
{
progressSignal_.connect(slot);
}
private:
DownloadProgress progressSignal_;
};
class HTTPRequest
{
public:
HTTPRequest() : response_(new HTTPResponse) { }
void onDownloadProgress(const DownloadProgress::slot_type& slot)
{
response_->connect(slot);
}
boost::shared_ptr<HTTPResponse> perform()
{
// start the request, which operates on response_.
return response_;
}
private:
boost::shared_ptr<HTTPResponse> response_;
};
class UpdaterDownloadTask : public AsyncTask
{
public:
DownloadTask() : request_(new HTTPRequest) { }
void onDownloadProgress(const DownloadProgress::slot_type& slot)
{
request_->connect(slot);
}
void run()
{
// set up the request_ and:
request_>perform();
}
private:
boost::shared_ptr<HTTPRequest> request_;
};
class Updater
{
public:
Updater() : downloadTask_(new UpdaterDownloadTask) { }
void onDownloadProgress(const DownloadProgress::slot_type& slot)
{
downloadTask_->onDownloadProgress(slot);
}
void runDownloadTask() { downloadTask_.submit() }
private:
boost::shared_ptr<UpdaterDownloadTask> downloadTask_;
};
So, my Updater has to have an instance of UpdaterDownloadTask that's
always around, which has an instance of HTTPRequest, which has an
instance of HTTPResponse—just because I have to forward the slot
connection from Updater (the public API entry point) to HTTPResponse
(where the signal belongs).
I would rather implement UpdaterDownloadTask::run() like so:
void run()
{
HTTPRequest request;
request.onDownloadProgress(slots_);
#if 0
// The above is more or less equivalent to
BOOST_FOREACH(const DownloadProgress::slot_type& slot, slots_)
{
request.onDownloadProgress(slot);
}
#endif
request.perform();
}
This would have similar implications at the HTTPRequest level (so I
don't have to construct the HTTPResponse until I perform the request)
and overall make for a nicer data flow with strong RAII semantics. I've
previously tried defining the slots_ variable as a vector:
std::vector<DownloadProgress::slot_type> slots_;
Yet I can only get this to work if I force the callers to call
onDownloadProgress(boost::ref(slot));.
Has anyone done this successfully, or have a good suggestion on how to
store and forward other than what I'm doing?
I think storing the slots in a vector should work ok. If you want to get rid of the need for boost::ref(...) you can remove the & from the onDownloadProgress parameter (since slot_type is copyable).
Alternatively, you could have your signal inside HTTPResponse fire and in turn fire a signal in HTTPRequest, doing that, you could connect all the slots to the signal in HTTPRequest, then once the HTTPResponse is created, you connect to the response signal onDownloadProgress(request.signalname). Where signalname is the signal your client.
pseudocode:
Request request;
request.onProgress(myProgressBarCallback);
//calls: this.signal.connect(myProgressBarCallback);
request.go();
//calls: Response response;
// and: response.onProgress(this.signal);
I hope that helps.
I am refactoring some code to decouple GUI from some state.
#include <StateObject>
Class GUI{
...
StateObject A;
void doSomething() { A->hullaballoo();}
**void ReFreshMyGui() { //take state A and redraw }**
};
State object is being shared by multiple classes to update the state but this Gui Object specializes in displaying the state. So I would like to call the Refresh function via StateObject whenever its modified.
I am not sure how or if signals will provide the solution. Any hints?
In order to decouple GUI and Data you could use the MVC pattern. This means your GUI should register to your model's (data) object(s) and whenever the data changes the GUI will be notified and it will be the GUI to redraw itself.
But careful, the model should not have the notion of a concrete GUI, instead the GUI should implement an observer interface containing a method (e.g. void Update()) that will be called whenever changes occur (notification handler).
Just look for the MVC-pattern on google. You'll find thousands of useful tutorials.
You may also take in consideration the MVP and the MVVM patterns.
Example:
class Observer
{
virtual void Update(void* data) = 0;
}
class GUI : public Observer
{
public:
virtual void Update(void* data)
{
//Redraw method and some other things you may
//want to do with the new data
}
}
class Model
{
private:
int m_iData;
List<Observer> observers;
public:
void SetData(int iData)
{
m_iData = iData;
for(int i = 0; i < observers.Length; i++)
{
observers[i].Update(NULL);
}
}
}