26 #include "rdconfig.h" |
26 #include "rdconfig.h" |
27 #include "lock.h" |
27 #include "lock.h" |
28 #include "mqtt.h" |
28 #include "mqtt.h" |
29 #include "mysql.h" |
29 #include "mysql.h" |
30 #include "nodes.h" |
30 #include "nodes.h" |
|
31 #include "websocket.h" |
31 |
32 |
32 |
33 |
33 int my_shutdown = FALSE; |
34 int my_shutdown = FALSE; |
34 int debug = FALSE; |
35 int debug = FALSE; |
35 static pid_t /*pgrp, */mypid; |
36 static pid_t mypid; |
36 char *Private_Path = NULL; /* Users data path */ |
37 char *Private_Path = NULL; /* Users data path */ |
|
38 pthread_t ws_thread; |
37 |
39 |
38 extern sys_config Config; |
40 extern sys_config Config; |
|
41 |
39 |
42 |
40 |
43 |
41 void help(void) |
44 void help(void) |
42 { |
45 { |
43 fprintf(stdout, "bmsd v%s starting\n\n", VERSION); |
46 fprintf(stdout, "bmsd v%s starting\n\n", VERSION); |
126 if (mqtt_connect()) { |
129 if (mqtt_connect()) { |
127 rc = 4; |
130 rc = 4; |
128 goto endit2; |
131 goto endit2; |
129 } |
132 } |
130 |
133 |
|
134 rc = pthread_create(&ws_thread, NULL, ws_loop, NULL); |
|
135 if (rc) { |
|
136 fprintf(stderr, "ws_loop thread didn't start rc=%d\n", rc); |
|
137 syslog(LOG_NOTICE, "ws_loop thread didn't start rc=%d", rc); |
|
138 rc = 5; |
|
139 goto endit3; |
|
140 } |
|
141 |
131 if (debug) |
142 if (debug) |
132 fprintf(stdout, "[main] Entering main loop\n"); |
143 fprintf(stdout, "[main] Entering main loop\n"); |
133 |
144 |
134 while (my_shutdown == FALSE) { |
145 while (my_shutdown == FALSE) { |
135 |
146 |
136 usleep(4000000); |
147 usleep(3000000); |
137 nodes_check_online(); |
148 nodes_check_online(); |
138 usleep(1000000); |
149 usleep(1000000); |
139 ispindel_mysql_check(); |
150 ispindel_mysql_check(); |
|
151 usleep(1000000); |
|
152 ws_check(); |
140 } |
153 } |
141 if (debug) |
154 if (debug) |
142 fprintf(stdout, "[main] Exit from main loop\n"); |
155 fprintf(stdout, "[main] Exit from main loop\n"); |
143 |
156 |
|
157 endit3: |
144 /* |
158 /* |
145 * Remove our topics and close MQTT connection. |
159 * Remove our topics and close MQTT connection. |
146 */ |
160 */ |
147 mqtt_disconnect(); |
161 mqtt_disconnect(); |
148 |
162 |
210 */ |
224 */ |
211 rc = server(); |
225 rc = server(); |
212 } else { |
226 } else { |
213 /* |
227 /* |
214 * Server initialization is complete. Now we can fork the |
228 * Server initialization is complete. Now we can fork the |
215 * daemon and return to the user. We need to do a setpgrp |
229 * daemon and return to the user. |
216 * so that the daemon will no longer be assosiated with the |
|
217 * users control terminal. This is done before the fork, so |
|
218 * that the child will not be a process group leader. Otherwise, |
|
219 * if the child were to open a terminal, it would become |
|
220 * associated with that terminal as its control terminal. |
|
221 */ |
230 */ |
222 // if ((pgrp = setpgid(0, 0)) == -1) { |
|
223 // syslog(LOG_NOTICE, "setpgid failed: %s", strerror(errno)); |
|
224 // } |
|
225 |
|
226 frk = fork(); |
231 frk = fork(); |
227 switch (frk) { |
232 switch (frk) { |
228 case -1: |
233 case -1: |
229 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); |
234 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); |
230 exit(1); |
235 exit(1); |