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

Commit

Permalink
encoding support for IPC
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRow committed Jun 30, 2020
1 parent 3c0dc0b commit 66fdc8d
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 37 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"ext-gmp" : "*",
"ext-mbstring" : "*",
"ext-openssl" : "*",
"google/flatbuffers" : "*",
"textalk/websocket" : "*"
},
"suggest" : {
Expand Down
107 changes: 76 additions & 31 deletions src/NanoIPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class NanoIPC

private $transportType;
private $transport;
private $encoding;
private $preamble;
private $pathToSocket;
private $hostname;
Expand All @@ -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

Expand Down Expand Up @@ -81,9 +82,6 @@ public function __construct(string $transport_type, array $parameters)
$this->error,
15
);
if ($this->transport === false) {
return false;
}


// #
Expand All @@ -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
// #
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/NanoRPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function __call($method, array $params)
}


// # Version switch
// # API switch

// v1
if ($this->API == 1) {
Expand Down Expand Up @@ -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) {
Expand Down
48 changes: 46 additions & 2 deletions test/NanoIPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
14 changes: 14 additions & 0 deletions test/NanoRPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 0 additions & 1 deletion test/NanoWS.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
$nanows = new php4nano\NanoWS('localhost', 7078);

$nanows->subscribe('confirmation');
//$nanows->keepalive();

while (true) {
print_r($nanows->listen());
Expand Down

0 comments on commit 66fdc8d

Please sign in to comment.