public List <int> RefreshStats() { SimulationStats.clearStats(); foreach (NetworkNode n in nodes) { SimulationStats.total_buffer_full_packet_count += n.stat_buffer_full_packet_count; SimulationStats.total_packets_gen_count += n.stat_packets_gen; SimulationStats.total_packets_term_count += n.stat_packets_term; SimulationStats.total_packets_trans_count += n.stat_packets_trans; SimulationStats.total_reached_dest_TTL_sum += n.stat_reached_dest_TTL_sum; SimulationStats.total_TTL_exceeded_packet_count += n.stat_TTL_exceeded_packet_count; SimulationStats.total_packets_remaining_in_buffers += (n.buffer_in.Count + n.buffer_out.Count); SimulationStats.total_sum_hops += n.stat_total_hops_count; } return(getBufferLoad()); }
private void Run_Click(object sender, EventArgs e) { if (nodesLabels != null && nodesLabels.Count > 0) { foreach (Label l in nodesLabels) { this.groupBox4.Controls.Remove(l); l.Dispose(); } nodesLabels = null; } if (nodesLoads != null && nodesLoads.Count > 0) { foreach (ProgressBar p in nodesLoads) { this.groupBox4.Controls.Remove(p); p.Dispose(); } nodesLoads = null; } if (nodesLoadNumberLabels != null && nodesLoadNumberLabels.Count > 0) { foreach (Label l in nodesLoadNumberLabels) { this.groupBox4.Controls.Remove(l); l.Dispose(); } nodesLoadNumberLabels = null; } if (nodesLabels == null || nodesLoadNumberLabels == null || nodesLoads == null) { nodesLabels = new List <Label>(); nodesLoadNumberLabels = new List <Label>(); nodesLoads = new List <ProgressBar>(); for (int i = 0; i < (int)this.networkSize.Value * (int)this.networkSize.Value; i++) { Label l = new Label(); ProgressBar p = new ProgressBar(); Label l2 = new Label(); l.AutoSize = true; l.Location = new System.Drawing.Point(4, (i + 1) * 20); l.Name = "NodeLabel" + (i + 1); l.Size = new System.Drawing.Size(39, 13); l.Text = "Node" + (i + 1); p.Location = new System.Drawing.Point(50, (i + 1) * 20); p.Name = "NodeProgressBar" + (i + 1); if (!showBufferNumberUsage) { p.Size = new System.Drawing.Size(80, 15); } else { p.Size = new System.Drawing.Size(110, 15); } p.Value = 0; p.Maximum = (int)this.BufferSize.Value + 1; p.Minimum = 0; l2.AutoSize = true; l2.Location = new System.Drawing.Point(15 + p.Size.Width + l.Size.Width , (i + 1) * 20); l2.Name = "NodeLoadNumberLabel" + (i + 1); l2.BackColor = System.Drawing.Color.Transparent; l2.Size = new System.Drawing.Size(20, 7); l2.Text = ""; nodesLoads.Add(p); nodesLabels.Add(l); nodesLoadNumberLabels.Add(l2); this.groupBox4.Controls.Add(p); this.groupBox4.Controls.Add(l); if (showBufferNumberUsage) { this.groupBox4.Controls.Add(l2); } l2.BringToFront(); l2.BringToFront(); l2.BringToFront(); } } if (!isRunning && !isPaused) { this.splitContainer1.SplitterDistance = 460; int seed = 0; if (RandSeed.Text.Length > 0) { seed = Int32.Parse(RandSeed.Text); } this.simulation = new Simulation((int)this.networkSize.Value, seed); currentTick = 0; if (this.RoutingManual.Checked == true) { if (textBox1.Text.Length == 0 && textBox2.Text.Length == 0) { MessageBox.Show("You need to specify at least one route"); return; } try { if (textBox1.Text.Length > 0) { string tmp = this.textBox1.Text.TrimEnd(); tmp = tmp.TrimStart(); tmp = tmp.Replace("\n", ""); tmp = tmp.Replace("\t", ""); tmp = tmp.Replace("\r", ""); tmp = tmp.Replace(" ", ""); if (tmp.Length > 0 && tmp[0] == ',') { tmp = tmp.Remove(0, 1); } if (tmp.Length > 0 && tmp[tmp.Length - 1] == ',') { tmp = tmp.Remove(tmp.Length - 1, 1); } if (tmp.Length == 0) { MessageBox.Show("You need to specify at least one route"); return; } string[] tmp2 = tmp.Split(','); List <int> route = new List <int>(); foreach (string text in tmp2) { route.Add(Int32.Parse(text)); } simulation.addManualRoute(route); } if (textBox2.Text.Length > 0) { string tmp = this.textBox2.Text.TrimEnd(); tmp = tmp.TrimStart(); tmp = tmp.Replace("\n", ""); tmp = tmp.Replace("\t", ""); tmp = tmp.Replace("\r", ""); tmp = tmp.Replace(" ", ""); if (tmp.Length > 0 && tmp[0] == ',') { tmp = tmp.Remove(0, 1); } if (tmp.Length > 0 && tmp[tmp.Length - 1] == ',') { tmp = tmp.Remove(tmp.Length - 1, 1); } if (tmp.Length == 0) { MessageBox.Show("You need to specify at least one route"); return; } string[] tmp2 = tmp.Split(','); List <int> route = new List <int>(); foreach (string text in tmp2) { route.Add(Int32.Parse(text)); } simulation.addManualRoute(route); } if (!simulation.checkManualRoutingAllRoutesConnectivity(true)) { MessageBox.Show("There is no connectivity between all nodes in the routing!"); return; } } catch (Exception ex) { MessageBox.Show("There was an error parsing the manual route" + ex.ToString()); return; } SimulationParams.clearParams(); SimulationStats.clearStats(); SimulationParams.routing_algorithm = 3; } else if (this.RoutingRandom.Checked == true) { SimulationParams.clearParams(); SimulationStats.clearStats(); SimulationParams.routing_algorithm = 2; } else if (this.RoutingShortest.Checked == true) { SimulationParams.clearParams(); SimulationStats.clearStats(); SimulationParams.routing_algorithm = 1; } this.richTextBox1.Text += " \n"; this.richTextBox1.Text += "Simulation started at " + DateTime.Now.ToLongTimeString() + "\n"; isRunning = true; isPaused = false; SimulationParams.buffer_size = (int)this.BufferSize.Value; SimulationParams.lambda = (int)this.lambdaParam.Value; SimulationParams.network_size = (int)networkSize.Value; SimulationParams.TTL = (int)this.TTL.Value; timer1.Enabled = true; BlockAllFields(); this.Run.Enabled = false; this.Pause.Enabled = true; //this.Step.Enabled = false; this.Stop.Enabled = true; //this.richTextBox1.Text += "Simulation started at " + DateTime.Now.ToLongTimeString() + "\n"; this.richTextBox1.Text += "with parameters: \n"; this.richTextBox1.Text += "λ = " + SimulationParams.lambda + " packets per simulation step\n"; this.richTextBox1.Text += "TTL (time to live - number of hops after which packet is removed) = " + SimulationParams.TTL + "\n"; this.richTextBox1.Text += "buffer size = " + SimulationParams.buffer_size + " packets\n"; this.richTextBox1.Text += "network size = " + SimulationParams.network_size + "x" + SimulationParams.network_size + " nodes \n"; } else if (isPaused) { this.richTextBox1.Text += "Simulation resumed at " + DateTime.Now.ToLongTimeString() + "\n"; timer1.Enabled = true; isPaused = false; isRunning = true; this.Run.Enabled = false; this.Pause.Enabled = true; //this.Step.Enabled = false; this.Stop.Enabled = true; } }
public Simulation(int network_size, int seed = 0) { SimulationStats.clearStats(); SimulationParams.network_size = network_size; List <NetworkNode> nodes = new List <NetworkNode>(); List <Link> links = new List <Link>(); int link_id = 0; int id = 0; //i - rows //j - columns //up, down, right, left - ids of neighbours if (SimulationParams.network_size > 0) { for (int i = 1; i <= SimulationParams.network_size; i++) { for (int j = 1; j <= SimulationParams.network_size; j++) { id++; int right = SimulationParams.network_size * (i - 1) + j + 1; int left = SimulationParams.network_size * (i - 1) + j - 1; int up = SimulationParams.network_size * (i - 2) + j; int down = SimulationParams.network_size * i + j; //overwrite above values in special cases: if (i == 1) { up = SimulationParams.network_size * (SimulationParams.network_size - 1) + j; } if (i == SimulationParams.network_size) { down = j; } if (j == 1) { left = SimulationParams.network_size * i; } if (j == SimulationParams.network_size) { right = SimulationParams.network_size * (i - 1) + 1; } //add links with proper directions if (i % 2 == 1) { links.Add(new Link(++link_id, id, right, Enums.direction.right)); } if (i % 2 == 0) { links.Add(new Link(++link_id, right, id, Enums.direction.left)); } if (j % 2 == 1) { links.Add(new Link(++link_id, id, up, Enums.direction.up)); } if (j % 2 == 0) { links.Add(new Link(++link_id, up, id, Enums.direction.down)); } //add node nodes.Add(new NetworkNode(id)); } } nc = new NodeController(nodes, links, seed); } }