<commit_before><?php

namespace Sockets;

use React\Promise\When;

use React\Promise\Deferred;
use React\EventLoop\LoopInterface;
use Socket\Raw\Factory as RawFactory;
use \Exception;

class Factory
{
    private $loop;
    private $rawFactory;
    private $poller = null;

    public function __construct(LoopInterface $loop)
    {
        $this->loop = $loop;
        $this->rawFactory = new RawFactory();
    }

    /**
     * create stream client socket connected to given address
     *
     * @param string $address
     * @return \Sockets\Stream
     * @throws Exception on error
     * @uses RawFactory::createClient()
     */
    public function createClient($address)
    {
        $socket = $this->rawFactory->createClient($address);
        if ($socket->getType() !== SOCK_STREAM) {
            $socket->close();
            throw new Exception('Not a stream address scheme');
        }
        return new Stream($socket, $this->getPoller());
    }

    /**
     * create stream server socket bound to and listening on the given address for incomming stream client connections
     *
     * @param string $address
     * @return \Sockets\Server
     * @throws Exception on error
     * @uses Server::listenAddress()
     */
    public function createServer($address)
    {
        $server = new Server($this->getPoller(), $this->rawFactory);
        $server->listenAddress($address);

        return $server;
    }

    public function createUdp4()
    {
        return new Datagram($this->rawFactory->createUdp4(), $this->getPoller());
    }

    public function createUdp6()
    {
        return new Datagram($this->rawFactory->createUdp6(), $this->getPoller());
    }

    public function createUdg()
    {
        return new Datagram($this->rawFactory->createUdg(), $this->getPoller());
    }

    /**
     *
     * @return SelectPoller
     */
    public function getPoller()
    {
        if ($this->poller === null) {
            $this->poller = new SelectPoller($this->loop);
        }
        return $this->poller;
    }
}
<commit_msg>Use Promises to prepare for async DNS resolution<commit_after><?php

namespace Sockets;

use React\Promise\When;

use React\Promise\Deferred;
use React\EventLoop\LoopInterface;
use Socket\Raw\Factory as RawFactory;
use \Exception;

class Factory
{
    private $loop;
    private $rawFactory;
    private $poller = null;

    public function __construct(LoopInterface $loop)
    {
        $this->loop = $loop;
        $this->rawFactory = new RawFactory();
    }

    /**
     * create stream client socket connected to given address
     *
     * @param string $address
     * @return PromiseInterface to return a \Sockets\Stream
     * @throws Exception on error
     * @uses RawFactory::createClient()
     */
    public function createClient($address)
    {
        $factory = $this->rawFactory;
        $that = $this;
        return $this->resolve($address)->then(function ($address) use ($factory, $that) {
            $socket = $factory->createClient($address);
            if ($socket->getType() !== SOCK_STREAM) {
                $socket->close();
                throw new Exception('Not a stream address scheme');
            }
            return new Stream($socket, $that->getPoller());
        });
    }

    /**
     * create stream server socket bound to and listening on the given address for incomming stream client connections
     *
     * @param string $address
     * @return PromiseInterface to return a \Sockets\Server
     * @throws Exception on error
     * @uses Server::listenAddress()
     */
    public function createServer($address)
    {
        return $this->resolve($address)->then(function ($address) {
            $server = new Server($this->getPoller(), $this->rawFactory);
            $server->listenAddress($address);

            return $server;
        });
    }

    public function createUdp4()
    {
        return new Datagram($this->rawFactory->createUdp4(), $this->getPoller());
    }

    public function createUdp6()
    {
        return new Datagram($this->rawFactory->createUdp6(), $this->getPoller());
    }

    public function createUdg()
    {
        return new Datagram($this->rawFactory->createUdg(), $this->getPoller());
    }

    /**
     *
     * @return SelectPoller
     */
    public function getPoller()
    {
        if ($this->poller === null) {
            $this->poller = new SelectPoller($this->loop);
        }
        return $this->poller;
    }

    /**
     * resolve given address via DNS if applicable
     *
     * Letting host names pass through will not break things, but it
     * requires a blocking resolution afterwards. So make sure to try to
     * resolve hostnames here.
     *
     * @param string $address
     * @return PromiseInterface
     * @todo use Resolver to perform async resolving
     */
    private function resolve($address)
    {
        return When::resolve($address);
    }
}

{'fraction_non_alphanumeric': 0.12256888168557536, 'fraction_numerical': 0.0016207455429497568, 'mean_word_length': 2.3954607977991746, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '189571', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1351, 'n_words': 365}