Events Listening, Callback functions

1 回表示 (過去 30 日間)
Bolivar
Bolivar 2013 年 7 月 24 日
Hello,
I'm wondering if it's possible to bind the excecution of a listener to an condition? for example : assume we have a system of three users :user1,user2,user3.
-user1 and user2 trigger event 'Hello_Request', which user3 is listening to so:
user3 = addlistener([user1,user2],'Hello_Request',@callback1)
-user3 responds with 'Hello_Response', which user1 and user3 are listening to. so:
user1 = addlistener([user3],'Hello_Response',@callback2)
user2 = addlistener([user3],'Hello_Response',@callback2)
Now my question: Is there a way to condition the execution of user1 and user2 that user3 respond s just to the one who send a 'Hello_Request'? I can use different events since my system gets a large number of user which can actually variate.
Thanks in advance for your support!
Bolivar

採用された回答

Daniel Shub
Daniel Shub 2013 年 7 月 24 日
編集済み: Daniel Shub 2013 年 7 月 25 日
You might be able to hack it, but I am not sure that you need to ...
Why not just have callback1 call a hello_response method of the user who triggered the event? If you want to keep it as event based it seems like the Hello_Response event should belong to user1, user1 should listen for this event, and user3 should trigger the event.
I am thinking something like
classdef user < handle
properties
name
helloRequestListener
end
events
Hello_Request
Hello_Response
end
methods
function obj = user(name)
obj.name = name;
obj.helloRequestListener = addlistener(obj, 'Hello_Response', @helloResponse);
end
function helloRequest(obj, src, evt)
disp(['Hello request by ', src.name, ' recieved by ', obj.name]);
notify(src, 'Hello_Response')
end
function helloResponse(src, evt)
disp(['Hello response by ', src.name]);
end
end
end
called by
user1 = user('user1'); user2 = user('user2'); user3 = user('user3');
addlistener([user1, user2], 'Hello_Request', @user3.helloRequest);
notify(user1, 'Hello_Request');
which returns
Hello request by user1 recieved by user3
Hello response by user1
Note that user2 does not give a response.
  2 件のコメント
Bolivar
Bolivar 2013 年 7 月 24 日
Hi,
user1 and user2 are independant objects of same type(here is just a sample I'll hvave tousands of it), that is they trigger same events and also listen to the same can be at same time or delayed times. Tell me more on how to hack it and excecute just the corresponding code? or would you propose anything else to solve that matter?
Daniel Shub
Daniel Shub 2013 年 7 月 25 日
See my edit ...

サインインしてコメントする。

その他の回答 (1 件)

Bolivar
Bolivar 2013 年 7 月 25 日
excellent! that not exactlly what I want but some little modification in the technic will to the way. Thanks a lot!
  2 件のコメント
Daniel Shub
Daniel Shub 2013 年 7 月 25 日
It would be helpful to myself, and possibly others, if you could post some simplified code showing what you ultimately do.
Bolivar
Bolivar 2013 年 7 月 29 日
Hallo Daniel, actually I'm just going to costomize this to my own code nothing really special. notification will be perform as you have demonstrate earlier. Thanks

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by