]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_unix/unix_fna.c
unix fna, it seems to work
[frescor/fna.git] / src_unix / unix_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 //  ******** ****     **     **
55 //  **///// /**/**   /**    ****
56 //  **      /**//**  /**   **//**
57 //  ******* /** //** /**  **  //**
58 //  **////  /**  //**/** **********
59 //  **      /**   //****/**//////**
60 //  **      /**    //***/**     /**
61 //  /       //      /// //      //
62 //
63 // FNA(Frescor Network Adaptation layer), pronounced "efe ene a"
64 //==============================================================
65
66 /**
67  * unix fna implementation
68  *
69  * In the following functions we implement a DUMMY FNA implementation without
70  * contracts or real-time requirements just for testing purposes. We provide
71  * send/receive capabilities between Linux processes through UNIX domain
72  * datagram sockets.
73  *
74  * The goal is to run FRSH on several processes by using the Linux_lib arch
75  * of MaRTE OS or Partikle to simulate a distributed system in a single PC.
76  *
77  * The main tricks of the implementation are the following:
78  *
79  *   - In Unix sockets the address is just a path (a string) so there is no
80  *     such a concept of streams.
81  *   - when the user negotiates a contract it must provide, as protocol
82  *     dependent information, the destination so we can create and bind a
83  *     socket for it
84  *   - The created socket id is stored as the 'vres'
85  *   - On the receiving part, when the user creates the receive endpoint, it
86  *     must provide the receiving path as endpoint information so we can
87  *     create a socket to receive from that path.
88  *   - Both informations are passed as an integer for a static array called
89  *     "the_unix_socket_paths" where we configure statically the unix paths.
90  *     They are passed using the void * fields as if they where integers.
91  *
92  **/
93
94 #include <malloc.h>   /* for malloc and free */
95 #include <assert.h>
96 #include "unix_fna.h" /* function prototypes */
97
98 #include <sys/socket.h>
99 #include <sys/un.h> /* struct sockaddr_un */
100
101 #if 1
102 #include <stdio.h>
103 #define DEBUG(x,args...) printf("%s: " x, __func__ , ##args)
104 #else
105 #define DEBUG(x,args...)
106 #endif
107
108 //////////////////////////////////////////////////////////////////////
109 //           INITIALIZATION
110 //////////////////////////////////////////////////////////////////////
111
112 char *the_unix_socket_paths[MX_UNIX_SOCKET_PATHS] = {
113         "/tmp/path00",
114         "/tmp/path01",
115         "/tmp/path02",
116         "/tmp/path03",
117         "/tmp/path04",
118         "/tmp/path05"
119 };
120
121 /**
122  * unix_fna_init()
123  *
124  **/
125
126 int unix_fna_init(const frsh_resource_id_t resource_id)
127 {
128         DEBUG("NOT IMPLEMENTED\n");
129         return 0;
130 }
131
132 ///////////////////////////////////////////////////////////////////
133 //           VIRTUAL RESOURCES
134 ///////////////////////////////////////////////////////////////////
135
136 /**
137  * unix_fna_contract_negotiate()
138  *
139  **/
140
141 int unix_fna_contract_negotiate(const frsh_resource_id_t resource_id,
142                                 const frsh_contract_t *contract,
143                                 fna_vres_id_t *vres)
144 {
145         int sock, err;
146         struct sockaddr_un sender_addr;
147         int unix_path_index;
148
149         DEBUG("Creating and binding the unix socket\n");
150
151         sock = socket(AF_UNIX, SOCK_DGRAM, 0);
152         assert(sock >= 0);
153
154         unix_path_index = (int)contract->protocol_info.body;
155
156         memset(&sender_addr, 0, sizeof(sender_addr));
157         sender_addr.sun_family = AF_UNIX;
158         strcpy(sender_addr.sun_path, the_unix_socket_paths[unix_path_index]);
159
160         err = bind(sock, (struct sockaddr *)&sender_addr, sizeof(sender_addr));
161         assert(err >= 0);
162
163         *vres = (fna_vres_id_t)sock;
164
165         return 0;
166 }
167
168 /**
169  * unix_fna_contract_renegotiate_sync()
170  *
171  **/
172
173 int unix_fna_contract_renegotiate_sync(const frsh_resource_id_t resource_id,
174                                        const fna_vres_id_t vres,
175                                        const frsh_contract_t *new_contract)
176 {
177         DEBUG("NOT IMPLEMENTED\n");
178         return 0;
179 }
180
181 /**
182  * unix_fna_contract_renegotiate_async()
183  *
184  **/
185
186 int unix_fna_contract_renegotiate_async(const frsh_resource_id_t resource_id,
187                                         const fna_vres_id_t vres,
188                                         const frsh_contract_t *new_contract,
189                                         frsh_signal_t signal_to_notify,
190                                         frsh_signal_info_t signal_info)
191 {
192         DEBUG("NOT IMPLEMENTED\n");
193         return 0;
194 }
195
196 /**
197  * unix_fna_vres_get_renegotiation_status()
198  *
199  **/
200
201 int unix_fna_vres_get_renegotiation_status(const frsh_resource_id_t resource_id,
202                                            const fna_vres_id_t vres,
203                                            frsh_renegotiation_status_t *renegotiation_status)
204 {
205         DEBUG("NOT IMPLEMENTED\n");
206         return 0;
207 }
208
209 /**
210  * unix_fna_vres_destroy()
211  *
212  **/
213
214 int unix_fna_vres_destroy(const frsh_resource_id_t resource_id,
215                           const fna_vres_id_t vres)
216 {
217         DEBUG("NOT IMPLEMENTED\n");
218         return 0;
219 }
220
221 /**
222  * unix_fna_vres_get_contract()
223  *
224  **/
225
226 int unix_fna_vres_get_contract(const frsh_resource_id_t resource_id,
227                                const fna_vres_id_t vres,
228                                frsh_contract_t *contract)
229 {
230         DEBUG("NOT IMPLEMENTED\n");
231         return 0;
232 }
233
234 /**
235  * unix_fna_vres_get_usage()
236  *
237  **/
238
239 int unix_fna_vres_get_usage(const frsh_resource_id_t resource_id,
240                             const fna_vres_id_t vres,
241                             struct timespec *usage)
242 {
243         DEBUG("NOT IMPLEMENTED\n");
244         return 0;
245 }
246
247 /**
248  * unix_fna_vres_get_remaining_budget()
249  *
250  **/
251
252 int unix_fna_vres_get_remaining_budget(const frsh_resource_id_t resource_id,
253                                        const fna_vres_id_t vres,
254                                        struct timespec *remaining_budget)
255 {
256         DEBUG("NOT IMPLEMENTED\n");
257         return 0;
258 }
259
260 /**
261  * unix_fna_vres_get_budget_and_period()
262  *
263  **/
264
265 int unix_fna_vres_get_budget_and_period(const frsh_resource_id_t resource_id,
266                                         const fna_vres_id_t vres,
267                                         struct timespec *budget,
268                                         struct timespec *period)
269 {
270         DEBUG("NOT IMPLEMENTED\n");
271         return 0;
272 }
273
274 ///////////////////////////////////////////////////////////////////
275 //           SPARE CAPACITY FUNCIONS
276 ///////////////////////////////////////////////////////////////////
277
278 /**
279  * unix_fna_resource_get_capacity()
280  *
281  **/
282
283 int unix_fna_resource_get_capacity(const frsh_resource_id_t resource_id,
284                                    const int importance,
285                                    uint32_t *capacity)
286 {
287         DEBUG("NOT IMPLEMENTED\n");
288         return 0;
289 }
290
291 /**
292  * unix_fna_resource_get_total_weight()
293  *
294  **/
295
296 int unix_fna_resource_get_total_weight(const frsh_resource_id_t resource_id,
297                                        const int importance,
298                                        int *total_weight)
299 {
300         DEBUG("NOT IMPLEMENTED\n");
301         return 0;
302 }
303
304 /**
305  * unix_fna_vres_decrease_capacity()
306  *
307  **/
308
309 int unix_fna_vres_decrease_capacity(const frsh_resource_id_t resource_id,
310                                     const fna_vres_id_t vres,
311                                     const struct timespec new_budget,
312                                     const struct timespec new_period)
313 {
314         DEBUG("NOT IMPLEMENTED\n");
315         return 0;
316 }
317
318 ///////////////////////////////////////////////////////////////////
319 //           SEND RECEIVE OPERATIONS
320 ///////////////////////////////////////////////////////////////////
321
322 /**
323  * unix_fna_send_sync()
324  *
325  **/
326
327 int unix_fna_send_sync(const fna_endpoint_data_t *endpoint,
328                        const void *msg,
329                        const size_t size)
330 {
331         DEBUG("NOT IMPLEMENTED\n");
332         return 0;
333 }
334
335 /**
336  * unix_fna_send_async()
337  *
338  **/
339
340 int unix_fna_send_async(const fna_endpoint_data_t *endpoint,
341                         const void *msg,
342                         const size_t size)
343 {
344
345         struct sockaddr_un receiver_addr;
346         ssize_t sent_bytes;
347
348         DEBUG("send async\n");
349
350         assert(endpoint->is_bound);
351
352         memset(&receiver_addr, 0, sizeof(receiver_addr));
353         receiver_addr.sun_family = AF_UNIX;
354         strcpy(receiver_addr.sun_path,
355                the_unix_socket_paths[endpoint->destination]);
356
357         sent_bytes = sendto(endpoint->vres, /* the socket */
358                             msg,
359                             size,
360                             0,
361                             (struct sockaddr *) &receiver_addr,
362                             sizeof(receiver_addr));
363
364         assert(sent_bytes >= 0);
365
366         return 0;
367 }
368
369 /**
370  * unix_fna_receive_sync()
371  *
372  **/
373
374 int unix_fna_receive_sync(const fna_endpoint_data_t *endpoint,
375                           void *buffer,
376                           const size_t buffer_size,
377                           size_t *received_bytes,
378                           frsh_network_address_t *from)
379 {
380         struct sockaddr_un sender_addr;
381         ssize_t recv_bytes;
382         socklen_t from_len;
383
384         DEBUG("receive sync\n");
385
386         from_len = sizeof(sender_addr);
387         recv_bytes = recvfrom(endpoint->vres, /* the socket */
388                               buffer,
389                               buffer_size,
390                               0,
391                               (struct sockaddr *)&sender_addr,
392                               &from_len);
393
394         assert(recv_bytes >= 0);
395
396         *received_bytes = recv_bytes;
397         /* TODO: fill from */
398
399         return 0;
400 }
401
402 /**
403  * unix_fna_receive_async()
404  *
405  **/
406
407 int unix_fna_receive_async(const fna_endpoint_data_t *endpoint,
408                            void *buffer,
409                            const size_t buffer_size,
410                            size_t *received_bytes,
411                            frsh_network_address_t *from)
412 {
413         DEBUG("NOT IMPLEMENTED\n");
414         return 0;
415 }
416
417 /**
418  * unix_fna_send_endpoint_get_status()
419  *
420  **/
421
422 int unix_fna_send_endpoint_get_status(const fna_endpoint_data_t *endpoint,
423                                       int *number_of_pending_messages,
424                                       frsh_endpoint_network_status_t *network_status,
425                                       frsh_protocol_status_t *protocol_status)
426 {
427         DEBUG("NOT IMPLEMENTED\n");
428         return 0;
429 }
430
431 /**
432  * unix_fna_receive_endpoint_created()
433  *
434  **/
435 int unix_fna_receive_endpoint_created(fna_endpoint_data_t *endpoint)
436 {
437         int sock, err;
438         struct sockaddr_un receiver_addr;
439         int unix_path_index;
440
441         DEBUG("creating the socket to receive\n");
442
443         sock = socket(AF_UNIX, SOCK_DGRAM, 0);
444         assert(sock >= 0);
445
446         unix_path_index = (int)endpoint->protocol_info.body;
447
448         memset(&receiver_addr, 0, sizeof(receiver_addr));
449         receiver_addr.sun_family = AF_UNIX;
450         strcpy(receiver_addr.sun_path, the_unix_socket_paths[unix_path_index]);
451
452         err = bind(sock, (struct sockaddr *)&receiver_addr, sizeof(receiver_addr));
453         assert(err >= 0);
454
455         endpoint->vres = sock;
456
457         return 0;
458 }
459
460 /**
461  * unix_fna_receive_endpoint_get_pending_messages
462  *
463  **/
464
465 int unix_fna_receive_endpoint_get_status(const fna_endpoint_data_t *endpoint,
466                                          int *number_of_pending_messages,
467                                          frsh_endpoint_network_status_t *network_status,
468                                          frsh_protocol_status_t *protocol_status)
469 {
470         DEBUG("NOT IMPLEMENTED\n");
471         return 0;
472 }
473
474 //////////////////////////////////////////////////////////////////////
475 //           NETWORK CONFIGURATION FUNCTIONS
476 //////////////////////////////////////////////////////////////////////
477
478 /**
479  * unix_fna_network_get_max_message_size()
480  *
481  **/
482
483 int unix_fna_network_get_max_message_size(const frsh_resource_id_t resource_id,
484                                           const frsh_network_address_t destination,
485                                           size_t *max_size)
486 {
487         DEBUG("NOT IMPLEMENTED\n");
488         return 0;
489 }
490
491 /**
492  * unix_fna_network_bytes_to_budget()
493  *
494  **/
495
496 int unix_fna_network_bytes_to_budget(const frsh_resource_id_t resource_id,
497                                      const size_t nbytes,
498                                      struct timespec *budget)
499 {
500         DEBUG("NOT IMPLEMENTED\n");
501         return 0;
502 }
503
504 /**
505  * unix_fna_network_budget_to_bytes()
506  *
507  **/
508
509 int unix_fna_network_budget_to_bytes(const frsh_resource_id_t resource_id,
510                                      const struct timespec *budget,
511                                      size_t *nbytes)
512 {
513         DEBUG("NOT IMPLEMENTED\n");
514         return 0;
515 }
516
517 /**
518  * unix_fna_network_get_min_eff_budget()
519  *
520  **/
521
522 int unix_fna_network_get_min_eff_budget(const frsh_resource_id_t resource_id,
523                                         struct timespec *budget)
524 {
525         DEBUG("NOT IMPLEMENTED\n");
526         return 0;
527 }
528
529 // GLOBAL variable to install the network protocol in FRESCOR
530
531 fna_operations_t unix_fna_operations = {
532         .fna_init = unix_fna_init,
533         .fna_contract_negotiate = unix_fna_contract_negotiate,
534         .fna_contract_renegotiate_sync = unix_fna_contract_renegotiate_sync,
535         .fna_contract_renegotiate_async = unix_fna_contract_renegotiate_async,
536         .fna_vres_get_renegotiation_status = unix_fna_vres_get_renegotiation_status,
537         .fna_vres_destroy = unix_fna_vres_destroy,
538         .fna_vres_get_contract = unix_fna_vres_get_contract,
539         .fna_vres_get_usage = unix_fna_vres_get_usage,
540         .fna_vres_get_remaining_budget = unix_fna_vres_get_remaining_budget,
541         .fna_vres_get_budget_and_period = unix_fna_vres_get_budget_and_period,
542         .fna_resource_get_capacity = unix_fna_resource_get_capacity,
543         .fna_resource_get_total_weight = unix_fna_resource_get_total_weight,
544         .fna_vres_decrease_capacity = unix_fna_vres_decrease_capacity,
545         .fna_send_sync = unix_fna_send_sync,
546         .fna_send_async = unix_fna_send_async,
547         .fna_receive_sync = unix_fna_receive_sync,
548         .fna_receive_async = unix_fna_receive_async,
549         .fna_send_endpoint_get_status = unix_fna_send_endpoint_get_status,
550         .fna_receive_endpoint_created = unix_fna_receive_endpoint_created,
551         .fna_receive_endpoint_get_status = unix_fna_receive_endpoint_get_status,
552         .fna_network_get_max_message_size = unix_fna_network_get_max_message_size,
553         .fna_network_bytes_to_budget = unix_fna_network_bytes_to_budget,
554         .fna_network_budget_to_bytes = unix_fna_network_budget_to_bytes,
555         .fna_network_get_min_eff_budget = unix_fna_network_get_min_eff_budget
556 };