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