Created
May 5, 2011 13:20
-
-
Save anonymous/957007 to your computer and use it in GitHub Desktop.
Problem with the php-zmq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Checker | |
{ | |
private $host; | |
private $port; | |
private $context; | |
private $poll; | |
public function __construct($host = "localhost", $port = 10101) | |
{ | |
$this->host = $host; | |
$this->port = $port; | |
$this->context = new ZMQContext(); | |
$this->poll = new ZMQPoll(); | |
} | |
public function check($ip, $service) | |
{ | |
$message = $service . '|' . $ip; | |
try | |
{ | |
$sock = new ZMQSocket($this->context, ZMQ::SOCKET_REQ); | |
$sock->connect('tcp://'.$this->host.':'.$this->port); | |
$sock->send($message); | |
$readable = array(); | |
$writeable = array(); | |
$sockId = $this->poll->add($sock, 1); | |
$this->poll->poll($readable, $writeable, 1000); | |
if(count($readable)<1) | |
{ | |
$this->poll->clear(); | |
throw new Exception("Server is too slow"); | |
} | |
$result = intval($readable[0]->recv()); | |
$this->poll->clear(); | |
} | |
catch (Exception $e) | |
{ | |
//print "Error: ".$e->getMessage() ."\n"; | |
return false; | |
} | |
if($result < 0) | |
throw new Exception("malformed IP or service"); | |
return (bool)$result; | |
} | |
} | |
$checker = new Checker(); | |
var_dump($checker->check('someip', 'someservice')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment