Matlab event listener for network simulator

I am trying to build a network simulator using object-oriented MatLab. The network simulator is intended to be useful for ad hoc networks, where the topology of the network is unknown beforehand. So, each Node in the Network has a position in the Field (note the use of italics to denote classes or instances) and a transmission radius.

My problem comes with message transmission and reception. I thought of the following approach: whenever a Node sends a Message it notifies the Network about this with a ‘MsgSent’ event. To handle these events, the Network registers a MsgListener for each Node. Upon the reception of a ‘MsgSent’ event, a handleMsgSent() method should be triggered. In this method, the Network finds out which Node(s) are in range of the sender and call the receive() for each of them.

Next I show some snippets of my classes to illustrate this:

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

The point here is I don’t seem to be receiving the ‘MsgSent’ event on the Network object and I am not sure whether my approach is correct or should I solve this sending and receiving in a different way.

Any suggestions are extremely appreciated 🙂


Source: oop

Leave a Reply