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; } }
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; }
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); } }
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; }
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"); } }
void HandleYes(Message msg) { //@todo: what with critical section? OnImportantMessageReply(peers[msg.FROM.UNIQUENAME], msg); }
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); }
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; } }
public void SendMessage(Message msg) { client.SendMessage(msg.toJson()); Console.WriteLine("sent from " + msg.FROM.UNIQUENAME + " type " + msg.TYPE); }
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 }
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); } }
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); } }
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); }
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(); } }
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; } }
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; } }
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"); } }
void HandleAreUThere(Message msg) { Message rsp = new Message(Message.messageType.YES_I_AM_HERE, itIsI); peers[msg.FROM.UNIQUENAME].SendMessage(rsp); }
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; } }
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++; } } }
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; } }
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; } }
public void SendMessage(Message msg) { client.SendMessage(msg.toJson()); Form1.printLog("sent to " + name + " type " + msg.TYPE); }