/// <summary> /// Reads a DistributionConfiguration from a specified stream. /// </summary> /// <param name="client">TCP Client that manages the network stream</param> /// <param name="br"></param> /// <returns></returns> public static DistributionConfiguration ReadFromStream(TcpClient client, BinaryReader br) { DistributionConfiguration cfg = new DistributionConfiguration(); while (client.Available < 17) ; // wait for data Byte[] problemid = br.ReadBytes(16); cfg.ProblemID = new Guid(problemid); Byte addrtype = br.ReadByte(); // Read Controller Server Information Int32 cblen = 6 + ((addrtype & 1) == 0 ? 0 : 12); while (client.Available < cblen) ; // Wait for data Byte[] cb = br.ReadBytes(cblen - 2); cfg.ControllerServer = new IPAddress(cb); cfg.ControllerServerPort = br.ReadUInt16(); // Read Redis Server Information Int32 rblen = 6 + ((addrtype & 2) == 0 ? 0 : 12); while (client.Available < rblen) ; // wait for data Byte[] rb = br.ReadBytes(rblen - 2); cfg.RedisServer = new IPAddress(rb); cfg.RedisServerPort = br.ReadUInt16(); // Read OSRM Server Information Int32 oblen = 6 + ((addrtype & 4) == 0 ? 0 : 12); while (client.Available < oblen) ; // wait for data Byte[] ob = br.ReadBytes(oblen - 2); cfg.OsrmServer = new IPAddress(ob); cfg.OsrmServerPort = br.ReadUInt16(); while (client.Available < 8) ; // wait for data // Read Random Seed cfg.RandomSeed = br.ReadInt32(); // Read Workers Int32 wcount = br.ReadInt32(); while (client.Available < Worker.SerializedLength * wcount) ; // wait for data cfg.Workers = new Worker[wcount]; for (int i = 0; i < wcount; i++) cfg.Workers[i] = Worker.ReadFromStream(br); // Read Clusters while (client.Available < 4) ; // wait for data Int32 tcount = br.ReadInt32(); cfg.Tasks = new Task[tcount]; for (int i = 0; i < tcount; i++) { while (client.Available < Task.SerializedLength) ; // wait for data cfg.Tasks[i] = Task.ReadFromStream(br); } return cfg; }
private void btnPpCgfNext_Click(object sender, EventArgs e) { // Construct config object config = new DistributionConfiguration(); // tree view TreeNode rootNode = new TreeNode("DictributionConfiguration (#" + config.GetHashCode().ToString() + ")"); // misc config.ControllerServer = IPAddress.Parse("127.0.0.1"); config.ControllerServerPort = 0; config.OsrmServerPort = 5000; config.RedisServerPort = 6379; // guid try { config.ProblemID = Guid.Parse(txtPpCfgId.Text); } catch { erp.SetError(txtPpCfgId, "Cannot parse GUID"); return; } rootNode.Nodes.Add(String.Format("ProblemID: {0}", config.ProblemID)); rootNode.Nodes.Add(String.Format("ControllerServer: {0}:{1}", config.ControllerServer, config.ControllerServerPort)); // redis ip try { config.RedisServer = IPAddress.Parse(txtPpCfgRedisAddr.Text); } catch { erp.SetError(txtPpCfgRedisAddr, "Invalid IP Address"); return; } rootNode.Nodes.Add(String.Format("RedisServer: {0}:{1}", config.RedisServer, config.RedisServerPort)); // osrm ip try { config.OsrmServer = IPAddress.Parse(txtPpCfgOsrmAddr.Text); } catch { erp.SetError(txtPpCfgOsrmAddr, "Invalid IP Address"); return; } rootNode.Nodes.Add(String.Format("OsrmServer: {0}:{1}", config.OsrmServer, config.OsrmServerPort)); // random seed try { config.RandomSeed = Int32.Parse(txtPpCfgRnd.Text); } catch { erp.SetError(txtPpCfgRnd, "Cannot parse number"); return; } rootNode.Nodes.Add(String.Format("RandomSeed: {0}", config.RandomSeed)); // generate workers TreeNode workersNode = new TreeNode("Workers"); List<Worker> workers = new List<Worker>(); for (int i = 0; i < nudPpCfgWorkerC.Value; i++) { Worker w = new Worker() { WorkerID = (uint)i }; workers.Add(w); workersNode.Nodes.Add(String.Format("Worker {{ID: {0}}}", w.WorkerID)); } config.Workers = workers.ToArray(); rootNode.Nodes.Add(workersNode); // generate tasks TreeNode tasksNode = new TreeNode("Tasks"); List<ProblemLib.DataModel.Task> tasks = new List<ProblemLib.DataModel.Task>(); for (int i = 0; i < nudPpCfgTaskC.Value; i++) { Coordinate c = locations[i]; ProblemLib.DataModel.Task t = new ProblemLib.DataModel.Task((uint)(100000 + i), (float)c.lon, (float)c.lat); tasks.Add(t); tasksNode.Nodes.Add(String.Format("Task {{ID: {0}; Location: ({1}, {2})}}", t.TaskID, t.Latitude, t.Longitude)); } config.Tasks = tasks.ToArray(); rootNode.Nodes.Add(tasksNode); // partition stuff Int32 dtPprocC = (int)nudPpCfgPprocC.Value; Int64 totalLen = ProblemLib.Preprocessing.PreprocessingPartition<int>.RowSum(config.Tasks.Length, config.Tasks.Length); Int32 distribution = (int)(totalLen / dtPprocC); TreeNode distributionNode = new TreeNode("Preprocessing Distribution"); for (int i = 0; i < dtPprocC - 1; i++) { distributionNode.Nodes.Add(String.Format("Distribution {{Start: {0}; Length: {1}}}", i * distribution, distribution)); } distributionNode.Nodes.Add(String.Format("Distribution {{Start: {0}; Length: {1}}}", (dtPprocC - 1) * distribution, totalLen - (dtPprocC - 1) * distribution)); rootNode.Nodes.Add(distributionNode); treeView1.Nodes.Add(rootNode); pprocStateMan.SetCurrentState("wsPprocReview"); }
// Basic structure for problem controller static void Main(string[] args) { // NOTE this is an implementation for testing only! var console = new ConsoleLogger(); GlobalLogger.AttachLogger(console); console.Run(); TcpClient client = new TcpClient(); try { client.Connect("localhost", 17924); } catch (Exception x) { GlobalLogger.SendLogMessage("Error", "Failed to connect to distribution server: {0}", x.Message); console.Stop(); return; } var s = client.GetStream(); var bw = new BinaryWriter(s); var br = new BinaryReader(s); var r = new Random(); try { bw.Write(ControlCodes.SendingConfiguration); var c = new DistributionConfiguration(); c.ProblemID = Guid.NewGuid(); c.ControllerServer = IPAddress.Parse("127.0.0.1"); c.ControllerServerPort = 3879; c.RedisServer = IPAddress.Parse("192.168.2.31"); c.RedisServerPort = 6379; c.OsrmServer = IPAddress.Parse("192.168.2.31"); c.OsrmServerPort = 5000; c.RandomSeed = 1234567; c.Workers = new[] { new Worker() { WorkerID = 0 }, new Worker() { WorkerID = 1 }, new Worker() { WorkerID = 2 } }; c.Tasks = new[] { new Task(0, 1111.1111F, 2222.2222F), new Task(0, 1111.1111F, 2222.2222F),new Task(0, 1111.1111F, 2222.2222F) ,new Task(0, 1111.1111F, 2222.2222F), new Task(0, 5511.1111F, 5522.2222F), new Task(0, 5511.1111F, 5522.2222F),new Task(0, 5511.1111F, 5522.2222F) ,new Task(0, 5511.5511F, 5522.2222F), new Task(0, 8811.1111F, 2222.2222F), new Task(0, 8811.1111F, 2222.2222F),new Task(0, 8811.1111F, 2222.2222F) ,new Task(0, 8811.1111F, 2222.2222F) }; c.WriteToStream(bw); while (client.Available < 2) ; UInt16 ctrl = br.ReadUInt16(); if (ctrl == ControlCodes.Acknowledge) GlobalLogger.SendLogMessage("ControllerEvent", "Distribution server replied with ACK!"); else if (ctrl == ControlCodes.Error) { while (client.Available < 12) ; // wait for data UInt32 errcode = br.ReadUInt32(); DateTime ts = new DateTime(br.ReadInt64()); GlobalLogger.SendLogMessage("DistributionError", "Distribution server reported error {0} at {1}:{2}:{3}.{4}", errcode, ts.Hour, ts.Minute, ts.Second, ts.Millisecond); } } finally { bw.Write(ControlCodes.TerminateConnection); // emulate disconnect code client.Close(); console.Stop(true); } Console.ReadKey(); }