Skip to content

Commit

Permalink
Added more middleware examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
andot committed Aug 7, 2016
1 parent 525cf78 commit be79589
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/src/middleware/CacheHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

class CacheHandler {
private $cache = array();
function handle($name, array &$args, stdClass $context, Closure $next) {
if (isset($context->userdata->cache)) {
$key = hprose_serialize($args);
if (isset($this->cache[$name])) {
if (isset($this->cache[$name][$key])) {
return $this->cache[$name][$key];
}
}
else {
$this->cache[$name] = array();
}
$result = $next($name, $args, $context);
$this->cache[$name][$key] = $result;
return $result;
}
return $next($name, $args, $context);
}
}
16 changes: 16 additions & 0 deletions examples/src/middleware/CacheHandler2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class CacheHandler2 {
private $cache = array();
function handle($request, stdClass $context, Closure $next) {
if (isset($context->userdata->cache)) {
if (isset($this->cache[$request])) {
return $this->cache[$request];
}
$response = $next($request, $context);
$this->cache[$request] = $response;
return $response;
}
return $next($request, $context);
}
}
19 changes: 19 additions & 0 deletions examples/src/middleware/SizeHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class SizeHandler {
private $message;
public function __construct($message) {
$this->message = $message;
}
public function asynchandle($request, stdClass $context, Closure $next) {
error_log($this->message . ' request size: ' . strlen($request));
$response = (yield $next($request, $context));
error_log($this->message . ' response size: ' . strlen($response));
}
public function synchandle($request, stdClass $context, Closure $next) {
error_log($this->message . ' request size: ' . strlen($request));
$response = $next($request, $context);
error_log($this->message . ' response size: ' . strlen($response));
return $response;
}
}
21 changes: 21 additions & 0 deletions examples/src/middleware/StatHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

class StatHandler {
private $message;
public function __construct($message) {
$this->message = $message;
}
public function asynchandle($request, stdClass $context, Closure $next) {
$start = microtime(true);
yield $next($request, $context);
$end = microtime(true);
error_log($this->message . ': It takes ' . ($end - $start) . 'ms.');
}
public function synchandle($request, stdClass $context, Closure $next) {
$start = microtime(true);
$response = $next($request, $context);
$end = microtime(true);
error_log($this->message . ': It takes ' . ($end - $start) . 'ms.');
return $response;
}
}
16 changes: 16 additions & 0 deletions examples/src/middleware/exam3/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
require_once "../../../vendor/autoload.php";
require_once '../logHandler.php';
require_once '../CacheHandler.php';

use Hprose\Client;
use Hprose\InvokeSettings;

$cacheSettings = new InvokeSettings(array("userdata" => array("cache" => true)));
$client = Client::create('tcp://127.0.0.1:1143/', false);
$client->addInvokeHandler(array(new CacheHandler(), 'handle'));
$client->addInvokeHandler($logHandler);
var_dump($client->hello("cache world", $cacheSettings));
var_dump($client->hello("cache world", $cacheSettings));
var_dump($client->hello("no cache world"));
var_dump($client->hello("no cache world"));
16 changes: 16 additions & 0 deletions examples/src/middleware/exam3/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
require_once "../../../vendor/autoload.php";
require_once '../logHandler.php';

use Hprose\Socket\Server;

function hello($name) {
return "Hello $name!";
}

$server = new Server('tcp://0.0.0.0:1143/');
$server->addFunction('hello');
$server->debug = true;
$server->addInvokeHandler($logHandler);
$server->start();

9 changes: 9 additions & 0 deletions examples/src/middleware/exam4/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
require_once "../../../vendor/autoload.php";
require_once '../logHandler2.php';

use Hprose\Client;

$client = Client::create('tcp://127.0.0.1:1143/', false);
$client->addBeforeFilterHandler($logHandler2);
var_dump($client->hello("world"));
16 changes: 16 additions & 0 deletions examples/src/middleware/exam4/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
require_once "../../../vendor/autoload.php";
require_once '../logHandler2.php';

use Hprose\Socket\Server;

function hello($name) {
return "Hello $name!";
}

$server = new Server('tcp://0.0.0.0:1143/');
$server->addFunction('hello');
$server->debug = true;
$server->addBeforeFilterHandler($logHandler2);
$server->start();

24 changes: 24 additions & 0 deletions examples/src/middleware/exam5/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
require_once "../../../vendor/autoload.php";
require_once '../../filter/CompressFilter.php';
require_once '../SizeHandler.php';
require_once '../StatHandler.php';
require_once '../CacheHandler2.php';

use Hprose\Client;
use Hprose\InvokeSettings;

$cacheSettings = new InvokeSettings(array("userdata" => array("cache" => true)));

$client = Client::create('tcp://127.0.0.1:1143/', false);
$client->addBeforeFilterHandler(array(new CacheHandler2(), 'handle'))
->addBeforeFilterHandler(array(new StatHandler('BeforeFilter'), 'synchandle'))
->addBeforeFilterHandler(array(new SizeHandler('Non compressed'), 'synchandle'))
->addFilter(new CompressFilter())
->addAfterFilterHandler(array(new StatHandler('AfterFilter'), 'synchandle'))
->addAfterFilterHandler(array(new SizeHandler('compressed'), 'synchandle'));

$value = range(0, 99999);
var_dump(count($client->echo($value, $cacheSettings)));
var_dump(count($client->echo($value, $cacheSettings)));

16 changes: 16 additions & 0 deletions examples/src/middleware/exam5/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
require_once "../../../vendor/autoload.php";
require_once '../../filter/CompressFilter.php';
require_once '../SizeHandler.php';
require_once '../StatHandler.php';

use Hprose\Socket\Server;

$server = new Server('tcp://0.0.0.0:1143/');
$server->addFunction(function($value) { return $value; }, 'echo')
->addBeforeFilterHandler(array(new StatHandler("BeforeFilter"), 'asynchandle'))
->addBeforeFilterHandler(array(new SizeHandler("compressedr"), 'asynchandle'))
->addFilter(new CompressFilter())
->addAfterFilterHandler(array(new StatHandler("AfterFilter"), 'asynchandle'))
->addAfterFilterHandler(array(new SizeHandler("Non compressed"), 'asynchandle'))
->start();
10 changes: 10 additions & 0 deletions examples/src/middleware/logHandler2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Hprose\Future;

$logHandler2 = function($request, stdClass $context, Closure $next) {
error_log($request);
$response = $next($request, $context);
Future\run('error_log', $response);
return $response;
};

0 comments on commit be79589

Please sign in to comment.