]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - tests/tests_unix_fna/test_unix_fna_send_receive.c
change to unix fna, now we encode streams... but i found a problem in dtm so it wont...
[frescor/fna.git] / tests / tests_unix_fna / test_unix_fna_send_receive.c
1 #include <stdio.h>
2 #include <stdlib.h> /* atoi */
3 #include <unistd.h> /* sleep */
4 #include <assert.h>
5 #include <string.h> /* memset */
6
7 #include "frsh.h" // for FRSH_CPU_ID_DEFAULT
8
9 #include "fna.h" // for fna_vres_id_t
10 #include "unix_fna.h" // for unix_fna_operations
11
12 #define THE_SENDER_ADDR         0
13 #define THE_RECEIVER_ADDR       1
14 #define THE_STREAM_ID           3
15 void sender(void);
16 void receiver(void);
17
18 int main ()
19 {
20         int err;
21
22         err = unix_fna_operations.fna_init(FRSH_NETWORK_ID_DEFAULT);
23         assert(err == 0);
24
25         printf("I am %d\n", FRSH_CPU_ID_DEFAULT);
26
27         switch(FRSH_CPU_ID_DEFAULT) {
28         case 0:
29                 sender();
30                 break;
31         case 1:
32                 receiver();
33                 break;
34         default:
35                 printf("wrong node number\n");
36                 return -1;
37         }
38
39         return 0;
40 }
41
42 void sender(void)
43 {
44         int err, i;
45         frsh_contract_t contract;
46         fna_endpoint_data_t endpoint;
47         char buffer[100];
48         size_t size;
49
50         printf("I am the sender\n");
51
52         err = unix_fna_operations.fna_contract_negotiate
53                         (FRSH_NETWORK_ID_DEFAULT,
54                          &contract,
55                          &endpoint.vres);
56         assert(err == 0);
57
58         endpoint.endpoint_type  = FRSH_SEND_ENDPOINT_TYPE;
59         endpoint.is_bound       = true;
60         endpoint.destination    = THE_RECEIVER_ADDR;
61         endpoint.resource_id    = FRSH_NETWORK_ID_DEFAULT;
62         endpoint.stream_id      = THE_STREAM_ID;
63
64         for(i=0; i<10; i++) {
65                 sleep(1);
66                 size = snprintf(buffer, sizeof(buffer), "hello world %d", i);
67
68                 err = unix_fna_operations.fna_send_async(&endpoint,
69                                                          buffer,
70                                                          size);
71                 assert (err == 0);
72
73                 printf("sender : sent %s\n", buffer);
74         }
75
76         printf("Sender done\n");
77 }
78
79 void receiver(void)
80 {
81         int err, i;
82         fna_endpoint_data_t endpoint;
83         frsh_network_address_t from;
84         char buffer[100];
85         size_t received_bytes;
86
87         printf("I am the receiver\n");
88
89         endpoint.endpoint_type  = FRSH_RECEIVE_ENDPOINT_TYPE;
90         endpoint.is_bound       = false;
91         endpoint.resource_id    = FRSH_NETWORK_ID_DEFAULT;
92         endpoint.stream_id      = THE_STREAM_ID;
93
94         err = unix_fna_operations.fna_receive_endpoint_created(&endpoint);
95         assert (err == 0);
96
97         for(i=0; i<10; i++) {
98                 err = unix_fna_operations.fna_receive_sync(&endpoint,
99                                                            buffer,
100                                                            100,
101                                                            &received_bytes,
102                                                            &from);
103                 assert (err == 0);
104
105                 buffer[received_bytes] = '\0';
106
107                 printf("receiver : received %s from %d\n", buffer, from);
108         }
109
110         printf("Receiver done\n");
111 }