]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - wme_test/wserver.c
Task runs with RT priority, added run script, common code added to common.c.
[frescor/fwp.git] / wme_test / wserver.c
1 #include <stdlib.h>
2 #include <errno.h>
3
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7
8 #include <signal.h>
9 #include <sys/wait.h>
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <time.h>
14 #include <string.h>
15 #include <pthread.h>
16 #include "common.h"
17
18 #define BUFFSIZE        65536
19
20 int ac_sockfd[AC_QUEUES];
21
22
23 void stopper()
24 {
25         int i;
26
27         for (i = 0 ; i < AC_QUEUES; i++) 
28                 close(ac_sockfd[AC_QUEUES]);
29
30         exit(0);
31 }
32
33 int create_ac_socket(unsigned int ac) 
34 {
35         struct sockaddr_in my_addr;
36         int sockfd;
37         unsigned int yes=1, tos;
38
39
40         if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
41         {
42                 perror("Socket nelze otevrit");
43                 return -1;
44         }
45         
46         if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
47                 perror("Chyba v nastaveni soketu");
48                 return -1;
49         }
50
51    //   bzero(&my_addr, sizeof(my_addr));
52         memset(&my_addr,0, sizeof(my_addr));
53         my_addr.sin_family = AF_INET;
54         my_addr.sin_addr.s_addr = INADDR_ANY;
55         my_addr.sin_port = htons(BASE_PORT + ac);
56         
57         if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr)) == -1) {
58                 perror("Chyba v bind");
59                 close(sockfd);
60                 return -1;
61         }
62         
63         //tos = ((AC_QUEUES - ac) *2 - 1)*32;
64         tos = ac_to_tos[ac];
65         if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
66                 perror("Unable to set TOS");
67                 close(sockfd);
68                 return -1;
69         }
70
71         return sockfd;
72 }
73
74 void* qhandler(void* queue)
75 {
76         char    buff[BUFFSIZE];
77         struct  sockaddr_in rem_addr;
78         int     mlen;
79         unsigned int ac, rem_addr_length; 
80         
81         ac = (int) queue;
82         printf("AC= %d\n",ac);
83         rem_addr_length=sizeof(rem_addr);
84
85         block_signals();
86         set_rt_prio(90-ac);
87
88         while (1) {
89                 while ((mlen = recvfrom(ac_sockfd[ac], buff, sizeof(buff) , 0, \
90                         (struct sockaddr*)&rem_addr, &rem_addr_length)) < 0) {
91                             if (errno == EINTR) continue;
92                             perror("Chyba pri prijimani pozadavku");
93                             return NULL;
94                 }
95
96 #ifdef DEBUG
97                 printf("%d",ac);
98                 fflush(stdout);
99 #endif  
100                 while (sendto(ac_sockfd[ac], buff, mlen,0 ,(struct sockaddr*)&rem_addr, \
101                         sizeof(rem_addr)) < 0){
102                             if (errno == EINTR) continue;
103                             perror("Chyba pri zapisu");
104                             return NULL;
105                 }
106         }
107 }
108
109
110 int main(int argc, char *argv[])
111 {
112         int i,rc;
113         pthread_attr_t attr;
114         pthread_t thread;
115
116         pthread_attr_init(&attr);
117
118         if (signal(SIGTERM, stopper) == SIG_ERR) {
119                 perror("Signal handler registration error");
120                 exit(1);
121         }
122                 
123         if (signal(SIGINT, stopper) == SIG_ERR) {
124                 perror("Signal handler registration error");
125                 exit(1);
126         }
127
128         for (i = 0; i < AC_QUEUES; i++) {
129                 ac_sockfd[i] = create_ac_socket(i);
130                 rc = pthread_create(&thread, &attr, qhandler, (void*) i); 
131
132         }
133         
134         while (1) {
135                 sleep(100000);
136         }
137
138         return 0;
139
140 }