GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 31696d...d014e0 )
by Stuart
05:40
created

UsingZmqContext::bindToPort()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 16

Duplication

Lines 5
Ratio 17.24 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 5
loc 29
rs 8.8571
cc 3
eloc 16
nc 3
nop 4
1
<?php
2
3
/**
4
 * Copyright (c) 2011-present Mediasift Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   Storyplayer/Modules/ZeroMQ
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2011-present Mediasift Ltd www.datasift.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://datasift.github.io/storyplayer
42
 */
43
44
namespace Storyplayer\SPv2\Modules\ZeroMQ;
45
46
use Prose\Prose;
47
use Storyplayer\SPv2\Modules\Exceptions;
48
use Storyplayer\SPv2\Modules\Host;
49
use Storyplayer\SPv2\Modules\Log;
50
use ZMQ;
51
use ZMQContext;
52
use ZMQSocket;
53
use DataSift\Storyplayer\PlayerLib\StoryTeller;
54
use DataSift\Stone\DataLib\DataPrinter;
55
56
/**
57
 * create a ZMQ socket for sending or receiving data
58
 *
59
 * @category  Libraries
60
 * @package   Storyplayer/Modules/ZeroMQ
61
 * @author    Stuart Herbert <[email protected]>
62
 * @copyright 2011-present Mediasift Ltd www.datasift.com
63
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
64
 * @link      http://datasift.github.io/storyplayer
65
 */
66
class UsingZmqContext extends Prose
67
{
68
    protected $socketMap = [
69
        "PUB"    => ZMQ::SOCKET_PUB,
70
        "SUB"    => ZMQ::SOCKET_SUB,
71
        "REQ"    => ZMQ::SOCKET_REQ,
72
        "REP"    => ZMQ::SOCKET_REP,
73
        "XREQ"   => ZMQ::SOCKET_XREQ,
74
        "XREP"   => ZMQ::SOCKET_XREP,
75
        "PUSH"   => ZMQ::SOCKET_PUSH,
76
        "PULL"   => ZMQ::SOCKET_PULL,
77
        "ROUTER" => ZMQ::SOCKET_ROUTER,
78
        "DEALER" => ZMQ::SOCKET_DEALER,
79
    ];
80
81
    public function __construct(Storyteller $st, $params = [])
82
    {
83
        // make sure we have a ZMQContext
84
        //
85
        // $params[0] is null when we need to create a ZMQContext
86
        if (!isset($params[0])) {
87
            $params[0] = new ZMQContext();
88
        }
89
90
        // now we're ready to call the parent constructor
91
        parent::__construct($st, $params);
92
    }
93
94
    public function getZmqContext()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
95
    {
96
        // what are we doing?
97
        $log = Log::usingLog()->startAction("get a ZMQContext object");
98
99
        // all done
100
        $log->endAction();
101
        return $this->args[0];
102
    }
103
104
    public function bindToPort($port, $socketType, $sendHwm = 100, $recvHwm = 100)
105
    {
106
        // what are we doing?
107
        $log = Log::usingLog()->startAction("bind() to ZMQ tcp '{$socketType}' socket at host 'localhost':{$port}");
108
109
        // do we have a supported socket?
110 View Code Duplication
        if (!isset($this->socketMap[$socketType])) {
111
            $msg = "unknown ZMQ socket type '{$socketType}'";
112
            $log->endAction($msg);
113
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
114
        }
115
116
        // make the connection
117
        $socket = new ZMQSocket($this->args[0], $this->socketMap[$socketType]);
118
        if (!$socket) {
119
            $msg = "unable to create ZMQ socket";
120
            $log->endAction($msg);
121
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
122
        }
123
        $socket->bind("tcp://*:{$port}");
124
125
        // set high-water marks now
126
        $socket->setSockOpt(ZMQ::SOCKOPT_SNDHWM, $sendHwm);
127
        $socket->setSockOpt(ZMQ::SOCKOPT_RCVHWM, $recvHwm);
128
129
        // all done
130
        $log->endAction();
131
        return $socket;
132
    }
133
134
    public function connectToHost($hostId, $port, $socketType, $sendHwm = 100, $recvHwm = 100)
135
    {
136
        // what are we doing?
137
        $log = Log::usingLog()->startAction("connect() to ZMQ '{$socketType}' socket on host '{$hostId}':{$port}");
138
139
        // do we have a supported socket?
140 View Code Duplication
        if (!isset($this->socketMap[$socketType])) {
141
            $msg = "unknown ZMQ socket type '{$socketType}'";
142
            $log->endAction($msg);
143
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
144
        }
145
146
        // where are we connecting to?
147
        $ipAddress = Host::fromHost($hostId)->getIpAddress();
148
149
        // create the socket
150
        $socket = new ZMQSocket($this->args[0], $this->socketMap[$socketType]);
151
        if (!$socket) {
152
            $msg = "unable to create ZMQ socket";
153
            $log->endAction($msg);
154
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
155
        }
156
157
        // set high-water marks now
158
        $socket->setSockOpt(ZMQ::SOCKOPT_SNDHWM, $sendHwm);
159
        $socket->setSockOpt(ZMQ::SOCKOPT_RCVHWM, $recvHwm);
160
161
        // make the connection
162
        //
163
        // NOTE: we use the 'force' parameter here to avoid Storyplayer
164
        // hanging if the remote end is not available
165
        $socket->connect("tcp://{$ipAddress}:{$port}", true);
166
167
        // all done
168
        $log->endAction();
169
        return $socket;
170
    }
171
}
172