]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - resources/cluster_tree/frm_cluster_tree.c
Added contract accessor functions to ITEM and cluster tree resources
[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_cluster_tree_traffic(c->contract);
49                 if(traffic_fill(&contract_traffic->nodes, &cum_traffic) == -1) {
50                         ret = -1;
51                         goto err_free_traffic;
52                 }               
53
54                 /* Find the most restricting deadline */
55                 if (fres_contract_get_deadline(&c->contract, &deadline)) {
56                         if (fosa_rel_time_is_null(min_deadline) ||
57                             fosa_rel_time_smaller(deadline, min_deadline)) {
58                                 min_deadline = deadline;
59                         }
60                 }
61         }
62         
63         application_params.ack_enable = FALSE;
64         application_params.alpha_data = max_alpha(cum_traffic); /* Find the maximal arrival curve from sensory traffic */
65         
66         output_params.p_buffer_requirements =
67                 (int *) malloc((topology->height+topology->h_sink+2)*sizeof(int));
68         if (output_params.p_buffer_requirements == NULL) {
69                 ret = -1;
70                 goto err_free_traffic;
71         }
72
73         output_params.p_number_of_time_slots =
74                 (unsigned short *) malloc((topology->height+topology->h_sink)*sizeof(int));
75         if(output_params.p_number_of_time_slots == NULL) {
76                 ret = -1;
77                 goto err_free_req;
78         }
79
80         /* Call the real admission test */
81         ret = ct_wsn(*topology, application_params, &output_params);
82         if(ret == -1)
83         {
84                 *schedulable = false;
85                 printf("error-code: %d\n", output_params.error_code);
86         } else {
87                 /* Compare the deadlines */
88                 if (output_params.d_e2e > fosa_rel_time_to_double(min_deadline)) {
89                         *schedulable = false;
90                 } else {
91                         *schedulable = true;
92                 }
93         }
94
95         ret = 0;
96
97         free((void *)output_params.p_number_of_time_slots);
98 err_free_req:
99         free((void *)output_params.p_buffer_requirements);
100 err_free_traffic:
101         free(cum_traffic.p_nodes);
102         return ret;
103 }
104
105 static const struct fres_res_manager frm = {
106         .res_type = FRSH_RT_NETWORK,
107         .res_id = FRSH_NETPF_CLUSTER_TREE,
108         .admission_test = admission_test,
109         .priv = &configuration,
110 };
111
112
113 int main(int argc, char *argv[])
114 {
115         forb_orb orb;
116         int ret;
117
118         orb = forb_init(&argc, &argv, "frm_cluster_tree");
119         if (!orb) error(1, errno, "forb_init");
120
121         fres_block_register_cluster_tree();
122
123         ret = frm_register_and_run(orb, &frm);
124
125         if (ret != 0) {
126                 error(1, errno, "frm_generic_run failed");
127         }
128         
129         return 0;
130 }