]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/lib/fwp/tests/fwp_prototest/fwp_sendrecv_test2.c
9f377dbe52fa113ba435e14011346d18c2d914fc
[frescor/fwp.git] / fwp / lib / fwp / tests / fwp_prototest / fwp_sendrecv_test2.c
1 /**
2  * \file fwp_sendrecv_test2.c  
3  *
4  * This a test application that:
5  * - creates vres without negotiation
6  * - creates reliable send endpoint
7  * - cretaes receiver thread and receive endpoint within it 
8  * - binds that endpoint to vres
9  * - sends two messages
10  * - receives messages
11  *
12  */
13 #define CONFIGURE_FWP_MNGT 0
14 #include "fwp_confdefs.h"
15 #include "fwp.h"
16
17 #include <errno.h>
18 #include <stdio.h>
19 #include <pthread.h>
20
21 #define MSGN 2
22
23 fwp_endpoint_attr_t  attr;
24
25 void* receiver(void* arg)
26 {
27         fwp_endpoint_d_t repoint_d1;
28         int i,len;
29         char buffer[30];
30         fwp_addr_t from;
31         
32         FWP_DEBUG("Creating receive endpoint\n");
33         if (fwp_receive_endpoint_create(7777, &attr,&repoint_d1) < 0){
34                 perror("Error while creating receive endpoint\n");
35                 return NULL;
36         }
37                 
38         FWP_DEBUG("Receive endpoint created \n");
39         for (i = 0; i < MSGN; i++) {
40                 if ((len = fwp_recv(repoint_d1, buffer, sizeof(buffer), &from,
41                          0)) < 0) {
42                         perror("Error while receiving data::");
43                         return NULL;
44                 } else {
45                         printf("Received %s\n",buffer);
46                         //for (j = 0 ; j < 10; i++)
47                         //      printf("%c", buffer[i]);
48
49                 }
50                 printf("END\n");
51         }
52
53         return NULL;
54 }
55
56 int main()
57 {
58         fwp_vres_d_t vres_d1;
59         struct fwp_vres_params vparam1, vparam2;
60         char msg1[] = "Hello1";
61         char msg2[] = "Hello2";
62         fwp_endpoint_d_t sepoint_d1;
63         pthread_t thread;
64         
65         vparam1.ac_id = FWP_AC_VO; 
66         vparam1.budget = 100;
67         vparam1.period.tv_sec = 0; 
68         vparam1.period.tv_nsec = 10000; 
69
70         vparam2.ac_id = FWP_AC_BK; 
71         vparam2.budget = 100;
72         vparam2.period.tv_sec = 0;
73         vparam2.period.tv_nsec = 100000;
74
75         printf("Start\n");
76         if (fwp_init() != 0) {
77                 printf("FWP initialization failed!\n");
78                 return -1;
79         }
80         
81         fwp_endpoint_attr_init(&attr);
82         fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_RELIABLE);
83         
84         pthread_create(&thread, NULL, &receiver, (void*) NULL);
85         printf("Create vres1, vres2\n");
86         if (fwp_vres_create(&vparam1, &vres_d1) < 0) {
87                 printf("Unable to create vres1\n");
88                 return -1;
89         }
90         printf("Vres1 created \n");
91         
92         if (fwp_send_endpoint_create(inet_addr("127.0.0.1"), 7777, &attr, 
93                                         &sepoint_d1) < 0) {
94                 return -1;
95         }
96         printf("Send endpoint 1 created\n");
97         fwp_send_endpoint_bind(sepoint_d1, vres_d1);
98         
99         fwp_send(sepoint_d1, msg1, sizeof(msg1), 0);
100         FWP_DEBUG("Sent msg1\n");
101         fwp_send(sepoint_d1, msg2, sizeof(msg2), 0);
102         FWP_DEBUG("Sent msg2\n");
103         
104         pthread_join(thread, (void**) NULL);    
105         /*if (fwp_vres_destroy(vres_d1) < 0) {
106                 perror("Unable to destroy vres1\n");
107                 return -1;
108         }
109         printf("Vres1 destroyed\n");
110         
111         if (fwp_vres_destroy(vres_d2) < 0){
112                 perror("Unable to destroy vres2\n");
113                 return -1;
114         }       
115         printf("Vres2 destroyed\n");
116         */
117
118         printf("Test PASSED!\n");
119         scanf("Press key");     
120         
121         return 0;
122 }