diff --git a/composer.json b/composer.json index fe23c6e..95e1348 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,6 @@ "ext-gmp" : "*", "ext-mbstring" : "*", "ext-openssl" : "*", - "google/flatbuffers" : "*", "textalk/websocket" : "*" }, "suggest" : { diff --git a/src/NanoIPC.php b/src/NanoIPC.php index 72014c2..e35fd16 100644 --- a/src/NanoIPC.php +++ b/src/NanoIPC.php @@ -12,6 +12,7 @@ class NanoIPC private $transportType; private $transport; + private $encoding; private $preamble; private $pathToSocket; private $hostname; @@ -36,7 +37,7 @@ class NanoIPC // ## Initialization // # - public function __construct(string $transport_type, array $parameters) + public function __construct(string $transport_type, array $params) { // # Unix domain Socket @@ -81,9 +82,6 @@ public function __construct(string $transport_type, array $parameters) $this->error, 15 ); - if ($this->transport === false) { - return false; - } // # @@ -93,10 +91,28 @@ public function __construct(string $transport_type, array $parameters) } $this->transportType = $transport_type; - $this->preamble = 'N' . chr(4) . chr(0) . chr(0); + $this->preamble = 'N' . chr(1) . chr(0) . chr(0); } + // # + // ## Set encoding + // # + + public function setEncoding(int $encoding) + { + if ($encoding != 1 && + $encoding != 2 && + $encoding != 4 + ) { + throw new NanoIPCException("Invalid encoding: $encoding"); + } + + $this->encoding = $encoding; + $this->preamble = 'N' . chr($this->encoding) . chr(0) . chr(0); + } + + // # // ## Set Nano authentication // # @@ -148,18 +164,33 @@ public function __call($method, array $params) } } - $envelope = [ - 'message_type' => $method, - 'message' => $arguments - ]; - // Nano auth type - if ($this->authType == 'Nano') { - $envelope['credentials'] = $this->nanoAPIkey; + // # Encoding switch + + // 1/2 + if ($this->encoding == 1 || + $this->encoding == 2 + ) { + $request = $arguments; + $request['action'] = $method; + // 4 + } elseif ($this->encoding == 4) { + $request = [ + 'id' => $this->id, + 'message_type' => $method, + 'message' => $arguments + ]; + + // Nano auth type + if ($this->authType == 'Nano') { + $request['credentials'] = $this->nanoAPIkey; + } + } else { + // } - - $envelope = json_encode($envelope); - $buffer = $this->preamble . pack("N", strlen($envelope)) . $envelope; + + $request = json_encode($request); + $buffer = $this->preamble . pack("N", strlen($request)) . $request; // # Transport switch @@ -198,30 +229,44 @@ public function __call($method, array $params) // # } else { - return false; + } - - // # Return and errors - $this->response = json_decode($this->responseRaw, true); - $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']; - } + // # Encoding switch - if ($this->response['message_type'] == 'Error') { - $this->error = $this->response['message']; - $this->errorCode = (int) $this->response['message']['code']; + // 1/2 + if ($this->encoding == 1 || + $this->encoding == 2 + ) { + if (isset($this->response['error'])) { + $this->error = $this->response['error']; + } + // 4 + } elseif ($this->encoding == 4) { + $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 ($this->response['message_type'] == 'Error') { + $this->error = $this->response['message']; + $this->errorCode = (int) $this->response['message']['code']; + } + + $this->response = $this->response['message']; + } else { + // } - $this->response = $this->response['message']; + // # Return if ($this->error) { return false; diff --git a/src/NanoRPC.php b/src/NanoRPC.php index 4e43d10..47135ab 100644 --- a/src/NanoRPC.php +++ b/src/NanoRPC.php @@ -168,7 +168,7 @@ public function __call($method, array $params) } - // # Version switch + // # API switch // v1 if ($this->API == 1) { @@ -254,7 +254,7 @@ public function __call($method, array $params) $this->response = json_decode($this->responseRaw, true); - // # Version switch + // # API switch // v1 if ($this->API == 1) { diff --git a/test/NanoIPC.php b/test/NanoIPC.php index 6dd82bc..08fb6b3 100644 --- a/test/NanoIPC.php +++ b/test/NanoIPC.php @@ -2,26 +2,70 @@ require_once __DIR__ . '/autoload.php'; + +// # Unix domain socket encoding 2 + +$nanoipc_unix = new php4nano\NanoIPC('unix_domain_socket', ['path_to_socket' => '/tmp/nano']); + +$nanoipc_unix->setEncoding(2); + +$account = 'nano_3dyo9e7wkf8kuykghbjdt78njux3yudhdrhtwaymc8fsmxhxpt1h48zffbse'; + +$t0 = microtime(true); + +$nanoipc_unix->account_weight(['account' => $account]); + +echo 'Time unix 2: ' . (microtime(true) - $t0) . PHP_EOL; + +var_dump($nanoipc_unix); + + +// # Unix domain socket encoding 4 + $nanoipc_unix = new php4nano\NanoIPC('unix_domain_socket', ['path_to_socket' => '/tmp/nano']); +$nanoipc_unix->setEncoding(4); + $account = 'nano_3dyo9e7wkf8kuykghbjdt78njux3yudhdrhtwaymc8fsmxhxpt1h48zffbse'; $t0 = microtime(true); $nanoipc_unix->AccountWeight(['account' => $account]); -echo 'Time unix: ' . (microtime(true) - $t0) . PHP_EOL; +echo 'Time unix 4: ' . (microtime(true) - $t0) . PHP_EOL; var_dump($nanoipc_unix); + +// # TCP encoding 2 + +$nanoipc_tcp = new php4nano\NanoIPC('TCP', ['hostname' => 'localhost', 'port' => 7077]); + +$nanoipc_tcp->setEncoding(2); + +$account = 'nano_3dyo9e7wkf8kuykghbjdt78njux3yudhdrhtwaymc8fsmxhxpt1h48zffbse'; + +$t0 = microtime(true); + +$nanoipc_tcp->account_weight(['account' => $account]); + +echo 'Time TCP 2: ' . (microtime(true) - $t0) . PHP_EOL; + +var_dump($nanoipc_tcp); + + +// # TCP encoding 4 + $nanoipc_tcp = new php4nano\NanoIPC('TCP', ['hostname' => 'localhost', 'port' => 7077]); +$nanoipc_tcp->setEncoding(4); + $account = 'nano_3dyo9e7wkf8kuykghbjdt78njux3yudhdrhtwaymc8fsmxhxpt1h48zffbse'; $t0 = microtime(true); $nanoipc_tcp->AccountWeight(['account' => $account]); -echo 'Time TCP: ' . (microtime(true) - $t0) . PHP_EOL; +echo 'Time TCP 4: ' . (microtime(true) - $t0) . PHP_EOL; var_dump($nanoipc_tcp); diff --git a/test/NanoRPC.php b/test/NanoRPC.php index a2d9acb..f829585 100644 --- a/test/NanoRPC.php +++ b/test/NanoRPC.php @@ -2,18 +2,32 @@ require_once __DIR__ . '/autoload.php'; + +// # API v1 + $nanorpc = new php4nano\NanoRPC('localhost', 7076); $account = 'nano_3dyo9e7wkf8kuykghbjdt78njux3yudhdrhtwaymc8fsmxhxpt1h48zffbse'; +$t0 = microtime(true); + $nanorpc->account_balance(['account' => $account]); +echo 'Time v1: ' . (microtime(true) - $t0) . PHP_EOL; + var_dump($nanorpc); + +// # API v2 + $nanorpc2 = new php4nano\NanoRPC('localhost', 7076, 'api/v2'); $nanorpc2->setAPI(2); +$t0 = microtime(true); + $nanorpc2->AccountWeight(['account' => $account]); +echo 'Time v2: ' . (microtime(true) - $t0) . PHP_EOL; + var_dump($nanorpc2); diff --git a/test/NanoWS.php b/test/NanoWS.php index 85c79e4..d74042a 100644 --- a/test/NanoWS.php +++ b/test/NanoWS.php @@ -5,7 +5,6 @@ $nanows = new php4nano\NanoWS('localhost', 7078); $nanows->subscribe('confirmation'); -//$nanows->keepalive(); while (true) { print_r($nanows->listen());