1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\RemoteRequest\Protocols; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\RemoteRequest; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AProtocol |
11
|
|
|
* @package kalanis\RemoteRequest\Protocols |
12
|
|
|
* Properties for query to remote server - raw UDP |
13
|
|
|
*/ |
14
|
|
|
abstract class AProtocol |
15
|
|
|
{ |
16
|
|
|
use RemoteRequest\Traits\TLang; |
17
|
|
|
|
18
|
|
|
protected RemoteRequest\Connection\Processor $processor; |
19
|
|
|
protected RemoteRequest\Connection\Params\AParams $params; |
20
|
|
|
protected Dummy\Query $query; |
21
|
|
|
protected Dummy\Answer $answer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array<string, array<string, string>|string> $contextOptions |
25
|
|
|
* @param bool $long |
26
|
|
|
* @param RemoteRequest\Interfaces\IRRTranslations|null $lang |
27
|
|
|
*/ |
28
|
8 |
|
public function __construct(array $contextOptions = [], bool $long = false, ?RemoteRequest\Interfaces\IRRTranslations $lang = null) |
29
|
|
|
{ |
30
|
8 |
|
$this->setRRLang($lang); |
31
|
8 |
|
$pointer = empty($contextOptions) |
32
|
7 |
|
? ($long ? new RemoteRequest\Sockets\PfSocket($lang) : new RemoteRequest\Sockets\FSocket($lang)) |
33
|
8 |
|
: (new RemoteRequest\Sockets\Stream($lang))->setContextOptions($contextOptions) ; |
34
|
8 |
|
$this->processor = new RemoteRequest\Connection\Processor($pointer, $lang); |
35
|
8 |
|
$this->params = $this->loadParams(); |
36
|
8 |
|
$this->query = $this->loadQuery(); |
37
|
8 |
|
$this->answer = $this->loadAnswer(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
abstract protected function loadParams(): RemoteRequest\Connection\Params\AParams; |
41
|
|
|
|
42
|
|
|
abstract protected function loadQuery(): Dummy\Query; |
43
|
|
|
|
44
|
|
|
abstract protected function loadAnswer(): Dummy\Answer; |
45
|
|
|
|
46
|
7 |
|
public function getParams(): RemoteRequest\Connection\Params\AParams |
47
|
|
|
{ |
48
|
7 |
|
return $this->params; |
49
|
|
|
} |
50
|
|
|
|
51
|
7 |
|
public function getQuery(): Dummy\Query |
52
|
|
|
{ |
53
|
7 |
|
return $this->query; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @throws RemoteRequest\RequestException |
58
|
|
|
* @return Dummy\Answer |
59
|
|
|
* @codeCoverageIgnore because it's about querying remote machine |
60
|
|
|
*/ |
61
|
|
|
public function getAnswer(): Dummy\Answer |
62
|
|
|
{ |
63
|
|
|
$target = $this->query; |
64
|
|
|
if (empty($this->params->getHost()) |
65
|
|
|
&& ($target instanceof RemoteRequest\Interfaces\ITarget)) { |
66
|
|
|
$this->params->setTarget($target->getHost(), $target->getPort()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->answer->setResponse( |
70
|
|
|
$this->processor |
71
|
|
|
->setConnectionParams($this->params) |
72
|
|
|
->setData($this->query) |
73
|
|
|
->process() |
74
|
|
|
->getResponse() |
75
|
|
|
); |
76
|
|
|
return $this->answer; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|