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