Contents

Disclaimer

This website, nor it's author isn't in any form affiliated or endorsed by the Apache Software Foundation.

Opinions or suggestions included in this document are based on the author's knowledge of Apache Thrift, which is still work-in-progress.


Quick start – running tutorial

As you may have noticed, in the "tutorial" directory of the Apache Thrift archive are contained some example files. In this part of the tutorial, you will learn, how to have Thrift based service up and running, without getting too deep into the technical details.

In the "tutorial" directory you will find tutorial.thrift file. It's the main file defining, what procedures and objects are going to be exchanged. You may want to read this file, to briefly see, what's going on there. We will prepare server in python and client in PHP. To generate necessary files, type:

thrift -r --gen py tutorial.thrift
thrift -r --gen php tutorial.thrift

Our files were generated in the directories:

gen-py
gen-php

In both of the directories structure is similar – in "shared" directory are files common to every project, while in "tutorial" - files connected to the tutorial application.

Starting python server is quite simple. Just go into "py" directory and run prepared file "PythonServer.py". If you encounter error like:

Traceback (most recent call last):
  File "./py/PythonServer.py", line 25, in <module>
    from tutorial import Calculator
ImportError: No module named tutorial

you should export environment variable PYTHONPATH stating, where "gen-py" directory could be found. In my case, it was:

home-debian:~# export PYTHONPATH=~/thrift-0.2.0/tutorial/gen-py

Successful starting of the server will result in the message:

home-debian:~# ./thrift-0.2.0/tutorial/py/PythonServer.py
Starting the server...

Now, we can concentrate on the PHP client. I assume, that you have http server running, with PHP 5.0+ enabled. There's a little mess in the files provided in the package, so don't expect them to work "out of the box".

Copy file PhpClient.php (from the "php" directory) to the location, from which it can be processed by the web server (you can run, of course, this php file from the command line, if you prefer). Open the file in the text editor. Remove first line (we don't need it). Then, you should work a little to fix the paths. First of all, change the value of $GLOBALS['THRIFT_ROOT'] to point to the place, where root directory of Thrift tools for PHP are (it's the directory lib/php/src in the archive). You can copy the files to wherever you want. In my case, it was something like:

$GLOBALS['THRIFT_ROOT'] = '/root/thrift-0.2.0/lib/php/src';

Fix the path stored in the $GEN_DIR variable, to point to the "gen-php" directory. In my case, it's

$GEN_DIR = '/root/thrift-0.2.0/tutorial/gen-php';

Notice, that in this dir, there are two subdirectiories, what is not properly reflected in the php file. So, the four consecutive lines should have looked like:

require_once $GEN_DIR.'/SharedService.php';
require_once $GEN_DIR.'/shared_types.php';
require_once $GEN_DIR.'/Calculator.php';
require_once $GEN_DIR.'/tutorial_types.php';

Notice comment (lines 30-37) – we are supressing errors, because this whole bunch of "require" calls is just dirty trick. In real application we should include only one, main file and everything else should be autoincluded. I don't know, why Thrift developers choose such approach to tutorial application, but let it be. We will discuss, how to prepare the script properly later on.

As a last thing (and not mandatory), I suggest to comment out lines 47-49 and 51 of original file, as we are not running the application from the command line:

//if (array_search('--http', $argv)) {
//  $socket = new THttpClient('localhost', 8080, '/php/PhpServer.php');
//} else {
  $socket = new TSocket('localhost', 9090);
//}

Then, you can access the script using web browser. You will see result similar to:

ping() 1+1=2 InvalidOperation: Cannot divide by 0 15-10=5 Log: 5

As you review the client script (lines 56-86), you'll see, how objects are exchanged and remote procedures are called. You may also notice - in the console window where you ran the server - that every procedure call is being logged.


Copyrights information

This document is published under the Creative Commons Attribute-Share Alike 3.0 license. Creative Commons License

This website template was prepaded by Studio7designs and is shared under the Creative Commons Attribution 3.0 Unported license.

About the author

Krzysztof Rakowski is living in Warsaw, Poland. He works as a Senior PHP Programmer at K2 Internet SA.

You may contact author by writing to the email krzysztof [at] rakowski.pl