Lab 2 - Thrift
Lab tasks
The tasks for today are:
- Use Apache Thrift in a simple example
- install Thrift
- download the example code
- compile and run the example program
- Lab activity
- modify the example program
- connect to our server and make a remote procedure call
- send the response by e-mail, to be counted as lab activity
- Read the homework assignment
Apache Thrift
Apache Thrift is a cross-platorfm cross-language RPC library.
Example Source
Download source code from https://d3s.mff.cuni.cz/legacy/files/teaching/nswi080/labs/Files/sources-2.zip .
Unzip the code and explore the sources in the Example-XXX directories.
C++
- Example.thrift - describes an interface of a remote service using Thrift IDL. This file is used by Thrift compiler to generate code for any programming language supported by Thrift
- client.cpp - a simple program which connects to a server and calls a remote procedure.
- Connects to
localhost:5000
.
- Uses a multiplexed binary protocol. Multiplexing allows providing multiple services on a single port.
- server.cpp - a simple server providing an implementation of
Example
.
- Listens on
localhost:5000
.
- Uses
TThreadedServer
to serve clients in multiple threads.
- Uses
TProcessorFactory
to create a processor and handler for each client connection.
This allows the handler to keep state associated with the client.
- Makefile - runs the Thrift compiler and C++ compiler
- Adds generated header files on the include path.
- Compiles
server.cpp
and client.cpp
and the generated source files.
- gen-cpp - a directory created after running the Thrift compiler.
- Contains generated
.h
and .cpp
files
- Example.h - defines
ExampleIf
, an abstract base class for the service handler
- Example_server.skeleton.cpp - not used in compilation, but contains skeleton code for implementation of the server
Java
- src/main/java/ExampleClient.java - a simple program which connects to a server and calls a remote procedure.
- pom.xml - Maven build file
- runs the Thrift compiler
- ensures Thrift libraries on the classpath
- target/generated-sources/thrift - a directory created after running the Thrift compiler from Maven.
- Contains generated
.java
files
Node
- index.js - a simple program which connects to a server and calls a remote procedure.
- package.json - npm package file
- specifies dependency on thrift
- defines a command to run the Thrift compiler
- gen-nodejs - a directory created after running the Thrift compiler.
- Contains generated
.js
files
Python
- client.py - a simple program which connects to a server and calls a remote procedure.
- generate.sh - runs the Thrift compiler
- run.sh - runs the client with the generated files on
PYTHONPATH
- gen-py - a directory created after running the Thrift compiler.
- Contains generated
.py
files
Running the example
C++
- Compile the programs
- Run the server
- Run the client
Setting up Thrift directories
If you have installed thrift manually to your home directory (for example if using lab computers), you might have to set up directory paths, so that the compiler and the linker can find the necessary files.
- Edit the
Makefile
:
- Add
-I$(HOME)/thrift/include
to the g++
commands (2 places)
- Add
-L$(HOME)/thrift/lib
to the g++
commands (2 places)
- Before running
make
, set up path to thrift binary:
export PATH="$PATH:$HOME/thrift/bin"
- Before running the programs, set up path to thrift libraries:
export LD_LIBRARY_PATH="$HOME/thrift/lib"
Java
- Compile the client
- Run the C++ server
- Run the client
Node
- Install required packages
- Generate code
- Run the C++ server
- Run the client
node .
Python
- Generate code
- Run the C++ server
- Run the client
Lab activity task
With a client in any language, connect to our server at lab.d3s.mff.cuni.cz:5000
, and call the method with an unique string. The server will respond with a text containing an integer key. Send this by e-mail to the lab teacher.
Note: if you do not receive an integer key on the first try, then follow the instructions you received.