Permalink
Browse files

Major refactoring, split text, logging and pairing concerns into apps

Decorator all the way, baby!
  • Loading branch information...
1 parent fc6b6d4 commit 797536713f31ea150ba73583f736b6e76145496b @igorw igorw committed Nov 4, 2012
View
@@ -2,21 +2,29 @@
// nc localhost 4000
+use React\Chatroulette\AppInterface;
+use React\Chatroulette\LoggingApp;
+use React\Chatroulette\PairApp;
+use React\Chatroulette\TextApp;
+
require 'vendor/autoload.php';
$logger = new Monolog\Logger('chatroulette');
$logger->pushHandler(new Monolog\Handler\StreamHandler(STDOUT));
-$chatroulette = new React\Chatroulette\Chatroulette($logger);
+$app = new LoggingApp(
+ new TextApp(new PairApp()),
+ $logger
+);
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$i = 0;
-$socket->on('connection', function ($conn) use (&$i, $chatroulette) {
+$socket->on('connection', function ($conn) use (&$i, $app) {
$conn->id = ++$i;
- $chatroulette->connect($conn);
+ $app->connect($conn);
});
$socket->listen(4000);
@@ -0,0 +1,10 @@
+<?php
+
+namespace React\Chatroulette;
+
+use React\Socket\ConnectionInterface;
+
+interface AppInterface
+{
+ function connect(ConnectionInterface $conn);
+}
@@ -1,46 +0,0 @@
-<?php
-
-namespace React\Chatroulette;
-
-use Monolog\Logger;
-use React\Socket\ConnectionInterface;
-
-class Chatroulette
-{
- private $logger;
- private $waiting;
-
- public function __construct(Logger $logger)
- {
- $this->logger = $logger;
- }
-
- public function connect(ConnectionInterface $conn)
- {
- $waiting = $this->waiting;
-
- $this->logger->info(sprintf("New connection %s", $conn->id));
- $conn->write(sprintf("Hello %s!\n", $conn->id));
-
- $conn->on('end', function () use ($conn) {
- $this->logger->info(sprintf("Connection %s disconnected", $conn->id));
- });
-
- if (null === $waiting || !$waiting->isReadable()) {
- $this->waiting = $conn;
- $conn->write("Please wait until a partner connects.\n");
- return;
- }
-
- $this->logger->info(sprintf("Pairing up connection %s with waiting connection %s",
- $conn->id, $waiting->id));
-
- $message = "You are now talking to %s.\n";
- $conn->write(sprintf($message, $waiting->id));
- $waiting->write(sprintf($message, $conn->id));
-
- $conn->pipe($waiting)->pipe($conn);
-
- $this->waiting = null;
- }
-}
@@ -0,0 +1,40 @@
+<?php
+
+namespace React\Chatroulette;
+
+use React\Socket\ConnectionInterface;
+use Monolog\Logger;
+
+class LoggingApp implements AppInterface
+{
+ private $app;
+ private $logger;
+
+ public function __construct(AppInterface $app, Logger $logger)
+ {
+ $this->app = $app;
+ $this->logger = $logger;
+ }
+
+ public function connect(ConnectionInterface $conn)
+ {
+ $this->logger->info(sprintf("New connection %s", $conn->id));
+
+ $conn->on('end', function () use ($conn) {
+ $this->logger->info(sprintf("Connection %s disconnected", $conn->id));
+ });
+
+ $conn->on('pipe', function ($source) use ($conn) {
+ if (!empty($conn->pipeLogged) || !empty($source->pipeLogged)) {
+ return;
+ }
+
+ $this->logger->info(sprintf("Pairing up connection %s with waiting connection %s",
+ $source->id, $conn->id));
+
+ $conn->pipeLogged = $conn->pipeLogged = true;
+ });
+
+ $this->app->connect($conn);
+ }
+}
@@ -0,0 +1,25 @@
+<?php
+
+namespace React\Chatroulette;
+
+use React\Socket\ConnectionInterface;
+
+class PairApp implements AppInterface
+{
+ private $waiting;
+
+ public function connect(ConnectionInterface $conn)
+ {
+ $waiting = $this->waiting;
+
+ if (null === $waiting || !$waiting->isReadable()) {
+ $this->waiting = $conn;
+ $conn->emit('wait');
+ return;
+ }
+
+ $conn->pipe($waiting)->pipe($conn);
+
+ $this->waiting = null;
+ }
+}
@@ -0,0 +1,31 @@
+<?php
+
+namespace React\Chatroulette;
+
+use React\Socket\ConnectionInterface;
+
+class TextApp implements AppInterface
+{
+ private $app;
+
+ public function __construct(AppInterface $app)
+ {
+ $this->app = $app;
+ }
+
+ public function connect(ConnectionInterface $conn)
+ {
+ $conn->write(sprintf("Hello %s!\n", $conn->id));
+
+ $conn->on('wait', function () use ($conn) {
+ $conn->write("Please wait until a partner connects.\n");
+ });
+
+ $conn->on('pipe', function ($source) use ($conn) {
+ $message = "You are now talking to %s.\n";
+ $conn->write(sprintf($message, $source->id));
+ });
+
+ $this->app->connect($conn);
+ }
+}
@@ -6,7 +6,7 @@
use Monolog\Handler\TestHandler;
use React\Socket\ConnectionInterface;
-class ChatrouletteTest extends \PHPUnit_Framework_TestCase
+class IntegrationTest extends \PHPUnit_Framework_TestCase
{
private $logger;
private $handler;
@@ -18,7 +18,10 @@ public function setUp()
$this->logger = new Logger('chatroulette');
$this->logger->pushHandler($this->handler);
- $this->chatroulette = new Chatroulette($this->logger);
+ $this->app = new LoggingApp(
+ new TextApp(new PairApp()),
+ $this->logger
+ );
}
/** @test */
@@ -27,7 +30,7 @@ public function connectingOneUserShouldMakeHimWait()
$alice = new ConnectionStub();
$alice->id = 'Alice';
- $this->chatroulette->connect($alice);
+ $this->app->connect($alice);
$this->assertConnectionData($alice, [
'Hello Alice!',
@@ -44,8 +47,8 @@ public function connectingTwoUsersShouldPairThemUp()
{
list($alice, $bob) = $this->createAliceAndBob();
- $this->chatroulette->connect($alice);
- $this->chatroulette->connect($bob);
+ $this->app->connect($alice);
+ $this->app->connect($bob);
$this->emitConnectionData($alice, 'Hallo Bob, wie geht es dir?');
$this->emitConnectionData($bob, 'Je ne comprends pas!');
@@ -75,8 +78,8 @@ public function disconnectingAliceShouldDisconnectBob()
{
list($alice, $bob) = $this->createAliceAndBob();
- $this->chatroulette->connect($alice);
- $this->chatroulette->connect($bob);
+ $this->app->connect($alice);
+ $this->app->connect($bob);
$alice->close();
@@ -89,8 +92,8 @@ public function disconnectingBobShouldDisconnectAlice()
{
list($alice, $bob) = $this->createAliceAndBob();
- $this->chatroulette->connect($alice);
- $this->chatroulette->connect($bob);
+ $this->app->connect($alice);
+ $this->app->connect($bob);
$bob->close();

0 comments on commit 7975367

Please sign in to comment.