1 //Kayo Fujiwara 1/8/2007
2 #include "gtnets_simulator.h"
3 #include "gtnets_topology.h"
6 void static tcp_sent_callback(int id, double completion_time);
9 // TODO: check the default values.
14 sim_ = new Simulator();
15 topo_ = new SGTopology();
17 // Set default values.
18 TCP::DefaultAdvWin(wsize);
19 TCP::DefaultSegSize(1000);
20 TCP::DefaultTxBuffer(128000);
21 TCP::DefaultRxBuffer(128000);
26 map<int, Linkp2p*>::iterator it;
27 for (it = gtnets_links_.begin(); it != gtnets_links_.end(); it++){
30 map<int, SGLink*>::iterator it2;
31 for (it2 = tmp_links_.begin(); it2 != tmp_links_.end(); it2++){
34 map<int, Node*>::iterator it3;
35 for (it3 = gtnets_nodes_.begin(); it3 != gtnets_nodes_.end(); it3++){
38 map<int, TCPServer*>::iterator it4;
39 for (it4 = gtnets_servers_.begin(); it4 != gtnets_servers_.end(); it4++){
42 map<int, TCPSend*>::iterator it5;
43 for (it5 = gtnets_clients_.begin(); it5 != gtnets_clients_.end(); it5++){
46 //TODO delete other objects!
50 int GTSim::add_link(int id, double bandwidth, double latency){
51 if (gtnets_links_.find(id) != gtnets_links_.end()){
52 fprintf(stderr, "can't add link %d. already exists.\n", id);
55 gtnets_links_[id] = new Linkp2p(bandwidth, latency);
59 int GTSim::add_route(int src, int dst, int* links, int nlink){
60 topo_->add_link(src, dst, links, nlink);
61 // topo_->create_tmplink(src, dst, links, nlink);
65 void GTSim::create_gtnets_topology(){
66 //TODO: add manual routing
67 //TODO: is this addressing fine?
68 static unsigned int address = IPAddr("192.168.0.1");
69 tmp_links_ = topo_->get_links();
70 map<int, SGLink*>::iterator it;
72 //By now, all links has two nodes.
73 //Connect left and right with the link.
74 for (it = tmp_links_.begin(); it != tmp_links_.end(); it++){
76 //both nodes must be not null. TODO check it!
77 int linkid = it->second->id();
78 int leftid = it->second->left_node()->id();
79 int rightid = it->second->right_node()->id();
81 cout << "linkid: " << linkid << endl;
82 cout << "leftid: " << leftid << endl;
83 cout << "rightid: " << rightid << endl;
86 map<int, Node*>::iterator nodeit = gtnets_nodes_.find(leftid);
87 // if the nodes don't exist, add one.
88 if (nodeit == gtnets_nodes_.end()){
89 gtnets_nodes_[leftid] = new Node();
90 gtnets_nodes_[leftid]->SetIPAddr(address++);
91 //add host-node relationships.
92 vector<int> tmphosts = it->second->left_node()->hosts();
94 for (int i = 0; i < tmphosts.size(); i++){
95 gtnets_hosts_[tmphosts[i]] = leftid;
96 cout << "host: " << tmphosts[i] << " node: " << leftid << endl;
100 nodeit = gtnets_nodes_.find(rightid);
101 if (nodeit == gtnets_nodes_.end()){//new entry
102 gtnets_nodes_[rightid] = new Node();
103 gtnets_nodes_[rightid]->SetIPAddr(address++);
105 //add host-node relationships.
106 vector<int> tmphosts = it->second->right_node()->hosts();
108 for (int i = 0; i < tmphosts.size(); i++){
109 gtnets_hosts_[tmphosts[i]] = rightid;
110 cout << "host: " << tmphosts[i] << " node: " << rightid << endl;
114 gtnets_nodes_[leftid]->
115 AddDuplexLink(gtnets_nodes_[rightid], *gtnets_links_[linkid]);
120 int GTSim::create_flow(int src, int dst, long datasize, void* metadata){
121 if (is_topology_ == 0){
122 create_gtnets_topology();
125 //TODO: what if more than two flows?
126 //TODO: check if src and dst exists.
127 //TODO: should use "flowID" to name servers and clients.
128 gtnets_servers_[nflow_] = (TCPServer*)gtnets_nodes_[gtnets_hosts_[dst]]->
129 AddApplication(TCPServer(TCPReno()));
130 gtnets_servers_[nflow_]->BindAndListen(80);
132 gtnets_clients_[nflow_] = (TCPSend*)gtnets_nodes_[gtnets_hosts_[src]]->
133 AddApplication(TCPSend(nflow_, gtnets_nodes_[gtnets_hosts_[dst]]->GetIPAddr(),
134 80, Constant(datasize), TCPReno()));
135 gtnets_clients_[nflow_]->SetSendCallBack(tcp_sent_callback);
136 gtnets_clients_[nflow_]->Start(0);
142 Time_t GTSim::get_time_to_next_flow_completion(){
151 read(pfds[0], &t1, sizeof(Time_t));
152 waitpid(-1, &status, 0);
155 t = sim_->RunUntilNextCompletion();
156 write(pfds[1], (const void*)&t, sizeof(Time_t));
162 int GTSim::run_until_next_flow_completion(void ***metadata, int *number_of_flows){
163 Time_t t1 = sim_->RunUntilNextCompletion();
165 //TODO set metadata and number of flows.
169 int GTSim::run(double delta){
175 int GTSim::finalize(){
183 void static tcp_sent_callback(int id, double completion_time){
184 // Schedule the flow complete event.
186 new SimulatorEvent(SimulatorEvent::FLOW_COMPLETE);
187 Simulator::instance->Schedule(e, 0, Simulator::instance);
190 printf("In tcp_sent_callback: flow id: %d, time: %f\n", id, completion_time);