示例#1
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 public void HandleRequestCriticalSection(Message msg)
 {
     if (currentState == states.INITIALIZATION || currentState == states.NOT_CONNECTED)
     {
         ErrorLog("protocol error OnRequestCriticalSection");
         return;
     }
     sequenceNumber++;
     Form1.printLog("incrementing sequence number. equals = " + sequenceNumber.ToString());
     switch (currentState)
     {
         case states.BUSY:
             csRequestsToBeHandled.Add(msg);
             break;
         case states.REQUESTING:
             if (isSequenceLower(msg))
             {
                 generateREPLY(msg);
             }
             else
             {
                 csRequestsToBeHandled.Add(msg);
             }
             break;
         case states.IDLE:
             generateREPLY(msg);
             break;
         default:
             break;
     }
 }
示例#2
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 public void generateREPLY(Message orginate)
 {
     if (peers.ContainsKey(orginate.FROM.UNIQUENAME))
     {
         peers[orginate.FROM.UNIQUENAME].SendMessage(new Message(Message.messageType.REPLY, this.itIsI));
         return;
     }
     ErrorLog("protocol error generateREPLY");
     return;
 }
示例#3
0
文件: Logic.cs 项目: mrs2mrs/SR
 public void dispose()
 {
     Message msg = new Message(Message.messageType.DEAD, itIsI);
     msg.CONTENT.STATUS = Message.statusType.REMOVE;
     msg.CONTENT.NODE = itIsI.UNIQUENAME;
     foreach (Peer peer in peers.Values)
     {
         peer.SendMessage(msg);
     }
 }
示例#4
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 bool isSequenceLower(Message msg)
 {
     int herSeq = msg.CONTENT.SEQNUM;
     if (herSeq < sequenceNumber || (sequenceNumber == herSeq && String.Compare(itIsI.UNIQUENAME, msg.FROM.UNIQUENAME, false) > 0))
     {
         return true;
     }
     return false;
 }
示例#5
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");
            }
        }
示例#6
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 void HandleYes(Message msg)
 {
     //@todo: what with critical section?
     OnImportantMessageReply(peers[msg.FROM.UNIQUENAME], msg);
 }
示例#7
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 void HandleReply(Message msg)
 {
     if (currentState != states.REQUESTING)
     {
         ErrorLog("protocol error HandleReply");
         return;
     }
     replies.Add(msg.FROM.UNIQUENAME);
     Form1.printLog("reply received from " + msg.FROM.UNIQUENAME);
     tryEnterCriticalSection();
     OnImportantMessageReply(peers[msg.FROM.UNIQUENAME], msg);
 }
示例#8
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;
            }
        }
示例#9
0
文件: Message.cs 项目: mrs2mrs/SR
 public void SendMessage(Message msg)
 {
     client.SendMessage(msg.toJson());
     Console.WriteLine("sent from " + msg.FROM.UNIQUENAME + " type " + msg.TYPE);
 }
示例#10
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
        }
示例#11
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 public void sendDead()
 {
     Form1.printLog("dicsonnecting, goodbye");
     Message msg = new Message(Message.messageType.DEAD, itIsI);
     msg.CONTENT = new Message.Content();
     msg.CONTENT.STATUS = Message.statusType.REMOVE;
     msg.CONTENT.NODE = itIsI.UNIQUENAME;
     foreach (Peer peer in peers.Values)
     {
         peer.SendMessage(msg);
     }
 }
示例#12
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 void CheckTimeouts()
 {
     DateTime current_time = DateTime.Now;
     List<Peer> dead = new List<Peer>();
     for (int i = 0; i < replayTimeout.Count; i++)
     {
         DateTime time = replayTimeout[i].timeout;
         Message.messageType type = replayTimeout[i].type;
         Peer peer = replayTimeout[i].peer;
         if (time <= current_time)
         {
             switch (type)
             {
                 case Message.messageType.ARE_YOU_THERE:
                     Message mss = new Message(Message.messageType.DEAD, itIsI);
                     mss.CONTENT = new Message.Content();
                     mss.CONTENT.NODE = peer.name;
                     dead.Add(peer);
                     foreach (Peer p in peers.Values)
                     {
                         if (!dead.Contains(p))
                             p.SendMessage(mss);
                     }
                     break;
                 case Message.messageType.REQUEST:
                     Message mms = new Message(Message.messageType.ARE_YOU_THERE, itIsI);
                     peer.SendMessage(mms);
                     OnImportantMessageSent(peer, mms, Message.messageType.REQUEST);
                     break;
                 case Message.messageType.HIGHEST_SEQ_NUM:
                     Message sms = new Message(Message.messageType.ARE_YOU_THERE, itIsI);
                     peer.SendMessage(sms);
                     OnImportantMessageSent(peer, sms, Message.messageType.HIGHEST_SEQ_NUM);
                     break;
             }
         }
     }
     foreach (Peer dead_peer in dead)
     {
         RemovePeer(dead_peer);
     }
 }
示例#13
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 public void requestSponsor(string _IP, int _port)
 {
     AddPeer(new Peer(_IP, _port, tempSponsor));
     Message msg = new Message(Message.messageType.INIT, itIsI);
     msg.CONTENT = new Message.Content();
     msg.CONTENT.ROLE = Message.roleType.NEW;
     peers[tempSponsor].SendMessage(msg);
     Log("requestSponsor IP: " + _IP);
 }
示例#14
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 public void RequestForCS()
 {
     currentState = states.REQUESTING;
     sequenceNumber++;
     Message msg = new Message(Message.messageType.REQUEST, itIsI);
     msg.CONTENT = new Message.Content();
     msg.CONTENT.SEQNUM = sequenceNumber;
     Log("Request For Critical Section. Incremented sequence number = " + sequenceNumber);
     foreach (Peer peer in peers.Values)
     {
         peer.SendMessage(msg);
         OnImportantMessageSent(peer, msg, Message.messageType.REQUEST);
     }
     if (0 == peers.Count)
     {
         tryEnterCriticalSection();
     }
 }
示例#15
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;
            }
        }
示例#16
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;
     }
 }
示例#17
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
        void SendHighestSeqNr(Message msg)
        {
            Message rsp = new Message(Message.messageType.HIGHEST_SEQ_NUM, itIsI);
            rsp.CONTENT = new Message.Content();
            rsp.CONTENT.STATUS = Message.statusType.RESPONSE;
            rsp.CONTENT.VALUE = this.sequenceNumber;

            if (peers.ContainsKey(msg.FROM.UNIQUENAME))
            {
                peers[msg.FROM.UNIQUENAME].SendMessage(rsp);
            }
            else
            {
                ErrorLog("protocol error SendHighestSeqNr");
            }
        }
示例#18
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 void HandleAreUThere(Message msg)
 {
     Message rsp = new Message(Message.messageType.YES_I_AM_HERE, itIsI);
     peers[msg.FROM.UNIQUENAME].SendMessage(rsp);
 }
示例#19
0
文件: Logic.cs 项目: mrs2mrs/SR
 void HandleDead(Message msg)
 {
     switch(msg.CONTENT.STATUS)
     {
         case Message.statusType.REMOVE:
             RemovePeer(peers[msg.CONTENT.NODE]);
             break;
         case Message.statusType.RE_INIT:
             ErrorLog("RE INIT");
             break;
     }
 }
示例#20
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++;
         }
     }
 }
示例#21
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 void HandleDead(Message msg)
 {
     if (null == msg.CONTENT)
     {
         Form1.printLog("protocol error - DEAD: missing content");
         return;
     }
     switch(msg.CONTENT.STATUS)
     {
         case Message.statusType.REMOVE:
             RemovePeer(peers[msg.CONTENT.NODE]);
             break;
         case Message.statusType.RE_INIT:
             ErrorLog("RE INIT");
             currentState = states.NOT_CONNECTED;
             Log("after RE-INIT currentState = NOT_CONNECTED");
             break;
     }
 }
示例#22
0
文件: Logic.cs 项目: mrs2mrs/RA-mutex
 void HandleHighestSeqNr(Message msg)
 {
     switch (msg.CONTENT.STATUS)
     {
         case Message.statusType.GET:
             if (currentState == states.NOT_CONNECTED || currentState == states.INITIALIZATION)
             {
                 ErrorLog("protocol error HandleHighestSeqNr statusType.GET");
                 return;
             }
             if (currentState == states.IDLE)
             {
                 SendHighestSeqNr(msg);
             }
             else
             {
                 highestSNawaiting.Add(msg);
             }
             break;
         case Message.statusType.RESPONSE:
             if (currentState != states.INITIALIZATION)
             {
                 ErrorLog("protocol error HandleHighestSeqNr statusType.RESPONSE");
             }
             initialIntro--;
             int tmp_seq = msg.CONTENT.VALUE;
             if (tmp_seq > this.sequenceNumber)
             {
                 this.sequenceNumber = tmp_seq;
             }
             if (initialIntro <= 0)
             {
                 currentState = states.IDLE;
                 Console.WriteLine("init done");
             }
             if (!peers.ContainsKey(msg.FROM.UNIQUENAME))
             {
                 ErrorLog("!peers.ContainsKey(msg.FROM.UniqueName)");
                 return;
             }
             OnImportantMessageReply(peers[msg.FROM.UNIQUENAME], msg);
             break;
     }
 }
示例#23
0
 public void SendMessage(Message msg)
 {
     client.SendMessage(msg.toJson());
     Form1.printLog("sent to " + name + " type " + msg.TYPE);
 }