]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/lib/fwp/tests/fwp_vrestest/fwp_vrestest2.c
22c5838f5a1d234c70a54b584bbe0e9ce793ba98
[frescor/fwp.git] / fwp / lib / fwp / tests / fwp_vrestest / fwp_vrestest2.c
1 /**
2  * \file fwp_vrestest2.c  
3  *
4  * This a test application that:
5  * - creates vres without negotiation
6  * - creates send and receive endpoint 
7  * - binds that endpoint to vres
8  * - in cycle (NUM loops) sends messages in separate thread and 
9  *   prints send time
10  * - receives messages in separate receiver thread
11  * - destroys vres
12  *
13  */
14 #define  CONFIGURE_FWP_MNGT 0
15 #include "fwp_confdefs.h"
16 #include "fwp.h"
17
18 #include <errno.h>
19 #include <stdio.h>
20 #include <pthread.h>
21
22 #define  NUM  20
23 #define  PORT 65111
24
25 int exit_flag = 0;
26 fwp_endpoint_attr_t attr;
27         
28 void* sender()
29 {
30         fwp_endpoint_d_t sepoint_d1;
31         fwp_vres_d_t vres_d1;
32         struct fwp_vres_params vparam1;
33         char msg1[10];
34         int count;
35         /*struct timespec  sendtime;*/
36         
37         vparam1.ac_id = FWP_AC_VO; 
38         vparam1.budget = 3;
39         vparam1.period.tv_sec = 5; 
40         vparam1.period.tv_nsec = 0; 
41         
42         printf("Create vres1\n");
43         if (fwp_vres_create(&vparam1, &vres_d1) < 0) {
44                 printf("Unable to open vres1\n");
45                 return NULL;
46         }
47         printf("Vres1 created\n");
48         
49         if (fwp_send_endpoint_create(inet_addr("127.0.0.1"), PORT, &attr, 
50                 &sepoint_d1) < 0){
51                 return NULL;
52         }
53         printf("Send endpoint 1 created\n");
54         fwp_send_endpoint_bind(sepoint_d1, vres_d1);
55         
56         sleep(2);
57         //for (count = 0; count < NUM; count++) {       
58         fwp_set_rt_prio(90);
59         
60         count = 0;
61         while (count < NUM){
62                 count++;
63                 sprintf(msg1,"msg%d sent\n",count);
64                 fwp_send(sepoint_d1, msg1, sizeof(msg1), 0);
65         
66                 printf(msg1);   
67                 /*clock_gettime(CLOCK_MONOTONIC, &sendtime);
68                 FWP_DEBUG("Sent %d: sec = %ld nsec = %ld \n", count,
69                                 sendtime.tv_sec, sendtime.tv_nsec);
70                 usleep(1000);*/
71         }
72         
73         while (!(exit_flag)) {
74                 sleep(1);
75         }
76         
77         if (fwp_vres_destroy(vres_d1) < 0) {
78                 perror("Unable to destroy vres1\n");
79                 return NULL;
80         }
81         printf("Vres1 destroyed\n");
82
83         return NULL;
84
85
86 void* receiver()
87 {
88         ssize_t len;
89         char buffer[30];
90         fwp_endpoint_d_t repoint_d;
91         int count;
92         struct timespec recvtime;
93         fwp_addr_t      from;
94         
95         /* local_addr should be handled when creating socket */
96         if (fwp_receive_endpoint_create(PORT, &attr, &repoint_d) < 0){
97                 perror("Not initialized\n");
98                 return NULL;
99         }
100         printf("Receive endpoint created\n");
101         
102         for (count = 1; count <= NUM; count++) {        
103                 
104                 if ((len = fwp_recv(repoint_d, buffer, sizeof(buffer), &from, 0)) < 0){
105                         perror("Error while receiving data");
106                         return NULL;
107                 } 
108                         else printf("Received - %s\n", buffer);
109                 
110                 clock_gettime(CLOCK_MONOTONIC, &recvtime);
111                 FWP_DEBUG("Received %d: sec = %ld nsec = %ld \n", count, 
112                                 recvtime.tv_sec, recvtime.tv_nsec);
113         }
114
115         exit_flag = 1;
116
117         return NULL;
118 }
119
120 int main()
121 {
122 //      struct sockaddr_in local_addr, rem_addr, from;
123         pthread_attr_t  thattr;
124         pthread_t       thread;
125
126         printf("Start\n");
127         fwp_init();
128         
129         fwp_endpoint_attr_init(&attr);
130         pthread_attr_init(&thattr);
131         pthread_create(&thread, &thattr, sender, NULL); 
132         pthread_create(&thread, &thattr, receiver, NULL); 
133         pthread_join(thread, (void**) NULL);    
134
135         printf("Test PASSED!\n");
136         return 0;
137 }