public static ChordInstance CreateChordInstance(byte[] id = null, string tag = "", System.Security.Cryptography.HashAlgorithm hashAlgorithm = null, IMessageTransceiver transceiver = null) { if (transceiver == null) transceiver = new SimpleUDP(); if (hashAlgorithm == null) hashAlgorithm = new System.Security.Cryptography.SHA512Managed(); ChordInstance i = new ChordInstance(hashAlgorithm, transceiver, id); i.tag = tag; return i; }
public void sendMessage(Message m, string destination, ChordInstance source) { m.myNode.knownEndpoints = new string[] { "Address " + (m.myNode.id).ToString() }; if (instances.ContainsKey(destination)) { instances[destination].messageReceived(m, "Address " + (m.myNode.id).ToString()); } else { throw new Exception(); } }
public void WhenChordInstanceIsCreated_ThenDisplayNameIsCorrect() { // Arrange var sourceChord = this.chordData.Chords[0]; var noteNames = NoteSequenceUtilities.GetNotes(this.chordData, "bb", sourceChord); // Act var chordInstance = ChordInstance.Create(sourceChord.Description, sourceChord.Notes, noteNames.ToList()); // Assert Assert.AreEqual("Bb/A# Major", chordInstance.DisplayName); }
public void checkMaxId() { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); ChordInstance i = new ChordInstance(md5, new SimpleUDP()); Assert.IsTrue(i.numBits == 128); Assert.IsTrue(i.numBytes == 16); SHA512Managed sha512 = new SHA512Managed(); ChordInstance i2 = new ChordInstance(sha512, new SimpleUDP()); Assert.IsTrue(i2.numBits == 512); Assert.IsTrue(i2.numBytes == 512 / 8); }
public IEnumerable <ChordInstance> ChordNotes(string root, string searchTerm) { searchTerm = searchTerm.Trim(); this.logger.LogInformation($"APP LOGGING ===> ChordNotes with root: {root} and searchTerm: {searchTerm}"); var chordResults = NoteSequenceUtilities.SearchDescriptions(this.chordData.Chords, searchTerm); var chordArray = chordResults as Chord[] ?? chordResults.ToArray(); var chordInstances = new List <ChordInstance>(chordArray.Length); foreach (var chord in chordArray) { var chordInstance = ChordInstance.Create( chord.Description, chord.Notes, (List <string>)NoteSequenceUtilities.GetNotes(this.chordData, root, chord)); chordInstances.Add(chordInstance); } return(chordInstances); }
public void sendMessage(Message m, string destination, ChordInstance source) { string t = m.GetType().FullName; string s = Newtonsoft.Json.JsonConvert.SerializeObject(m); System.IO.BinaryWriter b = new System.IO.BinaryWriter(new System.IO.MemoryStream()); b.Write(Encoding.UTF8.GetByteCount(t)); b.Write(Encoding.UTF8.GetBytes(t)); b.Write(Encoding.UTF8.GetByteCount(s)); b.Write(Encoding.UTF8.GetBytes(s)); IPEndPoint target = parseIPString(destination); byte[] bd = ((System.IO.MemoryStream)b.BaseStream).ToArray(); try { theClient.Send(bd, bd.Length, target); } catch { } }
public Node(IMessageTransceiver transceiver, ChordInstance instance) { this.transceiver = transceiver; this.instance = instance; }
static void Main(string[] args) { ChordInstance instance = ChordServer.GetInstance(ChordServer.LocalNode); instance.AddKey("hej"); }
static void Main(string[] args) { try { if (args.Length == 1) { // start new ring int portNum = Convert.ToInt32(args[0]); ChordServer.LocalNode = new ChordNode(System.Net.Dns.GetHostName(), portNum); if (ChordServer.RegisterService(portNum)) { ChordInstance instance = ChordServer.GetInstance(ChordServer.LocalNode); instance.Join(null, ChordServer.LocalNode.Host, ChordServer.LocalNode.PortNumber); while (true) { switch (Char.ToUpperInvariant(Console.ReadKey(true).KeyChar)) { case 'I': { PrintNodeInfo(instance, false); break; } case 'X': { PrintNodeInfo(instance, true); break; } case '?': { Console.WriteLine("Get Server [I]nfo, E[x]tended Info, [Q]uit, or Get Help[?]"); break; } case 'Q': { instance.Depart(); return; } default: { Console.WriteLine("Get Server [I]nfo, E[x]tended Info, [Q]uit, or Get Help[?]"); break; } } } } } else if (args.Length == 3) { // join to existing node int portNum = Convert.ToInt32(args[0]); int seedPort = Convert.ToInt32(args[2]); ChordServer.LocalNode = new ChordNode(System.Net.Dns.GetHostName(), portNum); if (ChordServer.RegisterService(portNum)) { ChordInstance instance = ChordServer.GetInstance(ChordServer.LocalNode); instance.Join(new ChordNode(args[1], seedPort), ChordServer.LocalNode.Host, ChordServer.LocalNode.PortNumber); while (true) { switch (Char.ToUpperInvariant(Console.ReadKey(true).KeyChar)) { case 'I': { PrintNodeInfo(instance, false); break; } case 'X': { PrintNodeInfo(instance, true); break; } case '?': { Console.WriteLine("Get Server [I]nfo, E[x]tended Info, [Q]uit, or Get Help[?]"); break; } case 'Q': { instance.Depart(); return; } default: { Console.WriteLine("Get Server [I]nfo, E[x]tended Info, [Q]uit, or Get Help[?]"); break; } } } } } else { Usage(); } } catch (Exception ex) { Console.WriteLine("Unhandled exception: {0}", ex); Usage(); } }
/// <summary> /// Print information about a given Chord node. /// </summary> /// <param name="instance">The Chord instance to get information from.</param> /// <param name="extended">Whether or not to print extended information.</param> static void PrintNodeInfo(ChordInstance instance, bool extended) { ChordNode successor = instance.Successor; ChordNode predecessor = instance.Predecessor; ChordFingerTable fingerTable = instance.FingerTable; ChordNode[] successorCache = instance.SuccessorCache; string successorString, predecessorString, successorCacheString, fingerTableString; if (successor != null) { successorString = successor.ToString(); } else { successorString = "NULL"; } if (predecessor != null) { predecessorString = predecessor.ToString(); } else { predecessorString = "NULL"; } successorCacheString = "SUCCESSOR CACHE:"; for (int i = 0; i < successorCache.Length; i++) { successorCacheString += string.Format("\n\r{0}: ", i); if (successorCache[i] != null) { successorCacheString += successorCache[i].ToString(); } else { successorCacheString += "NULL"; } } fingerTableString = "FINGER TABLE:"; for (int i = 0; i < fingerTable.Length; i++) { fingerTableString += string.Format("\n\r{0:x8}: ", fingerTable.StartValues[i]); if (fingerTable.Successors[i] != null) { fingerTableString += fingerTable.Successors[i].ToString(); } else { fingerTableString += "NULL"; } } Console.WriteLine("\n\rNODE INFORMATION:\n\rSuccessor: {1}\r\nLocal Node: {0}\r\nPredecessor: {2}\r\n", ChordServer.LocalNode, successorString, predecessorString); if (extended) { Console.WriteLine("\n\r" + successorCacheString); Console.WriteLine("\n\r" + fingerTableString); } }
public SimpleDHT(ChordInstance theInstance) { }
public void doAmbitiousTest() { MD5CryptoServiceProvider sha = new MD5CryptoServiceProvider(); ChordInstance[] instances = new ChordInstance[30]; LoopbackTransceiver t = new LoopbackTransceiver(); for (int i = 0; i < instances.Length; i++) { instances[i] = new ChordInstance(sha, t /*, new byte[] { (byte)i }*/); } Array.Sort(instances); for (int i = 0; i < instances.Length; i++) { instances[i].tag = i.ToString(); } t.setInstances(instances); Random r = new Random(); for (int i = 0; i < instances.Length; i++) { for (int q = 0; q < 4; q++) { ChordInstance z = instances[r.Next(instances.Length)]; instances[i].addNode(new DotChord.DTOs.Response.NodeResponseEntry() { id = z.id, knownEndpoints = new string[] { "Address " + (z.id).ToString() }, tag = z.tag }); } instances[i].constructFingers(5); } for (int i = instances.Length - 1; i >= 0; i--) { ChordInstance z2 = instances[(i + 1) % instances.Length]; instances[i].addNode(new DotChord.DTOs.Response.NodeResponseEntry() { id = z2.id, knownEndpoints = new string[] { "Address " + (z2.id).ToString() }, tag = z2.tag }); instances[i].constructFingers(5); } for (int i = 0; i < instances.Length; i++) { instances[i].constructFingers(5); } for (int i = 0; i < 100; i++) { ChordInstance z1 = instances[r.Next(instances.Length)]; ChordInstance z2 = instances[r.Next(instances.Length)]; var target = z2.tag; var results = z1.lookupNodes(z2.id, 5, false); Assert.IsTrue(results[0].tag == z2.tag); } }
public FingerTable(BigInteger targetId, BigInteger maxId, IMessageTransceiver transceiver, ChordInstance theInstance, int maxSize = 20, bool reverse = false) { this.targetId = targetId; this.maxId = maxId; this.maxSize = maxSize; this.transceiver = transceiver; this.theInstance = theInstance; this.reverse = reverse; }