示例#1
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 void RemovePeer(Peer peer)
 {
     peers.Remove(peer.name);
     for (int i = 0; i < replayTimeout.Count; )
     {
         if (replayTimeout[i].peer == peer)
         {
             Log("removing after timeout peer " + replayTimeout[i].peer.client.IPaddr);
             peer.dispose();
             replayTimeout.RemoveAt(i);
             if (currentState == states.INITIALIZATION && initialIntro > 0)
             {
                 initialIntro--;
             }
         }
         else
         {
             i++;
         }
     }
     replies.Remove(peer.name);
     LogPeerList();
     if (currentState == states.REQUESTING)
     {
         tryEnterCriticalSection();
     }
 }
示例#2
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 void RemoveTimeoutByPeerAndType(Peer peer, Message.messageType type)
 {
     for (int i = 0; i < replayTimeout.Count; )
     {
         if (replayTimeout[i].peer == peer && replayTimeout[i].type == type)
         {
             replayTimeout.RemoveAt(i);
         }
         else
         {
             i++;
         }
     }
 }
示例#3
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 void OnImportantMessageReply(Peer peer, Message msg)
 {
     if (null == peer || null == msg)
     {
         ErrorLog("error OnImportantMessageReply null == peer || null == msg");
     }
     switch (msg.TYPE)
     {
         case Message.messageType.YES_I_AM_HERE:
             RemoveTimeoutByPeerAndType(peer, Message.messageType.ARE_YOU_THERE);
             break;
         case Message.messageType.REPLY:
             RemoveTimeoutByPeerAndType(peer, Message.messageType.REQUEST);
             break;
         case Message.messageType.HIGHEST_SEQ_NUM:
             RemoveTimeoutByPeerAndType(peer, Message.messageType.HIGHEST_SEQ_NUM);
             break;
     }
 }
示例#4
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
        void OnImportantMessageSent(Peer peer, Message msg, Message.messageType typeToRemove)
        {
            RemoveTimeoutByPeerAndType(peer, typeToRemove);

            TimeoutData data = new TimeoutData();
            data.type = msg.TYPE;
            data.peer = peer;
            data.timeout = DateTime.Now + new TimeSpan(0, 0, Math.Max(peers.Count, 1) * (int)(networkDelay.TotalSeconds + criticalSectionHandling.TotalSeconds) );
            replayTimeout.Add(data);
            // CHECK IF ALWAYS THE SAME TIMEOUT
        }
示例#5
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
        void HandleIntroduction(Message msg)
        {
            Message.roleType role = msg.CONTENT.ROLE;
            switch (role)
            {
                case Message.roleType.NEW:
                    if (currentState == states.NOT_CONNECTED || currentState == states.INITIALIZATION)
                    {
                        ErrorLog("protocol error HandleIntroduction roleType.New");
                        return;
                    }
                    awaitingPeers.Add(new Peer(msg.FROM.IP, msg.FROM.PORT, msg.FROM.UNIQUENAME));
                    if (currentState != states.BUSY)
                    {
                        RequestForCS();
                    }
                    Log("HandleIntroduction isUniqueName");
                    break;
                case Message.roleType.SPONSOR:
                    if (currentState != states.INITIALIZATION)
                    {
                        ErrorLog("protocol error HandleIntroduction roleType.Sponsor");
                        return;
                    }
                    if (msg.CONTENT.STATUS == Message.statusType.OK)
                    {
                        Log("OK MESSAGE TEXT ! " + msg.toJson());
                        initialIntro = 0;
                        if (null != msg.CONTENT.NODESDATA)
                        {
                            foreach (Message.From peer in msg.CONTENT.NODESDATA)
                            {
                                Peer new_peer = new Peer(peer.IP, peer.PORT, peer.UNIQUENAME);
                                AddPeer(new_peer);
                                Form1.printLog("importing peers info " + new_peer.name);
                            }
                        }
                        RemovePeer(peers[tempSponsor]);
                        AddPeer(new Peer(msg.FROM.IP, msg.FROM.PORT, msg.FROM.UNIQUENAME));
                        foreach (Peer peer in peers.Values)
                        {
                            this.initialIntro++;
                            Message rsp = new Message(Message.messageType.HIGHEST_SEQ_NUM, itIsI);
                            rsp.CONTENT = new Message.Content();
                            rsp.CONTENT.STATUS = Message.statusType.GET;
                            peer.SendMessage(rsp);
                            OnImportantMessageSent(peer, rsp, Message.messageType.HIGHEST_SEQ_NUM);
                        }
                    }
                    if (msg.CONTENT.STATUS == Message.statusType.NOT_UNIQUE)
                    {
                        Console.WriteLine("Generate new Unique Name");
                        currentState = states.INITIALIZATION;
                    }
                    break;

                case Message.roleType.NODE:
                    if (currentState == states.NOT_CONNECTED || currentState == states.INITIALIZATION || msg.CONTENT.NEWDATA.UNIQUENAME == "")
                    {
                        ErrorLog("protocol error HandleIntroduction roleType.Node");
                        return;
                    }
                    AddPeer(msg.CONTENT.NEWDATA.UNIQUENAME, msg.CONTENT.NEWDATA.IP, msg.CONTENT.NEWDATA.PORT);
                    break;
            }
        }
示例#6
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
        void introduceAsSponsor(Peer newPeer)
        {
            if (!isUniqueName(newPeer.name))
            {
                Message new_msg = new Message(Message.messageType.INIT, itIsI);
                new_msg.CONTENT = new Message.Content();
                new_msg.CONTENT.ROLE = Message.roleType.SPONSOR;
                new_msg.CONTENT.STATUS = Message.statusType.NOT_UNIQUE;
                newPeer.SendMessage(new_msg);
                Log("introduceAsSponsor NOT_UNIQUE");
            }
            else
            {
                {
                    Message msg = new Message(Message.messageType.INIT, itIsI);
                    msg.CONTENT = new Message.Content();
                    msg.CONTENT.ROLE = Message.roleType.NODE;
                    msg.CONTENT.NEWDATA = new Message.From(newPeer.name, newPeer.client.port, newPeer.client.IPaddr);
                    foreach (Peer peer in peers.Values)
                    {
                        peer.SendMessage(msg);
                    }
                }

                {
                    Message msg = new Message(Message.messageType.INIT, itIsI);
                    msg.CONTENT = new Message.Content();
                    msg.CONTENT.ROLE = Message.roleType.SPONSOR;
                    msg.CONTENT.STATUS = Message.statusType.OK;
                    if (peers.Values.Count > 0) //// CHECK !!!
                    {
                        msg.CONTENT.NODESDATA = new Message.From[peers.Values.Count];
                        int i = 0;
                        foreach (Peer peer in peers.Values)
                        {
                            msg.CONTENT.NODESDATA[i] = new Message.From(peer.name, peer.client.port, peer.client.IPaddr);
                            i++;
                        }
                    }
                    newPeer.SendMessage(msg);
                }
                AddPeer(newPeer);

                Log("introduceAsSponsor NODE");
            }
        }
示例#7
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 void AddPeer(Peer new_peer)
 {
     if (peers.Keys.Contains(new_peer.name))
     {
         ErrorLog("AddPeer not unique name");
         return;
     }
     peers[new_peer.name] = new_peer;
     LogPeerList();
     Log("new peer added, IP: " + new_peer.client.IPaddr);
 }
示例#8
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
        public void OnReceive(Message msg)
        {
            ErrorLog("received from " + msg.FROM.UNIQUENAME + " type " + msg.TYPE + " current_state: " + currentState.ToString());

            if (currentState == states.NOT_CONNECTED)
            {
                return;
            }

            if (msg.TYPE != Message.messageType.INIT && !peers.Keys.Contains(msg.FROM.UNIQUENAME))
            {
                Message rsp = new Message(Message.messageType.DEAD, itIsI);
                rsp.CONTENT = new Message.Content();
                rsp.CONTENT.STATUS = Message.statusType.RE_INIT;
                Peer temp = new Peer(msg.FROM.IP, msg.FROM.PORT, msg.FROM.UNIQUENAME);
                temp.SendMessage(rsp);
                return;
            }

            switch (msg.TYPE)
            {
                case Message.messageType.INIT:
                    HandleIntroduction(msg);
                    break;
                case Message.messageType.HIGHEST_SEQ_NUM:
                    HandleHighestSeqNr(msg);
                    break;
                case Message.messageType.REQUEST:
                    HandleRequestCriticalSection(msg);
                    break;
                case Message.messageType.REPLY:
                    HandleReply(msg);
                    break;
                case Message.messageType.YES_I_AM_HERE:
                    HandleYes(msg);
                    break;
                case Message.messageType.ARE_YOU_THERE:
                    HandleAreUThere(msg);
                    break;
                case Message.messageType.DEAD:
                    HandleDead(msg);
                    break;
                default:
                    break;
            }
        }
示例#9
0
文件: Logic.cs 项目: mrs2mrs/SR
 void RemovePeer(Peer peer)
 {
     peers.Remove(peer.name);
     for (int i = 0; i < replayTimeout.Count; )
     {
         if (replayTimeout[i].peer == peer)
         {
             replayTimeout.RemoveAt(i);
         }
         else
         {
             i++;
         }
     }
     replies.Remove(peer.name);
     if (currentState == states.REQUESTING)
     {
         tryEnterCriticalSection();
     }
 }
示例#10
0
文件: Logic.cs 项目: mrs2mrs/SR
 void AddPeer(Peer new_peer)
 {
     if (peers.Keys.Contains(new_peer.name))
     {
         ErrorLog("AddPeer not unique name");
         return;
     }
     peers[new_peer.name] = new_peer;
 }
示例#11
0
文件: Logic.cs 项目: mrs2mrs/SR
 void AddPeer(string _uniqueName, string _ip, int _port)
 {
     Peer new_peer = new Peer(_ip, _port, _uniqueName);
     AddPeer(new_peer);
 }