]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src/fwp/fwp/demo/sender_adaptive.c
fwp: adaptive test updated
[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 int main(int argc, char* argv[])
83 {
84         char msg[MSGBUFFSIZE];
85
86         frsh_resource_id_t resource_id = TEST_RESOURCE_ID;
87         frsh_stream_id_t port = TEST_STREAM_ID;
88         long int num_msg = TEST_NUM_MSG;
89         size_t msg_size = MSGBUFFSIZE;
90         int time_period = 1000;
91         int dst_ip = 0;
92         char mac_addr[20];
93
94         int opt;
95         frsh_send_endpoint_t sepoint;
96         frsh_vres_id_t vres;
97         frsh_send_endpoint_protocol_info_t send_pinfo;
98         frsh_contract_t contract;
99         frsh_rel_time_t budget, period;
100         int count;
101
102         struct fwp_vres_params vparam;
103
104         bool opt_daemon = false;
105         char *opt_pidfile = NULL;
106
107         opterr = 0;
108         while ((opt = getopt (argc, argv, "e:d:p:s:m:t:a:")) != -1) {
109
110                 switch (opt) {
111                         case 'e':
112                                 opt_daemon = true;
113                                 opt_pidfile = optarg;
114                                 break;
115                         case 'd':
116                                 dst_ip = inet_addr(optarg);
117                                 break;
118                         case 'p':
119                                 port = atoi(optarg);
120                                 break;
121                         case 'm':
122                                 num_msg = atoi(optarg);
123                                 break;
124                         case 's':
125                                 msg_size = atoi(optarg);
126                                 break;
127                         case 't':
128                                 time_period = atoi(optarg);
129                                 break;
130                         case 'a':
131                                 strcpy(mac_addr, optarg);
132                                 break;
133                         case '?':
134                                 printf("Usage: %s -e -d dst_ip_addr -p port"
135                                                 "-m num_msg -s msg_size -a mac_addr\n",
136                                                 argv[0]);
137                 }
138         }
139
140         if (opt_daemon)
141                 forb_daemon_prepare(opt_pidfile);
142
143         WVFRSH(frsh_init());
144         send_pinfo.body = NULL;
145         WVPASSNE(frsh_send_endpoint_create(resource_id, dst_ip, port, send_pinfo, &sepoint), 0);
146
147         /* Contract parameters */
148         WVFRSH(frsh_contract_init(&contract));
149         WVFRSH(frsh_network_bytes_to_budget(resource_id, msg_size, &budget));
150         period = fosa_msec_to_rel_time(time_period);
151         WVFRSH(frsh_contract_set_basic_params(&contract,
152                                              &budget,
153                                              &period,
154                                              FRSH_WT_BOUNDED,
155                                              FRSH_CT_REGULAR));
156         WVFRSH(frsh_contract_set_resource_and_label(&contract,FRSH_RT_NETWORK,
157                                                 resource_id, "net_cont"));
158
159         /* Add fwp block with MAC address */
160         fres_block_fwp *fwp_block;
161         WVPASS((fwp_block = (fres_block_fwp*)malloc(sizeof(fres_block_fwp))) != NULL);
162         fwp_block->mac_address = mac_addr_string_to_number(mac_addr);
163         WVPASS(fres_contract_add_block(contract, FRES_BLOCK_FWP, fwp_block) == 0);
164
165         /* Add spare capacity block */
166         fres_block_spare_capacity *s;
167         WVPASS((s = malloc(sizeof(*s))) != NULL);
168         s->budget = (fosa_rel_time_t)budget;
169         s->period = (fosa_rel_time_t)period;
170         WVPASS(fres_contract_add_spare_capacity(contract, s) == 0);
171
172         /* Create virtual resource */
173         vparam.ac_id = FWP_AC_VO;       
174         vparam.budget = msg_size;
175         vparam.period.tv_sec = 0; 
176         vparam.period.tv_nsec = time_period*1000*1000; 
177         WVPASS(fwp_vres_create(&vparam, (fwp_vres_t**)&vres) == 0);
178         WVFRSH(frsh_contract_negotiate(&contract, &vres));
179         WVFRSH(frsh_send_endpoint_bind(vres, sepoint));
180
181         count = 0;
182         while (count != num_msg) {
183                 count++;
184                 WVPASS(frsh_vres_get_budget_and_period (vres, &budget, &period) == 0);
185                 WVFRSH(frsh_network_budget_to_bytes(resource_id, &budget, &msg_size));
186                 WVPASSEQ(frsh_send_sync(sepoint, msg, msg_size), msg_size);
187         }
188
189         /* TODO: destroy vres and send enpoint */
190
191         printf("Test PASSED!\n");
192         return 0;
193 }
194