#!/usr/bin/php * @version 2016-08-28 */ if (PHP_SAPI !== 'cli') { die("This program can only be run on the command line.\n"); } fclose(STDIN); fclose(STDOUT); fclose(STDERR); $STDIN = fopen('/dev/null', 'r'); $STDOUT = fopen('/dev/null', 'w'); $STDERR = fopen('/dev/null', 'w'); define('mfUI',"cli"); define('FRONKDB_SQLDEBUG',false); /* Set default Timezone and error reporting */ ini_set("date.timezone","Europe/Vienna"); error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED)); define("LOGFILENAME",realpath(dirname(__FILE__)."/../var/log/")."/rimo-websocket-client.log"); $configfilepath = realpath(dirname(__FILE__)."/../config/config.php"); if(file_exists($configfilepath)) { require($configfilepath); } else { die("CANNOT FIND CONFIGFILE!\n\nThis is a serious error. You should not run your mvcfronk application without a configfile. Not proceeding."); } if(defined('MFLOCALE_TIME')) { setlocale(LC_TIME, MFLOCALE_TIME); } if(defined('MFLOCALE_MONETARY')) { setlocale(LC_MONETARY, MFLOCALE_MONETARY); } if(defined('MFLOCALE_NUMERIC')) { setlocale(LC_NUMERIC, MFLOCALE_NUMERIC); } $request=array(); // Put commandline arguments into $request if(count($argv)) { $args=$argv; array_shift($args); // shift scriptname off of args array foreach($args as $i => $arg) { if(preg_match('/^--(.+)/',$arg,$m)) { if(isset($args[$i+1]) && !preg_match('/^-/',$args[$i+1])) { $request[$m[1]] = $args[$i+1]; } else { $request[$m[1]] = true; } } elseif(preg_match('/^-([a-zA-Z])(.+)/', $arg, $m)) { $request[$m[1]] = $m[2]; } elseif(preg_match('/^-([^-])/', $arg, $m)) { if(isset($args[$i+1]) && !preg_match('/^-/',$args[$i+1])) { $request[$m[1]] = $args[$i + 1]; } else { $request[$m[1]] = true; } } } } $is_daemon = false; if($request['d'] || $request['daemonize']) { if(pcntl_fork() !== 0) { exit; } $is_daemon = true; } require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php"); $log = mfLoghandler::singleton(); cli_set_process_title("thetool-rimo-client"); pcntl_async_signals(true); pcntl_signal(SIGTERM, 'signalHandler'); if(!defined("RIMO_CLIENT_MAX_FORK")) define("RIMO_CLIENT_MAX_FORK", 2); $forkcount = 0; $continue = true; $childpid = false; while($continue) { $log->debug("Starting new child."); $pid = pcntl_fork(); if ($pid === -1) { $log->debug("error forking"); } elseif ($pid > 0) { // in parent $forkcount++; $childpid = $pid; $status = false; while(!pcntl_waitpid($pid,$status, WNOHANG)) { sleep(1); } $log->debug("child returned"); if(RIMO_CLIENT_MAX_FORK && $forkcount > RIMO_CLIENT_MAX_FORK) { // exit daemon, have systemd restart it exit(1); } // wait RIMO_CLIENT_SLEEP_SEC seconds before next run sleep(RIMO_API_WS_SLEEP_SEC); } else { // in child $mypid = getmypid(); try { // open rimo websocket connection and process events //$continue = false; // don't fork again after child is done $ws_timeout = 10; $ws_url = RIMO_API_WS_URL."?login=".RIMO_API_WS_USER."&passcode=".RIMO_API_WS_PASS; $ws = new WebSocket\Client($ws_url, ["timeout" => $ws_timeout, "headers" => ["X-Api-Key" => RIMO_API_WS_PASS]]); $i = 0; while(1) { if($i == 0) { $ws->text("[$mypid] Hello into Websocket!"); } $output = $ws->receive(); client_log($mypid, "Received from websocket: $output"); sleep(1); $i++; } $ws->close(); } catch (\Exception $e) { // exit child process on error client_log($mypid, "Caught exception: ".$e->getMessage(), "error"); client_log($mypid); //$log->error("Caught exception: ".$e->getMessage()."\n"); exit; } } } function signalHandler($sig) { global $continue; global $childpid; $continue = false; //echo "in signal handler\n"; if($childpid) { posix_kill($childpid, SIGTERM); } exit; } function client_log($pid, $text, $severity = "notice") { global $log; global $is_daemon; if($is_daemon) { echo "[".date('Y-m-d H:i:s')."] [$pid] $text\n"; } $log->$severity($text); return true; }