protected function execute(InputInterface $input, OutputInterface $output) { #$this->executePre($input, $output); #$bytesizeFormatter = new Binary(); #$bytesize = new ByteSize($bytesizeFormatter); $bytesize = new ByteSize(); if ($input->hasOption('name') && $input->getOption('name')) { print PhpChat::NAME; } elseif ($input->hasOption('name_lc') && $input->getOption('name_lc')) { print strtolower(PhpChat::NAME); } elseif ($input->hasOption('version_number') && $input->getOption('version_number')) { print PhpChat::VERSION; } elseif ($input->hasOption('connections') && $input->getOption('connections')) { print 'Live Connections' . PHP_EOL . PHP_EOL; $this->executePre($input, $output); $this->log = new Logger($this->getName()); #$this->log->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG)); $this->log->pushHandler(new StreamHandler($this->getLogfilePath(), Logger::DEBUG)); $this->initIpcKernelConnection(); $color = new Color(); $startTime = time(); $time = time(); $seconds = 0; $oldClients = array(); $clientsIdMax = 0; $tcols = (int) exec('tput cols'); $tlines = (int) exec('tput lines'); #print 'cols: '.$tcols.PHP_EOL; #print 'lines: '.$tlines.PHP_EOL; $baseLines = 5; print ' Traffic IN: N/A' . PHP_EOL; print ' Traffic OUT: N/A' . PHP_EOL; #print ' Traffic AVG: N/A'.PHP_EOL; print ' Clients: N/A' . PHP_EOL; #sleep(1); #print '---A'.PHP_EOL; print '' . PHP_EOL; #sleep(1); #print '---B'.PHP_EOL; print ' ' . PHP_EOL; #sleep(1); Console::cursorJumpToColumn(1); #sleep(1); Console::cursorUp($baseLines); #sleep(1); while (!$this->getExit()) { #$this->log->debug('run'); if (!$this->ipcKernelConnection->run()) { $this->log->info('Connection to kernel process end unexpected.'); $this->setExit(1); } $update = false; if ($time != time()) { $time = time(); $seconds++; $tcols = (int) exec('tput cols'); $tlines = (int) exec('tput lines'); $update = true; } $update = true; if ($update) { $clientsInfo = $this->ipcKernelConnection->execSync('serverClientsInfo'); $clientsId = $clientsInfo['clientsId']; $clientsChanged = 0; foreach ($clientsInfo['clients'] as $newClientId => $newClient) { if (isset($oldClients[$newClientId])) { $oldClient = $oldClients[$newClientId]; $changed = false; foreach (static::$CONNECTION_INFO_FIELDS as $fieldName) { if ($oldClient[$fieldName] != $newClient[$fieldName]) { $this->log->debug('update ' . $newClientId . ': ' . $fieldName . '=' . (int) $newClient[$fieldName]); $oldClients[$newClientId][$fieldName] = $newClient[$fieldName]; $oldClients[$newClientId]['lastUpdate'] = time(); } } if ($changed) { $clientsChanged++; } } else { $this->log->debug('new client: ' . $newClientId); $oldClients[$newClientId] = array('lastUpdate' => time(), 'hasId' => $newClient['hasId'], 'hasTalkRequest' => $newClient['hasTalkRequest'], 'hasTalk' => $newClient['hasTalk'], 'hasTalkClose' => $newClient['hasTalkClose'], 'hasShutdown' => $newClient['hasShutdown'], 'isChannelPeer' => $newClient['isChannelPeer'], 'isChannelLocal' => $newClient['isChannelLocal'], 'isOutbound' => $newClient['isOutbound'], 'isInbound' => $newClient['isInbound'], 'isBridgeServer' => $newClient['isBridgeServer'], 'isBridgeClient' => $newClient['isBridgeClient'], 'shutdown' => 0, 'status' => '.'); } } foreach ($oldClients as $oldClientId => $oldClient) { if (!isset($clientsInfo['clients'][$oldClientId]) || $oldClient['hasShutdown']) { if (!$oldClients[$oldClientId]['shutdown']) { $this->log->debug('update ' . $oldClientId . ': shutdown=1'); $oldClients[$oldClientId]['shutdown'] = time(); $oldClients[$oldClientId]['lastUpdate'] = time(); } } if ($oldClient['isOutbound']) { $oldClients[$oldClientId]['status'] = 'o'; } if ($oldClient['isInbound']) { $oldClients[$oldClientId]['status'] = 'i'; } if ($oldClient['isChannelPeer'] || $oldClient['isChannelLocal']) { $oldClients[$oldClientId]['status'] = 'c'; } if ($oldClient['isBridgeServer'] || $oldClient['isBridgeClient']) { $oldClients[$oldClientId]['status'] = 'b'; } if ($oldClient['hasTalkRequest']) { $oldClients[$oldClientId]['status'] = 't'; } if ($oldClient['hasTalk']) { $oldClients[$oldClientId]['status'] = 'T'; } if ($oldClient['hasTalkClose']) { $oldClients[$oldClientId]['status'] = 'X'; } if ($oldClient['shutdown']) { #$this->log->debug('client '.$oldClientId.' has shutdown: '.(time() - $oldClient['shutdown'])); $oldClients[$oldClientId]['status'] = 'x'; if ($oldClient['shutdown'] <= time() - 5) { unset($oldClients[$oldClientId]); } } } $oldClientsLen = count($oldClients); Console::cursorJumpToColumn(15); #sleep(1); print $bytesize->format($clientsInfo['traffic']['in']); #sleep(1); Console::lineClearRight(); #sleep(1); print Console::cursorDown(); #sleep(1); Console::cursorJumpToColumn(15); #sleep(1); print $bytesize->format($clientsInfo['traffic']['out']); #sleep(1); Console::lineClearRight(); #sleep(1); print Console::cursorDown(); #sleep(1); Console::cursorJumpToColumn(11); #sleep(1); print $oldClientsLen . ' / ' . $clientsId; #sleep(1); Console::lineClearRight(); #sleep(1); print Console::cursorDown(); #sleep(1); /*Console::cursorJumpToColumn(15); #sleep(1); $trafficTotal = bcadd($clientsInfo['traffic']['in'], $clientsInfo['traffic']['out']); print $bytesize->format(bcdiv($trafficTotal, time() - $clientsInfo['timeCreated'])).'/s'; #print time() - $clientsInfo['timeCreated']; #sleep(1); Console::lineClearRight(); #sleep(1); print Console::cursorDown(); #sleep(1);*/ print Console::cursorDown(); #sleep(1); Console::cursorJumpToColumn(2); #sleep(1); #print PHP_EOL.' '; #sleep(1); $line = 0; $lineClients = 0; foreach ($oldClients as $oldClientId => $oldClient) { #$this->log->debug('client '.$oldClientId.' print: '.(time() - $oldClient['lastUpdate'])); $output = $oldClient['status']; if ($oldClient['lastUpdate'] >= time() - 2) { $output = $color($oldClient['status'])->bg_green; } elseif ($oldClient['lastUpdate'] <= time() - 60) { #$output = $color($oldClient['status'])->dark; $output = $color($oldClient['status'])->bg_blue; } print $output; $lineClients++; if ($lineClients >= $tcols - 2) { $lineClients = 0; $line++; print PHP_EOL . ' '; } } Console::screenClearToBottom(); #sleep(1); print PHP_EOL; #sleep(1); Console::cursorJumpToColumn(1); #sleep(1); Console::cursorUp($line + $baseLines); #sleep(1); } usleep(static::LOOP_USLEEP); } #sleep(1); Console::cursorUp(); #sleep(1); Console::screenClearToBottom(); #sleep(1); $this->executePost(); $this->log->info('exit'); } else { $settings = $this->getSettings(); $localNode = new Node(); $localNode->setIdHexStr($settings->data['node']['id']); $localNode->setUri($settings->data['node']['uriLocal']); $localNode->setSslKeyPub(file_get_contents($settings->data['node']['sslKeyPubPath'])); $trafficIn = $bytesize->format($settings->data['node']['traffic']['in']); $trafficIn .= ' (' . $settings->data['node']['traffic']['in'] . ' byte)'; $trafficOut = $bytesize->format($settings->data['node']['traffic']['out']); $trafficOut .= ' (' . $settings->data['node']['traffic']['out'] . ' byte)'; print '--------' . PHP_EOL; print 'Informations about local node:' . PHP_EOL; print ' Version: ' . PhpChat::NAME . '/' . PhpChat::VERSION . ' (release ' . PhpChat::RELEASE . ')' . PHP_EOL; print ' ID: ' . $localNode->getIdHexStr() . PHP_EOL; print ' Public key fingerprint: ' . $localNode->getSslKeyPubFingerprint() . PHP_EOL; print ' Last public IP: ' . $settings->data['node']['uriPub'] . PHP_EOL; print ' Listen IP:Port: ' . $settings->data['node']['uriLocal'] . PHP_EOL; print ' Traffic IN: ' . $trafficIn . PHP_EOL; print ' Traffic OUT: ' . $trafficOut . PHP_EOL; print ' Nickname: ' . $settings->data['user']['nickname'] . PHP_EOL; print ' SSL version: ' . OPENSSL_VERSION_TEXT . PHP_EOL; print '--------' . PHP_EOL; print ' Pub Key Base64:' . PHP_EOL . base64_encode($localNode->getSslKeyPub()) . PHP_EOL; print '--------' . PHP_EOL; } #$this->executePost(); }
public function testHistory3() { @unlink('test_data/history.yml'); $settings = new Settings(); $settings->data['datadir'] = 'test_data'; $settings->data['console']['history']['enabled'] = true; $settings->data['console']['history']['entriesMax'] = 1000; $settings->data['console']['history']['saveToFile'] = true; $consoleLog = new Logger('console'); #$consoleLog->pushHandler(new LoggerStreamHandler('php://stdout', Logger::DEBUG)); $console = new Console(); $console->setLog($consoleLog); $console->setSettings($settings); $console->handleLine('/help'); $console->handleLine('/history'); $console->run(); $console->shutdown(); $finder = new Finder(); $files = $finder->in('test_data')->depth(0)->name('history.yml'); $this->assertEquals(1, count($files)); $expect = ''; $expect .= '- /history' . "\n"; $expect .= '- /help' . "\n"; $this->assertEquals($expect, file_get_contents('test_data/history.yml')); @unlink('test_data/history.yml'); }