blob: d262b8fe922719034731679347410a90d81df5a3 [file] [log] [blame]
Mark Slee07a3aab2007-03-07 05:45:10 +00001#!/usr/bin/env php
2<?php
Roger Meier21c0a852012-09-05 19:47:14 +00003
4namespace tutorial\php;
5
6error_reporting(E_ALL);
7
8require_once __DIR__.'/../../lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
9
10use Thrift\ClassLoader\ThriftClassLoader;
11
12$GEN_DIR = realpath(dirname(__FILE__).'/..').'/gen-php';
13
14$loader = new ThriftClassLoader();
15$loader->registerNamespace('Thrift', __DIR__ . '/../../lib/php/lib');
16$loader->registerDefinition('shared', $GEN_DIR);
17$loader->registerDefinition('tutorial', $GEN_DIR);
18$loader->register();
19
David Reissea2cba82009-03-30 21:35:00 +000020/*
21 * Licensed to the Apache Software Foundation (ASF) under one
22 * or more contributor license agreements. See the NOTICE file
23 * distributed with this work for additional information
24 * regarding copyright ownership. The ASF licenses this file
25 * to you under the Apache License, Version 2.0 (the
26 * "License"); you may not use this file except in compliance
27 * with the License. You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing,
32 * software distributed under the License is distributed on an
33 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
34 * KIND, either express or implied. See the License for the
35 * specific language governing permissions and limitations
36 * under the License.
37 */
Mark Slee07a3aab2007-03-07 05:45:10 +000038
Roger Meier21c0a852012-09-05 19:47:14 +000039use Thrift\Protocol\TBinaryProtocol;
40use Thrift\Transport\TSocket;
41use Thrift\Transport\THttpClient;
42use Thrift\Transport\TBufferedTransport;
43use Thrift\Exception\TException;
Mark Slee07a3aab2007-03-07 05:45:10 +000044
Mark Slee07a3aab2007-03-07 05:45:10 +000045try {
David Reiss755c8152009-03-26 06:15:05 +000046 if (array_search('--http', $argv)) {
47 $socket = new THttpClient('localhost', 8080, '/php/PhpServer.php');
48 } else {
49 $socket = new TSocket('localhost', 9090);
50 }
Mark Slee76791962007-03-14 02:47:35 +000051 $transport = new TBufferedTransport($socket, 1024, 1024);
52 $protocol = new TBinaryProtocol($transport);
Roger Meier21c0a852012-09-05 19:47:14 +000053 $client = new \tutorial\CalculatorClient($protocol);
David Reiss0c90f6f2008-02-06 22:18:40 +000054
Mark Slee76791962007-03-14 02:47:35 +000055 $transport->open();
David Reiss0c90f6f2008-02-06 22:18:40 +000056
Mark Slee76791962007-03-14 02:47:35 +000057 $client->ping();
58 print "ping()\n";
David Reiss0c90f6f2008-02-06 22:18:40 +000059
Mark Slee76791962007-03-14 02:47:35 +000060 $sum = $client->add(1,1);
61 print "1+1=$sum\n";
David Reiss0c90f6f2008-02-06 22:18:40 +000062
Roger Meier21c0a852012-09-05 19:47:14 +000063 $work = new \tutorial\Work();
David Reiss0c90f6f2008-02-06 22:18:40 +000064
Roger Meier21c0a852012-09-05 19:47:14 +000065 $work->op = \tutorial\Operation::DIVIDE;
Mark Slee76791962007-03-14 02:47:35 +000066 $work->num1 = 1;
67 $work->num2 = 0;
68
69 try {
70 $client->calculate(1, $work);
71 print "Whoa! We can divide by zero?\n";
Roger Meier21c0a852012-09-05 19:47:14 +000072 } catch (\tutorial\InvalidOperation $io) {
Mark Slee76791962007-03-14 02:47:35 +000073 print "InvalidOperation: $io->why\n";
74 }
David Reiss0c90f6f2008-02-06 22:18:40 +000075
Roger Meier21c0a852012-09-05 19:47:14 +000076 $work->op = \tutorial\Operation::SUBTRACT;
Mark Slee76791962007-03-14 02:47:35 +000077 $work->num1 = 15;
78 $work->num2 = 10;
79 $diff = $client->calculate(1, $work);
80 print "15-10=$diff\n";
David Reiss0c90f6f2008-02-06 22:18:40 +000081
Mark Slee76791962007-03-14 02:47:35 +000082 $log = $client->getStruct(1);
83 print "Log: $log->value\n";
David Reiss0c90f6f2008-02-06 22:18:40 +000084
Mark Slee76791962007-03-14 02:47:35 +000085 $transport->close();
David Reiss0c90f6f2008-02-06 22:18:40 +000086
Mark Slee76791962007-03-14 02:47:35 +000087} catch (TException $tx) {
88 print 'TException: '.$tx->getMessage()."\n";
Mark Slee07a3aab2007-03-07 05:45:10 +000089}
90
Mark Slee07a3aab2007-03-07 05:45:10 +000091?>