function assertIsset($msg, $value, $key, $success = '', $error = 'ERROR') { if (!isset($value[$key])) { log_console($msg . ': ' . $error); log_console('Please fix the error above'); die; } else { if (empty($success)) { $success = "OK value = " . $value[$key]; } log_console($msg . ': ' . $success); } }
/** * Start server. */ public function start() { log_console('Starting RPC Server at ' . $this->config['ip'] . ' on port ' . $this->config['port']); $errno = 0; $err = ""; $this->master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // Non block socket type socket_set_option($this->master, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 10, "usec" => 0)); socket_set_option($this->master, SOL_SOCKET, SO_LINGER, array('l_onoff' => 1, 'l_linger' => 0)); socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1); @socket_bind($this->master, $this->config['ip'], $this->config['port']); while (socket_last_error() === 98) { log_console("Socket terminated but address already in use, wait 2 sec's and try again."); sleep(2); @socket_bind($this->master, $this->config['ip'], $this->config['port']); } $listen = socket_listen($this->master); if ($listen === FALSE) { unlock(); log_console("Can't listen on socket."); exit; } $this->listen(); }
/** * Switch the rig to the given miner. * * @param array $data * The orig data from phpminer. (Optional, default = array()) * * @return boolean * True on success, else false. */ public function switch_miner($data = array()) { // Check if miner was provided and if it exists. if (empty($data['miner']) || !isset($this->config['miners'][$data['miner']])) { return false; } // Get current miner. $current_miner = $this->config['rpc_config']->current_miner; // Check if it is already on that miner. if ($data['miner'] === $current_miner) { log_console('Already on that miner'); return true; } $this->kill_cgminer(); $run_tries = 0; while ($this->is_cgminer_running()) { sleep(1); if ($run_tries++ >= 10) { log_console('Could not stop current miner'); return false; } } $current_miner_config = $this->config['miners'][$data['miner']]; $this->config['miner_api_ip'] = $current_miner_config['ip']; $this->config['miner_api_port'] = $current_miner_config['port']; $this->config['miner'] = $current_miner_config['miner']; $this->config['miner_binary'] = $current_miner_config['miner_binary']; $this->config['cgminer_config_path'] = $current_miner_config['cgminer_config_path']; $this->config['cgminer_path'] = $current_miner_config['cgminer_path']; $this->config['amd_sdk'] = $current_miner_config['amd_sdk']; $this->config['rpc_config']->current_miner = $data['miner']; $this->restart_cgminer(); $client_api = new CGMinerAPI($this->config['miner_api_ip'], $this->config['miner_api_port']); $tries = 0; while ($tries++ <= 10) { try { $client_api->test_connection(); return true; } catch (Exception $ex) { } sleep(1); } log_console('Could not start new miner'); return false; }
/** * Writes the given data to the phpminer resource. * * @param string $string * The data to write. * * @return boolean|int * Returns the written bytes as an integer, boolean false on error. */ private function write_buffer($string) { $tmpBuf = $string; $iBufLen = strlen($tmpBuf); $res = socket_write($this->socket, $tmpBuf, $iBufLen); if ($res === false) { log_console("Couldn't send Data."); return false; } else { if ($res < $iBufLen) { $tmpBuf = substr($tmpBuf, $res); $this->write_buffer($tmpBuf); } } return $res; }