Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
*** empty log message ***
[simgrid.git] / src / surf / gtnets / gtnets_simulator.cc
1 //Kayo Fujiwara 1/8/2007
2 #include "gtnets_simulator.h"
3 #include "gtnets_topology.h"
4 #include <map>
5
6 void static tcp_sent_callback(int id, double completion_time);
7
8 // Constructor.
9 // TODO: check the default values.
10 GTSim::GTSim(){
11   int wsize = 20000;
12   is_topology_ = 0;
13   nflow_ = 0;
14   sim_ = new Simulator();
15   topo_ = new SGTopology();
16
17   // Set default values.
18   TCP::DefaultAdvWin(wsize);
19   TCP::DefaultSegSize(1000);
20   TCP::DefaultTxBuffer(128000);
21   TCP::DefaultRxBuffer(128000);
22
23 }
24 GTSim::~GTSim(){
25
26   map<int, Linkp2p*>::iterator it;
27   for (it = gtnets_links_.begin(); it != gtnets_links_.end(); it++){
28     delete it->second;
29   }
30   map<int, SGLink*>::iterator it2;
31   for (it2 = tmp_links_.begin(); it2 != tmp_links_.end(); it2++){
32     delete it2->second;
33   }
34   map<int, Node*>::iterator it3;
35   for (it3 = gtnets_nodes_.begin(); it3 != gtnets_nodes_.end(); it3++){
36     delete it3->second;
37   }
38   map<int, TCPServer*>::iterator it4;
39   for (it4 = gtnets_servers_.begin(); it4 != gtnets_servers_.end(); it4++){
40     delete it4->second;
41   }
42   map<int, TCPSend*>::iterator it5;
43   for (it5 = gtnets_clients_.begin(); it5 != gtnets_clients_.end(); it5++){
44     delete it5->second;
45   }
46   //TODO delete other objects!
47 }
48
49
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);
53     return -1;
54   }
55   gtnets_links_[id] = new Linkp2p(bandwidth, latency);
56   return 0;
57 }
58
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);
62   return 0;
63 }
64
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;
71
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++){
75     it->second->print();
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();
80
81     cout << "linkid: " << linkid << endl;
82     cout << "leftid: " << leftid << endl;
83     cout << "rightid: " << rightid << endl;
84
85
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();
93
94       for (int i = 0; i < tmphosts.size(); i++){
95         gtnets_hosts_[tmphosts[i]] = leftid;
96         cout << "host: " << tmphosts[i] << " node: " << leftid << endl;
97       }
98
99     }
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++);
104
105       //add host-node relationships.
106       vector<int> tmphosts = it->second->right_node()->hosts();
107
108       for (int i = 0; i < tmphosts.size(); i++){
109         gtnets_hosts_[tmphosts[i]] = rightid;
110         cout << "host: " << tmphosts[i] << " node: " << rightid << endl;
111       }
112     }
113
114     gtnets_nodes_[leftid]->
115       AddDuplexLink(gtnets_nodes_[rightid], *gtnets_links_[linkid]);
116
117   }
118 }
119
120 int GTSim::create_flow(int src, int dst, long datasize, void* metadata){
121   if (is_topology_ == 0){
122     create_gtnets_topology();
123     is_topology_ = 1;
124   }
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);
131
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);
137   nflow_++;
138
139   return 0;
140 }
141
142 Time_t GTSim::get_time_to_next_flow_completion(){
143   int status;
144   Time_t t1;
145   int pfds[2];
146   
147   pipe(pfds);
148   
149   t1 = 0;
150   if (fork() != 0){
151     read(pfds[0], &t1, sizeof(Time_t));
152     waitpid(-1, &status, 0);      
153   }else{
154     Time_t t;
155     t = sim_->RunUntilNextCompletion();
156     write(pfds[1], (const void*)&t, sizeof(Time_t));
157     exit(0);
158   }
159   return t1;
160 }
161
162 int GTSim::run_until_next_flow_completion(void ***metadata, int *number_of_flows){
163   Time_t t1 = sim_->RunUntilNextCompletion();
164   sim_->Run(t1);
165   //TODO set metadata and number of flows.
166   return 0;
167 }
168
169 int GTSim::run(double delta){
170   sim_->Run(delta);
171   return 0;
172 }
173
174 // Clean up.
175 int GTSim::finalize(){
176   //TODO
177   is_topology_ = 0;
178   delete sim_;
179   delete topo_;
180   return 0;
181 }
182
183 void static tcp_sent_callback(int id, double completion_time){
184   // Schedule the flow complete event.
185   SimulatorEvent* e =
186     new SimulatorEvent(SimulatorEvent::FLOW_COMPLETE);
187   Simulator::instance->Schedule(e, 0, Simulator::instance);
188
189   //TODO: set metadata
190   printf("In tcp_sent_callback: flow id: %d, time: %f\n", id, completion_time);
191 }
192