|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Beanie\Server; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Beanie\Command\CommandInterface; |
|
8
|
|
|
use Beanie\Exception\SocketException; |
|
9
|
|
|
use Beanie\Manager; |
|
10
|
|
|
use Beanie\Tube\TubeAwareInterface; |
|
11
|
|
|
use Beanie\Tube\TubeStatus; |
|
12
|
|
|
|
|
13
|
|
|
class Server implements TubeAwareInterface |
|
14
|
|
|
{ |
|
15
|
|
|
const DEFAULT_PORT = 11300; |
|
16
|
|
|
const DEFAULT_HOST = '127.0.0.1'; |
|
17
|
|
|
|
|
18
|
|
|
const EOL = "\r\n"; |
|
19
|
|
|
const EOL_LENGTH = 2; |
|
20
|
|
|
|
|
21
|
|
|
/** @var Socket */ |
|
22
|
|
|
protected $socket; |
|
23
|
|
|
|
|
24
|
|
|
/** @var TubeStatus */ |
|
25
|
|
|
protected $tubeStatus; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $hostName |
|
29
|
|
|
* @param int $port |
|
30
|
|
|
* @throws SocketException |
|
31
|
|
|
*/ |
|
32
|
13 |
|
public function __construct($hostName = self::DEFAULT_HOST, $port = self::DEFAULT_PORT) |
|
33
|
|
|
{ |
|
34
|
13 |
|
$this->socket = new Socket($hostName, $port); |
|
35
|
12 |
|
$this->tubeStatus = new TubeStatus(); |
|
36
|
12 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Socket $socket |
|
40
|
|
|
* |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
5 |
|
public function setSocket(Socket $socket) |
|
44
|
|
|
{ |
|
45
|
5 |
|
$this->socket = $socket; |
|
46
|
5 |
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param int $bytes |
|
51
|
|
|
* @param int $extra |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
4 |
|
public function readData($bytes, $extra = self::EOL_LENGTH) |
|
56
|
|
|
{ |
|
57
|
4 |
|
$this->ensureConnected(); |
|
58
|
|
|
|
|
59
|
3 |
|
$data = $this->socket->readData($bytes + $extra); |
|
60
|
|
|
|
|
61
|
2 |
|
return substr($data, 0, $bytes); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param \Beanie\Command\CommandInterface $command |
|
66
|
|
|
* @return \Beanie\Server\ResponseOath |
|
67
|
|
|
* @throws SocketException |
|
68
|
|
|
* @throws \Beanie\Exception\BadFormatException |
|
69
|
|
|
* @throws \Beanie\Exception\InternalErrorException |
|
70
|
|
|
* @throws \Beanie\Exception\OutOfMemoryException |
|
71
|
|
|
* @throws \Beanie\Exception\UnknownCommandException |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function dispatchCommand(CommandInterface $command) |
|
74
|
|
|
{ |
|
75
|
2 |
|
$this->ensureConnected(); |
|
76
|
2 |
|
$this->socket->write($command->getCommandLine() . self::EOL); |
|
77
|
|
|
|
|
78
|
2 |
|
if ($command->hasData()) { |
|
79
|
1 |
|
$this->socket->write($command->getData() . self::EOL); |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
return new ResponseOath($this->socket, $this, $command); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return Manager |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function getManager() |
|
89
|
|
|
{ |
|
90
|
1 |
|
return new Manager($this); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string hostname:port |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function __toString() |
|
97
|
|
|
{ |
|
98
|
2 |
|
return sprintf('%s:%s', $this->socket->getHostname(), $this->socket->getPort()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @throws SocketException |
|
103
|
|
|
*/ |
|
104
|
3 |
|
public function connect() |
|
105
|
|
|
{ |
|
106
|
3 |
|
$this->socket->connect(); |
|
107
|
2 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @throws SocketException |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function disconnect() |
|
113
|
|
|
{ |
|
114
|
1 |
|
$this->socket->disconnect(); |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
6 |
|
protected function ensureConnected() |
|
118
|
|
|
{ |
|
119
|
6 |
|
if (!$this->socket->isConnected()) { |
|
120
|
3 |
|
$this->connect(); |
|
121
|
2 |
|
} |
|
122
|
5 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param TubeStatus $goal |
|
126
|
|
|
* @param int $mode |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function transformTubeStatusTo(TubeStatus $goal, $mode = TubeStatus::TRANSFORM_BOTH) |
|
130
|
|
|
{ |
|
131
|
1 |
|
foreach ($this->tubeStatus->transformTo($goal, $mode) as $transformCommand) { |
|
132
|
1 |
|
$this->dispatchCommand($transformCommand)->invoke(); |
|
133
|
1 |
|
} |
|
134
|
|
|
|
|
135
|
1 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return TubeStatus |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public function getTubeStatus() |
|
142
|
|
|
{ |
|
143
|
1 |
|
return $this->tubeStatus; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|