/
main.php
326 lines (277 loc) · 12.6 KB
/
main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/php
<?php
const _MODULES_PATH_="./modules/";
const _LOGFILE_PATH_="./log/";
$TERMINATE = false;
$RELOAD = false;
$process = array();
declare(ticks = 1);
pcntl_signal(SIGTERM, "sig_handle");
pcntl_signal(SIGINT, "sig_handle");
pcntl_signal(SIGHUP, "sig_handle");
function sig_handle($signal)
{
global $TERMINATE;
switch($signal)
{
case SIGTERM:
writelog ("Got SIGTERM");
$TERMINATE = true;
break;
case SIGINT:
writelog ("User pressed Ctrl+C - Got SIGINT");
$TERMINATE = true;
break;
case SIGHUP:
writelog ("Got SIGHUP");
$TERMINATE = false;
$RELOAD = true;
break;
}
}
function writeLog($data)
{
list($usec, $sec) = explode(' ', microtime());
$datetime = strftime("%Y-%m-%d %H:%M:%S",time());
$log_file = _LOGFILE_PATH_.date("Y-m-d")."_logfile.log";
$msg = $datetime."-". sprintf("%06s",intval($usec*1000000)).": $data";
$fp = @fopen($log_file, 'a');
fputs($fp, "$msg\n");
fclose($fp);
}
function load_module($module_info)
{
global $process;
$process[$module_info["name"]]=$module_info;
}
/***************************************************/
/* This class manage Telnet connection and answer */
/***************************************************/
class clientHandler{
public $bClosed = false;
public $sckSocket = false;
public $cmd = false;
public $command = false;
public $parameter = false;
public $dtarr = array();
public function clientHandler($sckSocket){
$this->sckSocket = $sckSocket;
$this->send('<OK>');
}
public function handle(){
global $process;
global $TERMINATE;
$cmd = $this->read();
if($cmd){
@list($command, $parameter) = @explode(' ', $cmd, 2);
}
if(isset($command) && $command){
switch($command){
case 'time':
$this->send(date('Y-m-d H:i:s'));
break;
case 'plugwise':
/*************************************
* Sending request
*************************************/
$sent_parameter=$parameter."\n";
if (socket_write($process ["plugwise"]["socket"][1], $sent_parameter, strlen($sent_parameter)) === false) {
$this->send("socket_write() a échoué. Raison : ".socket_strerror(socket_last_error($process ["plugwise"]["socket"][1])));
}
else {
/***********************************
* Reading answser
***********************************/
$answer = socket_read($process ["plugwise"]["socket"][1], 65535, PHP_NORMAL_READ);
// Check Error Socket
if($answer === false) {
$error = socket_last_error($process ["plugwise"]["socket"][1]);
if($error != 11 && $error != 115) {
$this->send("error while reading : ".socket_strerror($error).$error."\n");
}
//ignore Empty answer
} elseif($answer === NULL) {
$this->send("answer is empty\n");
//managing Answer
} else {
//spliting all line of answers
$serialize_answer_arr = preg_split('/\n|\r\n?/', $answer,PREG_SPLIT_NO_EMPTY);
//managing each line of answer
foreach ($serialize_answer_arr as $serialize_line) {
//checking not empty
if (trim($serialize_line)<>'') {
//the answer
$answer_obj=unserialize($serialize_line);
//answer could be array or string depending on initial request
$answer_type=gettype($answer_obj);
switch ($answer_type) {
case "array" :
foreach ($answer_obj as $id=>$objet) {
$display="";
if (is_array($objet)) {
foreach ($objet as $field) {
$display.=$field.";";
}
} else {
$display=$objet;
}
$this->send($display);
}
break;
case "boolean":
case "string":
case "integer":
case "double":
$display=$answer_obj;
$this->send($display);
break;
default:
$display="unknown objet type : ". gettype($answer_obj);
$this->send($display);
}
}
}
$this->send("<fin>");
}
}
break;
case 'exit':
case 'quit':
$this->disconnect();
break;
case 'shutdown':
case 'stop':
$this->send('Shutting down...');
$TERMINATE=True;
$this->disconnect();
break;
default:
$this->send('unknown command');
}
}
}
public function send($sMessage){
@socket_write($this->sckSocket, $sMessage."\n");
}
public function read(){
return trim(@socket_read($this->sckSocket, 65535));
}
public function disconnect(){
$this->send('bye');
$this->bClosed = true;
socket_shutdown($this->sckSocket);
socket_close($this->sckSocket);
}
}
/*********************************************************************************
Main program start here
*********************************************************************************/
/* Loading modules */
include (_MODULES_PATH_."plugwise_usb.php");
writelog ("Starting Main Program...");
foreach ($process as $processname=>$module) {
writelog("Creating IPC Socket for module $processname ...");
/* Trying to create socket pair */
/* Unable to create socket pair */
if (socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $process [$processname]["socket"]) === false) {
writelog("socket_create_pair() a échoué. Raison : ".socket_strerror(socket_last_error()));
}
/* Socket pair successfully created */
else {
writelog("opening socket pair for $processname : OK");
/* Forking module*/
$pid = pcntl_fork();
/***********************************
/* Starting modules (child)
***********************************/
if (!$pid) {
switch($module["bootstrap"]) {
case $process[$processname]["bootstrap"] :
writelog("Starting module $processname ...");
include (_MODULES_PATH_.$process[$processname]["driver"]);
writelog ($process[$processname]["bootstrap"].": finished");
exit(0);
/* Module (child) finish here */
break;
default:
writelog("Unknown process : ".$processname. ", exiting");
exit(1);
break;
}
}
/***********************************
/* Parent continue here
***********************************/
else {
// we are in parent so we count process launch
writelog ("Starting ".$process[$processname]["bootstrap"]." module(".$processname.") as pid : ".$pid."...");
$childs[] = $pid;
//we close child socket
socket_close($process [$processname]["socket"][0]);
}
}
}
/***********************************
Main parent does processing here...
***********************************/
writelog("All Modules launched.");
set_time_limit(0);
/*Creating Telenet server on TCP 8080 */
$sckMain = socket_create(AF_INET, SOCK_STREAM, 0) or die();
socket_set_option($sckMain, SOL_SOCKET, SO_REUSEADDR, 1) or die();
socket_bind($sckMain, '127.0.0.1', '8080') or die();
socket_listen($sckMain) or die();
$oaClients = array();
/***************************************************************************************
* Main loop Program
***************************************************************************************/
writelog ("Running main process");
while(!$TERMINATE){
$sckaRead = array();
$sckaRead[0] = $sckMain;
foreach($oaClients as $x => $oClient){
if($oClient->bClosed == true){
unset($oaClients[$x]);
}else{
$sckaRead[] = $oClient->sckSocket;
}
}
@socket_select($sckaRead, $null = NULL, $null = NULL,0);
/* New Connection from client */
if(in_array($sckMain, $sckaRead)){
$sckNewClient = socket_accept($sckMain);
$oaClients[] = new clientHandler($sckNewClient);
}
/* Manage each connected client */
foreach($oaClients as $oClient){
if(in_array($oClient->sckSocket, $sckaRead)){
$oClient->handle();
}
}
usleep(100000);
}
writelog("Send terminate Signal to all modules");
// Send KILL Signal to Child module
posix_kill($pid, SIGTERM);
/***************************************************************************************
/* Closing all socket opened with child process
***************************************************************************************/
socket_close($sckMain);
foreach ($process as $processname=>$module) {
socket_close($process [$processname]["socket"][1]);
}
/***************************************************************************************
wait all child to finish
***************************************************************************************/
while(count($childs) > 0) {
foreach($childs as $key => $pid) {
$res = pcntl_waitpid($pid, $status, WNOHANG);
// If the process has already exited
if($res == -1 || $res > 0)
unset($childs[$key]);
}
sleep(1);
}
// end of the main program
writelog ("End of program : OK");
?>