Brunet:IDataHanlderApiCallSeq
From Grid-Appliance Wiki
Contents |
API call sequence
You can refer to the source code comment for detailed explaination. Here, I will explain api calling sequence for reference. This section may not be useful if you are not interested in how inside brunet data handler works.
Subscribing specific protocol type data to node demultiplexer
1. Create new protocol type.
PType HW = new PType("HelloWorld");
2. Subscribe the protocol type to the node demultiplexer handler. This enables any message with a specific PType arrive at this class "HandleData" method.
_node.DemuxHandler.GetTypeSource(HW).Subscribe(this, null);
3. MultiSource.Subscribe() module creates new Subscriber instance, and add the new instance to exisitng subscribe members. (brunet/src/protocol/util/Source.cs)
public void Subscribe(IDataHandler h, object state) { Subscriber s = new Subscriber(h, state); _subs = Functional.Add(_subs, s); }
Sending message to remote node
1. Create AHSender instance whose argument is node, which the end point is currently connected, and remote end point brunet address.
AHExactSender sender = new AHExactSender(_node, remote_addr);
2. Call AHSender.send method to send the message
sender.Send(new CopyList(HW, data));
3. Create packet inside AHSender.Send method and Call Node.HandleData method whose argument is mb_packet:data packet to send and destination address, _from:sender address(brunet/src/protocol/AHSender.cs)
public void Send(ICopyable data) { MemBlock mb_packet = MemBlock.Reference(ah_packet, packet_offset, packet_length); _n.HandleData(mb_packet, _from, this);
4. Create AnnounceState instance and enqueue the instance. (brunet/src/protocol/Node.cs)
public void HandleData(MemBlock data, ISender return_path, object state) { AnnounceState astate = new AnnounceState(this, data, return_path); EnqueueAction(astate); }
Receiving message from other end point
1. New arrived packet is delivered to ListeningThread(). This thread invokes HandleDataPacket method.(brunet/src/protocol/Transport/UdpEdgeListener.cs)
protected void ListenThread(){ while(1 == _running){ HandleDataPacket(remoteid, localid, packet_buffer, end, null); } }
2. This method creates an UdpEdge instance using local id and tells the edge to announce the packet(brunet/src/protocol/Transport/UdpEdgeListener.cs)
protected void HandleDataPacket(int remoteid, int localid, MemBlock packet, EndPoint end, object state){ UdpEdge edge = (UdpEdge)_id_ht[localid]; edge.ReceivedPacketEvent(packet); }
3. Send a packet to the subscriber list. (brunet/src/protocol/Transport/Edge.cs)
public void ReceivedPacketEvent(MemBlock b){ _sub.Handle(b, this); }
4. This method passes data to the subscribed handler and call HandleData method of each handler. At this time, the Handler is "Brunet.StructuredNode" (brunet/src/protocol/Util/Source.cs)
public void Handle(MemBlock b, ISender retpath) { Handler.HandleData(b, retpath, State); }
5. Create AnnounceState instance and enqueue the instance. (brunet/src/protocol/Node.cs)
public void HandleData(MemBlock data, ISender return_path, object state) { AnnounceState astate = new AnnounceState(this, data, return_path); EnqueueAction(astate); }
5.5. I cannot notice connection between step 5 and step 6 clearly. Comments will be appreciated.
6. Call Node.Announce method to deliver packet(brunet/src/protocol/Node.cs)
public void Start() { LocalNode.Announce(Data, From); }
7. This method calls the Announce method for the PType.(beunet/src/protocol/Node.cs)
protected virtual void Announce(MemBlock b, ISender from){ t = PType.Parse(b, out payload); ns = (MultiSource)DemuxHandler.GetTypeSource(t); handlers = ns.Announce(payload, from); }
8. Multisource.Announce() method gets subscriber list, and calls each subsceiber's Handle method. Handle method simply calls each handler's HandleData() method.(brunet/src/protocol/Util/Source.cs)
public int Announce(MemBlock b, ISender return_path) { for(int i = 0; i < handlers; i++) { Subscriber s = (Subscriber) subs[i]; s.Handle(b, return_path); } }
9. Step 4 - 8 is repeated once. At this time, the Handler in step 4 is "Brunet.AHHandler"
10. This method passes data to the subscribed handler and call HandleData method of each handler. At this time, the Handler is "Brunet.Applications.Examples.HelloWorldNodeDataHandler"(brunet/src/protocol/Util/Source.cs)
public void Handle(MemBlock b, ISender retpath) { Handler.HandleData(b, retpath, State); }
11. HelloWorldNodeDataHandler.HandleData() is called.

