]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - tests/tests_rtep_fna/test_c_rtep_fna.c
preparing to do unix fna
[frescor/fna.git] / tests / tests_rtep_fna / test_c_rtep_fna.c
1 //----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 by the FRESCOR consortium:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politecnica  Valencia,           SPAIN
9 //    Czech Technical University in Prague,  CZECH REPUBLIC
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org
17 //
18 //        The FRESCOR project (FP6/2005/IST/5-034026) is funded
19 //        in part by the European Union Sixth Framework Programme
20 //        The European Union is not liable of any use that may be
21 //        made of this code.
22 //
23 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 // This file is part of FNA (Frescor Network Adaptation)
32 //
33 // FNA is free software; you can redistribute it and/or modify it
34 // under terms of the GNU General Public License as published by the
35 // Free Software Foundation; either version 2, or (at your option) any
36 // later version.  FNA is distributed in the hope that it will be
37 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
38 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 // General Public License for more details. You should have received a
40 // copy of the GNU General Public License along with FNA; see file
41 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
42 // Cambridge, MA 02139, USA.
43 //
44 // As a special exception, including FNA header files in a file,
45 // instantiating FNA generics or templates, or linking other files
46 // with FNA objects to produce an executable application, does not
47 // by itself cause the resulting executable application to be covered
48 // by the GNU General Public License. This exception does not
49 // however invalidate any other reasons why the executable file might be
50 // covered by the GNU Public License.
51 // -----------------------------------------------------------------------
52
53 /*
54  * test_c_rtep_fna.c
55  *
56  * Goal:
57  *
58  * The goal of this program is to test the RTEP implementation of the functions
59  * defined at fna.h without passing through the FRSH interface.
60  *
61  * Algorithm:
62  *
63  * We have two nodes, a sender and a receiver. The sender negotiates a contract,
64  * send info to the receiver, then renegotiates the contract and finally it
65  * cancels the contract.
66  *
67  */
68
69 #include <assert.h> // for assert
70 #include <stdio.h> // for printf
71 #include <time.h> // for timespec
72
73 #include "frsh_core.h" // for frsh_contract_set_xxx
74 #include "frsh_core_types.h" // for FRSH_NETWORK_ID_DEFAULT
75 #include "frsh_distributed_types.h" // for frsh_network_address_t, frsh_stream_id_t
76
77 #include "fna.h" // for fna_vres_id_t
78 #include "frsh_fna.h" // for frsh_rtep_*
79 #include "rtep_fna.h" // for rtep_fna_operations
80
81 #include "rtep.h" // for rtep_station_id_t, rtep_channel_t
82
83 int main ()
84 {
85     int err;
86     rtep_station_id_t me, normal_station, multicast_station;
87     frsh_network_address_t frsh_address;
88     char multicast_name[] = "broadcast";
89     size_t max_size, max_multicast_size, budget_bytes, nbytes;
90     struct timespec budget_timespec, period_max;
91     frsh_contract_t frsh_contract;
92     fna_vres_id_t vres;
93     fna_endpoint_data_t endpoint;
94     const int MSG_BUFFER_MX = 10;
95     char msg_buffer[MSG_BUFFER_MX];
96     int msg_size;
97     int msg_counter;
98
99     printf("--------------------------------------------------\n");
100     printf("1.- fna_init\n");
101     printf("--------------------------------------------------\n");
102     err = rtep_fna_operations.fna_init(FRSH_NETWORK_ID_DEFAULT);
103     assert(err == 0);
104
105     printf("--------------------------------------------------\n");
106     printf("2.- fna_network_get_max_message_size\n");
107     printf("--------------------------------------------------\n");
108    // get broadcast address for FRESCOR
109     multicast_station = rtep_get_station_id_by_name
110             ((uint8_t *)multicast_name, sizeof(multicast_name)-1);
111
112     err = frsh_rtep_map_network_address
113             (FRSH_RESOURCE_ID_DEFAULT, multicast_station, &frsh_address);
114     assert (err == 0);
115
116    // maximum size per message for a broadcast address
117     err=rtep_fna_operations.fna_network_get_max_message_size
118             (FRSH_RESOURCE_ID_DEFAULT, frsh_address, &max_multicast_size);
119     printf("Max multicast message size: %d\n", max_multicast_size);
120     assert (err == 0);
121     assert (max_multicast_size == MULTICAST_MTU);
122
123    // now the same with a normal address (i.e: 2)
124     normal_station = 2;
125     err=frsh_rtep_map_network_address
126             (FRSH_RESOURCE_ID_DEFAULT, normal_station, &frsh_address);
127     assert (err == 0);
128
129    // maximum size per message for a normal address
130     err=rtep_fna_operations.fna_network_get_max_message_size
131             (FRSH_RESOURCE_ID_DEFAULT, frsh_address, &max_size);
132     printf("Max message size: %d\n", max_size);
133     assert (err == 0);
134     assert (max_size == MAX_RTEP_MTU);
135
136     printf("--------------------------------------------------\n");
137     printf("3.- fna_network_budget_to_bytes\n");
138     printf("--------------------------------------------------\n");
139     nbytes = 1700;
140     err=rtep_fna_operations.fna_network_bytes_to_budget
141             (FRSH_RESOURCE_ID_DEFAULT, nbytes, &budget_timespec);
142     assert (err == 0);
143
144     err=rtep_fna_operations.fna_network_budget_to_bytes
145             (FRSH_RESOURCE_ID_DEFAULT, &budget_timespec, &budget_bytes);
146     assert (err == 0);
147     printf("%d user bytes -> %d budget bytes\n", nbytes, budget_bytes);
148
149     printf("--------------------------------------------------\n");
150     printf("4.- fna_network_get_min_effective_budget\n");
151     printf("--------------------------------------------------\n");
152     err=rtep_fna_operations.fna_network_get_min_eff_budget
153             (FRSH_RESOURCE_ID_DEFAULT, &budget_timespec);
154     assert (err == 0);
155
156     err=rtep_fna_operations.fna_network_budget_to_bytes
157             (FRSH_RESOURCE_ID_DEFAULT, &budget_timespec, &budget_bytes);
158     assert (err == 0);
159     printf("Minimum effective budget: %d\n", budget_bytes);
160
161     // send or receive
162     me = rtep_get_station_id();
163
164     if (me == 1) {
165
166         printf("--------------------------------------------------\n");
167         printf("5.- fna_contract_negotiate\n");
168         printf("--------------------------------------------------\n");
169
170         period_max.tv_sec = 3;
171         period_max.tv_nsec = 0;
172
173         // Create the contract
174         frsh_contract.workload = FRSH_WT_INDETERMINATE;
175         frsh_contract.granularity = FRSH_DEFAULT_GRANULARITY;
176         frsh_contract.weight = FRSH_DEFAULT_WEIGHT;
177         frsh_contract.importance = FRSH_DEFAULT_IMPORTANCE;
178         frsh_contract.resource_id = FRSH_RESOURCE_ID_DEFAULT;
179         frsh_contract.resource_type = FRSH_RT_NETWORK;
180
181         frsh_contract.budget_min = budget_timespec;
182         frsh_contract.period_max = period_max;
183         frsh_contract.d_equals_t = true;
184         frsh_contract.preemption_level = 4;
185
186         // Negotiate the contract
187         err = rtep_fna_operations.fna_contract_negotiate
188                 (FRSH_RESOURCE_ID_DEFAULT, &frsh_contract, &vres);
189         assert (err == 0);
190         printf("Contract accepted\n");
191
192         // Bind the vres to an endpoint
193         endpoint.endpoint_type = FRSH_SEND_ENDPOINT_TYPE;
194         endpoint.vres = vres;
195         endpoint.is_bound = true;
196         endpoint.destination = 2; // only for send_endpoints
197         endpoint.resource_id = FRSH_RESOURCE_ID_DEFAULT;
198         endpoint.stream_id = 7;
199
200         msg_counter = 0;
201
202         while(1) {
203             sleep(4);
204             printf("sending message\n");
205
206             msg_counter = msg_counter + 1;
207             msg_size = snprintf(msg_buffer, MSG_BUFFER_MX-1, "MSG %d", msg_counter);
208
209             err = rtep_fna_operations.fna_send_async
210                    (&endpoint, msg_buffer, msg_size);
211             assert (err == 0);
212         }
213     } else {
214         size_t received_bytes;
215         frsh_network_address_t from;
216
217         endpoint.endpoint_type = FRSH_RECEIVE_ENDPOINT_TYPE;
218         endpoint.is_bound = false;
219         endpoint.resource_id = FRSH_RESOURCE_ID_DEFAULT;
220         endpoint.stream_id = 7;
221
222         while(1) {
223             printf("blocking to receive\n");
224             err = rtep_fna_operations.fna_receive_sync
225                     (&endpoint, msg_buffer, MSG_BUFFER_MX, &received_bytes, &from);
226             assert (err == 0);
227
228             msg_buffer[received_bytes] = '\0';
229
230             printf("msg received: %s\n", msg_buffer);
231         }
232     }
233
234     return 0;
235 }