现在在做一个多屏互动项目,需要Iphone设备与终端管理系统通信。
调研了下,决定使用Thrift框架实现通信模块。准备在Mac上安装Thrift,支持Objective-c和Java。
环境:Mac mini
Mac OS X Lion 10.7.5
1.安装Thrift
环境要求:http://wiki.apache.org/thrift/ThriftRequirements
Basic requirements
- A relatively POSIX-compliant *NIX system
- Cygwin or MinGW can be used on Windows
- g++ 3.3.5+
- boost 1.33.1+ (1.34.0 for building all tests)
- Runtime libraries for lex and yacc might be needed for the compiler.
Requirements for building from SVN
- GNU build tools: autoconf 2.59+ (2.60+ recommended), automake 1.9+, libtool 1.5.24+
-
pkg-config autoconf macros (pkg.m4) (Use MacPorts for Mac OS X)
- lex and yacc (developed primarily with flex and bison)
- libssl-dev
Language requirements
- C++
- Boost 1.33.1+
- libevent (optional, to build the nonblocking server)
- zlib (optional)
- Java
- Java 1.5+
- Apache Ant
- Apache Ivy (recommended)
- Apache Commons Lang (recommended)
- SLF4J
- C#: Mono 1.2.4+ (and pkg-config to detect it) or Visual Studio 2005+
- Python 2.4+ (including header files for extension modules)
- PHP 5.0+ (optionally including header files for extension modules)
- Ruby 1.8+ (including header files for extension modules)
- Erlang R12 (R11 works but not recommended)
- Perl 5
- Bit::Vector
- Class::Accessor
安装方法:http://wiki.apache.org/thrift/GettingOsxPackages
Install Boost
Download the boost library and bjam installer from http://www.boost.org Untar and place bjam in the boost folder and then compile with
$ sudo ./bjam toolset=darwin link=shared threading=multi runtime-link=shared variant=release address-model=64 stage install
Install libevent
Download libevent from http://monkey.org/~provos/libevent/ untar and compile with
$ ./configure --prefix=/usr/local --disable-static $ make $ sudo make install
Install Apache Thrift
Download apache thrift from http://thrift.apache.org untar and compile with
$ ./configure --prefix=/usr/local/ --disable-static --with-boost=/usr/local --with-libevent=/usr/local --without-python --without-csharp --without-ruby --without-perl --without-php --without-haskell --without-erlang $ make $ sudo make install
安装完完后,执行文件为/usr/local/bin/thrift
2.使用
http://wiki.apache.org/thrift/ThriftUsageObjectiveC
Getting started
This tutorial will walk you through creating a sample project which is a bulletin board application using Thrift. It consists of an iOS client app written in Objective-C and a Java server. It is assumed that you will have a basic knowledge of Objective-C and Xcode to complete this tutorial. Note that the client runs on Mac OS with small modification.
Acknowledgment: A part of this tutorial was borrowed from newacct's tutorial.
Sample Download
You can download the sample project from here. It includes:
- The whole myThriftApp project
- the runtime library in thrift/ directory from 0.8.0-dev (You should replace this with the latest one.)
- idl.thrift
-
Server.java and BulletinBoard.java in gen-java/ directory
Requirements
Make sure that your system meets the requirements as noted in ThriftRequirements.
- Thrift 0.9.0+ (0.8.0-dev+)
- iOS 4+ or Mac OS X 10.5+
- Xcode 4.2+
The following are required for the Java server; but, not the Objective-C client.
- Java 1.4+ (already installed in Mac OS X 10.5+)
-
SLF4J (available at http://www.slf4j.org/download.html)
Creating the Thrift file
We will use a simple thrift IDL file, myThriftApp/idl.thrift. This defines a bulletin board service. You can upload your message and date using add() method. get()method returns a list of the struct so that we demonstrate how to send/receive an array of structs in Objective-C.
// idl.thrift struct Message { 1: string text, 2: string date } service BulletinBoard { void add(1: Message msg), list<Message> get()
}
- Run the thrift compiler to generate the stub files (e.g. gen-cocoa/idl.h and gen-cocoa/idl.m).