blob: c5c0810120dd0075cd07e26cd91996f55b5c0e81 [file] [log] [blame]
Mark Slee07a3aab2007-03-07 05:45:10 +00001#!/usr/bin/env php
2<?php
David Reissea2cba82009-03-30 21:35:00 +00003/*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
Mark Slee07a3aab2007-03-07 05:45:10 +000021
22$GLOBALS['THRIFT_ROOT'] = '../../lib/php/src';
23
24require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
25require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
26require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
David Reiss755c8152009-03-26 06:15:05 +000027require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php';
Mark Slee07a3aab2007-03-07 05:45:10 +000028require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
29
30/**
31 * Suppress errors in here, which happen because we have not installed into
32 * $GLOBALS['THRIFT_ROOT'].'/packages/tutorial' like we are supposed to!
33 *
34 * Normally we would only have to include Calculator.php which would properly
35 * include the other files from their packages/ folder locations, but we
36 * include everything here due to the bogus path setup.
37 */
38error_reporting(E_NONE);
39$GEN_DIR = '../gen-php';
40require_once $GEN_DIR.'/SharedService.php';
41require_once $GEN_DIR.'/shared_types.php';
42require_once $GEN_DIR.'/Calculator.php';
43require_once $GEN_DIR.'/tutorial_types.php';
44error_reporting(E_ALL);
45
Mark Slee07a3aab2007-03-07 05:45:10 +000046try {
David Reiss755c8152009-03-26 06:15:05 +000047 if (array_search('--http', $argv)) {
48 $socket = new THttpClient('localhost', 8080, '/php/PhpServer.php');
49 } else {
50 $socket = new TSocket('localhost', 9090);
51 }
Mark Slee76791962007-03-14 02:47:35 +000052 $transport = new TBufferedTransport($socket, 1024, 1024);
53 $protocol = new TBinaryProtocol($transport);
54 $client = new CalculatorClient($protocol);
David Reiss0c90f6f2008-02-06 22:18:40 +000055
Mark Slee76791962007-03-14 02:47:35 +000056 $transport->open();
David Reiss0c90f6f2008-02-06 22:18:40 +000057
Mark Slee76791962007-03-14 02:47:35 +000058 $client->ping();
59 print "ping()\n";
David Reiss0c90f6f2008-02-06 22:18:40 +000060
Mark Slee76791962007-03-14 02:47:35 +000061 $sum = $client->add(1,1);
62 print "1+1=$sum\n";
David Reiss0c90f6f2008-02-06 22:18:40 +000063
Mark Slee76791962007-03-14 02:47:35 +000064 $work = new tutorial_Work();
David Reiss0c90f6f2008-02-06 22:18:40 +000065
Mark Slee76791962007-03-14 02:47:35 +000066 $work->op = tutorial_Operation::DIVIDE;
67 $work->num1 = 1;
68 $work->num2 = 0;
69
70 try {
71 $client->calculate(1, $work);
72 print "Whoa! We can divide by zero?\n";
73 } catch (tutorial_InvalidOperation $io) {
74 print "InvalidOperation: $io->why\n";
75 }
David Reiss0c90f6f2008-02-06 22:18:40 +000076
Mark Slee76791962007-03-14 02:47:35 +000077 $work->op = tutorial_Operation::SUBTRACT;
78 $work->num1 = 15;
79 $work->num2 = 10;
80 $diff = $client->calculate(1, $work);
81 print "15-10=$diff\n";
David Reiss0c90f6f2008-02-06 22:18:40 +000082
Mark Slee76791962007-03-14 02:47:35 +000083 $log = $client->getStruct(1);
84 print "Log: $log->value\n";
David Reiss0c90f6f2008-02-06 22:18:40 +000085
Mark Slee76791962007-03-14 02:47:35 +000086 $transport->close();
David Reiss0c90f6f2008-02-06 22:18:40 +000087
Mark Slee76791962007-03-14 02:47:35 +000088} catch (TException $tx) {
89 print 'TException: '.$tx->getMessage()."\n";
Mark Slee07a3aab2007-03-07 05:45:10 +000090}
91
Mark Slee07a3aab2007-03-07 05:45:10 +000092?>