/// <summary> /// Get the list of all of the nodes in a Chord ring by starting at seedNode /// and following the successor chain around the ring. There are better ways /// to do this, though churn can almost always screw things up in generating a /// map of what would ideally represent flawlessly the state of the entire /// distributed system at a single given point in time. Realizing that the world /// is not a perfect place can provide great peace at times - you just do the /// best you can and accept imperfection. That's what we do here. /// </summary> /// <param name="seedNode">The node to start collecting the list of nodes in the ring from.</param> /// <returns>A list of nodes in the Chord ring.</returns> private static List <ChordNode> GetNodeList(ChordNode seedNode) { List <ChordNode> nodeList = new List <ChordNode>(); nodeList.Add(seedNode); // don't forget to add the seedNode! // the current node (starting with the seed node's successor) // as we iterate through the ring. ChordNode currNode = ChordServer.GetSuccessor(seedNode); int i = 0; // the number of iterations we have made (so we don't loop infinitely) while (seedNode.ID != currNode.ID && i < MaxNodeLimit) { // get my successor and add it to the nodeList. // eventually, my successor will be the seedNode // or i will hit the maximum node limit, at which // point i just stop currNode = ChordServer.GetSuccessor(currNode); nodeList.Add(currNode); i++; } return(nodeList); }
public RealChordRing(IPAddress bootStrapIP, Guid bootStrapChordInstanceGuid) { chordServer = (ChordServer)(TashjikServer.joinExisting(bootStrapIP, "Chord", bootStrapChordInstanceGuid)); Guid chordInstanceGuid = chordServer.getGuid(); incentiveStorageManager = new IncentiveStorageManager(); }
public RealChordRing() { chordServer = (ChordServer)(TashjikServer.createNew("Chord")); Guid chordInstanceGuid = chordServer.getGuid(); incentiveStorageManager = new IncentiveStorageManager(); }
public ClientInstance() { _nodeCache = ChordServer.FindServiceAddress(); _findServers.DoWork += Discover; _findServers.WorkerSupportsCancellation = true; _findServers.RunWorkerAsync(); }
public static void Download(ChordNode node, string file, string pathToDownload, bool from) { var fileStream = ChordServer.Instance(node).GetStream(file, from); var request = new FileUploadMessage(); var fileMetadata = new FileMetaData(file); request.Metadata = fileMetadata; request.FileByteStream = fileStream; FileStream outfile = null; outfile = new FileStream(pathToDownload + file, FileMode.Create); const int bufferSize = 65536; // 64K byte[] buffer = new byte[bufferSize]; int bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize); while (bytesRead > 0) { outfile.Write(buffer, 0, bytesRead); bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize); } }
public static IEnumerable <string> GetAllFilesInSystem(ChordNode node) { var initInstance = ChordServer.Instance(node); SortedSet <string> result = new SortedSet <string>(); foreach (var item in initInstance.GetDb()) { result.Add(item); } initInstance = ChordServer.Instance(initInstance.Successor); while (initInstance.Id != node.Id) { foreach (var item in initInstance.GetDb()) { result.Add(item); } initInstance = ChordServer.Instance(initInstance.Successor); } return(result); }
public void SendFile(IFormFile file) { var node = GetValidNode(); try { if (node != null) { var key = ChordServer.GetHash(file.FileName); var request = new FileUploadMessage(); var fileMetadata = new FileMetaData(file.FileName); request.Metadata = fileMetadata; request.FileByteStream = file.OpenReadStream(); var conteinerNode = ChordServer.CallFindContainerKey(node, key); ChordServer.Instance(conteinerNode).AddNewFile(request); request.FileByteStream.Close(); } } catch (Exception e) { DHTChord.Logger.Logger.Log(DHTChord.Logger.Logger.LogLevel.Error, "Sending file", $"Error during sending file {file.FileName} {e.ToString()}"); SendFile(file); } }
static void processPutDataCallBack(IAsyncResult result) { ChordServer chord = (ChordServer)(result.AsyncState); String strKey = "key"; byte[] key = System.Text.Encoding.ASCII.GetBytes(strKey); chord.beginGetData(key, new AsyncCallback(processGetDataCallBack), null); }
private static void UpdateSeedCache(object sender, DoWorkEventArgs ea) { var me = (BackgroundWorker)sender; while (!me.CancellationPending) { Thread.Sleep(5000); AIRflyService.Nodes = ChordServer.FindServiceAddress(); } }
private void Discover(object sender, DoWorkEventArgs ea) { var me = (BackgroundWorker)sender; while (!me.CancellationPending) { Thread.Sleep(100000); _nodeCache = ChordServer.FindServiceAddress(); } }
private ChordServer joinExistingChord(String strBootStrapIP, String strBootStrapChordInstanceGuid) { Console.WriteLine("Client::joinExistingChord ENTER"); IPAddress bootStrapIP = UtilityMethod.convertStrToIP(strBootStrapIP); ChordServer chord = (ChordServer)(TashjikServer.joinExisting(bootStrapIP, "Chord", new Guid(strBootStrapChordInstanceGuid))); Console.WriteLine("Client::joinExistingChord CHORD RING JOINED"); return(chord); }
public Stream FindFile(string fileName) { var node = GetValidNode(); try { if (node != null) { var where = AIRfly.Download.Error; var key = ChordServer.GetHash(fileName); var instance = ChordServer.Instance(node); if (instance.ContainKey(key)) { where = AIRfly.Download.DataBase; } else if (instance.ContainInCache(fileName)) { where = AIRfly.Download.Cache; } else { var conteinerNodeInstance = ChordServer.Instance(ChordServer.CallFindContainerKey(node, key)); if (conteinerNodeInstance.ContainKey(key)) { var fileStream = conteinerNodeInstance.GetStream(fileName, false); var request = new FileUploadMessage(); var fileMetadata = new FileMetaData(fileName); request.Metadata = fileMetadata; request.FileByteStream = fileStream; instance.AddCacheFile(request); where = AIRfly.Download.Cache; } } if (AIRfly.Download.Error != where) { return(Download(node, fileName, where == AIRfly.Download.Cache)); } } } catch (Exception e) { DHTChord.Logger.Logger.Log(DHTChord.Logger.Logger.LogLevel.Error, "Find file", $"Error during finding file {fileName} {e.ToString()}"); FindFile(fileName); } return(null); }
// // Shared functionality // #region Shared Functionality /// <summary> /// Given a starting node for a valid Chord ring, gather the list of all nodes in the ring, /// and get the hopcount map from each node to each other node in the system. /// </summary> /// <param name="seedNode">The starting node in a valid Chord ring.</param> /// <param name="nodeList">The list of Chord nodes in the ring. This list is already sorted.</param> /// <param name="hopcountMap">An N x N matrix of hopcounts where x=y=nodeList.Count.</param> private static void GenerateMap(ChordNode seedNode, out List <ChordNode> nodeList, out int[,] hopcountMap) { // first get the list of all of the nodes in the system nodeList = GetNodeList(seedNode); // set up the hopcountMap to be an N x N matrix where N is the number of nodes in the ring. hopcountMap = new int[nodeList.Count, nodeList.Count]; // now, for each node in the nodelist, measure FindSuccessor() hopcounts to each other node in // the nodelist, and store them in the hopcount map. // // NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE // // Obviously, this can (and should) be multithreaded for any reasonably large system. I've // only made this sample program single-threaded for readability. Translating from a single- // threaded implementation to a multithreaded implementation of the style of the developer's // choice should be considered a reasonable enough task for anyone experimenting more than casually // with NChord (beyond a few thousand nodes). // // sort the nodelist by nodeId (ChordNode implements IComparable and compares on the Node ID value). nodeList.Sort(); // now, go to each node, and have it call FindSuccessor on that nodeId, and hold on to the hopCount // by storing it in the hopCountmap at the appropriate position int y = 0; // the "row" offset in hopcountMap foreach (ChordNode sourceNode in nodeList) { int x = 0; // the "column" offset in hopcountMap // get hopcount from the sourceNode to each destinationNode foreach (ChordNode destinationNode in nodeList) { // CallFindSuccessor connects to sourceNode and calls FindSuccessor for the destination node's // ID. 3 retries (in case of communication failure, etc.) should be more than sufficient. // since we are the initiating node, pass in a value of 0 as the starting hopcount, and store // the resultant hopcount in the appropriate place in the hopcountMap. ChordServer.CallFindSuccessor(sourceNode, destinationNode.ID, 3, 0, out hopcountMap[y, x]); x++; // move to the next column in the row } y++; // move to the next row in the map } // there you go, you have a hopcount map }
private ChordNode GetValidNode() { var list = GetNodes(); ChordNode node = null; foreach (var n in list) { if (ChordNodeInstance.IsInstanceValid(ChordServer.Instance(n), "send file")) { node = n; break; } } return(node); }
public static void Send(string fileName, string path, ChordNode node) { var key = ChordServer.GetHash(fileName); Stream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); var request = new FileUploadMessage(); var fileMetadata = new FileMetaData(fileName); request.Metadata = fileMetadata; request.FileByteStream = fileStream; var conteinerNode = ChordServer.CallFindContainerKey(node, key); ChordServer.Instance(conteinerNode).AddNewFile(request); }
private void exereciseChord(ChordServer chord) { for (int i = 0; i < 16; i++) { Thread.Sleep(10000); } String sg = "PUT DATA TESTING"; byte[] byteSg = System.Text.Encoding.ASCII.GetBytes(sg); byte[] byteSgKey = Tashjik.Common.UtilityMethod.sha.ComputeHash(byteSg); chord.beginPutData(byteSgKey, byteSg, 0, byteSg.Length, null, null); for (int i = 0; i < 6; i++) { Thread.Sleep(10000); } chord.beginGetData(byteSgKey, new AsyncCallback(processGetDataForExereciseChord), null); }
static void Main(string[] args) { if (args.Length == 0) { args = new string[] { "3030", "D:\\A" } } ; if (args.Length == 3) { System.Console.WriteLine("Start auto"); var tmp = ChordServer.FindServiceAddress(); var node = ChordServer.Instance(tmp[0]).LocalNode; StartServer.Start(int.Parse(args[0]), args[1], node); } else if (args.Length == 2) { StartServer.Start(int.Parse(args[0]), args[1]); } } }
public IEnumerable <string> GetAllFilesInSystem() { var node = GetValidNode(); try { if (node != null) { var initInstance = ChordServer.Instance(node); SortedSet <string> result = new SortedSet <string>(); foreach (var item in initInstance.GetDb()) { result.Add(item); } initInstance = ChordServer.Instance(initInstance.Successor); while (initInstance.Id != node.Id) { foreach (var item in initInstance.GetDb()) { result.Add(item); } initInstance = ChordServer.Instance(initInstance.Successor); } return(result); } } catch (Exception e) { DHTChord.Logger.Logger.Log(DHTChord.Logger.Logger.LogLevel.Error, "showing all files", $"Error during showing all files {e.ToString()}"); GetAllFilesInSystem(); } return(new List <string>()); }
public static void Find(string fileName, ChordNode node, string pathToDownload) { var where = Client.Download.Error; var key = ChordServer.GetHash(fileName); var instance = ChordServer.Instance(node); if (instance.ContainKey(key)) { where = Client.Download.DataBase; } else if (instance.ContainInCache(fileName)) { where = Client.Download.Cache; } else { var conteinerNodeInstance = ChordServer.Instance(ChordServer.CallFindContainerKey(node, key)); if (conteinerNodeInstance.ContainKey(key)) { var fileStream = conteinerNodeInstance.GetStream(fileName, false); var request = new FileUploadMessage(); var fileMetadata = new FileMetaData(fileName); request.Metadata = fileMetadata; request.FileByteStream = fileStream; instance.AddCacheFile(request); where = Client.Download.Cache; } } if (Client.Download.Error != where) { Download(node, fileName, pathToDownload, where == Client.Download.Cache); } }
private ChordServer joinExistingChord(String strBootStrapIP, String strBootStrapChordInstanceGuid) { Console.WriteLine("Client::joinExistingChord ENTER"); IPAddress bootStrapIP = UtilityMethod.convertStrToIP(strBootStrapIP); ChordServer chord = (ChordServer)(TashjikServer.joinExisting(bootStrapIP, "Chord", new Guid(strBootStrapChordInstanceGuid))); Console.WriteLine("Client::joinExistingChord CHORD RING JOINED"); //testing BeginTransportLayerSendTwoWay //not the appropriate place to test // String strMsg = "testing BeginTransportLayerSendTwoWay"; // byte[] msg = System.Text.Encoding.ASCII.GetBytes(strMsg); // transportLayerCommunicator.BeginTransportLayerSendTwoWay(bootStrapIP, msg, 0, strMsg.Length, ClientGuid, null, null); //new AsyncCallback(sendDataCallBack), ipAddress); //testing BeginTransportLayerSendTwoWayRelay //not the appropriate place to test // String strMsg = "testing BeginTransportLayerSendTwoWayRelay"; // byte[] msg = System.Text.Encoding.ASCII.GetBytes(strMsg); // transportLayerCommunicator.BeginTransportLayerSendTwoWayRelay(bootStrapIP, msg, 0, strMsg.Length, ClientGuid, null, null, new Guid("00000000-0000-0000-0000-000000000000")); //new AsyncCallback(sendDataCallBack), ipAddress); return(chord); }
public RealHalo() { chordServer = (ChordServer)(TashjikServer.createNew("Chord")); Guid chordInstanceGuid = chordServer.getGuid(); }
public static void Main(string[] args) { AIRflyService.Nodes = ChordServer.FindServiceAddress(); StartMaintenance(); BuildWebHost(args).Run(); }
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(); } }
static void Main(string[] args) { ChordInstance instance = ChordServer.GetInstance(ChordServer.LocalNode); instance.AddKey("hej"); }
public static void Start(int port, string path, ChordNode seed = null) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } if (!Directory.Exists(path + "Cache\\")) { Directory.CreateDirectory(path + "Cache\\"); } ChordServer.LocalNode = new ChordNode(Dns.GetHostName(), port) { Path = path }; Uri baseAddress = new Uri($"net.tcp://{ChordServer.LocalNode.Host}:{port}/chord"); using (ServiceHost serviceHost = new ServiceHost(typeof(ChordNodeInstance), baseAddress)) { serviceHost.AddServiceEndpoint(typeof(IChordNodeInstance), ChordServer.CreategBinding(), baseAddress); serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior()); serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint()); serviceHost.Open(); var instance = ChordServer.Instance(ChordServer.LocalNode); instance.Join(seed); while (true) { switch (char.ToUpperInvariant(Console.ReadKey(true).KeyChar)) { case 'I': { PrintNodeInfo(instance); break; } case 'X': { PrintNodeInfo(instance); break; } case 'Q': { instance.Depart(); return; } case 'F': { for (int i = 0; i < 10; ++i) { var val = ChordServer.CallGetValue(ChordServer.LocalNode, ChordServer.GetHash($"Hello Wolrd {i}"), out var tmp); if (val == null) { Log(LogLevel.Error, "GetValue", $"The instance is null. Value is not found: Hello Wolrd {i}"); } else { Console.WriteLine($"Download from {tmp}: {val}"); } } break; } case 'D': { instance.ViewDataBase(); break; } case 'M': { //string path = // "G:\\!!from adriano\\music from\\Imagine Dragons\\Discos\\[2013] Night Visions/"; //var directorys = Directory.EnumerateFiles(path); //foreach (var file in directorys) //{ // ChordServer.AddFile(GetFileName(file), path, ChordServer.LocalNode); //} break; } default: { Console.WriteLine("Get Server [I]nfo, E[x]tended Info, [Q]uit, or Get Help[?]"); break; } } } } }
private static Stream Download(ChordNode node, string file, bool from) { return(ChordServer.Instance(node).GetStream(file, from)); }