public NodeInfo FindSuccessor(BigInteger id) { if (TestKey(myData.ID, id, ((NodeInfo)successors[0]).ID)) { return((NodeInfo)successors[0]); } NodeInfo cpn = ClosestsPrecedingNode(id); if (cpn.Equals(myData)) { return(myData); } ChordNodeProxy obj = (ChordNodeProxy)Activator.GetObject(typeof(ChordNodeProxy), cpn.Address + ":" + cpn.Port + "/ChordServices"); if (obj != null) { try { return(obj.Findsuccessor(id)); } catch (IOException) { } catch (SocketException) { } } //Unable to find successor for node. Return null return(null); }
public void Remove(string key, string value) { BigInteger keyID = new BigInteger(ChordNode.SHA(key)); NodeInfo ni = ChordModule.Instance.NodeInstance.FindSuccessor(keyID); ChordNodeProxy obj = (ChordNodeProxy)Activator.GetObject(typeof(ChordNodeProxy), ni.Address + ":" + ni.Port + "/ChordServices"); if (obj != null) { try { obj.Remove(keyID, value); } catch (IOException) { } catch (SocketException) { } } }
public List <string> Get(string key) { BigInteger keyID = new BigInteger(ChordNode.SHA(key)); NodeInfo ni = ChordModule.Instance.NodeInstance.FindSuccessor(keyID); ChordNodeProxy obj = (ChordNodeProxy)Activator.GetObject(typeof(ChordNodeProxy), ni.Address + ":" + ni.Port + "/ChordServices"); if (obj != null) { try { return(obj.Get(keyID)); } catch (IOException) { } catch (SocketException) { } } return(null); }
public NodeInfo Join(string knownChordNodeAddress, int knownChordNodePort) //Return successor { Predecessor = null; ChordNodeProxy obj = (ChordNodeProxy)Activator.GetObject(typeof(ChordNodeProxy), knownChordNodeAddress + ":" + knownChordNodePort + "/ChordServices"); if (obj != null) { try { return(obj.Findsuccessor(myData.ID)); } catch (IOException) { } catch (SocketException) { } } return(null); }
private void FixSucessors(ArrayList succs) { //This should keep the first successor, copy the rest and remove the last one for (int i = 1; i < MAX_SUCESSORS; i++) { successors[i] = succs[i - 1]; } //Replicate my data to my successors Dictionary <BigInteger, List <string> > data; if (Predecessor != null) { data = new Dictionary <BigInteger, List <string> >(); foreach (BigInteger key in keyTable.Keys.Where <BigInteger>(x => x > Predecessor.ID)) { data.Add(key, keyTable[key]); } } else { data = new Dictionary <BigInteger, List <string> >(keyTable); } foreach (object succ in successors) { NodeInfo casted = (NodeInfo)succ; if (casted != myData) { ChordNodeProxy obj = (ChordNodeProxy)Activator.GetObject(typeof(ChordNodeProxy), casted.Address + ":" + casted.Port + "/ChordServices"); if (obj != null) { try { obj.ReplicateData(data); } catch (IOException) {} catch (SocketException) {} } } } }
private void Stabilize() { while (true) { if (successors.Count != 0 && !((NodeInfo)successors[0]).Equals(myData)) { NodeInfo pred = null; NodeInfo suc = (NodeInfo)successors[0]; ChordNodeProxy obj = (ChordNodeProxy)Activator.GetObject(typeof(ChordNodeProxy), suc.Address + ":" + suc.Port + "/ChordServices"); if (obj != null) { try { pred = obj.Predecessor(); if (pred != null && TestKey(myData.ID, pred.ID, ((NodeInfo)this.successors[0]).ID)) { successors[0] = pred; } NodeInfo ni = (NodeInfo)successors[0]; ChordNodeProxy obj2 = (ChordNodeProxy)Activator.GetObject(typeof(ChordNodeProxy), ni.Address + ":" + ni.Port + "/ChordServices"); if (obj2 != null) { try { FixSucessors(obj2.Notify(myData)); } catch (IOException) { } catch (SocketException) { } } } catch (IOException) { RotateSucessors(); } catch (SocketException) { RotateSucessors(); } } } Thread.Sleep(5000); //5s } }
private void CheckPredecessor() { while (true) { if (Predecessor != null) { ChordNodeProxy obj = (ChordNodeProxy)Activator.GetObject(typeof(ChordNodeProxy), Predecessor.Address + ":" + Predecessor.Port + "/ChordServices"); if (obj != null) { try { obj.Ping(); continue; } catch (IOException) { } catch (SocketException) { } } Predecessor = null; } Thread.Sleep(30000); //30s } }
public ChordNode(string myAddress, int myPort, string knownChordNodeAddress, int knownChordNodePort) { Predecessor = null; fingers = new NodeInfo[chordKeyLength]; successors = ArrayList.Synchronized(new ArrayList()); myData = new NodeInfo(myAddress, myPort, new BigInteger(SHA(myAddress + ":" + myPort))); byte[] max = new byte[20]; for (int i = 0; i < 20; i++) { max[i] = 255; } maxValue = new BigInteger(max); NodeInfo ni = Join(knownChordNodeAddress, knownChordNodePort); if (ni != null) { //In the beginning this node should only know it's sucessor. //So we initialize them all as the same one. for (int i = 0; i < MAX_SUCESSORS; i++) { successors.Add(ni); } ChordNodeProxy obj = (ChordNodeProxy)Activator.GetObject(typeof(ChordNodeProxy), ni.Address + ":" + ni.Port + "/ChordServices"); if (obj != null) { try { keyTable = obj.CopyKeys(myData.ID); } catch (IOException) { } catch (SocketException) { } } } else { //known node is dead throw new ServiceUnavailableException("Known chord node is dead."); } }