]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - tests/tests_rtep_fna/test_c_rtep_fna.c
3f5daecf6f42328cdf509eb8d0bdf5c21ed61228
[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.h" // for frsh_contract_set_xxx, FRSH_NETWORK_ID_DEFAULT, frsh_network_address_t, frsh_stream_id_t
74
75 #include "fna.h" // for fna_vres_id_t
76 #include "frsh_fna.h" // for frsh_rtep_*
77 #include "rtep_fna.h" // for rtep_fna_operations
78
79 #include "rtep.h" // for rtep_station_id_t, rtep_channel_t
80
81 int main ()
82 {
83     int err;
84     rtep_station_id_t me, normal_station, multicast_station;
85     frsh_network_address_t frsh_address;
86     char multicast_name[] = "broadcast";
87     size_t max_size, max_multicast_size, budget_bytes, nbytes;
88     struct timespec budget_timespec, period_max;
89     frsh_contract_t frsh_contract;
90     fna_vres_id_t vres;
91     fna_endpoint_data_t endpoint;
92     const int MSG_BUFFER_MX = 10;
93     char msg_buffer[MSG_BUFFER_MX];
94     int msg_size;
95     int msg_counter;
96
97     printf("--------------------------------------------------\n");
98     printf("1.- fna_init\n");
99     printf("--------------------------------------------------\n");
100     err = rtep_fna_operations.fna_init(FRSH_NETWORK_ID_DEFAULT);
101     assert(err == 0);
102
103     printf("--------------------------------------------------\n");
104     printf("2.- fna_network_get_max_message_size\n");
105     printf("--------------------------------------------------\n");
106    // get broadcast address for FRESCOR
107     multicast_station = rtep_get_station_id_by_name
108             ((uint8_t *)multicast_name, sizeof(multicast_name)-1);
109
110     err = frsh_rtep_map_network_address
111             (FRSH_RESOURCE_ID_DEFAULT, multicast_station, &frsh_address);
112     assert (err == 0);
113
114    // maximum size per message for a broadcast address
115     err=rtep_fna_operations.fna_network_get_max_message_size
116             (FRSH_RESOURCE_ID_DEFAULT, frsh_address, &max_multicast_size);
117     printf("Max multicast message size: %d\n", max_multicast_size);
118     assert (err == 0);
119     assert (max_multicast_size == MULTICAST_MTU);
120
121    // now the same with a normal address (i.e: 2)
122     normal_station = 2;
123     err=frsh_rtep_map_network_address
124             (FRSH_RESOURCE_ID_DEFAULT, normal_station, &frsh_address);
125     assert (err == 0);
126
127    // maximum size per message for a normal address
128     err=rtep_fna_operations.fna_network_get_max_message_size
129             (FRSH_RESOURCE_ID_DEFAULT, frsh_address, &max_size);
130     printf("Max message size: %d\n", max_size);
131     assert (err == 0);
132     assert (max_size == MAX_RTEP_MTU);
133
134     printf("--------------------------------------------------\n");
135     printf("3.- fna_network_budget_to_bytes\n");
136     printf("--------------------------------------------------\n");
137     nbytes = 1700;
138     err=rtep_fna_operations.fna_network_bytes_to_budget
139             (FRSH_RESOURCE_ID_DEFAULT, nbytes, &budget_timespec);
140     assert (err == 0);
141
142     err=rtep_fna_operations.fna_network_budget_to_bytes
143             (FRSH_RESOURCE_ID_DEFAULT, &budget_timespec, &budget_bytes);
144     assert (err == 0);
145     printf("%d user bytes -> %d budget bytes\n", nbytes, budget_bytes);
146
147     printf("--------------------------------------------------\n");
148     printf("4.- fna_network_get_min_effective_budget\n");
149     printf("--------------------------------------------------\n");
150     err=rtep_fna_operations.fna_network_get_min_eff_budget
151             (FRSH_RESOURCE_ID_DEFAULT, &budget_timespec);
152     assert (err == 0);
153
154     err=rtep_fna_operations.fna_network_budget_to_bytes
155             (FRSH_RESOURCE_ID_DEFAULT, &budget_timespec, &budget_bytes);
156     assert (err == 0);
157     printf("Minimum effective budget: %d\n", budget_bytes);
158
159     // send or receive
160     me = rtep_get_station_id();
161
162     if (me == 1) {
163
164         printf("--------------------------------------------------\n");
165         printf("5.- fna_contract_negotiate\n");
166         printf("--------------------------------------------------\n");
167
168         period_max.tv_sec = 3;
169         period_max.tv_nsec = 0;
170
171         // Create the contract
172         frsh_contract.workload = FRSH_WT_INDETERMINATE;
173         frsh_contract.granularity = FRSH_DEFAULT_GRANULARITY;
174         frsh_contract.weight = FRSH_DEFAULT_WEIGHT;
175         frsh_contract.importance = FRSH_DEFAULT_IMPORTANCE;
176         frsh_contract.resource_id = FRSH_RESOURCE_ID_DEFAULT;
177         frsh_contract.resource_type = FRSH_RT_NETWORK;
178
179         frsh_contract.budget_min = budget_timespec;
180         frsh_contract.period_max = period_max;
181         frsh_contract.d_equals_t = true;
182         frsh_contract.preemption_level = 4;
183
184         // Negotiate the contract
185         err = rtep_fna_operations.fna_contract_negotiate
186                 (FRSH_RESOURCE_ID_DEFAULT, &frsh_contract, &vres);
187         assert (err == 0);
188         printf("Contract accepted\n");
189
190         // Bind the vres to an endpoint
191         endpoint.endpoint_type = FRSH_SEND_ENDPOINT_TYPE;
192         endpoint.vres = vres;
193         endpoint.is_bound = true;
194         endpoint.destination = 2; // only for send_endpoints
195         endpoint.resource_id = FRSH_RESOURCE_ID_DEFAULT;
196         endpoint.stream_id = 7;
197
198         msg_counter = 0;
199
200         while(1) {
201             sleep(4);
202             printf("sending message\n");
203
204             msg_counter = msg_counter + 1;
205             msg_size = snprintf(msg_buffer, MSG_BUFFER_MX-1, "MSG %d", msg_counter);
206
207             err = rtep_fna_operations.fna_send_async
208                    (&endpoint, msg_buffer, msg_size);
209             assert (err == 0);
210         }
211     } else {
212         size_t received_bytes;
213         frsh_network_address_t from;
214
215         endpoint.endpoint_type = FRSH_RECEIVE_ENDPOINT_TYPE;
216         endpoint.is_bound = false;
217         endpoint.resource_id = FRSH_RESOURCE_ID_DEFAULT;
218         endpoint.stream_id = 7;
219
220         while(1) {
221             printf("blocking to receive\n");
222             err = rtep_fna_operations.fna_receive_sync
223                     (&endpoint, msg_buffer, MSG_BUFFER_MX, &received_bytes, &from);
224             assert (err == 0);
225
226             msg_buffer[received_bytes] = '\0';
227
228             printf("msg received: %s\n", msg_buffer);
229         }
230     }
231
232     return 0;
233 }