Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
give Link a proper public interface
[simgrid.git] / src / simdag / sd_link.c
1 /* Copyright (c) 2006-2014. The SimGrid Team.
2  * 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 #include "private.h"
8 #include "simgrid/simdag.h"
9 #include "xbt/dict.h"
10 #include "xbt/sysdep.h"
11 #include "surf/surf.h"
12 #include "simgrid/link.h"
13
14 /* Creates a link and registers it in SD.
15  */
16 SD_link_t __SD_link_create(void *surf_link, void *data)
17 {
18
19   SD_link_t link;
20   const char *name;
21
22   link = xbt_new(s_SD_link_t, 1);
23   link->surf_link = surf_link;
24   link->data = data;            /* user data */
25   if (surf_network_link_is_shared(surf_link))
26     link->sharing_policy = SD_LINK_SHARED;
27   else
28     link->sharing_policy = SD_LINK_FATPIPE;
29
30   name = SD_link_get_name(link);
31   xbt_lib_set(link_lib,name,SD_LINK_LEVEL,link);
32
33   return link;
34 }
35
36 /**
37  * \brief Returns the link list
38  *
39  * Use SD_link_get_number() to know the array size.
40  *
41  * \return an array of \ref SD_link_t containing all links
42  * \see SD_link_get_number()
43  */
44 const SD_link_t *SD_link_get_list(void)
45 {
46
47   xbt_lib_cursor_t cursor;
48   char *key;
49   void **data;
50   int i;
51
52   if (sd_global->link_list == NULL) {   /* this is the first time the function is called */
53     sd_global->link_list = xbt_new(SD_link_t, xbt_lib_length(link_lib));
54
55     i = 0;
56     xbt_lib_foreach(link_lib, cursor, key, data) {
57         sd_global->link_list[i++] = (SD_link_t) data[SD_LINK_LEVEL];
58     }
59   }
60   return sd_global->link_list;
61 }
62
63 /**
64  * \brief Returns the number of links
65  *
66  * \return the number of existing links
67  * \see SD_link_get_list()
68  */
69 int SD_link_get_number(void)
70 {
71   return xbt_lib_length(link_lib);
72 }
73
74 /**
75  * \brief Returns the user data of a link
76  *
77  * \param link a link
78  * \return the user data associated with this link (can be \c NULL)
79  * \see SD_link_set_data()
80  */
81 void *SD_link_get_data(SD_link_t link)
82 {
83   return link->data;
84 }
85
86 /**
87  * \brief Sets the user data of a link
88  *
89  * The new data can be \c NULL. The old data should have been freed first
90  * if it was not \c NULL.
91  *
92  * \param link a link
93  * \param data the new data you want to associate with this link
94  * \see SD_link_get_data()
95  */
96 void SD_link_set_data(SD_link_t link, void *data)
97 {
98   link->data = data;
99 }
100
101 /**
102  * \brief Returns the name of a link
103  *
104  * \param link a link
105  * \return the name of this link (cannot be \c NULL)
106  */
107 const char *SD_link_get_name(SD_link_t link)
108 {
109   return surf_resource_name(link->surf_link);
110 }
111
112 /**
113  * \brief Returns the current bandwidth of a link
114  *
115  * \param link a link
116  * \return the current bandwidth of this link, in bytes per second
117  */
118 double SD_link_get_current_bandwidth(SD_link_t link)
119 {
120   return surf_network_link_get_bandwidth(link->surf_link);
121 }
122
123 /**
124  * \brief Returns the current latency of a link
125  *
126  * \param link a link
127  * \return the current latency of this link, in seconds
128  */
129 double SD_link_get_current_latency(SD_link_t link)
130 {
131   return surf_network_link_get_latency(link->surf_link);
132 }
133
134 /**
135  * \brief Returns the sharing policy of this workstation.
136  *
137  * \param link a link
138  * \return the sharing policyfor the flows going through this link:
139  * SD_LINK_SHARED or SD_LINK_FATPIPE
140  *
141  */
142 e_SD_link_sharing_policy_t SD_link_get_sharing_policy(SD_link_t link)
143 {
144   return link->sharing_policy;
145 }