public void Start() { stopwatch.Start(); //int[] nodes = SelectNodes1(0); int[] nodes = SelectNodes2(0, null); for (int j = 0; j < nodes.Length; j++) { SimulationSocket ss = new SimulationSocket(sn); Thread.Sleep(connectWait); ss.Connect(IPAddress.Loopback, (ushort)(startPortNumber + nodes[j])); connections.Add(ss.localPortNumber, (ushort)(startPortNumber + nodes[j])); connections.Add(ss.remotePortNumber, 9000); ss.Write(nodeInfos[0].ToBinary()); ss.Write(new byte[1024]); ss.Close(); } }
public DiffuseSimulation() { nodeInfos = new NodeInformation[numberOfNodes]; for (int i = 0; i < numberOfNodes; i++) nodeInfos[i] = new NodeInformation(IPAddress.Loopback, (ushort)(startPortNumber + i), Network.localtest, string.Empty); for (int i = 0; i < numberOfNodes; i++) portNumberToIndex.Add(nodeInfos[i].portNumber, i); sn = new SimulationNetwork(); sss = new SimulationSocket[numberOfNodes]; cremlias = new Cremlia[numberOfNodes]; randomNumss = numberOfNodes.PipeForever((non) => non.RandomNums()).Where((ns) => ns.Select((n, i) => new { n, i }).All((ni) => ni.n != ni.i)).Take(numberOfDiffuseNodes).ToArray(); int receiveCounter = 0; for (int i = 0; i < numberOfNodes; i++) { sss[i] = new SimulationSocket(sn); sss[i].Bind(new IPEndPoint(IPAddress.Any, nodeInfos[i].portNumber)); sss[i].Listen(100); cremlias[i] = new Cremlia(new CremliaIdFactorySha256(), new CremliaDatabaseIo(), new CremliaNetworkIoSimulation(sss[i]), new CremliaNodeInfomationSha256(nodeInfos[i])); int[] randomNums = numberOfNodes.RandomNums(); for (int j = 0; j < numberOfNodes; j++) if (randomNums[j] != i) cremlias[i].UpdateNodeStateWhenJoin(new CremliaNodeInfomationSha256(nodeInfos[randomNums[j]])); int index = i; this.StartTask(string.Empty, string.Empty, () => { object l = new object(); int counter = 0; while (true) { SimulationSocket ss = sss[index].Accept() as SimulationSocket; bool flag = false; lock (l) if (counter++ == 0) flag = true; this.StartTask(string.Empty, string.Empty, () => { NodeInformation ni = SHAREDDATA.FromBinary<NodeInformation>(ss.Read()); byte[] data = ss.Read(); ss.Close(); Thread.Sleep(connectWait); if (flag && index != 0) { Received(this, ++receiveCounter); this.ConsoleWriteLine(string.Join(",", receiveCounter.ToString(), stopwatch.ElapsedMilliseconds.ToString())); if (receiveCounter == numberOfNodes) stopwatch.Stop(); //int[] nodes = SelectNodes1(index); int[] nodes = SelectNodes2(index, ni); for (int j = 0; j < nodes.Length; j++) { SimulationSocket ss2 = new SimulationSocket(sn); Thread.Sleep(verifyWait); ss2.Connect(IPAddress.Loopback, (ushort)(startPortNumber + nodes[j])); connections.Add(ss2.localPortNumber, (ushort)(startPortNumber + nodes[j])); connections.Add(ss2.remotePortNumber, nodeInfos[index].portNumber); ss2.Write(nodeInfos[index].ToBinary()); ss2.Write(new byte[1024]); ss2.Close(); } } }); } }); } }
public void Close() { ss.Close(); }
public DiffuseSimulation() { nodeInfos = new NodeInformation[numberOfNodes]; for (int i = 0; i < numberOfNodes; i++) { nodeInfos[i] = new NodeInformation(IPAddress.Loopback, (ushort)(startPortNumber + i), Network.localtest, string.Empty); } for (int i = 0; i < numberOfNodes; i++) { portNumberToIndex.Add(nodeInfos[i].portNumber, i); } sn = new SimulationNetwork(); sss = new SimulationSocket[numberOfNodes]; cremlias = new Cremlia[numberOfNodes]; randomNumss = numberOfNodes.PipeForever((non) => non.RandomNums()).Where((ns) => ns.Select((n, i) => new { n, i }).All((ni) => ni.n != ni.i)).Take(numberOfDiffuseNodes).ToArray(); int receiveCounter = 0; for (int i = 0; i < numberOfNodes; i++) { sss[i] = new SimulationSocket(sn); sss[i].Bind(new IPEndPoint(IPAddress.Any, nodeInfos[i].portNumber)); sss[i].Listen(100); cremlias[i] = new Cremlia(new CremliaIdFactorySha256(), new CremliaDatabaseIo(), new CremliaNetworkIoSimulation(sss[i]), new CremliaNodeInfomationSha256(nodeInfos[i])); int[] randomNums = numberOfNodes.RandomNums(); for (int j = 0; j < numberOfNodes; j++) { if (randomNums[j] != i) { cremlias[i].UpdateNodeStateWhenJoin(new CremliaNodeInfomationSha256(nodeInfos[randomNums[j]])); } } int index = i; this.StartTask(string.Empty, string.Empty, () => { object l = new object(); int counter = 0; while (true) { SimulationSocket ss = sss[index].Accept() as SimulationSocket; bool flag = false; lock (l) if (counter++ == 0) { flag = true; } this.StartTask(string.Empty, string.Empty, () => { NodeInformation ni = SHAREDDATA.FromBinary <NodeInformation>(ss.Read()); byte[] data = ss.Read(); ss.Close(); Thread.Sleep(connectWait); if (flag && index != 0) { Received(this, ++receiveCounter); this.ConsoleWriteLine(string.Join(",", receiveCounter.ToString(), stopwatch.ElapsedMilliseconds.ToString())); if (receiveCounter == numberOfNodes) { stopwatch.Stop(); } //int[] nodes = SelectNodes1(index); int[] nodes = SelectNodes2(index, ni); for (int j = 0; j < nodes.Length; j++) { SimulationSocket ss2 = new SimulationSocket(sn); Thread.Sleep(verifyWait); ss2.Connect(IPAddress.Loopback, (ushort)(startPortNumber + nodes[j])); connections.Add(ss2.localPortNumber, (ushort)(startPortNumber + nodes[j])); connections.Add(ss2.remotePortNumber, nodeInfos[index].portNumber); ss2.Write(nodeInfos[index].ToBinary()); ss2.Write(new byte[1024]); ss2.Close(); } } }); } }); } }