MATLAB Answers

Anyone listening for a particular event?

조회 수: 2(최근 30일)
Hi,
I have an object that can fire many events, and computing the events takes a significant amount of time. Therefore, I would like to compute the events only if I know there is someone listening for them.
Is there any way for the object to get notified when a listener is added or removed? Is there any way to get the list of current listeners? Is there any way to check the count of active listeners for a given event?
Thanks, Joan

  댓글 수: 0

댓글을 달려면 로그인하십시오.

채택된 답변

Andrew Newell
Andrew Newell 21 Mar 2011
@Joan, This is an interesting problem! I don't know what the ideal solution is, but maybe you could create three classes: subscriber, contract and broker. The broker class would manage the other two. It would behave something like this:
  • Maintain a structure marketFeeds with dynamic fields feedname. Each field has two subfields: subscriberHandle and listenerHandle.
  • Input handle to new contract ( broker.addContract(contractHandle) ).
For each feed name, see if it is one of the field names in marketFeeds.
If it exists, add contractHandle to list of handles maintained by the corresponding listener ( i.e., listenerHandle.addContract(marketFeeds.feedName.contractHandle)).
If it doesn't exist, add a new subscriber object and a listener for that object. Give the handle of the contract to the listener.
When a listener is told of a change in the market feed, it calls each of the handles with some command like contractHandle.reactToMarketChange(feedName,NewValue).
When the contract is terminated, send its handle to broker.deleteContract, which then does all the necessary actions on marketFeeds.

  댓글 수: 2

Joan Puig
Joan Puig 26 Mar 2011
I have tried implementing this and it seems to work as I wanted. I made the following changes:
I called the "Broker" a PricingEngine. But it has the addContract, and removeContract methods as described
Given a Contract the PricingEngine knows what market data it needs, and establishes the right feeds with the DataProvider. There is only one DataProvider instance for every instance of MATLAB. There might be different PricingEngines (for example, we might want to have different models and compare results)
For the DataProvider I overrided the addlistener and removelistener functions. While I still need to keep a list of the event listeners (which is kind of inefficient because MATLAB must be doing this as well) it will allow for easy migration if MATLAB ever provides access to the listener list.
Internally, the DataProvider works as you described. By intercepting the listeners added and removed it knows if there is anyone listening for a particular market data feed.
Andrew Newell
Andrew Newell 28 Mar 2011
@Joan, I'm glad it worked out! Unlike most of my answers, I couldn't test this one, so I wasn't sure if it would really solve your problem.

댓글을 달려면 로그인하십시오.

추가 답변(2개)

Andrew Newell
Andrew Newell 20 Mar 2011
I would think that the easiest way would be to define an event property in for each event, like this:
classdef complicatedClass < handle
...
events
event1
...
eventN
end
methods
function obj = longEvent1(obj,varargin)
...
notify(obj,'event1');
end
...
end
and then have a listener class, e.g., respondToEvent1, for each event. Then start them all at once and you can be sure something is listening without a lot of bookkeeping.

  댓글 수: 0

댓글을 달려면 로그인하십시오.


Joan Puig
Joan Puig 20 Mar 2011
Hi Andrew, thanks for your answer, but I don't think it would work because my setup is a little bit more complex:
My events are changes in real time market data. I can't create a class with an event for every single ticker out there. Maybe if there was something like dynamicprops for events I could use that to create the events dynamically.
My listeners are financial contracts whose value depends on the market data.
My ultimate goal is to create a class that will know which market data feeds it needs to subscribe to depending on the contracts that are attached to it. This way I could manage in an optimized and centralized way all my data feed subscriptions.

  댓글 수: 0

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by