Exemplo n.º 1
0
        /// <summary>
        /// Processes <c>Events</c> travelling up the stack
        /// </summary>
        /// <param name="evt">The Event to be processed</param>
        public override void up(Event evt)
        {
            Message    msg, rsp_msg;
            Object     obj;
            PINGHeader hdr, rsp_hdr;
            PingRsp    rsp;
            Address    coord;

            switch (evt.Type)
            {
            case Event.MSG:
                msg = (Message)evt.Arg;
                obj = msg.getHeader(name);
                if (obj == null || !(obj is PINGHeader))
                {
                    passUp(evt);
                    return;
                }
                hdr = (PINGHeader)msg.removeHeader(name);

                switch (hdr.type)
                {
                case PINGHeader.GET_MBRS_REQ:                           // return Rsp(local_addr, coord)
                    if (!is_server)
                    {
                        return;
                    }
                    lock (members)
                    {
                        coord = members.Count > 0 ? (Address)members[0] : local_addr;
                    }
                    rsp_msg = new Message(msg.Source, null, null);
                    rsp_hdr = new PINGHeader(PINGHeader.GET_MBRS_RSP, new PingRsp(local_addr, coord));
                    rsp_msg.putHeader(name, rsp_hdr);
                    if (Trace.trace)
                    {
                        Trace.info("PING.up()", "received GET_MBRS_REQ from " + msg.Source + ", returning " + rsp_hdr);
                    }
                    passDown(new Event(Event.MSG, rsp_msg));
                    return;

                case PINGHeader.GET_MBRS_RSP:                           // add response to vector and notify waiting thread
                    rsp = (PingRsp)hdr.arg;
                    lock (initial_members)
                    {
                        if (Trace.trace)
                        {
                            Trace.info("PING.up()", "received FIND_INITAL_MBRS_RSP, rsp=" + rsp);
                        }
                        initial_members.Add(rsp);
                        Monitor.Pulse(initial_members);
                    }
                    return;

                default:
                    if (Trace.trace)
                    {
                        Trace.warn("PING.up()", "got PING header with unknown type (" + hdr.type + ")");
                    }
                    return;
                }

            case Event.SET_LOCAL_ADDRESS:
                passUp(evt);
                local_addr = (Address)evt.Arg;
                break;

            default:
                passUp(evt);                                    // Pass up to the layer above us
                break;
            }
        }
Exemplo n.º 2
0
        /// <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;
            PINGHeader		hdr;
            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

                    initial_members.Clear();

                    // 1. Mcast GET_MBRS_REQ message
                    if(Trace.trace) Trace.info("PING.down()", "FIND_INITIAL_MBRS");
                    hdr=new PINGHeader(PINGHeader.GET_MBRS_REQ, null);
                    msg=new Message(null, null, null);  // mcast msg
                    msg.putHeader(name, hdr);
                    passDown(new Event(Event.MSG, msg));

                    // 2. Wait 'timeout' ms or until 'num_initial_members' have been retrieved
                    lock(initial_members)
                    {
                        start_time = System.Environment.TickCount;
                        time_to_wait=timeout;

                        while(initial_members.Count < num_initial_members && time_to_wait > 0)
                        {

                            if(Trace.trace)
                                Trace.info("PING.down()", "waiting for initial members: time_to_wait=" + time_to_wait +
                                ", got " + initial_members.Count + " rsps");

                            Monitor.Wait(initial_members, (int)time_to_wait);
                            time_to_wait -= System.Environment.TickCount - start_time;
                        }

                        if(Trace.trace)
                            Trace.info("PING.down()", "No longer waiting for members as Initial Members = " + initial_members.Count.ToString()  + "|" + num_initial_members + "   TimeToWait = " + time_to_wait );
                    }

                    // 3. Send response
                    if(Trace.trace)
                        Trace.info("PING.down()", "initial mbrs are " + initialMembersString());
                    passUp(new Event(Event.FIND_INITIAL_MBRS_OK, initial_members));
                    break;
                //case Event.TMP_VIEW:
                case Event.VIEW_CHANGE:
                    ArrayList tmp;
                    if((tmp=((View)evt.Arg).getMembers()) != null)
                    {
                        lock(members)
                        {
                            members.Clear();
                            for(int i=0; i < tmp.Count; i++)
                                members.Add(tmp[i]);
                        }
                    }
                    passDown(evt);
                    break;

                case Event.BECOME_SERVER: // called after client has joined and is fully working group member
                    passDown(evt);
                    is_server=true;
                    break;

                case Event.CONNECT:
                    group_addr=(String)evt.Arg;
                    passDown(evt);
                    break;
                default:
                    passDown(evt);          // Pass on to the layer below us
                    break;
            }
        }
Exemplo n.º 3
0
        /// <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;
            PINGHeader hdr;
            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

                initial_members.Clear();

                // 1. Mcast GET_MBRS_REQ message
                if (Trace.trace)
                {
                    Trace.info("PING.down()", "FIND_INITIAL_MBRS");
                }
                hdr = new PINGHeader(PINGHeader.GET_MBRS_REQ, null);
                msg = new Message(null, null, null);                        // mcast msg
                msg.putHeader(name, hdr);
                passDown(new Event(Event.MSG, msg));


                // 2. Wait 'timeout' ms or until 'num_initial_members' have been retrieved
                lock (initial_members)
                {
                    start_time   = System.Environment.TickCount;
                    time_to_wait = timeout;

                    while (initial_members.Count < num_initial_members && time_to_wait > 0)
                    {
                        if (Trace.trace)
                        {
                            Trace.info("PING.down()", "waiting for initial members: time_to_wait=" + time_to_wait +
                                       ", got " + initial_members.Count + " rsps");
                        }

                        Monitor.Wait(initial_members, (int)time_to_wait);
                        time_to_wait -= System.Environment.TickCount - start_time;
                    }

                    if (Trace.trace)
                    {
                        Trace.info("PING.down()", "No longer waiting for members as Initial Members = " + initial_members.Count.ToString() + "|" + num_initial_members + "   TimeToWait = " + time_to_wait);
                    }
                }

                // 3. Send response
                if (Trace.trace)
                {
                    Trace.info("PING.down()", "initial mbrs are " + initialMembersString());
                }
                passUp(new Event(Event.FIND_INITIAL_MBRS_OK, initial_members));
                break;

            //case Event.TMP_VIEW:
            case Event.VIEW_CHANGE:
                ArrayList tmp;
                if ((tmp = ((View)evt.Arg).getMembers()) != null)
                {
                    lock (members)
                    {
                        members.Clear();
                        for (int i = 0; i < tmp.Count; i++)
                        {
                            members.Add(tmp[i]);
                        }
                    }
                }
                passDown(evt);
                break;

            case Event.BECOME_SERVER:                     // called after client has joined and is fully working group member
                passDown(evt);
                is_server = true;
                break;

            case Event.CONNECT:
                group_addr = (String)evt.Arg;
                passDown(evt);
                break;

            default:
                passDown(evt);                                  // Pass on to the layer below us
                break;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Processes <c>Events</c> travelling up the stack
        /// </summary>
        /// <param name="evt">The Event to be processed</param>
        public override void up(Event evt)
        {
            Message      msg, rsp_msg;
            Object       obj;
            PINGHeader   hdr, rsp_hdr;
            PingRsp      rsp;
            Address      coord;

            switch(evt.Type)
            {
                case Event.MSG:
                    msg=(Message)evt.Arg;
                    obj=msg.getHeader(name);
                    if(obj == null || !(obj is PINGHeader))
                    {
                        passUp(evt);
                        return;
                    }
                    hdr=(PINGHeader)msg.removeHeader(name);

                switch(hdr.type)
                {

                    case PINGHeader.GET_MBRS_REQ:   // return Rsp(local_addr, coord)
                        if(!is_server)
                        {
                            return;
                        }
                        lock(members)
                        {
                            coord=members.Count > 0 ? (Address)members[0] : local_addr;
                        }
                        rsp_msg=new Message(msg.Source, null, null);
                        rsp_hdr=new PINGHeader(PINGHeader.GET_MBRS_RSP, new PingRsp(local_addr, coord));
                        rsp_msg.putHeader(name, rsp_hdr);
                        if(Trace.trace) Trace.info("PING.up()", "received GET_MBRS_REQ from " + msg.Source+ ", returning " + rsp_hdr);
                        passDown(new Event(Event.MSG, rsp_msg));
                        return;

                    case PINGHeader.GET_MBRS_RSP:   // add response to vector and notify waiting thread
                        rsp=(PingRsp)hdr.arg;
                        lock(initial_members)
                        {
                            if(Trace.trace)
                                Trace.info("PING.up()", "received FIND_INITAL_MBRS_RSP, rsp=" + rsp);
                            initial_members.Add(rsp);
                            Monitor.Pulse(initial_members);
                        }
                        return;

                    default:
                        if(Trace.trace)
                            Trace.warn("PING.up()", "got PING header with unknown type (" + hdr.type + ")");
                        return;
                }

                case Event.SET_LOCAL_ADDRESS:
                    passUp(evt);
                    local_addr=(Address)evt.Arg;
                    break;

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