/** * Получение списка worker-ов * * @param string $worker имя воркеров, параметры которого нам нужно получить * @return array массив доступных worker-ов с их загрузкой и тд */ public function getWorkers($worker = null) { // получаем статистику по worker-ам \App::uses('CakeSocket', 'Network'); $Socket = new \CakeSocket($this->_config['server']); $Socket->connect(); $workers = array(); // делаем 2 замера с интервалом в 1 секунду для получение точного результата for ($i = 0; $i <= 2; $i++) { $Socket->write("status\n"); $content = $Socket->read(50000); $answers = explode("\n", trim($content)); foreach ($answers as $string) { $temp = explode("\t", $string); $title = trim($temp[0]); if (strpos($title, 'restart') !== false || strpos($title, '.') !== false) { continue; } if (!empty($workers[$title])) { // тут нас интересует только макс. значение доступных worker-ов $workers[$title][3] = intval($workers[$title][3]) < intval($temp[3]) ? $temp[3] : $workers[$title][3]; } else { $workers[$title] = $temp; } } sleep(1); } $Socket->disconnect(); return $worker ? $workers[$worker] : $workers; }
/** * Protected method for sending data to SMTP connection * * @param string $data data to be sent to SMTP server * @param mixed $checkCode code to check for in server response, false to skip * @return void * @throws SocketException */ protected function _smtpSend($data, $checkCode = '250') { if (!is_null($data)) { $this->_socket->write($data . "\r\n"); } while ($checkCode !== false) { $response = ''; $startTime = time(); while (substr($response, -2) !== "\r\n" && time() - $startTime < $this->_config['timeout']) { $response .= $this->_socket->read(); } if (substr($response, -2) !== "\r\n") { throw new SocketException(__d('cake_dev', 'SMTP timeout.')); } $responseLines = explode("\r\n", rtrim($response, "\r\n")); $response = end($responseLines); if (preg_match('/^(' . $checkCode . ')(.)/', $response, $code)) { if ($code[2] === '-') { continue; } return $code[1]; } throw new SocketException(__d('cake_dev', 'SMTP Error: %s', $response)); } }
/** * Connect * * @param string $host * @param integer $port * @return FtpSocket */ public function connect($host = null, $port = null) { if (isset($host)) { $this->config['host'] = $host; } if (isset($port)) { $this->config['port'] = $port; } parent::connect(); return $this; }
/** * testReset method * * @return void */ public function testReset() { $config = array('persistent' => true, 'host' => '127.0.0.1', 'protocol' => 'udp', 'port' => 80, 'timeout' => 20); $anotherSocket = new CakeSocket($config); $anotherSocket->reset(); $this->assertEquals(array(), $anotherSocket->config); }
/** * Resets the state of this HttpSocket instance to it's initial state (before Object::__construct got executed) or does * the same thing partially for the request and the response property only. * * @param bool $full If set to false only HttpSocket::response and HttpSocket::request are reset * @return bool True on success */ public function reset($full = true) { static $initalState = array(); if (empty($initalState)) { $initalState = get_class_vars(__CLASS__); } if (!$full) { $this->request = $initalState['request']; $this->response = $initalState['response']; return true; } parent::reset($initalState); return true; }
/** * Test that protocol in the host doesn't cause cert errors. * * @return void */ public function testConnectProtocolInHost() { $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.'); $configSslTls = array('host' => 'ssl://smtp.gmail.com', 'port' => 465, 'timeout' => 5); $socket = new CakeSocket($configSslTls); try { $socket->connect(); $this->assertEquals('smtp.gmail.com', $socket->config['host']); $this->assertEquals('ssl', $socket->config['protocol']); } catch (SocketException $e) { $this->markTestSkipped('Cannot test network, skipping.'); } }
public function write($data, $method = false, $size = 1024) { $didWrite = parent::write($data . $this->eol); if ($didWrite && $size > 0) { if ($method && is_callable(array($this, '_' . $method))) { $data = parent::read($size, $method); $method = '_' . $method; return $this->{$method}($data); } return parent::read($size); } return $didWrite; }
/** * Disconnect * * @return void * @throws SocketException */ protected function _disconnect() { $this->_smtpSend('QUIT', FALSE); $this->_socket->disconnect(); }
/** * Build an HTTP Socket using the specified configuration. * * @param array $config Configuration */ function __construct($config = array()) { if (is_string($config)) { $this->configUri($config); } elseif (is_array($config)) { $this->config = Set::merge($this->config, $config); } parent::__construct($this->config); }