Exemplo n.º 1
0
$server->bind("tcp://127.0.0.1:5555");
/* Create new pollset for incoming/outgoing message */
$poll = new ZMQPoll();
/* Add the object and listen for poll in/out */
$id = $poll->add($server, ZMQ::POLL_IN | ZMQ::POLL_OUT);
echo "Added object with id " . $id . "\n";
/* Initialise readable and writable arrays */
$readable = array();
$writable = array();
while (true) {
    /* Amount of events retrieved */
    $events = 0;
    try {
        /* Poll until there is something to do */
        $events = $poll->poll($readable, $writable, -1);
        $errors = $poll->getLastErrors();
        if (count($errors) > 0) {
            foreach ($errors as $error) {
                echo "Error polling object " . $error . "\n";
            }
        }
    } catch (ZMQPollException $e) {
        echo "poll failed: " . $e->getMessage() . "\n";
    }
    if ($events > 0) {
        /* Loop through readable objects and recv messages */
        foreach ($readable as $r) {
            try {
                echo "Received message: " . $r->recv() . "\n";
            } catch (ZMQException $e) {
                echo "recv failed: " . $e->getMessage() . "\n";
Exemplo n.º 2
0
 /**
  * Must be started when already running as daemon.
  */
 public function start()
 {
     $this->registerSignals();
     // For SIGTERM handling
     $poll = new \ZMQPoll();
     foreach ($this->connections as $connection) {
         $connection->connect();
         $poll->add($connection->pull_socket, \ZMQ::POLL_IN);
     }
     // We are using polling to not block indefinitely and be able
     // to process the SIGTERM signal. The poll timeout is .5 second.
     $timeout = 500;
     $to_read = $to_write = array();
     $gc = gc_enabled();
     $i = 0;
     while (true) {
         $events = 0;
         try {
             $events = $poll->poll($to_read, $to_write, $timeout);
             $errors = $poll->getLastErrors();
             if (count($errors) > 0) {
                 foreach ($errors as $error) {
                     Log::error('Error polling object: ' . $error);
                 }
             }
         } catch (\ZMQPollException $e) {
             Log::fatal('Poll failed: ' . $e->getMessage());
             return 1;
         }
         if ($events > 0) {
             foreach ($to_read as $r) {
                 foreach ($this->connections as $connection) {
                     if ($connection->pull_socket === $r) {
                         $this->processRequest($connection);
                         break;
                     }
                 }
                 $i++;
             }
         }
         pcntl_signal_dispatch();
         if ($gc && 500 < $i) {
             $collected = gc_collect_cycles();
             Log::debug(array('photon.server.start', 'collected_cycles', $collected));
             $i = 0;
         }
     }
 }
Exemplo n.º 3
0
 public function run($task, $payload, $encode = true, $timeout = -1)
 {
     // Ensure a valid socket exist for this task
     if ($this->connectTask($task) === false) {
         return null;
     }
     if ($encode) {
         $payload = json_encode($payload);
     }
     // Send the message to the Task
     $mess = sprintf('%s %s %s', $task, $this->id, $payload);
     try {
         self::$sockets[$task]->send($mess, \ZMQ::MODE_NOBLOCK);
     } catch (\ZMQSocketException $e) {
         $this->disconnectTask($task);
         return null;
     }
     if ('async' === self::$types[$task]) {
         return true;
     }
     // Wait the answer
     $poll = new \ZMQPoll();
     $poll->add(self::$sockets[$task], \ZMQ::POLL_IN);
     if ($timeout !== -1) {
         $timeout = $timeout * 1000;
         // Convert timeout from seconds to ms
     }
     $to_read = $to_write = array();
     try {
         $events = $poll->poll($to_read, $to_write, $timeout);
         $errors = $poll->getLastErrors();
         if (count($errors) > 0) {
             foreach ($errors as $error) {
                 Log::error('Error polling object: ' . $error);
             }
         }
     } catch (\ZMQPollException $e) {
         Log::fatal('Poll failed: ' . $e->getMessage());
         $this->disconnectTask($task);
         return null;
     }
     // An answer has been received
     if ($events > 0) {
         list(, , $res) = explode(' ', self::$sockets[$task]->recv(), 3);
         return $encode ? json_decode($res) : $res;
     }
     return null;
 }