public static Protocol createStack(Protocol middle, out Protocol bot) { ProtocolSinkStack stack = new ProtocolSinkStack(null,null); middle.ProtocolSinkStack = stack; TOP top = new TOP("TOP"); BOTTOM bottom = new BOTTOM(); bot = bottom; top.DownProtocol = middle; middle.DownProtocol = bottom; bottom.DownProtocol = null; top.UpProtocol = null; middle.UpProtocol = top; bottom.UpProtocol = middle; bottom.startDownHandler(); bottom.startUpHandler(); middle.startDownHandler(); middle.startUpHandler(); top.startDownHandler(); top.startUpHandler(); top.start(); middle.start(); bottom.start(); bottom.up(new Event(Event.SET_LOCAL_ADDRESS,localAddr)); return top; }
public static void stopProtocols(Protocol top) { Protocol next = top; while (next!=null) { next.stop(); next = next.DownProtocol; } }
/// <summary> /// Starts the Up and Down Handler threads in every Protocol. /// </summary> /// <param name="bottom_prot">Start every Protocol from this one and above</param> public void startProtocolStack(Protocol bottom_prot) { while(bottom_prot != null) { if(Trace.trace) Trace.info("Configutator.startProtocolStack()", "Starting Protocol: " + bottom_prot.Name); bottom_prot.startDownHandler(); bottom_prot.startUpHandler(); bottom_prot=bottom_prot.UpProtocol; } }
/// <summary> /// Moves down the stack until the last <c>Protocol</c> is discovered /// </summary> /// <param name="prot_stack">Top protocol in the stack</param> /// <returns>The Bottom-most <c>Protocol</c> found</returns> public Protocol getBottommostProtocol(Protocol prot_stack) { Protocol tmp=null, curr_prot=prot_stack; while(true) { tmp=curr_prot.DownProtocol; if(tmp == null) break; curr_prot=tmp; } if(curr_prot == null) throw new Exception("Configurator.getBottommostProtocol(): bottommost protocol is null"); return curr_prot; }
/// <summary> /// Stops all <c>Protocol</c> Up and Down Handler threads. /// </summary> /// <param name="start_prot">Stop every Protocol from this one down</param> public void stopProtocolStack(Protocol start_prot) { while(start_prot != null) { start_prot.stopInternal(); start_prot = start_prot.DownProtocol; } }
/// <summary> /// Creates an array of <c>Protocols</c> from the configurations /// </summary> /// <param name="protocol_configs">Array of <c>ProtocolConfiguration</c> objects</param> /// <param name="stack">Instance <c>ProtocolSinkStack</c> which the Protocol layers should reference</param> /// <returns>Array of <c>Protocol</c> objects</returns> private Protocol[] createProtocols(ProtocolConfiguration[] protocol_configs, ProtocolSinkStack stack) { Protocol[] retval = new Protocol[protocol_configs.Length]; Protocol layer; for(int i=0; i < protocol_configs.Length; i++) { layer = (Protocol)protocol_configs[i].createLayer(stack); //stack if(layer == null) return null; retval[i] = layer; } sanityCheck(retval); return retval; }
/// <summary> /// Checks all <c>Protocol</c>s are unique and that /// each required up/down service is provided /// </summary> /// <param name="protocols">Protocols that will be used for the stack</param> private void sanityCheck(Protocol[] protocols) { Protocol prot; String name; ProtocolReq req; ArrayList req_list=new ArrayList(); int evt_type; // Checks for unique names for(int i=0; i < protocols.Length; i++) { prot = (Protocol)protocols[i]; name = prot.Name; for(int j=0; j < protocols.Length; j++) { if(i==j) continue; if(String.Compare(name,protocols[j].Name)==0) { throw new Exception("Configurator.sanityCheck(): protocol name " + name + " has been used more than once; protocol names have to be unique !"); } } } // Checks whether all requirements of all layers are met for(int i=0; i < protocols.Length; i++) { prot=(Protocol)protocols[i]; req=new ProtocolReq(prot.Name); req.up_reqs=prot.requiredUpServices(); req.down_reqs=prot.requiredDownServices(); req.up_provides=prot.providedUpServices(); req.down_provides=prot.providedDownServices(); req_list.Add(req); } for(int i=0; i < req_list.Count; i++) { req=(ProtocolReq)req_list[i]; // check whether layers above this one provide corresponding down services if(req.up_reqs != null) { for(int j=0; j < req.up_reqs.Count; j++) { evt_type=((int)req.up_reqs[j]); if(!providesDownServices(i, req_list, evt_type)) { throw new Exception("Configurator.sanityCheck(): event " + Event.type2String(evt_type) + " is required by " + req.name + ", but not provided by any of the layers above"); } } } // check whether layers below this one provide corresponding up services if(req.down_reqs != null) { // check whether layers above this one provide up_reqs for(int j=0; j < req.down_reqs.Count; j++) { evt_type=((int)req.down_reqs[j]); if(!providesUpServices(i, req_list, evt_type)) { throw new Exception("Configurator.sanityCheck(): event " + Event.type2String(evt_type) + " is required by " + req.name + ", but not provided by any of the layers below"); } } } } }
/// <remarks> /// Prootocols will be linked according to their position in the list. /// </remarks> /// <summary> /// Connects all the <c>Protocols</c> together. /// </summary> /// <param name="layer_list">List of <c>Protocol</c>s to be connected</param> /// <returns>The top-most <c>Protocol</c> in the connected stack</returns> private Protocol connectProtocols(Protocol[] layer_list) { Protocol current_layer=null, next_layer=null; for(int i=0; i < layer_list.Length; i++) { current_layer=(Protocol)layer_list[i]; if(i+1 >= layer_list.Length) break; next_layer=(Protocol)layer_list[i+1]; current_layer.UpProtocol = next_layer; next_layer.DownProtocol = current_layer; } return current_layer; }
/// <summary> /// This constructor whould be used when we want AckSenderWindow to send the message added /// by add(), rather then ourselves /// </summary> /// <param name="com">Method to be called when transmission occurs</param> /// <param name="interval">Array of intervals between retransmissions</param> /// <param name="transport">The Protocol that should be used to <code>passDown</code> the message</param> public AckSenderWindow(RetransmitCommand com, long[] interval, Protocol transport) { retransmitter=new Retransmitter(null, this); retransmit_command=com; this.interval=interval; this.transport=transport; retransmitter.setRetransmitTimeouts(interval); }
/// <summary> /// Constructor /// </summary> /// <param name="stable_prot">STABLE protocol associated with the Task</param> /// <param name="d">Stable Digest</param> /// <param name="delay">Delay before sending message</param> public StabilitySendTask(Protocol stable_prot, Digest d, long delay) { this.stable_prot=stable_prot; this.d=d; this.delay=delay; }
/// <remarks> /// The Up and Down handlers will be started. /// </remarks> /// <summary> /// Configures and initilises stack. /// </summary> public void setup() { if(top_prot == null) { top_prot = conf.setupStack(setup_string, this); if(top_prot == null) throw new Exception("ProtocolStack.setup(): couldn't create protocol stack"); top_prot.UpProtocol = (Protocol)this; bottom_prot=conf.getBottommostProtocol(top_prot); conf.startProtocolStack(bottom_prot); // sets up queues and threads } }
/// <summary> /// Destroys message queues and threads /// </summary> public void destroy() { if(top_prot != null) { conf.stopProtocolStack(top_prot); top_prot=null; } }