Commit 333f7e4e authored by tufengqi's avatar tufengqi

finish system

parent 796f43d9
<?php
namespace tutorial\php;
error_reporting(E_ALL);
$root_path = __DIR__;
require_once $root_path . '/vendor/thrift_33cn/Thrift.back/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
$GEN_DIR = $root_path . '/vendor/thrift_33cn/Thrift.back/gen-php';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift.back', $root_path . '/vendor/thrift_33cn');
$loader->registerDefinition('shared', $GEN_DIR);
$loader->registerDefinition('tutorial', $GEN_DIR);
$loader->register();
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;
use tutorial\CalculatorClient;
try {
$socket = new THttpClient('127.0.0.1', 80, '/classes/fpf/thrift/Server.php');
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($transport);
$client = new CalculatorClient($protocol);
$transport->open();
echo 333;
$client->ping();
echo 222;
print "ping()\n";
$sum = $client->add(1, 1);
print "1+1=$sum\n";
$work = new \tutorial\Work();
$work->op = \tutorial\Operation::DIVIDE;
$work->num1 = 1;
$work->num2 = 0;
try {
$client->calculate(1, $work);
print "Whoa! We can divide by zero?\n";
} catch (\tutorial\InvalidOperation $io) {
print "InvalidOperation: $io->why\n";
}
$work->op = \tutorial\Operation::SUBTRACT;
$work->num1 = 15;
$work->num2 = 10;
$diff = $client->calculate(1, $work);
print "15-10=$diff\n";
$log = $client->getStruct(1);
print "Log: $log->value\n";
$transport->close();
} catch (TException $tx) {
print 'TException: ' . $tx->getMessage() . "\n";
}
...@@ -3,16 +3,15 @@ ...@@ -3,16 +3,15 @@
namespace tutorial\php; namespace tutorial\php;
error_reporting(E_ALL); error_reporting(E_ALL);
$root_path = dirname(dirname(dirname(__DIR__))); $root_path = dirname(dirname(dirname(__DIR__)));
require_once $root_path . '/vendor/apache/thrift/lib/php/lib/ClassLoader/ThriftClassLoader.php'; require_once $root_path . '/vendor/thrift_33cn/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader; use Thrift\ClassLoader\ThriftClassLoader;
$GEN_DIR = $root_path . '/vendor/apache/thrift/gen-php/'; $GEN_DIR = $root_path . '/vendor/thrift_33cn/Thrift/gen-php/';
$loader = new ThriftClassLoader(); $loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', $root_path . '/vendor/apache/thrift/lib/php/lib'); $loader->registerNamespace('Thrift', $root_path . '/vendor/thrift_33cn');
//$loader->registerDefinition('shared', $GEN_DIR); $loader->registerDefinition('shared', $GEN_DIR);
//$loader->registerDefinition('tutorial', $GEN_DIR); $loader->registerDefinition('tutorial', $GEN_DIR);
$loader->register(); $loader->register();
/* /*
...@@ -39,13 +38,12 @@ use Thrift\Transport\TSocket; ...@@ -39,13 +38,12 @@ use Thrift\Transport\TSocket;
use Thrift\Transport\THttpClient; use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport; use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException; use Thrift\Exception\TException;
use Tutorial\CalculatorClient;
try { try {
$socket = new THttpClient('127.0.0.1', 80, '/server.php'); $socket = new THttpClient('localhost', 80, '/classes/fpf/thrift/Server.php');
$transport = new TBufferedTransport($socket, 1024, 1024); $transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($transport); $protocol = new TBinaryProtocol($transport);
$client = new CalculatorClient($protocol); $client = new \tutorial\CalculatorClient($protocol);
$transport->open(); $transport->open();
$client->ping(); $client->ping();
print "ping()\n"; print "ping()\n";
......
<?php <?php
namespace tutorial\php; namespace tutorial\php;
error_reporting(E_ALL); error_reporting(E_ALL);
$root_path = dirname(dirname(dirname(__DIR__)));
require_once __DIR__.'/../../lib/php/lib/ClassLoader/ThriftClassLoader.php'; require_once $root_path . '/vendor/thrift_33cn/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader; use Thrift\ClassLoader\ThriftClassLoader;
$GEN_DIR = $root_path . '/vendor/thrift_33cn/Thrift/gen-php/';
$GEN_DIR = realpath(dirname(__FILE__).'/..').'/gen-php';
$loader = new ThriftClassLoader(); $loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__ . '/../../lib/php/lib'); $loader->registerNamespace('Thrift', $root_path . '/vendor/thrift_33cn/');
$loader->registerDefinition('shared', $GEN_DIR); $loader->registerDefinition('shared', $GEN_DIR);
$loader->registerDefinition('tutorial', $GEN_DIR); $loader->registerDefinition('tutorial', $GEN_DIR);
$loader->register(); $loader->register();
include("../../../../autoload.php");
/* /*
* Licensed to the Apache Software Foundation (ASF) under one * Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file * or more contributor license agreements. See the NOTICE file
...@@ -42,77 +39,78 @@ include("../../../../autoload.php"); ...@@ -42,77 +39,78 @@ include("../../../../autoload.php");
*/ */
if (php_sapi_name() == 'cli') { if (php_sapi_name() == 'cli') {
ini_set("display_errors", "stderr"); ini_set("display_errors", "stderr");
} }
use Thrift\Protocol\TBinaryProtocol; use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TPhpStream; use Thrift\Transport\TPhpStream;
use Thrift\Transport\TBufferedTransport; use Thrift\Transport\TBufferedTransport;
class CalculatorHandler implements \tutorial\CalculatorIf { class CalculatorHandler implements \tutorial\CalculatorIf {
protected $log = array(); protected $log = array();
public function ping() { public function ping() {
rror_log("ping()"); error_log("ping()");
}
public function add($num1, $num2) {
error_log("add({$num1}, {$num2})");
return $num1 + $num2;
}
public function calculate($logid, \tutorial\Work $w) {
error_log("calculate({$logid}, {{$w->op}, {$w->num1}, {$w->num2}})");
switch ($w->op) {
case \tutorial\Operation::ADD:
$val = $w->num1 + $w->num2;
break;
case \tutorial\Operation::SUBTRACT:
$val = $w->num1 - $w->num2;
break;
case \tutorial\Operation::MULTIPLY:
$val = $w->num1 * $w->num2;
break;
case \tutorial\Operation::DIVIDE:
if ($w->num2 == 0) {
$io = new \tutorial\InvalidOperation();
$io->whatOp = $w->op;
$io->why = "Cannot divide by 0";
throw $io;
}
$val = $w->num1 / $w->num2;
break;
default:
$io = new \tutorial\InvalidOperation();
$io->whatOp = $w->op;
$io->why = "Invalid Operation";
throw $io;
} }
$log = new \shared\SharedStruct(); public function add($num1, $num2) {
$log->key = $logid; error_log("add({$num1}, {$num2})");
$log->value = (string)$val; return $num1 + $num2;
$this->log[$logid] = $log; }
return $val; public function calculate($logid, \tutorial\Work $w) {
} error_log("calculate({$logid}, {{$w->op}, {$w->num1}, {$w->num2}})");
switch ($w->op) {
case \tutorial\Operation::ADD:
$val = $w->num1 + $w->num2;
break;
case \tutorial\Operation::SUBTRACT:
$val = $w->num1 - $w->num2;
break;
case \tutorial\Operation::MULTIPLY:
$val = $w->num1 * $w->num2;
break;
case \tutorial\Operation::DIVIDE:
if ($w->num2 == 0) {
$io = new \tutorial\InvalidOperation();
$io->whatOp = $w->op;
$io->why = "Cannot divide by 0";
throw $io;
}
$val = $w->num1 / $w->num2;
break;
default:
$io = new \tutorial\InvalidOperation();
$io->whatOp = $w->op;
$io->why = "Invalid Operation";
throw $io;
}
$log = new \shared\SharedStruct();
$log->key = $logid;
$log->value = (string)$val;
$this->log[$logid] = $log;
return $val;
}
public function getStruct($key) { public function getStruct($key) {
error_log("getStruct({$key})"); error_log("getStruct({$key})");
// This actually doesn't work because the PHP interpreter is // This actually doesn't work because the PHP interpreter is
// restarted for every request. // restarted for every request.
//return $this->log[$key]; //return $this->log[$key];
return new \shared\SharedStruct(array("key" => $key, "value" => "PHP is stateless!")); return new \shared\SharedStruct(array("key" => $key, "value" => "PHP is stateless!"));
} }
public function zip() { public function zip() {
error_log("zip()"); error_log("zip()");
} }
}; };
header('Content-Type', 'application/x-thrift'); header('Content-Type', 'application/x-thrift');
if (php_sapi_name() == 'cli') { if (php_sapi_name() == 'cli') {
echo "\r\n"; echo "\r\n";
} }
$handler = new CalculatorHandler(); $handler = new CalculatorHandler();
...@@ -120,11 +118,7 @@ $processor = new \tutorial\CalculatorProcessor($handler); ...@@ -120,11 +118,7 @@ $processor = new \tutorial\CalculatorProcessor($handler);
$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W)); $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
$protocol = new TBinaryProtocol($transport, true, true); $protocol = new TBinaryProtocol($transport, true, true);
$transport->open(); $transport->open();
try{ $processor->process($protocol, $protocol);
$processor->process($protocol, $protocol);
file_put_contents("./a.txt",22,FILE_APPEND);
}catch(Exception $e){
file_put_contents("./a.txt",333,FILE_APPEND);
}
$transport->close(); $transport->close();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment