]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src/fwp/fwp/demo/sender_adaptive.c
fwp: demo_sender: added contract cancellation
[frescor/frsh-forb.git] / src / fwp / fwp / demo / sender_adaptive.c
1 /**
2  * \file sender_adaptive.c
3  * sends data according to the actual budget and period
4  *
5  */
6 #include <frsh.h>
7 #include "test_config.h"
8
9 #include <errno.h>
10 #include <stdio.h>
11 #include <pthread.h>
12
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16
17 #include <wvtest.h>
18 #include "fwp_confdefs.h"
19 #include <fres_sa_scenario.h>
20 #include <fwp_res.h>
21 #include <fwp_vres.h>
22
23 /*
24  *      Mac address conversion functions
25  */
26 /**
27  *      Char to number conversion
28  */
29 int atoint(char a)
30 {
31         if( (a >= '0') && (a <= '9') )
32                 return a - '0';
33         else if ( (a >= 'A') && (a <= 'F') ) 
34                 return a - 'A' + 10;
35         else if ( (a >= 'a') && (a <= 'f') ) 
36                 return a - 'a' + 10;
37         return 0;
38 }
39
40 /**
41  *      Mac addres char to number conversion
42  *      @param mac mac address in string format
43  *      
44  *      @return Mac address in long long format
45  */
46 long long mac_addr_string_to_number(char * mac)
47 {
48         long long number;
49         long long help;
50         int ix;
51         int ix2;
52         char mac_num[12];
53
54         mac_num[0] = mac[0];
55         mac_num[1] = mac[1];
56         mac_num[2] = mac[3];
57         mac_num[3] = mac[4];
58         mac_num[4] = mac[6];
59         mac_num[5] = mac[7];
60         mac_num[6] = mac[9];
61         mac_num[7] = mac[10];
62         mac_num[8] = mac[12];
63         mac_num[9] = mac[13];
64         mac_num[10] = mac[15];
65         mac_num[11] = mac[16];
66
67         number = 0;
68
69         for(ix = 0; ix <= 12 ; ix++)
70         {       
71                 help = 1;
72                 for(ix2 = 1; ix2 < 12 - ix; ix2++)
73                         help *= 16;  
74                 number += atoint(mac_num[ix]) * help;
75
76         }
77         
78         printf("%llu \n", number);
79         return number;
80 }
81
82 char * prepare_msg(int size) {
83         int i;
84         char * ret = (char*)malloc(size*sizeof(char));
85         for(i=0; i<size; i++) {
86                 ret[i] = '0'; //just insert some characters
87         }
88         return ret;
89 }
90
91 int main(int argc, char* argv[])
92 {
93         char *msg;
94
95         frsh_resource_id_t resource_id = TEST_RESOURCE_ID;
96         frsh_stream_id_t port = TEST_STREAM_ID;
97         long int num_msg = TEST_NUM_MSG;
98         size_t msg_size = MSGBUFFSIZE;
99         int time_period = 100;
100         int dst_ip = 0;
101         char mac_addr[20];
102
103         int opt;
104         frsh_send_endpoint_t sepoint;
105         frsh_vres_id_t vres;
106         frsh_send_endpoint_protocol_info_t send_pinfo;
107         frsh_contract_t contract;
108         frsh_rel_time_t budget, period;
109         int count;
110
111         bool opt_daemon = false;
112         char *opt_pidfile = NULL;
113         frsh_rel_time_t zero = fosa_msec_to_rel_time(0);
114
115         frsh_utilization_set_t utilization_set;
116         utilization_set.size = 4;
117         utilization_set.utilizations[1].period = fosa_msec_to_rel_time(100);
118         utilization_set.utilizations[1].budget = fosa_usec_to_rel_time(3);
119         utilization_set.utilizations[1].deadline = fosa_msec_to_rel_time(100);
120         utilization_set.utilizations[2].period = fosa_msec_to_rel_time(100);
121         utilization_set.utilizations[2].budget = fosa_usec_to_rel_time(9);
122         utilization_set.utilizations[2].deadline = fosa_msec_to_rel_time(100);
123         utilization_set.utilizations[3].period = fosa_msec_to_rel_time(100);
124         utilization_set.utilizations[3].budget = fosa_usec_to_rel_time(14);
125         utilization_set.utilizations[3].deadline = fosa_msec_to_rel_time(100);
126
127         opterr = 0;
128         while ((opt = getopt (argc, argv, "e:d:p:s:m:t:a:")) != -1) {
129
130                 switch (opt) {
131                         case 'e':
132                                 opt_daemon = true;
133                                 opt_pidfile = optarg;
134                                 break;
135                         case 'd':
136                                 dst_ip = inet_addr(optarg);
137                                 break;
138                         case 'p':
139                                 port = atoi(optarg);
140                                 break;
141                         case 'm':
142                                 num_msg = atoi(optarg);
143                                 break;
144                         case 's':
145                                 msg_size = atoi(optarg);
146                                 break;
147                         case 't':
148                                 time_period = atoi(optarg);
149                                 break;
150                         case 'a':
151                                 strcpy(mac_addr, optarg);
152                                 break;
153                         case '?':
154                                 printf("Usage: %s -e -d dst_ip_addr -p port"
155                                                 "-m num_msg -s msg_size -a mac_addr\n",
156                                                 argv[0]);
157                 }
158         }
159
160         if (opt_daemon)
161                 forb_daemon_prepare(opt_pidfile);
162
163         WVFRSH(frsh_init());
164         send_pinfo.body = NULL;
165         WVPASSNE(frsh_send_endpoint_create(resource_id, dst_ip, port, send_pinfo, &sepoint), 0);
166         
167         /* Contract parameters */
168         WVFRSH(frsh_contract_init(&contract));
169         WVFRSH(frsh_network_bytes_to_budget(resource_id, msg_size, &utilization_set.utilizations[0].budget));
170         utilization_set.utilizations[0].period = fosa_msec_to_rel_time(time_period);
171         WVFRSH(frsh_contract_set_basic_params(&contract,
172                                              &utilization_set.utilizations[0].budget,
173                                              &utilization_set.utilizations[0].period,
174                                              FRSH_WT_BOUNDED,
175                                              FRSH_CT_REGULAR));
176         WVFRSH(frsh_contract_set_resource_and_label(&contract,FRSH_RT_NETWORK,
177                                                 resource_id, "net_cont"));
178
179         /* Add fwp block with MAC address */
180         fres_block_fwp *fwp_block;
181         WVPASS((fwp_block = (fres_block_fwp*)malloc(sizeof(fres_block_fwp))) != NULL);
182         fwp_block->mac_address = mac_addr_string_to_number(mac_addr);
183         WVPASS(fres_contract_add_block(contract, FRES_BLOCK_FWP, fwp_block) == 0);
184
185         /* Add spare capacity block */
186         //TODO: Delete extra spare capacity block initialization, since spare capacity is initialized in set_reclamation_params function        
187         //fres_block_spare_capacity *s;
188         //WVPASS((s = malloc(sizeof(*s))) != NULL);
189         //s->budget = (fosa_rel_time_t)budget;
190         //s->budget = (fosa_rel_time_t)utilization_set.utilizations[0].budget;
191         //s->period = (fosa_rel_time_t)utilization_set.utilizations[0].period;
192         //WVPASS(fres_contract_add_spare_capacity(contract, s) == 0);
193
194         WVFRSH(frsh_contract_set_reclamation_params(&contract,
195                 &zero,
196                 &utilization_set.utilizations[utilization_set.size-1].budget,
197                 &utilization_set.utilizations[utilization_set.size-1].period,
198                 FRSH_GR_DISCRETE,
199                 &utilization_set,
200                 0,
201                 0));
202
203         WVFRSH(frsh_contract_negotiate(&contract, &vres));
204         WVFRSH(frsh_send_endpoint_bind(vres, sepoint));
205
206         if (opt_daemon)
207                 forb_daemon_ready();
208
209         count = 0;
210         while (count != num_msg) {
211                 count++;
212                 WVPASS(frsh_vres_get_budget_and_period (vres, &budget, &period) == 0);
213                 WVFRSH(frsh_network_budget_to_bytes(resource_id, &budget, &msg_size));
214
215                 //TODO: Delete printf below
216                 printf("msg_size....%d\n", msg_size);
217                 printf("count....%d\n", count);
218
219                 msg = prepare_msg(msg_size);
220                 WVPASSEQ(frsh_send_sync(sepoint, msg, msg_size), msg_size);
221         }
222
223         frsh_contract_cancel(vres);
224         frsh_contract_destroy((struct fres_contract **)contract);
225         printf("Test PASSED!\n");
226         return 0;
227 }
228