Mon, 19 May 2014 17:49:04 +0200
Server communication works
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | /***************************************************************************** |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | * Copyright (C) 2008-2014 |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | * Michiel Broek <mbroek at mbse dot eu> |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | * This file is part of the mbsePi-apps |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | * This is free software; you can redistribute it and/or modify it |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | * under the terms of the GNU General Public License as published by the |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | * Free Software Foundation; either version 2, or (at your option) any |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | * later version. |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | * mbsePi-apps is distributed in the hope that it will be useful, but |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | * General Public License for more details. |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | * You should have received a copy of the GNU General Public License |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | * along with EC-65K; see the file COPYING. If not, write to the Free |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
21 | *****************************************************************************/ |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
22 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
23 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
24 | #include "../lib/mbselib.h" |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | #include "server.h" |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | extern bool my_shutdown; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | extern bool debug; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | extern int lcdHandle; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
31 | extern unsigned char lcdbuf[MAX_LCDS][20][4]; |
43 | 32 | extern sys_config Config; |
33 | extern int clients; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | |
43 | 35 | int s; /* connected socket */ |
36 | int ls; /* listen socket */ | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | |
43 | 38 | struct sockaddr_in myaddr_in; /* for local socket address */ |
39 | struct sockaddr_in peeraddr_in; /* for peer socket address */ | |
40 | ||
41 | struct hostent *hp; | |
42 | ||
43 | #define SS_BUFSIZE 1024 | |
44 | #define SS_TIMEOUT 300 | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
46 | |
43 | 47 | /* |
48 | * Send message to client | |
49 | */ | |
50 | int srv_send(const char *format, ...) | |
51 | { | |
52 | char out[SS_BUFSIZE]; | |
53 | va_list va_ptr; | |
54 | ||
55 | if (s == -1) | |
56 | return -1; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | |
43 | 58 | va_start(va_ptr, format); |
59 | vsnprintf(out, SS_BUFSIZE-1, format, va_ptr); | |
60 | va_end(va_ptr); | |
61 | ||
44 | 62 | syslog(LOG_NOTICE, "send: \"%s\"", out); |
63 | ||
43 | 64 | if (send(s, out, strlen(out), 0) != strlen(out)) { |
65 | syslog(LOG_NOTICE, "srv_send failed"); | |
66 | return -1; | |
67 | } | |
68 | ||
69 | if (send(s, (char *)"\r\n", 2, 0) != 2) { | |
70 | syslog(LOG_NOTICE, "srv_send failed"); | |
71 | return -1; | |
72 | } | |
73 | ||
74 | return 0; | |
75 | } | |
76 | ||
77 | ||
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
78 | |
43 | 79 | void cmd_die(int onsig) |
80 | { | |
81 | syslog(LOG_NOTICE, "Server process die on signal %d", onsig); | |
82 | close(s); | |
83 | exit(0); | |
84 | } | |
85 | ||
86 | ||
87 | ||
88 | void cmd_server(void) | |
89 | { | |
44 | 90 | char *hostname, buf[SS_BUFSIZE], obuf[SS_BUFSIZE]; |
43 | 91 | int i, rc, rlen, timer; |
92 | socklen_t fromlen; | |
93 | struct pollfd pfd[1]; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
94 | |
43 | 95 | /* |
96 | * Close listen socket | |
97 | */ | |
98 | close(ls); | |
99 | /* | |
100 | * Install private signal handler. | |
101 | */ | |
102 | for (i = 0; i < NSIG; i++) { | |
103 | if ((i == SIGHUP) || (i == SIGPIPE) || (i == SIGBUS) || (i == SIGILL) || (i == SIGSEGV)) | |
104 | signal(i, (void (*))cmd_die); | |
105 | else | |
106 | signal(i, SIG_IGN); | |
107 | } | |
108 | ||
109 | hp = gethostbyaddr ((char *) &peeraddr_in.sin_addr, sizeof(struct in_addr), peeraddr_in.sin_family); | |
110 | if (hp == NULL) { | |
111 | hostname = inet_ntoa(peeraddr_in.sin_addr); | |
112 | } else { | |
113 | hostname = hp->h_name; | |
114 | } | |
115 | ||
116 | clients++; | |
117 | syslog(LOG_NOTICE, "Start new client connection (%d) from %s port %u", clients, hostname, ntohs(peeraddr_in.sin_port)); | |
44 | 118 | if (debug) |
119 | fprintf(stdout, "Start new client connection (%d) from %s port %u\n", clients, hostname, ntohs(peeraddr_in.sin_port)); | |
43 | 120 | timer = SS_TIMEOUT * 4; |
121 | ||
122 | /* | |
123 | * Receive loop | |
124 | */ | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
125 | for (;;) { |
43 | 126 | /* |
127 | * Poll socket until a define timeout of 0,25 second. | |
128 | */ | |
129 | pfd[0].fd = s; | |
130 | pfd[0].events = POLLIN; | |
131 | pfd[0].revents = 0; | |
132 | rc = poll(pfd, 1, 250); | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
133 | |
43 | 134 | if (rc == -1) { |
135 | /* | |
136 | * Poll can be interrupted by a finished child so that's not a real error. | |
137 | */ | |
138 | if (errno != EINTR) { | |
139 | syslog(LOG_NOTICE, "poll() rc=%d sock=%d events=%04x", rc, s, pfd[0].revents); | |
140 | } | |
141 | } else if (rc) { | |
142 | if (pfd[0].revents & POLLIN) { | |
143 | memset((char *)&buf, 0, SS_BUFSIZE); | |
144 | fromlen = sizeof(peeraddr_in); | |
145 | rlen = recvfrom(s, buf, sizeof(buf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen); | |
146 | if (rlen == -1) { | |
147 | syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno)); | |
148 | } else { | |
149 | for (i = 0; i < strlen(buf); i++) { | |
150 | if (buf[i] == '\n') | |
151 | buf[i] = '\0'; | |
152 | if (buf[i] == '\r') | |
153 | buf[i] = '\0'; | |
154 | } | |
155 | timer = SS_TIMEOUT * 4; | |
156 | if (strlen(buf)) { | |
157 | syslog(LOG_NOTICE, "recv: \"%s\"", buf); | |
158 | ||
159 | /* | |
44 | 160 | * Process commands from the client |
43 | 161 | */ |
44 | 162 | if (strncmp(buf, "ack", 3) == 0) { |
163 | srv_send((char *)"ack"); | |
164 | } else if (strncmp(buf, "lcd", 3) == 0) { | |
165 | for (i = 0; i < 20; i++) { | |
166 | obuf[i] = lcdbuf[lcdHandle][i][0]; | |
167 | obuf[i+21] = lcdbuf[lcdHandle][i][1]; | |
168 | obuf[i+42] = lcdbuf[lcdHandle][i][2]; | |
169 | obuf[i+63] = lcdbuf[lcdHandle][i][3]; | |
170 | } | |
171 | obuf[20] = obuf[41] = obuf[62] = ','; | |
172 | obuf[83] = '\0'; | |
173 | srv_send(obuf); | |
174 | } else if (strncmp(buf, "getMode", 7) == 0) { | |
175 | srv_send("b"); | |
176 | } else if (strncmp(buf, "getControlSettings", 18) == 0) { | |
177 | srv_send("mode='b', beerSet=20.0, fridgeSet=20.0, heatEstimator=0.2, coolEstimator=5"); | |
178 | } else { | |
179 | if (debug) | |
180 | fprintf(stdout, "unknown command \"%s\"\n", buf); | |
181 | srv_send((char *)"ERR"); | |
182 | } | |
183 | break; | |
43 | 184 | } |
185 | } | |
186 | } else { | |
187 | syslog(LOG_NOTICE, "poll other event"); | |
188 | } | |
189 | ||
190 | } else { | |
191 | /* | |
192 | * Poll timeout, do some housekeeping | |
193 | */ | |
194 | if (timer) { | |
195 | timer--; | |
196 | } else { | |
197 | /* Inactivity timeout */ | |
198 | break; | |
199 | } | |
200 | if (my_shutdown) { | |
201 | break; | |
202 | } | |
203 | } | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
204 | } |
44 | 205 | |
206 | if (clients) | |
207 | clients--; | |
208 | syslog(LOG_NOTICE, "End connection from %s port %u", hostname, ntohs(peeraddr_in.sin_port)); | |
209 | if (debug) | |
210 | fprintf(stdout, "End connection from %s port %u\n", hostname, ntohs(peeraddr_in.sin_port)); | |
211 | close(s); | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
212 | } |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
213 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
214 | |
43 | 215 | |
216 | PI_THREAD (my_server_loop) | |
217 | { | |
218 | socklen_t addrlen; | |
44 | 219 | int optval = 1; |
43 | 220 | |
221 | syslog(LOG_NOTICE, "Thread my_server_loop started"); | |
222 | if (debug) | |
223 | fprintf(stdout, "Thread my_server_loop started\n"); | |
224 | ||
44 | 225 | memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in)); |
226 | memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in)); | |
227 | myaddr_in.sin_family = AF_INET; | |
228 | myaddr_in.sin_addr.s_addr = INADDR_ANY; | |
229 | myaddr_in.sin_port = htons(Config.my_port); | |
230 | ||
231 | ls = socket(AF_INET, SOCK_STREAM, 0); | |
232 | if (ls == -1) { | |
233 | syslog(LOG_NOTICE, "Can't create listen socket: %s", strerror(errno)); | |
234 | fprintf(stderr, "Can't create listen socket: %s\n", strerror(errno)); | |
235 | return 0; | |
236 | } | |
237 | ||
238 | if (setsockopt(ls, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1) { | |
239 | syslog(LOG_NOTICE, "Can't setsockopt SO_KEEPALIVE socket: %s", strerror(errno)); | |
240 | close(ls); | |
241 | return 0; | |
242 | } | |
243 | ||
244 | if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { | |
245 | syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno)); | |
246 | close(ls); | |
247 | return NULL; | |
248 | } | |
249 | ||
250 | if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) { | |
251 | syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno)); | |
252 | close(ls); | |
253 | return NULL; | |
254 | } | |
255 | ||
256 | if (listen(ls, 5) == -1) { | |
257 | syslog(LOG_NOTICE, "Can't listen on listen socket: %s", strerror(errno)); | |
258 | close(ls); | |
259 | return NULL; | |
260 | } | |
261 | ||
262 | syslog(LOG_NOTICE, "listen socket created %d", ls); | |
263 | if (debug) | |
264 | fprintf(stdout, "listen socket created %d\n", ls); | |
265 | ||
266 | ||
43 | 267 | /* |
268 | * Loop forever until the external shutdown variable is set. | |
269 | */ | |
270 | for (;;) { | |
271 | ||
272 | addrlen = sizeof(struct sockaddr_in); | |
273 | /* | |
274 | * This call will block until a new connection | |
275 | * arrives. Then it will return the address of | |
276 | * the connecting peer, and a new socket | |
277 | * descriptor, s, for that connection. | |
278 | */ | |
279 | s = accept(ls, (struct sockaddr *)&peeraddr_in, &addrlen); | |
280 | if (s == -1) { | |
281 | syslog(LOG_NOTICE, "my_server_loop accept failed %s", strerror(errno)); | |
282 | if (debug) | |
283 | fprintf(stdout, "my_server_loop accept failed %s\n", strerror(errno)); | |
44 | 284 | return 0; |
43 | 285 | } |
286 | ||
287 | switch (fork()) { | |
288 | case -1: /* | |
289 | * Can't fork, just continue. | |
290 | */ | |
44 | 291 | return 0; |
43 | 292 | case 0: /* |
293 | * Child process, the commandline server. | |
294 | */ | |
295 | cmd_server(); | |
296 | return 0; | |
297 | default: /* | |
298 | * Daemon process comes here. The daemon | |
299 | * needs to remember to close the new | |
300 | * accept socket after forking the child. | |
301 | */ | |
302 | close(s); | |
303 | } | |
304 | ||
305 | if (my_shutdown) { | |
306 | syslog(LOG_NOTICE, "Thread my_server_loop stopped"); | |
307 | if (debug) | |
308 | fprintf(stdout, "Thread my_server_loop stopped\n"); | |
309 | return 0; | |
310 | } | |
311 | ||
312 | } | |
313 | } | |
314 | ||
315 | ||
316 |