From 0cf2ea5a1ece52f91d31efefc95247f1fab8635f Mon Sep 17 00:00:00 2001 From: Mike Row <47996463+mikerow@users.noreply.github.com> Date: Tue, 30 Jun 2020 14:28:45 +0200 Subject: [PATCH] Update --- src/NanoIPC.php | 53 ++++++++++++++++++++++++++++-------------------- src/NanoRPC.php | 28 +++++++++++++------------ test/NanoRPC.php | 2 +- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/NanoIPC.php b/src/NanoIPC.php index e6f91d8..18c7c24 100644 --- a/src/NanoIPC.php +++ b/src/NanoIPC.php @@ -28,6 +28,7 @@ class NanoIPC public $responseType; public $responseTime; public $responseRaw; + public $responseId; public $response; @@ -35,16 +36,16 @@ class NanoIPC // ## Initialization // # - public function __construct(string $transport_type, array $params) + public function __construct(string $transport_type, array $parameters) { // # Unix domain Socket if ($transport_type == 'unix_domain_socket') { - if (!isset($params['path_to_socket']) || !is_string($params['path_to_socket'])) { - throw new NanoIPCException("Invalid path to socket: " . $params['path_to_socket']); + if (!isset($parameters['path_to_socket']) || !is_string($parameters['path_to_socket'])) { + throw new NanoIPCException("Invalid path to socket: " . $parameters['path_to_socket']); } - $this->pathToSocket = $params['path_to_socket']; + $this->pathToSocket = $parameters['path_to_socket']; $this->transport = stream_socket_client( "unix://{$this->pathToSocket}", $this->errorCode, @@ -58,22 +59,22 @@ public function __construct(string $transport_type, array $params) // # TCP } elseif ($transport_type == 'TCP') { - if (!isset($params['hostname']) || !is_string($params['hostname'])) { - throw new NanoIPCException("Invalid hostname: " . $params['hostname']); + if (!isset($parameters['hostname']) || !is_string($parameters['hostname'])) { + throw new NanoIPCException("Invalid hostname: " . $parameters['hostname']); } - if (!isset($params['port']) || !is_int((int) $params['port'])) { - throw new NanoIPCException("Invalid port: " . $params['port']); + if (!isset($parameters['port']) || !is_int((int) $parameters['port'])) { + throw new NanoIPCException("Invalid port: " . $parameters['port']); } - if (strpos($params['hostname'], 'http://') === 0) { - $params['hostname'] = substr($params['hostname'], 7); + if (strpos($parameters['hostname'], 'http://') === 0) { + $parameters['hostname'] = substr($parameters['hostname'], 7); } - if (strpos($params['hostname'], 'https://') === 0) { - $params['hostname'] = substr($params['hostname'], 8); + if (strpos($parameters['hostname'], 'https://') === 0) { + $parameters['hostname'] = substr($parameters['hostname'], 8); } - $this->hostname = $params['hostname']; - $this->port = (int) $params['port']; + $this->hostname = $parameters['hostname']; + $this->port = (int) $parameters['port']; $this->transport = stream_socket_client( "tcp://{$this->hostname}:{$this->port}", $this->errorCode, @@ -130,8 +131,10 @@ public function __call($method, array $params) $this->id++; $this->error = null; $this->errorCode = null; + $this->responseType = null; $this->responseTime = null; $this->responseRaw = null; + $this->responseId = null; $this->response = null; @@ -159,7 +162,7 @@ public function __call($method, array $params) $buffer = $this->preamble . pack("N", strlen($envelope)) . $envelope; - // # Unix domain socket, TCP + // # Transport switch if ($this->transportType == 'unix_domain_socket' || $this->transportType == 'TCP' @@ -201,19 +204,25 @@ public function __call($method, array $params) // # Return and errors - $response = json_decode($this->responseRaw, true); - $this->response = $response['message']; - $this->responseType = $response['message_type']; + $this->response = json_decode($this->responseRaw, true); - if (isset($response['time'])) { - $this->responseTime = (int) $response['time']; + $this->responseType = $this->response['message_type']; + + if (isset($this->response['time'])) { + $this->responseTime = (int) $this->response['time']; + } + + if (isset($this->response['id'])) { + $this->responseId = (int) $this->response['id']; } - if ($response['message_type'] == 'Error') { + if ($this->response['message_type'] == 'Error') { $this->error = $this->response['message']; - $this->errorCode = (int) $this->response['code']; + $this->errorCode = (int) $this->response['message']['code']; } + $this->response = $this->response['message']; + if ($this->error) { return false; } else { diff --git a/src/NanoRPC.php b/src/NanoRPC.php index 3de7374..6a46bdc 100644 --- a/src/NanoRPC.php +++ b/src/NanoRPC.php @@ -13,7 +13,7 @@ class NanoRPC private $hostname; private $port; private $url; - private $version; + private $API; private $proto; private $pathToCACertificate; private $authType; @@ -57,23 +57,23 @@ public function __construct(string $hostname = 'localhost', int $port = 7076, st $this->port = $port; $this->url = $url; $this->proto = 'http'; - $this->version = 1; + $this->API = 1; } // # - // ## Set version + // ## Set API // # - public function setVersion(int $version) + public function setAPI(int $API) { - if ($version != 1 && - $version != 2 + if ($API != 1 && + $API != 2 ) { - throw new NanoRPCException("Invalid version: $version"); + throw new NanoRPCException("Invalid API: $API"); } - $this->version = $version; + $this->API = $API; } @@ -169,11 +169,11 @@ public function __call($method, array $params) // # Version switch // v1 - if ($this->version == 1) { + if ($this->API == 1) { $request = $arguments; $request['action'] = $method; // v2 - } elseif ($this->version == 2) { + } elseif ($this->API == 2) { $request = [ 'id' => $this->id, 'message_type' => $method, @@ -255,12 +255,12 @@ public function __call($method, array $params) // # Version switch // v1 - if ($this->version == 1) { + if ($this->API == 1) { if (isset($this->response['error'])) { $this->error = $this->response['error']; } // v2 - } elseif ($this->version == 2) { + } elseif ($this->API == 2) { $this->responseType = $this->response['message_type']; if (isset($this->response['time'])) { @@ -273,8 +273,10 @@ public function __call($method, array $params) if ($this->response['message_type'] == 'Error') { $this->error = $this->response['message']; - $this->errorCode = (int) $this->response['code']; + $this->errorCode = (int) $this->response['message']['code']; } + + $this->response = $this->response['message']; } else { // } diff --git a/test/NanoRPC.php b/test/NanoRPC.php index bf07971..a2d9acb 100644 --- a/test/NanoRPC.php +++ b/test/NanoRPC.php @@ -12,7 +12,7 @@ $nanorpc2 = new php4nano\NanoRPC('localhost', 7076, 'api/v2'); -$nanorpc2->setVersion(2); +$nanorpc2->setAPI(2); $nanorpc2->AccountWeight(['account' => $account]);