Tue, 20 May 2014 22:40:16 +0200
More client/server communications
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" |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
26 | #include "thermferm.h" |
42
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 | |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
47 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
48 | void defaultControlsettings(void) |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
49 | { |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
50 | beer->cs_mode = 'o'; /* o = Off, f = fridge, b = beer, p = profile-run */ |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
51 | beer->cs_beerSet = 20.0; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
52 | beer->cs_fridgeSet = 20.0; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
53 | beer->cs_heatEstimator = 0.2; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
54 | beer->cs_coolEstimator = 5; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
55 | } |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
56 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
57 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
58 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
59 | void defaultControlConstants(void) |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
60 | { |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
61 | beer->cc_tempFormat = 'C'; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
62 | beer->cc_tempSetMin = 1.0; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
63 | beer->cc_tempSetMax = 30.0; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
64 | beer->cc_idleRangeH = 1.000; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
65 | beer->cc_idleRangeL = -1.000; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
66 | } |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
67 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
68 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
69 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
70 | void defaultControlVariables(void) |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
71 | { |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
72 | beer->cv_beerDiff = 0.0; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
73 | } |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
74 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
75 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
76 | |
43 | 77 | /* |
78 | * Send message to client | |
79 | */ | |
80 | int srv_send(const char *format, ...) | |
81 | { | |
82 | char out[SS_BUFSIZE]; | |
83 | va_list va_ptr; | |
84 | ||
85 | if (s == -1) | |
86 | return -1; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
87 | |
43 | 88 | va_start(va_ptr, format); |
89 | vsnprintf(out, SS_BUFSIZE-1, format, va_ptr); | |
90 | va_end(va_ptr); | |
91 | ||
44 | 92 | syslog(LOG_NOTICE, "send: \"%s\"", out); |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
93 | if (debug) |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
94 | fprintf(stdout, "send: \"%s\"\n", out); |
44 | 95 | |
43 | 96 | if (send(s, out, strlen(out), 0) != strlen(out)) { |
97 | syslog(LOG_NOTICE, "srv_send failed"); | |
98 | return -1; | |
99 | } | |
100 | ||
101 | if (send(s, (char *)"\r\n", 2, 0) != 2) { | |
102 | syslog(LOG_NOTICE, "srv_send failed"); | |
103 | return -1; | |
104 | } | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
109 | ||
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
110 | |
43 | 111 | void cmd_die(int onsig) |
112 | { | |
113 | syslog(LOG_NOTICE, "Server process die on signal %d", onsig); | |
114 | close(s); | |
115 | exit(0); | |
116 | } | |
117 | ||
118 | ||
119 | ||
120 | void cmd_server(void) | |
121 | { | |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
122 | char *inp, *hostname, buf[SS_BUFSIZE], obuf[SS_BUFSIZE]; |
43 | 123 | int i, rc, rlen, timer; |
124 | socklen_t fromlen; | |
125 | struct pollfd pfd[1]; | |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
126 | float newtemp; |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
127 | |
43 | 128 | /* |
129 | * Close listen socket | |
130 | */ | |
131 | close(ls); | |
132 | /* | |
133 | * Install private signal handler. | |
134 | */ | |
135 | for (i = 0; i < NSIG; i++) { | |
136 | if ((i == SIGHUP) || (i == SIGPIPE) || (i == SIGBUS) || (i == SIGILL) || (i == SIGSEGV)) | |
137 | signal(i, (void (*))cmd_die); | |
138 | else | |
139 | signal(i, SIG_IGN); | |
140 | } | |
141 | ||
142 | hp = gethostbyaddr ((char *) &peeraddr_in.sin_addr, sizeof(struct in_addr), peeraddr_in.sin_family); | |
143 | if (hp == NULL) { | |
144 | hostname = inet_ntoa(peeraddr_in.sin_addr); | |
145 | } else { | |
146 | hostname = hp->h_name; | |
147 | } | |
148 | ||
149 | clients++; | |
150 | syslog(LOG_NOTICE, "Start new client connection (%d) from %s port %u", clients, hostname, ntohs(peeraddr_in.sin_port)); | |
44 | 151 | if (debug) |
152 | fprintf(stdout, "Start new client connection (%d) from %s port %u\n", clients, hostname, ntohs(peeraddr_in.sin_port)); | |
43 | 153 | timer = SS_TIMEOUT * 4; |
154 | ||
155 | /* | |
156 | * Receive loop | |
157 | */ | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
158 | for (;;) { |
43 | 159 | /* |
160 | * Poll socket until a define timeout of 0,25 second. | |
161 | */ | |
162 | pfd[0].fd = s; | |
163 | pfd[0].events = POLLIN; | |
164 | pfd[0].revents = 0; | |
165 | rc = poll(pfd, 1, 250); | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
166 | |
43 | 167 | if (rc == -1) { |
168 | /* | |
169 | * Poll can be interrupted by a finished child so that's not a real error. | |
170 | */ | |
171 | if (errno != EINTR) { | |
172 | syslog(LOG_NOTICE, "poll() rc=%d sock=%d events=%04x", rc, s, pfd[0].revents); | |
173 | } | |
174 | } else if (rc) { | |
175 | if (pfd[0].revents & POLLIN) { | |
176 | memset((char *)&buf, 0, SS_BUFSIZE); | |
177 | fromlen = sizeof(peeraddr_in); | |
178 | rlen = recvfrom(s, buf, sizeof(buf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen); | |
179 | if (rlen == -1) { | |
180 | syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno)); | |
181 | } else { | |
182 | for (i = 0; i < strlen(buf); i++) { | |
183 | if (buf[i] == '\n') | |
184 | buf[i] = '\0'; | |
185 | if (buf[i] == '\r') | |
186 | buf[i] = '\0'; | |
187 | } | |
188 | timer = SS_TIMEOUT * 4; | |
189 | if (strlen(buf)) { | |
190 | syslog(LOG_NOTICE, "recv: \"%s\"", buf); | |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
191 | if (debug) |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
192 | fprintf(stdout, "recv: \"%s\"\n", buf); |
43 | 193 | |
194 | /* | |
44 | 195 | * Process commands from the client |
43 | 196 | */ |
44 | 197 | if (strncmp(buf, "ack", 3) == 0) { |
198 | srv_send((char *)"ack"); | |
199 | } else if (strncmp(buf, "lcd", 3) == 0) { | |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
200 | sprintf(obuf, "[\" \", \" \", \" \", \" \"]"); |
44 | 201 | for (i = 0; i < 20; i++) { |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
202 | obuf[i+2] = lcdbuf[lcdHandle][i][0]; |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
203 | obuf[i+26] = lcdbuf[lcdHandle][i][1]; |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
204 | obuf[i+50] = lcdbuf[lcdHandle][i][2]; |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
205 | obuf[i+74] = lcdbuf[lcdHandle][i][3]; |
44 | 206 | } |
207 | srv_send(obuf); | |
208 | } else if (strncmp(buf, "getMode", 7) == 0) { | |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
209 | srv_send("%c", beer->cs_mode); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
210 | } else if (strncmp(buf, "getFridge", 9) == 0) { |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
211 | srv_send("%.1f", beer->cs_fridgeSet); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
212 | } else if (strncmp(buf, "getBeer", 7) == 0) { |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
213 | srv_send("%.1f", beer->cs_beerSet); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
214 | } else if (strncmp(buf, "getControlConstants", 19) == 0) { |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
215 | srv_send("{ \"tempFormat\":\"%c\", \"tempSetMin\":%.1f, \"tempSetMax\":%.1f, \"idleRangeH\":%.3f, \"idleRangeL\":%.3f }", |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
216 | beer->cc_tempFormat, beer->cc_tempSetMin, beer->cc_tempSetMax, beer->cc_idleRangeH, beer->cc_idleRangeL ); |
44 | 217 | } else if (strncmp(buf, "getControlSettings", 18) == 0) { |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
218 | srv_send("{ \"mode\":\"%c\", \"beerSet\":%.1f, \"fridgeSet\":%.1f, \"heatEstimator\":%.1f, \"coolEstimator\":%.1f }", |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
219 | beer->cs_mode, beer->cs_beerSet, beer->cs_fridgeSet, beer->cs_heatEstimator, beer->cs_coolEstimator); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
220 | } else if (strncmp(buf, "getControlVariables", 19) == 0) { |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
221 | srv_send("{ \"beerDiff\":%.2f }", beer->cv_beerDiff); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
222 | } else if (strncmp(buf, "setBeer=", 8) == 0) { |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
223 | inp = xstrcpy(buf+8); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
224 | rc = sscanf(inp, "%f", &newtemp); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
225 | if (debug) |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
226 | fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
227 | if (rc == 1) { |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
228 | srv_send("ack"); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
229 | beer->cs_mode = 'b'; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
230 | beer->cs_beerSet = newtemp; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
231 | } else { |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
232 | srv_send("err"); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
233 | } |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
234 | } else if (strncmp(buf, "setFridge=", 10) == 0) { |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
235 | inp = xstrcpy(buf+10); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
236 | rc = sscanf(inp, "%f", &newtemp); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
237 | if (debug) |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
238 | fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
239 | if (rc == 1) { |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
240 | srv_send("ack"); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
241 | beer->cs_mode = 'f'; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
242 | beer->cs_fridgeSet = newtemp; |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
243 | } else { |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
244 | srv_send("err"); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
245 | } |
44 | 246 | } else { |
247 | if (debug) | |
248 | fprintf(stdout, "unknown command \"%s\"\n", buf); | |
249 | srv_send((char *)"ERR"); | |
250 | } | |
251 | break; | |
43 | 252 | } |
253 | } | |
254 | } else { | |
255 | syslog(LOG_NOTICE, "poll other event"); | |
256 | } | |
257 | ||
258 | } else { | |
259 | /* | |
260 | * Poll timeout, do some housekeeping | |
261 | */ | |
262 | if (timer) { | |
263 | timer--; | |
264 | } else { | |
265 | /* Inactivity timeout */ | |
266 | break; | |
267 | } | |
268 | if (my_shutdown) { | |
269 | break; | |
270 | } | |
271 | } | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
272 | } |
44 | 273 | |
274 | if (clients) | |
275 | clients--; | |
276 | syslog(LOG_NOTICE, "End connection from %s port %u", hostname, ntohs(peeraddr_in.sin_port)); | |
277 | if (debug) | |
278 | fprintf(stdout, "End connection from %s port %u\n", hostname, ntohs(peeraddr_in.sin_port)); | |
279 | close(s); | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
280 | } |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
281 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
282 | |
43 | 283 | |
284 | PI_THREAD (my_server_loop) | |
285 | { | |
286 | socklen_t addrlen; | |
44 | 287 | int optval = 1; |
43 | 288 | |
289 | syslog(LOG_NOTICE, "Thread my_server_loop started"); | |
290 | if (debug) | |
291 | fprintf(stdout, "Thread my_server_loop started\n"); | |
292 | ||
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
293 | defaultControlsettings(); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
294 | defaultControlConstants(); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
295 | defaultControlVariables(); |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
296 | |
44 | 297 | memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in)); |
298 | memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in)); | |
299 | myaddr_in.sin_family = AF_INET; | |
300 | myaddr_in.sin_addr.s_addr = INADDR_ANY; | |
301 | myaddr_in.sin_port = htons(Config.my_port); | |
302 | ||
303 | ls = socket(AF_INET, SOCK_STREAM, 0); | |
304 | if (ls == -1) { | |
305 | syslog(LOG_NOTICE, "Can't create listen socket: %s", strerror(errno)); | |
306 | fprintf(stderr, "Can't create listen socket: %s\n", strerror(errno)); | |
307 | return 0; | |
308 | } | |
309 | ||
310 | if (setsockopt(ls, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1) { | |
311 | syslog(LOG_NOTICE, "Can't setsockopt SO_KEEPALIVE socket: %s", strerror(errno)); | |
312 | close(ls); | |
313 | return 0; | |
314 | } | |
315 | ||
316 | if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { | |
317 | syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno)); | |
318 | close(ls); | |
319 | return NULL; | |
320 | } | |
321 | ||
322 | if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) { | |
323 | syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno)); | |
324 | close(ls); | |
325 | return NULL; | |
326 | } | |
327 | ||
328 | if (listen(ls, 5) == -1) { | |
329 | syslog(LOG_NOTICE, "Can't listen on listen socket: %s", strerror(errno)); | |
330 | close(ls); | |
331 | return NULL; | |
332 | } | |
333 | ||
334 | syslog(LOG_NOTICE, "listen socket created %d", ls); | |
335 | if (debug) | |
336 | fprintf(stdout, "listen socket created %d\n", ls); | |
337 | ||
338 | ||
43 | 339 | /* |
340 | * Loop forever until the external shutdown variable is set. | |
341 | */ | |
342 | for (;;) { | |
343 | ||
344 | addrlen = sizeof(struct sockaddr_in); | |
345 | /* | |
346 | * This call will block until a new connection | |
347 | * arrives. Then it will return the address of | |
348 | * the connecting peer, and a new socket | |
349 | * descriptor, s, for that connection. | |
350 | */ | |
351 | s = accept(ls, (struct sockaddr *)&peeraddr_in, &addrlen); | |
352 | if (s == -1) { | |
353 | syslog(LOG_NOTICE, "my_server_loop accept failed %s", strerror(errno)); | |
354 | if (debug) | |
355 | fprintf(stdout, "my_server_loop accept failed %s\n", strerror(errno)); | |
44 | 356 | return 0; |
43 | 357 | } |
358 | ||
359 | switch (fork()) { | |
360 | case -1: /* | |
361 | * Can't fork, just continue. | |
362 | */ | |
44 | 363 | return 0; |
43 | 364 | case 0: /* |
365 | * Child process, the commandline server. | |
366 | */ | |
367 | cmd_server(); | |
368 | return 0; | |
369 | default: /* | |
370 | * Daemon process comes here. The daemon | |
371 | * needs to remember to close the new | |
372 | * accept socket after forking the child. | |
373 | */ | |
374 | close(s); | |
375 | } | |
376 | ||
377 | if (my_shutdown) { | |
378 | syslog(LOG_NOTICE, "Thread my_server_loop stopped"); | |
379 | if (debug) | |
380 | fprintf(stdout, "Thread my_server_loop stopped\n"); | |
381 | return 0; | |
382 | } | |
383 | ||
384 | } | |
385 | } | |
386 | ||
387 | ||
388 |