Пример #1
0
 public function __construct(EndlessSubject $observable = null, Dns $dns = null)
 {
     $this->loop = EventLoop::getLoop();
     $this->observable = $observable ?: new EndlessSubject();
     $this->subscribe($this->observable);
     $this->dns = $dns ?: new Dns();
 }
Пример #2
0
 public static function getLoop()
 {
     if (static::$loop === null) {
         static::$loop = EventLoop::getLoop();
     }
     return static::$loop;
 }
Пример #3
0
 public function __construct(\Iterator $iterator, ReplaySubject $replaySubject = null, VirtualTimeScheduler $scheduler = null)
 {
     $this->iterator = $iterator;
     $scheduler = $scheduler ?: new EventLoopScheduler(EventLoop::getLoop());
     $this->obs = $replaySubject ?: new ReplaySubject(null, null, $scheduler);
     $this->completed = false;
 }
Пример #4
0
 /**
  * RabbitMq constructor.
  * @param $cfg
  * @param Serializer|null $serializer
  */
 public function __construct($cfg, Serializer $serializer = null)
 {
     $this->loop = EventLoop::getLoop();
     $this->serializer = $serializer ?: new MsgPack();
     if (is_string($cfg)) {
         $cfg = parse_url($cfg);
         $cfg['vhost'] = $cfg['path'];
     }
     $this->cfg = $cfg;
 }
Пример #5
0
 public function __construct(Udp $connector = null, EndlessSubject $subject = null)
 {
     $this->connector = $connector ?: new Udp(EventLoop::getLoop());
     $this->observable = $subject ?: new EndlessSubject();
     $this->decoder = (new DecoderFactory())->create();
     $this->question = new QuestionFactory();
     $this->message = new MessageFactory();
     $this->encoder = (new EncoderFactory())->create();
     $this->cache = [];
     $this->parseEtcHost();
 }
Пример #6
0
 private static function registerLoopRunner()
 {
     $hasBeenRun = false;
     register_shutdown_function(function () use(&$hasBeenRun) {
         if (!$hasBeenRun) {
             EventLoop::getLoop()->run();
         }
     });
     static::$loop->nextTick(function () use(&$hasBeenRun) {
         $hasBeenRun = true;
     });
 }
Пример #7
0
 /**
  * {@inheritdoc}
  */
 public function write($data = null)
 {
     $req = new UDPRequest($data, [], true);
     return $this->dns->resolve($this->host)->flatMap(function ($ip) {
         return $this->connector->connect($ip, $this->port);
     })->flatMap(function (ConnectorEvent $event) use($req) {
         /** @var Datagram $stream */
         $stream = $event->getStream();
         $stream->subscribe($req);
         $stream->subscribeCallback(null, [$stream, 'close'], [$stream, 'close'], new EventLoopScheduler(EventLoop::getLoop()));
         $stream->write($req->data, null, false);
         return $req;
     });
 }
Пример #8
0
 public function dispose()
 {
     if (!$this->stream instanceof Stream) {
         parent::dispose();
         return;
     }
     if ($socket = $this->stream->getSocket()) {
         EventLoop::getLoop()->removeReadStream($socket);
         if (is_resource($socket)) {
             @fclose($socket);
         }
     }
     parent::dispose();
 }
Пример #9
0
 public static function getLoop()
 {
     return EventLoop::getLoop();
 }
Пример #10
0
/**
 * @return \React\EventLoop\LibEventLoop
 */
function getLoop()
{
    return EventLoop::getLoop();
}
Пример #11
0
 public function __construct()
 {
     $this->loop = EventLoop::getLoop();
 }
Пример #12
0
<?php

describe("ReactiveX Bunny client", function () {
    it("Connects to bunny", function () {
        $loop = \EventLoop\EventLoop::getLoop();
        $mq = new \Rxnet\RabbitMq\RabbitMq(['host' => '127.0.0.1', 'port' => 5672, 'user' => 'guest', 'password' => 'guest']);
        $cnx = $mq->connect()->retryWhen(function ($errors) {
            return $errors->delay(2000)->doOnNext(function () {
                echo "Disconnected, retry\n";
            });
        });
        \Rxnet\await($cnx);
        $mq->consume("test", 2)->delay(1000, new \Rx\Scheduler\EventLoopScheduler($loop))->subscribeCallback(function (\Rxnet\RabbitMq\RabbitMessage $message) {
            echo "message {$message->getData()}\n";
            $message->ack();
            //var_dump(func_get_args());
        });
        $loop->run();
    });
});
Пример #13
0
 /**
  * @param StreamEvent $event
  */
 public function onNext($event)
 {
     if ($event instanceof ConnectorEvent) {
         $this->__invoke($event);
         return;
     }
     $stream = $this->connectorEvent->getStream();
     if (!stristr($event->data, 'HTTP/1.1 200 Connection established')) {
         $stream->removeObserver($this);
         $this->notifyError(new \UnexpectedValueException("Proxy connection failed {$event->data}"));
         return;
     }
     if (!$this->toggleCrypto) {
         $stream->removeObserver($this);
         foreach ($this->observers as $observer) {
             $observer->onNext($this->getConnectorEvent());
         }
         return;
     }
     // Enable crypto
     $socket = $stream->getSocket();
     $loop = EventLoop::getLoop();
     // Stop event loop for this socket
     $loop->removeReadStream($socket);
     $method = STREAM_CRYPTO_METHOD_TLS_CLIENT;
     if (defined('STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT')) {
         $method |= STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT;
     }
     if (defined('STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT')) {
         $method |= STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
     }
     if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
         $method |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
     }
     // Stop listening on proxy connect
     $stream->removeObserver($this);
     //stream_set_blocking ($socket, true);
     set_error_handler(function ($errno, $errstr) {
         $this->notifyError(new \UnexpectedValueException("Failed to enable crypto ({$errno}) : {$errstr} "));
     });
     // Wait until handshake is finished
     while (true) {
         $res = stream_socket_enable_crypto($socket, true, $method);
         if ($res === true) {
             // Handshake ok, time to pass the hand to the request
             $loop->removeReadStream($socket);
             foreach ($this->observers as $observer) {
                 /* @var \Rx\ObserverInterface $observer */
                 $observer->onNext($this->getConnectorEvent());
             }
             break;
         } elseif ($res === false) {
             // handshake failed
             // Error handler should have done the work
             break;
         } else {
             // let's loop until handshake is ok
             $loop->tick();
         }
     }
     restore_error_handler();
 }
Пример #14
0
use Rxnet\Routing\RoutableSubject;
require __DIR__ . "/../../vendor/autoload.php";
$loop = EventLoop::getLoop();
$scheduler = new EventLoopScheduler($loop);
$rabbit = new \Rxnet\RabbitMq\RabbitMq('rabbit://*****:*****@127.0.0.1:5672/', new \Rxnet\Serializer\Serialize());
// Wait for rabbit to be connected before starting
\Rxnet\awaitOnce($rabbit->connect());
$queue = $rabbit->queue('test_queue', 'amq.direct', []);
$exchange = $rabbit->exchange('amq.direct');
$debug = new StdOutObserver();
// Consume with a specified id
$queue->consume('consumer-2')->subscribeCallback(function (RabbitMessage $subject) use($debug) {
    // Everything that append will be to my logger
    $subject->subscribe($debug);
    // Give 30s to handle the subject or reject it to bottom (with all its changes)
    $subject->timeout(30 * 1000)->subscribeCallback(null, function ($e) use($subject) {
        echo "#";
        $subject->rejectToBottom();
    }, function () use($subject) {
        echo ".";
        $subject->ack();
    }, new EventLoopScheduler(EventLoop::getLoop()));
    // send the subject to an event dispatcher
    $subject->onNext(new Event("I've done this"));
    $subject->onNext(new Event("Pass me to many handlers", ['with' => 'data'], ['some' => 'labels']));
    // When an handler wants, it can complete and message will be acknowledged
    $subject->onCompleted();
    // Or throw to stop the given message and add it back to bottom
    //$subject->onError(new Exception("Doesn't work"));
});
$loop->run();
Пример #15
0
 public function __construct(LoopInterface $loop = null)
 {
     $this->loop = $loop ?: EventLoop::getLoop();
 }
Пример #16
0
<?php

use EventLoop\EventLoop;
use Rxnet\Observer\StdOutObserver;
use Rxnet\RabbitMq\RabbitMessage;
require __DIR__ . "/../../vendor/autoload.php";
$loop = EventLoop::getLoop();
$redis = new \Rxnet\Redis\Redis();
$redis->connect('localhost:6379')->doOnNext(function () {
    echo "Redis is connected\n";
})->subscribeCallback(function () use($redis, $loop) {
    // produce this array to my queue
    \Rx\Observable::fromArray(['coucou', 'hi'])->flatMap(function ($data) use($redis) {
        // add to set
        return $redis->sAdd('my_set', $data);
    })->doOnNext(function () {
        echo "Data added\n";
    })->flatMap(function () use($redis) {
        return $redis->sRandMember('my_set', 1);
    })->map(function ($data) {
        $data = $data[0];
        return $data;
    })->subscribeCallback(function ($data) use($redis) {
        echo "Read data {$data} add it to key\n";
        return $redis->set($data, 1);
    }, null, function () {
        // And close process
        EventLoop::getLoop()->stop();
    });
});
$loop->run();
Пример #17
0
<?php

use EventLoop\EventLoop;
use Rxnet\Operator\RetryWithDelay;
require __DIR__ . "/../../vendor/autoload.php";
$loop = EventLoop::getLoop();
$scheduler = new \Rx\Scheduler\EventLoopScheduler($loop);
$http = new \Rxnet\Http\Http();
// Simple query
$http->get("http://www.perdu.com")->subscribeCallback(function (\GuzzleHttp\Psr7\Response $response) {
    echo "\nLost :\n" . html_entity_decode($response->getBody()) . "\n";
});
// Handling errors
$http->get("http://www.thisdomaindoesnotexistforreal.com")->doOnError(function (\Exception $e) {
    printf("[%s]Can't find this domain : %s\n", date('H:i:s'), $e->getMessage());
})->retryWhen(new RetryWithDelay(4, 1000))->subscribeCallback(null, function (\Exception $e) {
    printf("[%s]Definitly can't find this domain : %s\n", date('H:i:s'), $e->getMessage());
}, null, $scheduler);
Пример #18
0
 /**
  * Httpd constructor.
  * @param Server $io
  * @param EndlessSubject $observable
  */
 public function __construct(Server $io = null, EndlessSubject $observable = null)
 {
     $this->io = $io ?: new Server(EventLoop::getLoop());
     $this->observable = $observable ?: new EndlessSubject();
     $this->subscribe($this->observable);
 }