]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - resources/cluster_tree/frm_cluster_tree.c
Added fres_contract_get_deadline() convenience function
[frescor/frsh.git] / resources / cluster_tree / frm_cluster_tree.c
1 #include <frm_generic.h>
2 #include <forb.h>
3 #include <error.h>
4 #include <errno.h>
5 #include <fres_sa_scenario.h>
6 #include <stdbool.h>
7 #include <ul_log.h>
8 #include <stdio.h>
9 #include <frsh_distributed.h>
10 #include "ct_wsn.h"
11 #include <cluster_tree.h>
12
13 struct cluster_tree {
14         TOPOLOGY_PARAMS topology;
15 };
16
17 struct cluster_tree configuration = {
18         .topology = {
19                 .max_routers = 2,
20                 .max_end_nodes = 2,
21                 .height = 2,
22                 .h_sink = 1,
23                 .SO = 3,
24                 .sensing_cap = FALSE,
25         }
26 };
27
28
29 int admission_test(struct fres_sa_scenario *scenario, void *priv, bool *schedulable)
30 {
31         struct cluster_tree *configuration = priv;
32         TOPOLOGY_PARAMS *topology = &configuration->topology;
33         struct fres_sa_contract *c;
34         APPLICATION_PARAMS application_params;
35         int ret;
36         OUTPUT_PARAMS output_params;
37         TRAFFIC cum_traffic;
38         frsh_rel_time_t min_deadline = fosa_msec_to_rel_time(0);
39         
40         cum_traffic.length = 0;
41         cum_traffic.p_nodes = NULL;
42
43         fres_sa_scenario_for_each_contract(scenario, c) {
44                 fres_block_cluster_tree_traffic *contract_traffic;
45                 frsh_rel_time_t deadline;
46
47                 /* Extract the cumulative traffic through all contracts */
48                 contract_traffic = fres_contract_get_block(c->contract,
49                                                   FRES_BLOCK_CLUSTER_TREE_TRAFFIC);
50                                                   
51                 if(traffic_fill(&contract_traffic->nodes, &cum_traffic) == -1) {
52                         ret = -1;
53                         goto err_free_traffic;
54                 }               
55
56                 /* Find the most restricting deadline */
57                 if (fres_contract_get_deadline(&c->contract, &deadline)) {
58                         if (fosa_rel_time_is_null(min_deadline) ||
59                             fosa_rel_time_smaller(deadline, min_deadline)) {
60                                 min_deadline = deadline;
61                         }
62                 }
63         }
64         
65         application_params.ack_enable = FALSE;
66         application_params.alpha_data = max_alpha(cum_traffic); /* Find the maximal arrival curve from sensory traffic */
67         
68         output_params.p_buffer_requirements =
69                 (int *) malloc((topology->height+topology->h_sink+2)*sizeof(int));
70         if (output_params.p_buffer_requirements == NULL) {
71                 ret = -1;
72                 goto err_free_traffic;
73         }
74
75         output_params.p_number_of_time_slots =
76                 (unsigned short *) malloc((topology->height+topology->h_sink)*sizeof(int));
77         if(output_params.p_number_of_time_slots == NULL) {
78                 ret = -1;
79                 goto err_free_req;
80         }
81
82         /* Call the real admission test */
83         ret = ct_wsn(*topology, application_params, &output_params);
84         if(ret == -1)
85         {
86                 *schedulable = false;
87                 printf("error-code: %d\n", output_params.error_code);
88         } else {
89                 /* Compare the deadlines */
90                 if (output_params.d_e2e > fosa_rel_time_to_double(min_deadline)) {
91                         *schedulable = false;
92                 } else {
93                         *schedulable = true;
94                 }
95         }
96
97         ret = 0;
98
99         free((void *)output_params.p_number_of_time_slots);
100 err_free_req:
101         free((void *)output_params.p_buffer_requirements);
102 err_free_traffic:
103         free(cum_traffic.p_nodes);
104         return ret;
105 }
106
107 static const struct fres_res_manager frm = {
108         .res_type = FRSH_RT_NETWORK,
109         .res_id = FRSH_NETPF_CLUSTER_TREE,
110         .admission_test = admission_test,
111         .priv = &configuration,
112 };
113
114
115 int main(int argc, char *argv[])
116 {
117         forb_orb orb;
118         int ret;
119
120         orb = forb_init(&argc, &argv, "frm_cluster_tree");
121         if (!orb) error(1, errno, "forb_init");
122
123         fres_block_register_cluster_tree();
124
125         ret = frm_register_and_run(orb, &frm);
126
127         if (ret != 0) {
128                 error(1, errno, "frm_generic_run failed");
129         }
130         
131         return 0;
132 }