/** * Broadcast a message to a subset of nodes that fulfill a predicate. * * @param \Closure $predicate Predicate. Take a node in argument. * @param string $message Message. * @param … … … * @return void * @throws \Hoa\Exception\Group */ public function broadcastIf(\Closure $predicate, $message) { $connection = $this->getConnection(); $currentSocket = $this->getOriginalConnection()->getSocket(); $arguments = array_slice(func_get_args(), 2); array_unshift($arguments, $message, null); $callable = [$this, 'send']; $exceptions = new HoaException\Group('Message cannot be sent to some nodes.'); foreach ($connection->getNodes() as $node) { if (true === $predicate($node) && $node->getConnection()->getSocket() === $currentSocket) { $arguments[1] = $node; try { call_user_func_array($callable, $arguments); } catch (Socket\Exception $e) { $exceptions[$node->getId()] = $e; } } } if (0 < $exceptions->count()) { throw $exceptions; } return; }