MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / char / mxhwenp / test / metaserver.c
blob96b9e639ba318a10bf824764e5b15a9e4b7573bd
1 #include <stdio.h>
2 #include <string.h>
3 #include <time.h>
4 #ifndef WIN32
5 #include <sys/times.h>
6 #endif
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <stdarg.h>
10 #include "netp.h"
12 enum
14 TCPSERVER,
15 TCPCLIENT
18 unsigned int
19 lookup_ip(char *host)
21 struct hostent *he;
22 unsigned int ip;
24 ip = inet_addr(host);
25 if (ip==INADDR_NONE)
27 he=gethostbyname(host);
28 if (!he)
29 return 0;
30 memcpy(&ip, he->h_addr_list[0],he->h_length);
32 return ip;
35 int
36 bind_interface(int fd, unsigned int ip, int port)
38 struct sockaddr_in sin;
40 memset(&sin,0,sizeof(sin));
41 sin.sin_family = AF_INET;
42 sin.sin_addr.s_addr=ip;
43 sin.sin_port = htons((unsigned short) port);
44 if (bind(fd, (struct sockaddr *) &sin,sizeof(sin)) < 0)
45 return -1;
46 else
47 return 0;
50 int
51 set_nonblocking(int fd)
53 #ifdef WIN32
54 long val=1;
55 if (ioctlsocket (fd, FIONBIO, &val) != 0)
56 #else
57 if (fcntl (fd, F_SETFL, O_NONBLOCK) != 0)
58 #endif
59 return -1;
60 else
61 return 0;
64 int
65 tcp_socket(char *host, int port, int type)
67 struct sockaddr_in sin;
68 int sock;
70 memset(&sin,0,sizeof(sin));
71 sin.sin_family = AF_INET;
72 sin.sin_port = htons((unsigned short)port);
73 sin.sin_addr.s_addr = host? lookup_ip(host):htonl(INADDR_ANY);
75 sock = socket(AF_INET, SOCK_STREAM, 0);
76 if (sock > 0)
78 if (type==TCPSERVER)
79 bind(sock, (struct sockaddr*) &sin,sizeof(sin));
80 else
81 connect(sock, (struct sockaddr*) &sin,sizeof(sin));
83 return sock;
86 ssize_t
87 readn(int fd, void *vptr, size_t n)
89 size_t nleft=n;
90 ssize_t nread;
91 char *p = vptr;
93 while(nleft > 0)
95 if ((nread = read(fd, p, nleft)) < 0)
97 if (errno==EINTR)
98 nread=0;
99 else
100 return -1;
101 } else if (nread==0)
102 break;
103 nleft -= nread;
104 p += nread;
106 return n-nleft;
109 ssize_t
110 writen(int fd, void *vptr, size_t n)
112 size_t nleft=n;
113 ssize_t nread;
114 char *p = vptr;
116 while(nleft > 0)
118 if ((nread = write(fd, p, nleft)) < 0)
120 if (errno==EINTR)
121 nread=0;
122 else
123 return -1;
124 } else if (nread==0)
125 break;
126 nleft -= nread;
127 p += nread;
129 return n;
132 unsigned char Buf[2048];
134 void
135 send_cmd (int fd, unsigned int cmd, const char *fmt, ...)
137 va_list ap;
138 size_t l;
139 u_short s;
141 va_start (ap, fmt);
142 vsnprintf (Buf + 4, sizeof (Buf) - 4, fmt, ap);
143 va_end (ap);
145 l = strlen (Buf + 4);
146 s = (u_short) l;
148 memcpy(Buf, &s, 2);
149 s = (short) cmd;
150 memcpy(Buf+2, &s, 2);
152 writen (fd, Buf, 4 + l);
153 *(Buf+4+l)=0;
154 printf("SEND:[%d,%d] %s\n",cmd, l, Buf+4);
157 #define HANDLER(f) void f (int fd, unsigned int cmd, char *pkt, unsigned int len)
159 typedef struct
161 unsigned int cmd;
162 HANDLER ((*handler));
163 } HNDLR;
165 static int
166 split_line(char **av, char *pkt, int max)
168 int n=0;
170 while(*pkt && n < max)
172 while(*pkt==' ') pkt++;
173 av[n++] = pkt++;
174 while(*pkt==' ') pkt++;
176 return n;
179 /* <name> <runs> <len1> [<len2> ....] */
180 HANDLER(meta_cipher_process)
182 char *av[32];
183 int i,ac, numPkts;
184 double swT, hwT;
186 ac = split_line(av, pkt, 32);
187 if (ac < 3)
188 return;
190 numPkts = atoi(av[1]);
191 for (i=2; i < ac; i++)
193 if (test_meta_cipher_process(av[0], numPkts, atoi(av[i]), &swT, &hwT))
194 continue;
195 send_cmd(fd, cmd, "%s %.2f %.2f", av[1], swT, hwT);
199 HNDLR hndls[] =
201 {0, NULL},
202 {1, meta_cipher_process},
205 int HNDLR_SIZE = sizeof(hndls)/sizeof(hndls[0]);
207 static void
208 server_dispatch(int fd)
210 int n;
211 u_short cmd, len;
213 while(1)
215 n = readn(fd, Buf, 4);
216 if (n<=0)
217 return;
218 memcpy(&len, Buf, 2);
219 memcpy(&cmd, Buf+2, 2);
220 if (cmd==0)
221 break;
222 n = readn(fd, Buf, len);
223 if (n!=len)
224 return;
225 *(Buf+len)=0;;
226 printf("RECV:[%d,%d] %s\n",cmd, len, Buf);
228 for (n=0; n < HNDLR_SIZE; n++)
229 if (hndls[n].cmd == cmd)
231 hndls[n].handler(fd, cmd, Buf, len);
232 break;
237 int
238 meta_main()
240 struct sockaddr_in caddr;
241 socklen_t len;
242 int fd, sock;
244 #if defined(WIN32)
245 WSADATA wsa;
247 WSAStartup (MAKEWORD (1, 1), &wsa);
248 #endif /* !WIN32 */
249 sock = tcp_socket(NULL,SERVER_PORT,TCPSERVER);
250 listen(sock, 5);
251 printf("listen at port %d\n",SERVER_PORT);
252 for (;;)
254 len = sizeof(caddr);
255 fd = accept(sock,(struct sockaddr*) &caddr,&len);
256 if (fd > 0)
258 printf("accept a client at fd %d\n",fd);
259 server_dispatch(fd);
260 close(fd);
263 close(sock);
264 #if defined(WIN32) && !defined(__CYGWIN__)
265 WSACleanup ();
266 #endif
267 return 0;