Ejemplo n.º 1
0
        public override bool Equals(object obj)
        {
            PingRsp rsp = obj as PingRsp;

            if (rsp == null)
            {
                return(false);
            }

            bool oe = false;

            if (own_addr != null)
            {
                oe = own_addr.Equals(rsp.own_addr);
            }
            else
            {
                oe = (own_addr == rsp.own_addr);
            }

            bool ce = false;

            if (own_addr != null)
            {
                ce = coord_addr.Equals(rsp.coord_addr);
            }
            else
            {
                ce = (coord_addr == rsp.coord_addr);
            }

            return(ce && oe);
        }
Ejemplo n.º 2
0
        public override void  down(Event evt)
        {
            Message msg;
            long    time_to_wait, start_time;

            switch (evt.Type)
            {
            case Event.FIND_INITIAL_MBRS:                      // sent by GMS layer, pass up a GET_MBRS_OK event

                //We pass this event down to tcp so that it can take some measures.
                passDown(evt);

                initial_members.Clear();
                msg = new Message(null, null, null);

                msg.putHeader(HeaderType.TCPPING, new PingHeader(PingHeader.GET_MBRS_REQ, (System.Object)local_addr, group_addr));

                // if intitial nodes have been specified and static is true, then only those
                // members will form the cluster, otherwise, nodes having the same IP Multicast and port
                // will form the cluster dyanamically.

                mbrDiscoveryInProcess = true;
                lock (members.SyncRoot)
                {
                    if (initial_hosts != null)
                    {
                        for (System.Collections.IEnumerator it = initial_hosts.GetEnumerator(); it.MoveNext();)
                        {
                            Address addr = (Address)it.Current;
                            msg.Dest = addr;
                            if (Stack.NCacheLog.IsInfoEnabled)
                            {
                                Stack.NCacheLog.Info("[FIND_INITIAL_MBRS] sending PING request to " + msg.Dest);
                            }
                            passDown(new Event(Event.MSG_URGENT, msg.copy(), Priority.Critical));
                        }
                    }
                }

                // 2. Wait 'timeout' ms or until 'num_initial_members' have been retrieved

                if (Stack.NCacheLog.IsInfoEnabled)
                {
                    Stack.NCacheLog.Info("TcpPing.down()", "[FIND_INITIAL_MBRS] waiting for results...............");
                }
                lock (initial_members.SyncRoot)
                {
                    start_time   = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;
                    time_to_wait = timeout;

                    while (initial_members.Count < num_initial_members && time_to_wait > 0)
                    {
                        try
                        {
                            if (Stack.NCacheLog.IsInfoEnabled)
                            {
                                Stack.NCacheLog.Info("TcpPing.down()", "initial_members Count: " + initial_members.Count + "initialHosts Count: " + num_initial_members);
                            }
                            if (Stack.NCacheLog.IsInfoEnabled)
                            {
                                Stack.NCacheLog.Info("TcpPing.down()", "Time to wait for next response: " + time_to_wait);
                            }
                            ///Big_clusterd: initial members will be pulsed in case connection is not available.
                            ///so here we dont have to wait till each member is timed out.
                            ///this significantly improves time for initial member discovery.
                            bool timeExpire = System.Threading.Monitor.Wait(initial_members.SyncRoot, TimeSpan.FromMilliseconds(time_to_wait));
                        }
                        catch (System.Exception e)
                        {
                            Stack.NCacheLog.Error("TCPPing.down(FIND_INITIAL_MBRS)", e.ToString());
                        }
                        time_to_wait = timeout - ((System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start_time);
                    }
                    mbrDiscoveryInProcess = false;
                }
                if (Stack.NCacheLog.IsInfoEnabled)
                {
                    Stack.NCacheLog.Info("TcpPing.down()", "[FIND_INITIAL_MBRS] initial members are " + Global.CollectionToString(initial_members));
                }
                if (Stack.NCacheLog.IsInfoEnabled)
                {
                    Stack.NCacheLog.Info("TcpPing.down()", "[FIND_INITIAL_MBRS] initial members count " + initial_members.Count);
                }

                //remove those which are not functional due to twoPhaseConnect
                for (int i = initial_members.Count - 1; i >= 0; i--)
                {
                    PingRsp rsp = initial_members[i] as PingRsp;
                    if (!rsp.IsStarted)
                    {
                        initial_members.RemoveAt(i);
                    }
                }

                // 3. Send response
                passUp(new Event(Event.FIND_INITIAL_MBRS_OK, initial_members));
                break;


            case Event.TMP_VIEW:
            case Event.VIEW_CHANGE:
                System.Collections.ArrayList tmp;
                if ((tmp = ((View)evt.Arg).Members) != null)
                {
                    lock (members.SyncRoot)
                    {
                        members.Clear();
                        members.AddRange(tmp);
                    }
                }
                passDown(evt);
                break;

            /****************************After removal of NackAck *********************************/
            //TCPPING emulates a GET_DIGEST call, which is required by GMS. This is needed
            //since we have now removed NAKACK from the stack!
            case Event.GET_DIGEST:
                pbcast.Digest digest = new pbcast.Digest(members.Count);
                for (int i = 0; i < members.Count; i++)
                {
                    Address sender = (Address)members[i];
                    digest.add(sender, 0, 0);
                }
                passUp(new Event(Event.GET_DIGEST_OK, digest));
                return;

            case Event.SET_DIGEST:
                // Not needed! Just here to let you know that it is needed by GMS!
                return;

            /********************************************************************************/

            case Event.BECOME_SERVER:                      // called after client has joined and is fully working group member
                if (Stack.NCacheLog.IsInfoEnabled)
                {
                    Stack.NCacheLog.Info("TcpPing.down()", "received BECOME_SERVER event");
                }
                passDown(evt);
                is_server = true;
                break;


            case Event.CONNECT:
                object[] addrs = ((object[])evt.Arg);
                group_addr    = (string)addrs[0];
                subGroup_addr = (string)addrs[1];

                twoPhaseConnect = (bool)addrs[3];
                if (twoPhaseConnect)
                {
                    timeout = 1000;
                }
                passDown(evt);
                break;


            case Event.DISCONNECT:
                passDown(evt);
                break;

            case Event.HAS_STARTED:
                hasStarted = true;
                passDown(evt);
                break;

            default:
                passDown(evt);                         // Pass on to the layer below us
                break;
            }
        }
Ejemplo n.º 3
0
        public override void  up(Event evt)
        {
            Message msg, rsp_msg;

            System.Object obj;
            PingHeader    hdr, rsp_hdr;
            PingRsp       rsp;
            Address       coord;


            switch (evt.Type)
            {
            case Event.MSG:
                msg = (Message)evt.Arg;

                obj = msg.getHeader(HeaderType.TCPPING);
                if (obj == null || !(obj is PingHeader))
                {
                    passUp(evt);
                    return;
                }

                hdr = (PingHeader)msg.removeHeader(HeaderType.TCPPING);

                switch (hdr.type)
                {
                case PingHeader.GET_MBRS_REQ:


                    if (!hdr.group_addr.Equals(group_addr))
                    {
                        if (Stack.NCacheLog.IsInfoEnabled)
                        {
                            Stack.NCacheLog.Info("TcpPing.up()", "GET_MBRS_REQ from different group , so discarded");
                        }
                        return;
                    }
                    Address src = (Address)hdr.arg;
                    msg.Src = src;

                    if (Stack.NCacheLog.IsInfoEnabled)
                    {
                        Stack.NCacheLog.Info("TCPPING.up()", "GET_MBRS_REQ from " + msg.Src.ToString());
                    }


                    lock (members.SyncRoot)
                    {
                        coord = members.Count > 0 ? (Address)members[0] : local_addr;
                    }
                    if (Stack.NCacheLog.IsInfoEnabled)
                    {
                        Stack.NCacheLog.Info("TCPPING.up()", "my coordinator is " + coord.ToString());
                    }

                    rsp_msg = new Message(msg.Src, null, null);
                    rsp_hdr = new PingHeader(PingHeader.GET_MBRS_RSP, new PingRsp(local_addr, coord, Stack.IsOperational, Stack.IsOperational));
                    rsp_msg.putHeader(HeaderType.TCPPING, rsp_hdr);
                    if (Stack.NCacheLog.IsInfoEnabled)
                    {
                        Stack.NCacheLog.Info("TCPPING.up()", "responding to GET_MBRS_REQ back to " + msg.Src.ToString());
                    }

                    if (Stack.NCacheLog.IsInfoEnabled)
                    {
                        Stack.NCacheLog.Info(local_addr + " - [FIND_INITIAL_MBRS] replying PING request to " + rsp_msg.Dest);
                    }


                    passDown(new Event(Event.MSG, rsp_msg, Priority.Critical));

                    return;


                case PingHeader.GET_MBRS_RSP:                          // add response to vector and notify waiting thread


                    if (Stack.NCacheLog.IsInfoEnabled)
                    {
                        Stack.NCacheLog.Info("TCPPING.up()", "GET_MBRS_RSP from " + msg.Src.ToString());
                    }
                    rsp = (PingRsp)hdr.arg;


                    //muds:
                    //check if the received response is valid i.e. successful security authorization
                    //at other end.
                    if (rsp.OwnAddress == null && rsp.CoordAddress == null && rsp.HasJoined == false)
                    {
                        lock (initial_members.SyncRoot)
                        {
                            if (Stack.NCacheLog.IsInfoEnabled)
                            {
                                Stack.NCacheLog.Info("TCPPING.up()", "I am not authorized to join to " + msg.Src.ToString());
                            }
                            System.Threading.Monitor.PulseAll(initial_members.SyncRoot);
                        }
                    }
                    else
                    {
                        if (Stack.NCacheLog.IsInfoEnabled)
                        {
                            Stack.NCacheLog.Info("TCPPING.up()", "Before Adding initial members response");
                        }
                        lock (initial_members.SyncRoot)
                        {
                            if (Stack.NCacheLog.IsInfoEnabled)
                            {
                                Stack.NCacheLog.Info("TCPPING.up()", "Adding initial members response");
                            }
                            if (!initial_members.Contains(rsp))
                            {
                                initial_members.Add(rsp);
                                if (Stack.NCacheLog.IsInfoEnabled)
                                {
                                    Stack.NCacheLog.Info("TCPPING.up()", "Adding initial members response for " + rsp.OwnAddress);
                                }
                            }
                            else
                            if (Stack.NCacheLog.IsInfoEnabled)
                            {
                                Stack.NCacheLog.Info("TcpPing.up()", "response already received");
                            }

                            System.Threading.Monitor.PulseAll(initial_members.SyncRoot);
                        }
                    }


                    return;


                default:
                    Stack.NCacheLog.Warn("got TCPPING header with unknown type (" + hdr.type + ')');
                    return;
                }
            //goto case Event.SET_LOCAL_ADDRESS;



            case Event.SET_LOCAL_ADDRESS:
                passUp(evt);
                local_addr = (Address)evt.Arg;
                // Add own address to initial_hosts if not present: we must always be able to ping ourself !
                if (initial_hosts != null && local_addr != null)
                {
                    if (!initial_hosts.Contains(local_addr))
                    {
                        Stack.NCacheLog.Debug("[SET_LOCAL_ADDRESS]: adding my own address (" + local_addr + ") to initial_hosts; initial_hosts=" + Global.CollectionToString(initial_hosts));
                        initial_hosts.Add(local_addr);
                    }
                }
                break;

            case Event.CONNECT_OK:
                obj = evt.Arg;

                if (obj != null && obj is Address)
                {
                    tcpServerPort = ((Address)obj).Port;
                }
                passUp(evt);
                break;

            case Event.CONNECTION_NOT_OPENED:
                if (mbrDiscoveryInProcess)
                {
                    Address node     = evt.Arg as Address;
                    PingRsp response = new PingRsp(node, node, true, false);
                    lock (initial_members.SyncRoot)
                    {
                        initial_members.Add(response);
                        System.Threading.Monitor.PulseAll(initial_members.SyncRoot);
                    }
                    Stack.NCacheLog.CriticalInfo(Name + ".up", "connection failure with " + node);
                }

                break;

            // end services
            default:
                passUp(evt);                         // Pass up to the layer above us
                break;
            }
        }
Ejemplo n.º 4
0
		public override void  up(Event evt)
		{
			Message msg, rsp_msg;
			System.Object obj;
			PingHeader hdr, rsp_hdr;
			PingRsp rsp;
			Address coord;
			
			
			switch (evt.Type)
			{
				 
				case Event.MSG: 
					msg = (Message) evt.Arg;
                   
                    obj = msg.getHeader(HeaderType.TCPPING);
					if (obj == null || !(obj is PingHeader))
					{
						passUp(evt);
						return ;
					}
                    
                    hdr = (PingHeader)msg.removeHeader(HeaderType.TCPPING);
					
				switch (hdr.type)
				{
											
					case PingHeader.GET_MBRS_REQ:  

                       
						if( !hdr.group_addr.Equals(group_addr))
						{
                            if (Stack.NCacheLog.IsInfoEnabled) Stack.NCacheLog.Info("TcpPing.up()",   "GET_MBRS_REQ from different group , so discarded");
							return;
						}
						Address src = (Address) hdr.arg;
						msg.Src = src;

						if(Stack.NCacheLog.IsInfoEnabled) Stack.NCacheLog.Info("TCPPING.up()",  "GET_MBRS_REQ from " + msg.Src.ToString());

						
						lock (members.SyncRoot)
						{
							coord = members.Count > 0 ? (Address)members[0] : local_addr;
						}
						if(Stack.NCacheLog.IsInfoEnabled) Stack.NCacheLog.Info("TCPPING.up()",   "my coordinator is " + coord.ToString());

						rsp_msg = new Message(msg.Src, null, null);
						rsp_hdr = new PingHeader(PingHeader.GET_MBRS_RSP, new PingRsp(local_addr, coord, Stack.IsOperational,Stack.IsOperational));
						rsp_msg.putHeader(HeaderType.TCPPING, rsp_hdr);
						if(Stack.NCacheLog.IsInfoEnabled) Stack.NCacheLog.Info("TCPPING.up()",   "responding to GET_MBRS_REQ back to " + msg.Src.ToString());

                       if (Stack.NCacheLog.IsInfoEnabled) Stack.NCacheLog.Info(local_addr + " - [FIND_INITIAL_MBRS] replying PING request to " + rsp_msg.Dest);

                        
						passDown(new Event(Event.MSG, rsp_msg, Priority.Critical));

return ;
						
						
					case PingHeader.GET_MBRS_RSP:  // add response to vector and notify waiting thread
                       

						if(Stack.NCacheLog.IsInfoEnabled) Stack.NCacheLog.Info("TCPPING.up()",  "GET_MBRS_RSP from " + msg.Src.ToString());
						rsp = (PingRsp) hdr.arg;


                        //muds:
                        //check if the received response is valid i.e. successful security authorization
                        //at other end.
                        if (rsp.OwnAddress == null && rsp.CoordAddress == null && rsp.HasJoined == false)
                        {
                            lock (initial_members.SyncRoot)
                            {
                                if (Stack.NCacheLog.IsInfoEnabled) Stack.NCacheLog.Info("TCPPING.up()", "I am not authorized to join to " + msg.Src.ToString());
                                System.Threading.Monitor.PulseAll(initial_members.SyncRoot);
                            }
                        }
                        else
                        {
                            if (Stack.NCacheLog.IsInfoEnabled) Stack.NCacheLog.Info("TCPPING.up()", "Before Adding initial members response");
                            lock (initial_members.SyncRoot)
                            {
                                if (Stack.NCacheLog.IsInfoEnabled) Stack.NCacheLog.Info("TCPPING.up()", "Adding initial members response");
                                if (!initial_members.Contains(rsp))
                                {
                                    initial_members.Add(rsp);
                                    if (Stack.NCacheLog.IsInfoEnabled) Stack.NCacheLog.Info("TCPPING.up()", "Adding initial members response for " + rsp.OwnAddress);
                                }
                                else
                                    if (Stack.NCacheLog.IsInfoEnabled) Stack.NCacheLog.Info("TcpPing.up()", "response already received");

                                System.Threading.Monitor.PulseAll(initial_members.SyncRoot);
                            }
                        }

                       
						return ;
						
						
					default:
                        Stack.NCacheLog.Warn("got TCPPING header with unknown type (" + hdr.type + ')');
						return ;
						
				}
					//goto case Event.SET_LOCAL_ADDRESS;
				
				
				
				case Event.SET_LOCAL_ADDRESS: 
					passUp(evt);
					local_addr = (Address) evt.Arg;
					// Add own address to initial_hosts if not present: we must always be able to ping ourself !
					if (initial_hosts != null && local_addr != null)
					{
						if (!initial_hosts.Contains(local_addr))
						{
                            Stack.NCacheLog.Debug("[SET_LOCAL_ADDRESS]: adding my own address (" + local_addr + ") to initial_hosts; initial_hosts=" + Global.CollectionToString(initial_hosts));
							initial_hosts.Add(local_addr);
						}
					}
					break;
				case Event.CONNECT_OK: 
					obj = evt.Arg;

					if(obj != null && obj is Address)
					{
						tcpServerPort = ((Address)obj).Port;
					}
					passUp(evt);
					break;
                case Event.CONNECTION_NOT_OPENED:
                    if (mbrDiscoveryInProcess)
                    {
                        Address node = evt.Arg as Address;
                        PingRsp response = new PingRsp(node, node, true, false);
                        lock (initial_members.SyncRoot)
                        {
                            initial_members.Add(response);
                            System.Threading.Monitor.PulseAll(initial_members.SyncRoot);
                        }
                        Stack.NCacheLog.CriticalInfo(Name + ".up", "connection failure with " + node);
                    }

                    break;
				// end services
				default: 
					passUp(evt); // Pass up to the layer above us
					break;
				
			}
		}