summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Client.php36
-rw-r--r--src/MasterClient.php3
-rw-r--r--src/Message.php6
-rw-r--r--src/PingMessage.php6
-rw-r--r--src/PongMessage.php6
-rw-r--r--src/RequestMessage.php14
-rw-r--r--src/ResponseMessage.php17
-rw-r--r--src/WorkerClient.php3
8 files changed, 59 insertions, 32 deletions
diff --git a/src/Client.php b/src/Client.php
index d1f8b7b..7f7d663 100644
--- a/src/Client.php
+++ b/src/Client.php
@@ -25,7 +25,8 @@ class Client {
* @param string $password
* @throws \Exception
*/
- public function __construct(int $port, string $host = '127.0.0.1', string $password = '') {
+ public function __construct(int $port, string $host = '127.0.0.1', string $password = '')
+ {
$this->port = $port;
$this->host = $host;
$this->password = $password;
@@ -41,15 +42,17 @@ class Client {
/**
* JobdClient destructor.
*/
- public function __destruct() {
+ public function __destruct()
+ {
$this->close();
}
/**
- * @return ResponseMessage
+ * @return PongMessage
* @throws \Exception
*/
- public function ping() {
+ public function ping(): PongMessage
+ {
$this->send(new PingMessage());
return $this->recv();
}
@@ -58,7 +61,8 @@ class Client {
* @param RequestMessage $request
* @return int
*/
- public function sendRequest(RequestMessage $request) {
+ public function sendRequest(RequestMessage $request): int
+ {
if ($this->password && !$this->passwordSent) {
$request->setPassword($this->password);
$this->passwordSent = true;
@@ -75,17 +79,19 @@ class Client {
/**
* @param Message $message
*/
- public function send(Message $message) {
+ public function send(Message $message)
+ {
$serialized = $message->serialize();
fwrite($this->sock, $serialized . self::EOT);
}
/**
* @param int $request_no
- * @return ResponseMessage
+ * @return RequestMessage|ResponseMessage|PingMessage|PongMessage
* @throws \Exception
*/
- public function recv(int $request_no = -1) {
+ public function recv(int $request_no = -1)
+ {
$messages = [];
$buf = '';
while (!feof($this->sock)) {
@@ -165,7 +171,8 @@ class Client {
/**
* @return int
*/
- protected function getNextOutgoingRequestNo() {
+ protected function getNextOutgoingRequestNo()
+ {
$this->lastOutgoingRequestNo++;
if ($this->lastOutgoingRequestNo >= self::REQUEST_NO_LIMIT)
@@ -179,7 +186,8 @@ class Client {
* @return RequestMessage|ResponseMessage|PingMessage|PongMessage
* @throws \Exception
*/
- protected static function parseMessage(string $raw_string) {
+ protected static function parseMessage(string $raw_string)
+ {
$raw = json_decode($raw_string, true);
if (!is_array($raw) || count($raw) < 1)
throw new \Exception("Malformed message: {$raw_string}");
@@ -195,7 +203,7 @@ class Client {
['type', 's', true],
['no', 'i', true],
['password', 's', false],
- ['data', 'aifs', false]
+ ['data', 'a', false]
]);
} catch (\Exception $e) {
throw new \Exception("Malformed REQUEST message: {$e->getMessage()}");
@@ -239,7 +247,8 @@ class Client {
* @param array $schema
* @throws \Exception
*/
- protected static function validateData($data, array $schema) {
+ protected static function validateData($data, array $schema)
+ {
if (!$data || !is_array($data))
throw new \Exception('data must be array');
@@ -294,7 +303,8 @@ class Client {
/**
* Close connection.
*/
- public function close() {
+ public function close()
+ {
if (!$this->sock)
return;
diff --git a/src/MasterClient.php b/src/MasterClient.php
index a804ce5..1cfb58e 100644
--- a/src/MasterClient.php
+++ b/src/MasterClient.php
@@ -4,7 +4,8 @@ namespace jobd;
class MasterClient extends Client {
- public function __construct(int $port = Client::MASTER_PORT, ...$args) {
+ public function __construct(int $port = Client::MASTER_PORT, ...$args)
+ {
parent::__construct($port, ...$args);
}
diff --git a/src/Message.php b/src/Message.php
index 92c6005..d5df6ab 100644
--- a/src/Message.php
+++ b/src/Message.php
@@ -16,7 +16,8 @@ abstract class Message {
* Message constructor.
* @param int $type
*/
- public function __construct(int $type) {
+ public function __construct(int $type)
+ {
$this->type = $type;
}
@@ -28,7 +29,8 @@ abstract class Message {
/**
* @return string
*/
- public function serialize(): string {
+ public function serialize(): string
+ {
$data = [$this->type];
$content = $this->getContent();
diff --git a/src/PingMessage.php b/src/PingMessage.php
index 2b751a2..f3441c9 100644
--- a/src/PingMessage.php
+++ b/src/PingMessage.php
@@ -4,11 +4,13 @@ namespace jobd;
class PingMessage extends Message {
- public function __construct() {
+ public function __construct()
+ {
parent::__construct(Message::PING);
}
- protected function getContent(): array {
+ protected function getContent(): array
+ {
return [];
}
diff --git a/src/PongMessage.php b/src/PongMessage.php
index dc7af33..a0a38a0 100644
--- a/src/PongMessage.php
+++ b/src/PongMessage.php
@@ -4,11 +4,13 @@ namespace jobd;
class PongMessage extends Message {
- public function __construct() {
+ public function __construct()
+ {
parent::__construct(Message::PING);
}
- protected function getContent(): array {
+ protected function getContent(): array
+ {
return [];
}
diff --git a/src/RequestMessage.php b/src/RequestMessage.php
index dc28e4d..7799937 100644
--- a/src/RequestMessage.php
+++ b/src/RequestMessage.php
@@ -12,9 +12,10 @@ class RequestMessage extends Message {
/**
* Request constructor.
* @param string $request_type
- * @param null $request_data
+ * @param null|array $request_data
*/
- public function __construct(string $request_type, $request_data = null) {
+ public function __construct(string $request_type, $request_data = null)
+ {
parent::__construct(Message::REQUEST);
$this->requestData = $request_data;
@@ -24,21 +25,24 @@ class RequestMessage extends Message {
/**
* @param string $password
*/
- public function setPassword(string $password) {
+ public function setPassword(string $password)
+ {
$this->password = $password;
}
/**
* @param int $no
*/
- public function setRequestNo(int $no) {
+ public function setRequestNo(int $no)
+ {
$this->requestNo = $no;
}
/**
* @return string[]
*/
- protected function getContent(): array {
+ protected function getContent(): array
+ {
$request = [
'type' => $this->requestType,
'no' => $this->requestNo,
diff --git a/src/ResponseMessage.php b/src/ResponseMessage.php
index 8e687e2..1f91dd4 100644
--- a/src/ResponseMessage.php
+++ b/src/ResponseMessage.php
@@ -11,11 +11,12 @@ class ResponseMessage extends Message {
/**
* Response constructor.
*
- * @param int $requestNo
+ * @param int $request_no
* @param null $error
* @param null $data
*/
- public function __construct($request_no, $error = null, $data = null) {
+ public function __construct(int $request_no, $error = null, $data = null)
+ {
parent::__construct(Message::RESPONSE);
$this->requestNo = $request_no;
@@ -26,7 +27,8 @@ class ResponseMessage extends Message {
/**
* @return array
*/
- protected function getContent(): array {
+ protected function getContent(): array
+ {
$response = [
'no' => $this->requestNo
];
@@ -43,21 +45,24 @@ class ResponseMessage extends Message {
/**
* @return mixed
*/
- public function getError() {
+ public function getError()
+ {
return $this->error;
}
/**
* @return mixed
*/
- public function getData() {
+ public function getData()
+ {
return $this->data;
}
/**
* @return int
*/
- public function getRequestNo() {
+ public function getRequestNo(): int
+ {
return $this->requestNo;
}
diff --git a/src/WorkerClient.php b/src/WorkerClient.php
index dbf0f78..240c200 100644
--- a/src/WorkerClient.php
+++ b/src/WorkerClient.php
@@ -4,7 +4,8 @@ namespace jobd;
class WorkerClient extends Client {
- public function __construct(int $port = Client::WORKER_PORT, ...$args) {
+ public function __construct(int $port = Client::WORKER_PORT, ...$args)
+ {
parent::__construct($port, ...$args);
}