]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src/fwp/fwp/demo/sender_adaptive.c
879831b7fcebacc1fa0a15ed99125b7ac7787702
[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 = 3;
117         utilization_set.utilizations[1].period = fosa_msec_to_rel_time(100);
118         WVFRSH(frsh_network_bytes_to_budget(resource_id, 3000, &utilization_set.utilizations[1].budget));
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         WVFRSH(frsh_network_bytes_to_budget(resource_id, 14000, &utilization_set.utilizations[2].budget));
122         utilization_set.utilizations[2].deadline = fosa_msec_to_rel_time(100);
123
124         opterr = 0;
125         while ((opt = getopt (argc, argv, "e:d:p:s:m:t:a:")) != -1) {
126
127                 switch (opt) {
128                         case 'e':
129                                 opt_daemon = true;
130                                 opt_pidfile = optarg;
131                                 break;
132                         case 'd':
133                                 dst_ip = inet_addr(optarg);
134                                 break;
135                         case 'p':
136                                 port = atoi(optarg);
137                                 break;
138                         case 'm':
139                                 num_msg = atoi(optarg);
140                                 break;
141                         case 's':
142                                 msg_size = atoi(optarg);
143                                 break;
144                         case 't':
145                                 time_period = atoi(optarg);
146                                 break;
147                         case 'a':
148                                 strcpy(mac_addr, optarg);
149                                 break;
150                         case '?':
151                                 printf("Usage: %s -e -d dst_ip_addr -p port"
152                                                 "-m num_msg -s msg_size -a mac_addr\n",
153                                                 argv[0]);
154                 }
155         }
156
157         if (opt_daemon)
158                 forb_daemon_prepare(opt_pidfile);
159
160         WVFRSH(frsh_init());
161         send_pinfo.body = NULL;
162         WVPASSNE(frsh_send_endpoint_create(resource_id, dst_ip, port, send_pinfo, &sepoint), 0);
163         
164         /* Contract parameters */
165         WVFRSH(frsh_contract_init(&contract));
166         WVFRSH(frsh_network_bytes_to_budget(resource_id, msg_size, &utilization_set.utilizations[0].budget));
167         utilization_set.utilizations[0].period = fosa_msec_to_rel_time(time_period);
168         WVFRSH(frsh_contract_set_basic_params(&contract,
169                                              &utilization_set.utilizations[0].budget,
170                                              &utilization_set.utilizations[0].period,
171                                              FRSH_WT_BOUNDED,
172                                              FRSH_CT_REGULAR));
173         WVFRSH(frsh_contract_set_resource_and_label(&contract,FRSH_RT_NETWORK,
174                                                 resource_id, "net_cont"));
175
176         /* Add fwp block with MAC address */
177         fres_block_fwp *fwp_block;
178         WVPASS((fwp_block = (fres_block_fwp*)malloc(sizeof(fres_block_fwp))) != NULL);
179         fwp_block->mac_address = mac_addr_string_to_number(mac_addr);
180         fwp_block->src = 0;
181         WVPASS(fres_contract_add_block(contract, FRES_BLOCK_FWP, fwp_block) == 0);
182
183         /* Add spare capacity block */
184         //TODO: Delete extra spare capacity block initialization, since spare capacity is initialized in set_reclamation_params function        
185         //fres_block_spare_capacity *s;
186         //WVPASS((s = malloc(sizeof(*s))) != NULL);
187         //s->budget = (fosa_rel_time_t)budget;
188         //s->budget = (fosa_rel_time_t)utilization_set.utilizations[0].budget;
189         //s->period = (fosa_rel_time_t)utilization_set.utilizations[0].period;
190         //WVPASS(fres_contract_add_spare_capacity(contract, s) == 0);
191
192         WVFRSH(frsh_contract_set_reclamation_params(&contract,
193                 &zero,
194                 &utilization_set.utilizations[utilization_set.size-1].budget,
195                 &utilization_set.utilizations[utilization_set.size-1].period,
196                 FRSH_GR_DISCRETE,
197                 &utilization_set,
198                 0,
199                 0));
200
201         WVFRSH(frsh_contract_negotiate(&contract, &vres));
202         WVFRSH(frsh_send_endpoint_bind(vres, sepoint));
203
204         if (opt_daemon)
205                 forb_daemon_ready();
206
207         count = 0;
208         while (count != num_msg) {
209                 count++;
210                 WVPASS(frsh_vres_get_budget_and_period (vres, &budget, &period) == 0);
211                 WVFRSH(frsh_network_budget_to_bytes(resource_id, &budget, &msg_size));
212
213                 //TODO: Delete printf below
214                 printf("msg_size....%d\n", msg_size);
215                 printf("count....%d\n", count);
216
217                 msg = prepare_msg(msg_size);
218                 WVPASSEQ(frsh_send_sync(sepoint, msg, msg_size), msg_size);
219         }
220
221         frsh_contract_cancel(vres);
222         frsh_contract_destroy((struct fres_contract **)contract);
223         printf("Test PASSED!\n");
224         return 0;
225 }
226