用于网络模拟器的Matlab事件监听器

时间:2015-07-08 08:01:52

标签: matlab oop events listener

我正在尝试使用面向对象的MatLab构建网络模拟器。网络模拟器旨在用于ad hoc网络,其中网络拓扑预先未知。因此,网络中的每个 Node Field 中都有一个位置(注意使用斜体来表示类或实例)和传输半径。

我的问题是信息传输和接收。我想到了以下方法:每当节点发送消息时,它会通过“MsgSent”事件通知网络。要处理这些事件, Network 会为每个 Node 注册一个MsgListener。收到'MsgSent'事件后,应触发handleMsgSent()方法。在此方法中,网络找出哪个节点在发件人的范围内,并为每个调用receive()。

接下来,我展示了我的课程的一些片段来说明这一点:

classdef Network
    properties
        Nodes       % A list of the Nodes
        Field       % The field associated to the network
    end
    properties (Transient)
         MsgListeners     % List of MsgListeners (1 per node)
    end

    methods
        function net = Network(nnodes, field)
             % Deploys nnodes in the field
             % ....
             for i=1:nnodes
                 net.Nodes = [net.Nodes Node(i, randpos(i,:))];
                 net.MsgListener = [net.MsgListener addlistener(net.Nodes(i), 'MsgSent', @handleMsgSent)];
             end
             % ....
        end

        function handleMsgSent(this, src, msg)
             % Find out the neighbours in range of src and send them the msg
             % ...
             for i=NeighInRange
                 NeighInRange(i).receive(msg)
             end
             % ...
        end
        % ...
    end
end


classdef Node < handle
    properties 
        id              % Node identifier
        pos             % Geographic position [x, y]
        radius  = 0.2   % Transmission radius (default 0.2)
    end

    events
        MsgSent         % Indicates the node has sent a message
    end

    %...
    function send(this, dest, data)
         notify(this, 'MsgSent', Message(this.id, dest, data));
    end        

    function data = receive(this, msg)
         %Check if msg is addressed to this node and do whatever...
    end
    %...
end

这里的重点是我似乎没有收到网络对象上的'MsgSent'事件,我不确定我的方法是否正确或者我应该解决这个发送和接收一种不同的方式。

非常感谢任何建议:)

1 个答案:

答案 0 :(得分:0)

由于handleMsgSent是一个类方法,您需要指定需要调用的对象:

 addlistener(net.Nodes(i), 'MsgSent', @net.handleMsgSent)

这也记录在TimeSeriesChartDemo1

以下适用于我:

<强> Network.m

    classdef Network
    properties
        Nodes
    end
    properties (Transient)
         MsgListeners
    end

    methods
        function net = Network(nnodes)
             for i=1:nnodes
                 net.Nodes = [net.Nodes Node(i)];
             end
             for i=1:nnodes 
                net.MsgListeners = [net.MsgListeners addlistener(net.Nodes(i), 'MsgSent', @net.handleMsgSent)];
             end
        end

        function handleMsgSent(this, src, msg)
             for i=1:size(this.Nodes,2)
                 if(this.Nodes(i).id == msg.receiver)
                    this.Nodes(i).receive(msg);
                 end
             end
        end
    end
end

<强> Node.m

classdef Node < handle
    properties
        id
    end

    events
        MsgSent
    end
    methods
        function this = Node(i)
            this.id = i;
        end
        function send(this, dest, data)
            notify(this, 'MsgSent', Message(this.id, dest, data));
        end

        function data = receive(this, msg)
            disp(msg);
        end
    end
end

<强> Message.m

classdef (ConstructOnLoad) Message < event.EventData
    properties
        sender
        receiver
        data  
    end

    methods
       function msg = Message(s, r, d)
             msg.sender = s;
             msg.receiver = r;
             msg.data = d;
        end

    end
end