Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRow committed Jun 30, 2020
1 parent 5d51b3a commit 0cf2ea5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 36 deletions.
53 changes: 31 additions & 22 deletions src/NanoIPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,24 @@ class NanoIPC
public $responseType;
public $responseTime;
public $responseRaw;
public $responseId;
public $response;


// #
// ## 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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;


Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 15 additions & 13 deletions src/NanoRPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class NanoRPC
private $hostname;
private $port;
private $url;
private $version;
private $API;
private $proto;
private $pathToCACertificate;
private $authType;
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'])) {
Expand All @@ -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 {
//
}
Expand Down
2 changes: 1 addition & 1 deletion test/NanoRPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

$nanorpc2 = new php4nano\NanoRPC('localhost', 7076, 'api/v2');

$nanorpc2->setVersion(2);
$nanorpc2->setAPI(2);

$nanorpc2->AccountWeight(['account' => $account]);

Expand Down

0 comments on commit 0cf2ea5

Please sign in to comment.