Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Factorized better debugging output.
[simgrid.git] / src / surf / gtnets / gtnets_interface.cc
1 /*      $Id$     */
2 /* Copyright (c) 2007 Kayo Fujiwara. All rights reserved.                  */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7
8 #include "gtnets_simulator.h"
9 #include "gtnets_interface.h"
10 #ifdef DEBUG0
11         #undef DEBUG0
12 #endif
13 #include "xbt/log.h"
14 #include "xbt/asserts.h"
15
16 static GTSim* gtnets_sim = 0;
17
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets_interface, surf_network_gtnets,
20                                 "Logging specific to the SURF network GTNetS interface");
21
22
23
24 // initialize the GTNetS interface and environment
25 int gtnets_initialize(){
26
27   DEBUG0("Using logging.");
28
29   xbt_assert0(!gtnets_sim, "gtnets already initialized");
30
31   gtnets_sim = new GTSim();
32   return 0;
33 }
34
35 // add a link (argument link is just an index starting at 0... 
36 // add link 0, add link 1, etc.)
37 int gtnets_add_link(int id, double bandwidth, double latency){
38   return gtnets_sim->add_link(id, bandwidth, latency);
39 }
40
41 // add a route between a source and a destination as an array of link indices
42 // (note that there is no gtnets_add_network_card(), as we discover them
43 // on the fly via calls to gtnets_add_route()
44 int gtnets_add_route(int src, int dst, int* links, int nlink){
45   return gtnets_sim->add_route(src, dst, links, nlink);
46 }
47
48 // add a router
49 int gtnets_add_router(int id){
50   return gtnets_sim->add_router(id);
51 }
52
53 // add a route between a source and a destination as an array of link indices
54 // (note that there is no gtnets_add_network_card(), as we discover them
55 // on the fly via calls to gtnets_add_route()
56 int gtnets_add_onehop_route(int src, int dst, int link){
57   return gtnets_sim->add_onehop_route(src, dst, link);
58 }
59
60 // create a new flow on a route
61 // one can attach arbitrary metadata to a flow
62 int gtnets_create_flow(int src, int dst, long datasize, void* metadata){
63   return gtnets_sim->create_flow(src, dst, datasize, metadata);
64 }
65
66 // get the time (double) until a flow completes (the first such flow)
67 // if no flows exist, return -1.0
68 double gtnets_get_time_to_next_flow_completion(){
69   ofstream file;
70   streambuf* sbuf;
71   double value;
72
73   if (!XBT_LOG_ISENABLED(surf_network_gtnets_interface, xbt_log_priority_debug)) {
74           file.open ("/dev/null");
75           sbuf = cout.rdbuf();
76           cout.rdbuf(file.rdbuf());
77           DEBUG0("Enable GTNetS library quite mode");
78   }else {
79           DEBUG0("Disable GTNetS library quite mode");
80   }
81
82   value = gtnets_sim->get_time_to_next_flow_completion();
83
84   if (!XBT_LOG_ISENABLED(surf_network_gtnets_interface, xbt_log_priority_debug)) {
85           cout.rdbuf(sbuf);
86           file.close();
87   }
88   return value;
89 }
90
91 // run until a flow completes (returns that flow's metadata)
92 int gtnets_run_until_next_flow_completion(void ***metadata, int *number_of_flows){
93   ofstream file;
94   streambuf* sbuf;
95   double value;
96
97   if (!XBT_LOG_ISENABLED(surf_network_gtnets_interface, xbt_log_priority_debug)) {
98           file.open ("/dev/null");
99           sbuf = cout.rdbuf();
100           cout.rdbuf(file.rdbuf());
101           DEBUG0("Enable GTNetS library quite mode");
102   }else {
103           DEBUG0("Disable GTNetS library quite mode");
104   }
105
106   value = gtnets_sim->run_until_next_flow_completion(metadata, number_of_flows);
107
108   if (!XBT_LOG_ISENABLED(surf_network_gtnets_interface, xbt_log_priority_debug)) {
109           cout.rdbuf(sbuf);
110           file.close();
111   }
112   return value;
113 }
114
115 // get the total received in bytes using the TCPServer object totRx field
116 double gtnets_get_flow_rx(void *metadata){
117   return gtnets_sim->gtnets_get_flow_rx(metadata);
118 }
119
120
121 // run for a given time (double)
122 int gtnets_run(Time_t deltat){
123   gtnets_sim->run(deltat);
124   return 0;
125 }
126
127 // clean up
128 int gtnets_finalize(){
129   if (!gtnets_sim) return -1;
130   delete gtnets_sim;
131   gtnets_sim = 0;
132   return 0;
133 }
134
135 // print topology
136 void gtnets_print_topology(void){
137   gtnets_sim->print_topology();
138 }
139