Commit d819e87f authored by Hannes Peterseim's avatar Hannes Peterseim
Browse files

adding the client

parent c0d91e2f
<?php
#################################
# YAMS v1.0 #
# 2021 by Hannes Peterseim #
# more can be found on #
# https://www.petit-souris.de #
# #
# licensed under GPL 3.0 #
# https://www.gnu.org/licenses #
#################################
namespace hp\yams;
error_reporting(E_ALL);
/**
* Class client
* @package hp\yams
*/
class client
{
/**
* @var false|resource
*/
private $socket;
/**
* client constructor.
*/
public function __construct()
{
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->socket === false) {
echo "socket_create() failed: " . socket_strerror(socket_last_error()) . "\n";
}
}
/**
* connects to the server
*
* @param string $address
* @param string $port
* @return bool
*/
public function connect ($address = '127.0.0.1', $port = '5000')
{
$result = socket_connect($this->socket, $address, $port);
if ($result === false) {
echo "socket_connect() failed: " . socket_strerror(socket_last_error($this->socket)) . "\n";
return false;
}
return true;
}
/**
* gets some stats from the server
*
* @return bool|mixed
*/
public function stats ()
{
$msg = json_encode(['command' => 'stats']);
socket_write($this->socket, $msg, strlen($msg));
return $this->getreturn();
}
/**
* determine wether an key exists
*
* @param string $key
* @param string $store
* @return bool|mixed
*/
public function exist ($key = '', $store = 'standard')
{
if ($key == '') {
return false;
}
$msg = json_encode(['command' => 'exist', 'key' => $key, 'store' => $store]);
socket_write($this->socket, $msg, strlen($msg));
return $this->getreturn();
}
/**
* gets the value of an given key
*
* @param string $key
* @param string $store
* @return bool|mixed
*/
public function get ($key = '', $store = 'standard')
{
if ($key == '') {
return false;
}
$msg = json_encode(['command' => 'get', 'key' => $key, 'store' => $store]);
socket_write($this->socket, $msg, strlen($msg));
return $this->getreturn();
}
/**
* sets an value reachable under the given key
*
* @param string $key
* @param string $value
* @param string $store
* @return bool|mixed
*/
public function set ($key = '', $value = '', $store = 'standard')
{
if ($key == '') {
return false;
}
$msg = json_encode(['command' => 'set', 'key' => $key, 'value' => $value, 'store' => $store]);
socket_write($this->socket, $msg, strlen($msg));
return $this->getreturn();
}
/**
* deletes an given key
*
* @param string $key
* @param string $store
* @return bool|mixed
*/
public function del ($key = '', $store = 'standard')
{
if ($key == '') {
return false;
}
$msg = json_encode(['command' => 'del', 'key' => $key, 'store' => $store]);
socket_write($this->socket, $msg, strlen($msg));
return $this->getreturn();
}
/**
* increment the value of an given key
*
* @param string $key
* @param string $store
* @return bool|mixed
*/
public function inc ($key = '', $store = 'standard')
{
if ($key == '') {
return false;
}
$msg = json_encode(['command' => 'inc', 'key' => $key, 'store' => $store]);
socket_write($this->socket, $msg, strlen($msg));
return $this->getreturn();
}
/**
* decrement the value of an given key
*
* @param string $key
* @param string $store
* @return bool|mixed
*/
public function dec ($key = '', $store = 'standard')
{
if ($key == '') {
return false;
}
$msg = json_encode(['command' => 'dec', 'key' => $key, 'store' => $store]);
socket_write($this->socket, $msg, strlen($msg));
return $this->getreturn();
}
/**
* adds an new memory store
*
* @param string $store
* @return bool|mixed
*/
public function addstore ($store = '')
{
if ($store == '' || $store == 'standard') {
return false;
}
$msg = json_encode(['command' => 'addstore', 'memstore' => $store]);
socket_write($this->socket, $msg, strlen($msg));
return $this->getreturn();
}
/**
* determines wether an memory store exists
*
* @param string $store
* @return bool|mixed
*/
public function existstore ($store = '')
{
if ($store == '') {
return false;
}
$msg = json_encode(['command' => 'existstore', 'store' => $store]);
socket_write($this->socket, $msg, strlen($msg));
return $this->getreturn();
}
/**
* deletes an new memory store
*
* @param string $store
* @return bool|mixed
*/
public function delstore ($store = '')
{
if ($store == '' || $store == 'standard') {
return false;
}
$msg = json_encode(['command' => 'delstore', 'memstore' => $store]);
socket_write($this->socket, $msg, strlen($msg));
return $this->getreturn();
}
/**
* closes the server connection
*
* @return bool|mixed
*/
public function close ()
{
$msg = json_encode(['command' => 'close']);
socket_write($this->socket, $msg, strlen($msg));
$return = $this->getreturn();
socket_close($this->socket);
return $return;
}
/**
* gets the return value
*
* @return bool|mixed
*/
private function getreturn ()
{
$msg = '';
while (0 != 1) {
if (false === ($out = socket_read($this->socket, 2048))) {
echo 'socket_read() error: ' . socket_strerror(socket_last_error($this->socket)) . "\n";
}
if (substr_count($out,'endofcommunication') > 0 || trim($out) == '') {
$msg .= str_replace('endofcommunication','',$out);
break;
} else {
$msg .= $out;
}
}
$return = json_decode(trim($msg), true);
if (isset($return['value'])) {
return $return['value'];
}
if (isset($return['return']) && $return['return'] == 'ok') {
return true;
}
return false;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment