Skip to content

Instantly share code, notes, and snippets.

Created May 5, 2011 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/957007 to your computer and use it in GitHub Desktop.
Save anonymous/957007 to your computer and use it in GitHub Desktop.
Problem with the php-zmq
<?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