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