public function case_open_with_context() { $self = $this; $this->given($this->mockGenerator->orphanize('__construct'), $server = new \Mock\Hoa\Socket\Server(), $streamName = 'foobar', $context = Stream\Context::getInstance('foo'), $flag = SUT::BIND | SUT::LISTEN, $oldMasters = $this->invoke($server)->getMasters(), $oldServers = $this->invoke($server)->getServers(), $oldStack = $this->invoke($server)->getStack(), $this->calling($server)->getFlag = $flag, $this->function->stream_socket_server = function ($_streamName, &$_errno, &$_errstr, $_flag, $_context) use($self, &$called, $streamName, $flag, $context) { $called = true; $self->string($_streamName)->isEqualTo($streamName)->integer($_flag)->isEqualTo($flag)->resource($_context)->isStreamContext()->isIdenticalTo($context->getContext()); return fopen(__FILE__, 'r'); })->when($result = $this->invoke($server)->_open($streamName, $context))->then->resource($result)->let($masters = $this->invoke($server)->getMasters())->integer(count($masters))->isEqualTo(count($oldMasters) + 1)->isEqualTo(1)->array($masters)->resource($masters[0])->isIdenticalTo($result)->let($servers = $this->invoke($server)->getServers())->integer(count($servers))->isEqualTo(count($oldServers) + 1)->isEqualTo(1)->array($servers)->object($servers[0])->isIdenticalTo($server)->let($stack = $this->invoke($server)->getStack())->integer(count($stack))->isEqualTo(count($oldStack) + 1)->isEqualTo(1)->array($stack)->resource($stack[0])->isIdenticalTo($result); }
/** * Open the stream and return the associated resource. * * @param string $streamName Socket URI. * @param \Hoa\Stream\Context $context Context. * @return resource * @throws \Hoa\Socket\Exception */ protected function &_open($streamName, Stream\Context $context = null) { if (null === $context) { $connection = @stream_socket_client($streamName, $errno, $errstr, $this->getTimeout(), $this->getFlag()); } else { $connection = @stream_socket_client($streamName, $errno, $errstr, $this->getTimeout(), $this->getFlag(), $context->getContext()); } if (false === $connection) { if ($errno === 0) { throw new Exception('Client cannot join %s.', 0, $streamName); } else { throw new Exception('Client returns an error (number %d): %s while trying ' . 'to join %s.', 1, [$errno, $errstr, $streamName]); } } $this->_stack[] = $connection; $id = $this->getNodeId($connection); $this->_node = Consistency\Autoloader::dnew($this->getNodeName(), [$id, $connection, $this]); $this->_nodes[$id] = $this->_node; return $connection; }
/** * Open the stream and return the associated resource. * * @access protected * @param string $streamName Stream name (e.g. path or URL). * @param \Hoa\Stream\Context $context Context. * @return resource * @throw \Hoa\File\Exception\FileDoesNotExist * @throw \Hoa\File\Exception */ protected function &_open($streamName, \Hoa\Stream\Context $context = null) { if (substr($streamName, 0, 4) == 'file' && false === is_dir(dirname($streamName))) { throw new Exception('Directory %s does not exist. Could not open file %s.', 0, array(dirname($streamName), basename($streamName))); } if (null === $context) { if (false === ($out = @fopen($streamName, $this->getMode(), true))) { throw new Exception('Failed to open stream %s.', 1, $streamName); } return $out; } $out = @fopen($streamName, $this->getMode(), true, $context->getContext()); if (false === $out) { throw new Exception('Failed to open stream %s.', 2, $streamName); } return $out; }
/** * Open the stream. * * @return \Hoa\Stream * @throws \Hoa\Stream\Exception */ public final function open() { $context = $this->_context; if (true === $this->_hasBeenDiffered) { if (null === $context) { $handle = Context::getInstance(uniqid()); $handle->setParameters(['notification' => [$this, '_notify']]); $context = $handle->getId(); } elseif (true === Context::contextExists($context)) { $handle = Context::getInstance($context); $parameters = $handle->getParameters(); if (!isset($parameters['notification'])) { $handle->setParameters(['notification' => [$this, '_notify']]); } } } $this->_bucket = self::_getStream($this->_streamName, $this, $context); return $this; }
/** * Open the stream and return the associated resource. * * @param string $streamName Socket URI. * @param \Hoa\Stream\Context $context Context. * @return resource * @throws \Hoa\Socket\Exception */ protected function &_open($streamName, Stream\Context $context = null) { if (null === $context) { $this->_master = @stream_socket_server($streamName, $errno, $errstr, $this->getFlag()); } else { $this->_master = @stream_socket_server($streamName, $errno, $errstr, $this->getFlag(), $context->getContext()); } if (false === $this->_master) { throw new Exception('Server cannot join %s and returns an error (number %d): %s.', 1, [$streamName, $errno, $errstr]); } $i = count($this->_masters); $this->_masters[$i] = $this->_master; $this->_servers[$i] = $this; $this->_stack[] = $this->_masters[$i]; return $this->_master; }
/** * Create a directory. * * @param string $name Directory name. * @param string $mode Create mode. Please, see the self::MODE_CREATE* * constants. * @param string $context Context ID (please, see the * \Hoa\Stream\Context class). * @return bool * @throws \Hoa\File\Exception */ public static function create($name, $mode = self::MODE_CREATE_RECURSIVE, $context = null) { if (true === is_dir($name)) { return true; } if (empty($name)) { return false; } if (null !== $context) { if (false === Stream\Context::contextExists($context)) { throw new Exception('Context %s was not previously declared, cannot retrieve ' . 'this context.', 2, $context); } else { $context = Stream\Context::getInstance($context); } } if (null === $context) { return @mkdir($name, 0755, self::MODE_CREATE_RECURSIVE === $mode); } return @mkdir($name, 0755, self::MODE_CREATE_RECURSIVE === $mode, $context->getContext()); }
public function case_open_with_context() { $self = $this; $this->given($this->mockGenerator->orphanize('__construct'), $client = new \Mock\Hoa\Socket\Client(), $streamName = 'foobar', $context = Stream\Context::getInstance('foo'), $timeout = 42, $flag = SUT::CONNECT, $oldStack = $this->invoke($client)->getStack(), $oldNodes = $client->getNodes(), $this->calling($client)->getTimeout = $timeout, $this->calling($client)->getFlag = $flag, $this->function->stream_socket_client = function ($_streamName, &$_errno, &$_errstr, $_timeout, $_flag, $_context) use($self, &$called, $streamName, $timeout, $flag, $context) { $called = true; $self->string($_streamName)->isEqualTo($streamName)->integer($_timeout)->isEqualTo($timeout)->integer($_flag)->isEqualTo($flag)->resource($_context)->isStreamContext()->isIdenticalTo($context->getContext()); return fopen(__FILE__, 'r'); })->when($result = $this->invoke($client)->_open($streamName, $context))->then->resource($result)->let($stack = $this->invoke($client)->getStack())->integer(count($stack))->isEqualTo(count($oldStack) + 1)->isEqualTo(1)->array($stack)->resource($stack[0])->isIdenticalTo($result)->let($node = $client->getCurrentNode())->object($node)->isInstanceOf('Hoa\\Socket\\Node')->string($node->getId())->isEqualTo($this->invoke($client)->getNodeId($result))->resource($node->getSocket())->isIdenticalTo($result)->object($node->getConnection())->isIdenticalTo($client)->let($nodes = $client->getNodes())->integer(count($nodes))->isEqualTo(count($oldNodes) + 1)->array($nodes)->object($nodes[$node->getId()])->isIdenticalTo($node); }