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