| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 1 | #!/usr/bin/env php | 
 | 2 | <?php | 
| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | /* | 
 | 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 Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 21 |  | 
 | 22 | $GLOBALS['THRIFT_ROOT'] = '../../lib/php/src'; | 
 | 23 |  | 
 | 24 | require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php'; | 
 | 25 | require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php'; | 
 | 26 | require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php'; | 
| David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 27 | require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php'; | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 28 | require_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 |  */ | 
 | 38 | error_reporting(E_NONE); | 
 | 39 | $GEN_DIR = '../gen-php'; | 
 | 40 | require_once $GEN_DIR.'/SharedService.php'; | 
 | 41 | require_once $GEN_DIR.'/shared_types.php'; | 
 | 42 | require_once $GEN_DIR.'/Calculator.php'; | 
 | 43 | require_once $GEN_DIR.'/tutorial_types.php'; | 
 | 44 | error_reporting(E_ALL); | 
 | 45 |  | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 46 | try { | 
| David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 47 |   if (array_search('--http', $argv)) { | 
 | 48 |     $socket = new THttpClient('localhost', 8080, '/php/PhpServer.php'); | 
 | 49 |   } else { | 
 | 50 |     $socket = new TSocket('localhost', 9090); | 
 | 51 |   } | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 52 |   $transport = new TBufferedTransport($socket, 1024, 1024); | 
 | 53 |   $protocol = new TBinaryProtocol($transport); | 
 | 54 |   $client = new CalculatorClient($protocol); | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 55 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 56 |   $transport->open(); | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 57 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 58 |   $client->ping(); | 
 | 59 |   print "ping()\n"; | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 60 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 61 |   $sum = $client->add(1,1); | 
 | 62 |   print "1+1=$sum\n"; | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 63 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 64 |   $work = new tutorial_Work(); | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 65 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 66 |   $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 Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 76 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 77 |   $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 Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 82 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 83 |   $log = $client->getStruct(1); | 
 | 84 |   print "Log: $log->value\n"; | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 85 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 86 |   $transport->close(); | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 87 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 88 | } catch (TException $tx) { | 
 | 89 |   print 'TException: '.$tx->getMessage()."\n"; | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 90 | } | 
 | 91 |  | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 92 | ?> |