Test Failed
Push — master ( 68289f...b25dc9 )
by huang
03:32
created

ManagerServer::doWork()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 6
nop 4
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @link      https://www.github.com/janhuang
7
 * @link      http://www.fast-d.cn/
8
 */
9
10
namespace FastD\Servitization\Server;
11
12
13
use FastD\Application;
14
use FastD\Pool\PoolInterface;
15
use FastD\Swoole\Server\TCP;
16
use swoole_server;
17
18
/**
19
 * Class MonitorStatusServer
20
 * @package FastD\Servitization\Server
21
 */
22
class ManagerServer extends TCP
23
{
24
    /**
25
     * @param swoole_server $server
26
     * @param int $worker_id
27
     */
28
    public function onWorkerStart(swoole_server $server, $worker_id)
29
    {
30
        parent::onWorkerStart($server, $worker_id);
31
32
        foreach (app() as $service) {
33
            if ($service instanceof PoolInterface) {
34
                $service->initPool();
35
            }
36
        }
37
    }
38
39
    /**
40
     * @param swoole_server $server
41
     * @param $fd
42
     * @param $from_id
43
     */
44
    public function doConnect(swoole_server $server, $fd, $from_id)
45
    {
46
        $server->send($fd, sprintf('server: %s %s', app()->getName(), Application::VERSION) . PHP_EOL);
47
    }
48
49
    /**
50
     * @param swoole_server $server
51
     * @param $fd
52
     * @param $data
53
     * @param $from_id
54
     * @return mixed
55
     */
56
    public function doWork(swoole_server $server, $fd, $data, $from_id)
57
    {
58
        switch (trim($data)) {
59
            case 'quit':
60
                $server->send($fd, 'connection closed');
61
                $server->close($fd);
62
                break;
63
            case 'reload':
64
                $this->getSwoole()->reload();
65
                break;
66
            case 'status':
67
            default:
68
                $info = $server->stats();
69
                $status = '';
70
                foreach ($info as $key => $value) {
71
                    $status .= "[" .date('Y-m-d H:i:s'). "]: " . $key . ': ' . $value . PHP_EOL;
72
                }
73
                $server->send($fd, $status);
74
                break;
75
        }
76
    }
77
}