ignore = array_merge($this->hubs, $this->ignore); $this->streamLoop(); } /** * */ protected function openStream() { $this->stream = fopen($this->getAllMonUri(), "r"); } /** * @return string */ public function getAllMonUri() { $hubsStr = implode(",", $this->hubs); return "http://localhost/server.php?nodes=" . $hubsStr; // Use this return if you want all nodes // return "http://localhost/server.php"; // return __DIR__ . '/test.stream'; } /** * Main event loop */ public function streamLoop() { $buffer = ''; $this->openStream(); if (!$this->stream) { $this->appendToStreamOutput($this->timeFormatted() . " rpt0000 KEY [via 0000] [Allmon Connection Failed]"); sleep(120); // Systemd / Supervisor will restart us anyway exit; } while (!feof($this->stream)) { $buffer .= stream_get_line($this->stream, 2048, "\n\n"); if (false !== strpos($buffer, '}}')) { $buffer .= "\n\n"; if (preg_match("/event: (.*?)\ndata: (.*)\n\n/m", $buffer, $matches)) { $buffer = ''; if ($matches[1] !== "nodes") continue; $this->parseStreamData($matches[2]); } } } // Redundant exit; } /** * @param $node * @return bool */ protected function isIgnored($node) { return in_array($node, $this->ignore); } /** * @param $json */ protected function parseStreamData($json) { // Debugging -- sometimes allmon doesn't send the proper keyup // file_put_contents(__DIR__.'/storage/event_nodes.txt', "$json,\n", FILE_APPEND); $obj = json_decode($json); foreach ($obj as $node => $v) { foreach ($v->remote_nodes as $remoteNode) { $via = intval($node); $node = $remoteNode->node; if ($this->isIgnored($node)) continue; // Capture previous keyed values $keyedNow = $remoteNode->keyed === "yes"; $keyedBefore = isset($this->keyed[$node]); // Set current key state. In PHP null !== isset $this->keyed[$node] = $keyedNow ?: null; if ($keyedBefore !== $keyedNow) { if (filesize($this->streamOutput) >= 8192) { echo filesize($this->streamOutput) . "\n"; $so = file_get_contents($this->streamOutput); $so = substr($so, 4096); file_put_contents($this->streamOutput, $so); } // Permanently set to Allstar for now $nodePrefix = 'rpt'; $keyedLabel = $keyedNow ? "KEY" : "UNKEY"; $timeFormatted = $this->timeFormatted(); $toWrite = "{$timeFormatted} $nodePrefix{$node} {$keyedLabel} [via {$via}] [{$remoteNode->info}]"; $this->appendToStreamOutput($toWrite); echo $toWrite; } } } } /** * @return string */ protected function timeFormatted() { $time = Carbon::now(); return $time->format("M d h:i:s"); } /** * @param $toWrite */ protected function appendToStreamOutput($toWrite) { file_put_contents($this->streamOutput, $toWrite."\n", FILE_APPEND); } }