/// <summary> /// Processes <c>Events</c> traveling down the stack /// </summary> /// <param name="evt">The Event to be processed</param> public override void down(Event evt) { Message msg; double r; if (evt.Type == Event.MSG) { msg = (Message) evt.Arg; if (probDown > 0) { r = (double)((double)random.Next(100)/(double)100); if (r < probDown) { if (excludeItself && msg.Source.Equals(localAddress)) { if (Trace.trace) Trace.info("DISCARD.down()", "excluding itself"); } else { if (Trace.trace) Trace.info("DISCARD.down()", "dropping message"); return; } } } } passDown(evt); }
public override void down(Event evt) { Console.WriteLine(name.PadRight(5) + " [DOWN]: " + Event.type2String(evt.Type)); if (down_hold && evt.Type == Event.MSG) return; passDown(evt); }
public override void up(Event evt) { Console.WriteLine(name.PadRight(5) + " [UP] : " + Event.type2String(evt.Type)); switch(evt.Type) { case(Event.SET_LOCAL_ADDRESS): { localAddr = (Address)evt.Arg; break; } } passUp(evt); }
public override void up(Event evt) { Console.WriteLine(name.PadRight(5) + " [UP] : " + Event.type2String(evt.Type)); if (up_hold && evt.Type == Event.MSG) { //Thread.Sleep(10000); for(int i = 0; i<10000000;i++) { Console.Write(""); } Console.WriteLine(""); Console.WriteLine("FINISHED HOLDING"); Console.WriteLine(""); } passUp(evt); }
/// <summary> /// Processes <c>Events</c> traveling down the stack /// </summary> /// <param name="evt">The Event to be processed</param> public override void down(Event evt) { switch (evt.Type) { case Event.MSG: Message msg = (Message)evt.Arg; if(msg.Destination != null && msg.Destination.isMulticastAddress()) break; localStamp.increment(); //System.Diagnostics.Trace.WriteLine("Incremented LTS = " + localStamp.getLocalTimeStamp()); Header hdr = new CausalHeader(localStamp.getTransportableVectorTimeStamp()); msg.putHeader(getName(),hdr); break; } passDown(evt); }
public override void up(Event evt) { Console.WriteLine(name.PadRight(5) + " [UP] : " + Event.type2String(evt.Type)); switch(evt.Type) { case(Event.GET_DIGEST): { Thread.Sleep(10000); Digest d = new Digest(1); d.add(localAddr,0,0,0); evt = new Event(Event.GET_DIGEST_OK,d); passDown(evt); break; } case(Event.SET_LOCAL_ADDRESS): { localAddr = (Address)evt.Arg; return; } } }
public override void down(Event evt) { Console.WriteLine(name.PadRight(5) + " [DOWN]: " + Event.type2String(evt.Type)); Event tmpEvent = evt; switch(evt.Type) { case(Event.SET_LOCAL_ADDRESS): { localAddr = (Address)evt.Arg; return; } case(Event.MSG): { if(((Message)tmpEvent.Arg).Source==null) ((Message)tmpEvent.Arg).Source = localAddr; if(((Message)tmpEvent.Arg).Destination==null) passUp(tmpEvent); break; } } }
public override void down(Event evt) { Console.WriteLine(name.PadRight(5) + " [DOWN]: " + Event.type2String(evt.Type)); passDown(evt); }
/// <summary> /// Processes <c>Events</c> traveling up the stack /// </summary> /// <param name="evt">The Event to be processed</param> public override void up(Event evt) { Message msg; double r; if (evt.Type == Event.SET_LOCAL_ADDRESS) localAddress = (Address) evt.Arg; if (evt.Type == Event.MSG) { msg = (Message) evt.Arg; if (probUp > 0) { r = (double)((double)random.Next(100)/(double)100); if (r < probUp) { if (excludeItself && msg.Source.Equals(localAddress)) { if (Trace.trace) Trace.info("DISCARD.up()", "excluding itself"); } else { if (Trace.trace) Trace.info("DISCARD.up()", "dropping message"); return; } } } } passUp(evt); }
/// <summary> /// Called by the ProtocolSinkStack when messages are recevied. /// </summary> /// <param name="evt">Event that has been received.</param> public void up(Event evt) { int type=evt.Type; Message msg; /*if the queue is not available, there is no point in *processing the message at all*/ if(mq == null) { if(Trace.trace) Trace.error("GroupChannel.up()", "message queue is null"); return; } switch(type) { case Event.MSG: msg=(Message)evt.Arg; if(!receive_local_msgs) { // discard local messages (sent by myself to me) if(local_addr != null && msg.Source != null) if(local_addr.Equals(msg.Source)) return; } break; case Event.VIEW_CHANGE: my_view=(View)evt.Arg; if(!receive_views) // discard if client has not set receving views to on return; break; case Event.SUSPECT: if(!receive_suspects) return; break; case Event.CONNECT_OK: lock(connect_mutex) { Monitor.Pulse(connect_mutex); } break; case Event.DISCONNECT_OK: lock(disconnect_mutex) { Monitor.Pulse(disconnect_mutex); } break; case Event.SET_LOCAL_ADDRESS: local_addr=(Address)evt.Arg; break; case Event.EXIT: handleExit(evt); return; // no need to pass event up; already done in handleExit() default: break; } // If UpHandler is installed, pass all events to it and return (UpHandler is e.g. a building block) if(up_handler != null) { up_handler.up(evt); return; } if(type == Event.MSG || type == Event.VIEW_CHANGE || type == Event.SUSPECT || type == Event.EXIT) //type == Event.GET_APPLSTATE || type == Event.BLOCK { try { mq.Add(evt); } catch(Exception e) { if(Trace.trace) Trace.error("GroupChannel.up()", "exception: " + e); } } }
// -=-=-=-=-=-=-=- Channel Methods -=-=-=-=-=-=-=-=-=-=-=- /// <summary> /// Sends an Event down the stack /// </summary> /// <param name="evt">Event to send down</param> public override void down(Event evt) { if(prot_stack != null) prot_stack.down(evt); else if(Trace.trace) Trace.error("GroupChannel.down()", "no protocol stack available"); }
/// <summary> /// Disconnect from current group. /// </summary> public override void disconnect() { if(connected) { /* Send down a DISCONNECT event. The DISCONNECT event travels down to the GMS, where a * DISCONNECT_OK response is generated and sent up the stack. GroupChannel blocks until a * DISCONNECT_OK has been received, or until timeout has elapsed. */ lock(disconnect_mutex) { try { Event disconnect_event=new Event(Event.DISCONNECT, local_addr); down(disconnect_event); // DISCONNECT is handled by each layer Monitor.Wait(disconnect_mutex,disconnectTimeout,true); // wait for DISCONNECT_OK event } catch(Exception e) { if(Trace.trace) Trace.error("GroupChannel.disconnect()", "exception: " + e); } } /*stop the protocol stack*/ try { prot_stack.stop(); } catch(Exception e) { if(Trace.trace) Trace.error("GroupChannel.disconnect()", "exception: " + e); } connected=false; if(channel_listener != null) channel_listener.channelDisconnected(this); } }
/// <summary> /// Connects the channel the specified group /// </summary> /// <param name="channel_name">Group to connect to.</param> public override void connect(String channel_name) { if (!connected) { /*make sure we have a valid channel name*/ if(channel_name == null) { if(Trace.trace) Trace.error("GroupChannel.connect()", "channel_name is null"); return; } else this.channel_name = channel_name; prot_stack.start(); /* Wait LOCAL_ADDR_TIMEOUT milliseconds for local_addr to have a non-null value (set by SET_LOCAL_ADDRESS) */ lock(local_addr_mutex) { long wait_time=LOCAL_ADDR_TIMEOUT; long start = System.Environment.TickCount; while(local_addr == null && wait_time > 0) { try { Monitor.Wait(local_addr_mutex,(int)wait_time); } catch(ThreadInterruptedException ex) {;} wait_time-=System.Environment.TickCount - start; } } // ProtocolStack.start() must have given us a valid local address; if not we won't be able to continue if(local_addr == null) { if(Trace.trace) Trace.error("GroupChannel.connect()", "local_addr == null; cannot connect"); throw new Exception("local_addr is null"); } ArrayList t=new ArrayList(); t.Add(local_addr); my_view=new View(local_addr, 0, t); // create a dummy view /* Send down a CONNECT event. The CONNECT event travels down to the GMS, where a * CONNECT_OK response is generated and sent up the stack. GroupChannel blocks until a * CONNECT_OK has been received, or until timeout has elapsed. */ //Event connect_event = new Event(Event.CONNECT, channel_name); //down(connect_event); lock(connect_mutex) { try { Event connect_event = new Event(Event.CONNECT, channel_name); down(connect_event); // CONNECT is handled by each layer Monitor.Wait(connect_mutex,connectTimeout,true); // wait for CONNECT_OK event } catch(Exception e) { if(Trace.trace) Trace.error("GroupChannel.connect()", "exception: " + e); } } connected = true; /*notify any channel listeners*/ connected=true; if(channel_listener != null) channel_listener.channelConnected(this); } }
/// <summary> /// Passes an event down the protocol stack /// </summary> /// <param name="evt">Event to be passed down the stack</param> public virtual void down(Event evt) { }
static void Main(string[] args) { Tester.SetupDebug(); NAKACK nak = new NAKACK(); Protocol bottom = null; Protocol top = Tester.createStack(nak, out bottom); Address remote1 = new Address("1.1.1.1",8005); Address remote2 = new Address("1.1.1.2",8005); Address remote3 = new Address("1.1.1.3",8005); ArrayList members = new ArrayList(); members.Add(Tester.localAddr); members.Add(remote1); members.Add(remote2); members.Add(remote3); View newView = new View(new ViewId(Tester.localAddr,0),members); evt = new Event(Event.VIEW_CHANGE,newView); top.down(evt); evt = new Event(Event.MSG,new Message(null,null,"Message 1")); top.down(evt); evt = new Event(Event.MSG,new Message(null,null,"Message 2")); top.down(evt); evt = new Event(Event.MSG,new Message(null,null,"Message 3")); top.down(evt); Console.WriteLine("-=-=-=-=- Received Msg 0 from Remote 1 -=-=-=-=-=-"); Message msg = new Message(null,remote1,"Incoming Message 0"); msg.Headers.Add("NAKACK",new NakAckHeader(NakAckHeader.MSG, 0)); evt = new Event(Event.MSG,msg); bottom.up(evt); Console.WriteLine("-=-=-=-=- Received Msg 0 from Remote 2 -=-=-=-=-=-"); msg = new Message(null,remote2,"Incoming Message 0"); msg.Headers.Add("NAKACK",new NakAckHeader(NakAckHeader.MSG, 0)); evt = new Event(Event.MSG,msg); bottom.up(evt); Console.WriteLine("-=-=-=-=- Received Msg 1 from Remote 1 -=-=-=-=-=-"); msg = new Message(null,remote1,"Incoming Message 1"); msg.Headers.Add("NAKACK",new NakAckHeader(NakAckHeader.MSG, 1)); evt = new Event(Event.MSG,msg); bottom.up(evt); Console.WriteLine("-=-=-=-=- Received Msg 2 from Remote 2 -=-=-=-=-=-"); msg = new Message(null,remote2,"Incoming Message 2"); msg.Headers.Add("NAKACK",new NakAckHeader(NakAckHeader.MSG, 2)); evt = new Event(Event.MSG,msg); bottom.up(evt); Thread.Sleep(20000); Console.WriteLine("-=-=-=-=- Received Msg 1 from Remote 2 -=-=-=-=-=-"); msg = new Message(null,remote2,"Incoming Message 1"); msg.Headers.Add("NAKACK",new NakAckHeader(NakAckHeader.MSG, 1)); evt = new Event(Event.MSG,msg); bottom.up(evt); Thread.Sleep(3000); Console.WriteLine("-=-=-=-=- Receiving STABLE event -=-=-=-=-=-"); Digest digest = new Digest(4); // 4 members digest.add(Tester.localAddr, 0, 2, 2); digest.add(remote1, 0, 1, 1); digest.add(remote2, 0, 2, 2); digest.add(remote3, 0, 0, 0); evt = new Event(Event.STABLE,digest); bottom.up(evt); Console.ReadLine(); Tester.stopProtocols(top); Console.ReadLine(); }
/// <summary> /// Delivers exit event to UpHandler. /// </summary> /// <param name="evt"></param> private void handleExit(Event evt) { if(channel_listener != null) channel_listener.channelShunned(); exitEvent = evt; Thread closerThrd = new Thread(new ThreadStart(closerThread)); closerThrd.Name = "CloserThread"; closerThrd.IsBackground = true; closerThrd.Start(); }
/// <summary> /// Processes <c>Events</c> traveling up the stack /// </summary> /// <param name="evt">The Event to be processed</param> public override void up(Event evt) { switch(evt.Type) { case Event.SET_LOCAL_ADDRESS: localAddress = (Address) evt.Arg; localStamp = new VectorTimeStamp(localAddress); delayQueue = new ArrayList(); break; case Event.VIEW_CHANGE: ArrayList newViewMembers = ((View) evt.Arg).getMembers(); localStamp.merge((ArrayList) newViewMembers.Clone()); localStamp.reset(); break; case Event.MSG: Message msg = (Message)evt.Arg; Header hdr = msg.getHeader(getName()); //Check that the correct header is available if(hdr == null || !(hdr is CausalHeader)) { if(Trace.trace) Trace.error("Causal.up()","No Causal Header Found!"); passUp(evt); return; } CausalHeader cHdr = (CausalHeader)hdr; //Pass message up if it is causually next String diag = ""; bool next = localStamp.causallyNext(cHdr.VectorTime, out diag); //passUp(new Event(Event.MSG, new Message(null,localAddress,diag))); if(Trace.trace) Trace.info("CAUSAL.up()", diag + "Causally Next = " + next); if(next) { if(Trace.trace) Trace.info("CAUSAL.up()","Message received in sequence."); passUp(evt); localStamp.max(cHdr.VectorTime); } else //Else add to the delayed queue { if(Trace.trace) Trace.warn("CAUSAL.up()","Message received out of sequence."); cHdr.VectorTime.AssosicatedMessage = msg; addToQueue(cHdr.VectorTime); } //Check the delayed queue removing all items that can be passed up TransportableVectorTimeStamp queuedVector = null; while ((delayQueue.Count > 0) && localStamp.causallyNext((queuedVector = (TransportableVectorTimeStamp)delayQueue[0]), out diag)) { delayQueue.Remove(queuedVector); passUp(new Event(Event.MSG, queuedVector.AssosicatedMessage)); localStamp.max(queuedVector); } return; } passUp(evt); }
static void Main(string[] args) { Tester.SetupDebug(); Event evt = null; UNICAST uni = new UNICAST(); Protocol bottom = null; Protocol top = Tester.createStack(uni, out bottom); Address remote1 = new Address("1.1.1.1",8005); Address remote2 = new Address("1.1.1.2",8005); Address remote3 = new Address("1.1.1.3",8005); Console.WriteLine("-=-=-=-=- Send msgs 0-2 to Remote 1 -=-=-=-=-=-"); evt = new Event(Event.MSG,new Message(remote1,null,"Message 1")); top.down(evt); evt = new Event(Event.MSG,new Message(remote1,null,"Message 2")); top.down(evt); evt = new Event(Event.MSG,new Message(remote1,null,"Message 3")); top.down(evt); Thread.Sleep(7000); Console.WriteLine("-=-=-=-=- Ack 0 from Remote 1 -=-=-=-=-=-"); Message msg = new Message(Tester.localAddr,remote1,"Incoming Message 0"); msg.Headers.Add("UNICAST",new UnicastHeader(UnicastHeader.DATA_ACK, 0)); evt = new Event(Event.MSG,msg); bottom.up(evt); Console.WriteLine("-=-=-=-=- Ack 1 from Remote 1 -=-=-=-=-=-"); msg = new Message(Tester.localAddr,remote1,"Incoming Message 0"); msg.Headers.Add("UNICAST",new UnicastHeader(UnicastHeader.DATA_ACK, 1)); evt = new Event(Event.MSG,msg); bottom.up(evt); Thread.Sleep(7000); Console.WriteLine("-=-=-=-=- Ack 2 from Remote 1 -=-=-=-=-=-"); msg = new Message(Tester.localAddr,remote1,"Incoming Message 0"); msg.Headers.Add("UNICAST",new UnicastHeader(UnicastHeader.DATA_ACK, 2)); evt = new Event(Event.MSG,msg); bottom.up(evt); Thread.Sleep(2000); Console.WriteLine("-=-=-=-=- Recv msg 0 from Remote 1 -=-=-=-=-=-"); msg = new Message(Tester.localAddr,remote1,"Incoming Message 0"); msg.Headers.Add("UNICAST",new UnicastHeader(UnicastHeader.DATA, 0)); evt = new Event(Event.MSG,msg); bottom.up(evt); Console.WriteLine("-=-=-=-=- Recv msg 1 from Remote 1 -=-=-=-=-=-"); msg = new Message(Tester.localAddr,remote1,"Incoming Message 1"); msg.Headers.Add("UNICAST",new UnicastHeader(UnicastHeader.DATA, 1)); evt = new Event(Event.MSG,msg); bottom.up(evt); Console.WriteLine("-=-=-=-=- Recv msg 3 from Remote 1 -=-=-=-=-=-"); msg = new Message(Tester.localAddr,remote1,"Incoming Message 1"); msg.Headers.Add("UNICAST",new UnicastHeader(UnicastHeader.DATA, 3)); evt = new Event(Event.MSG,msg); bottom.up(evt); Thread.Sleep(10000); Console.WriteLine("-=-=-=-=- Recv msg 2 from Remote 1 -=-=-=-=-=-"); msg = new Message(Tester.localAddr,remote1,"Incoming Message 1"); msg.Headers.Add("UNICAST",new UnicastHeader(UnicastHeader.DATA, 2)); evt = new Event(Event.MSG,msg); bottom.up(evt); Thread.Sleep(10000); }