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