You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by Apache Wiki <wi...@apache.org> on 2012/02/20 09:22:17 UTC
[Thrift Wiki] Update of "ThriftUsageObjectiveC-new" by satoshihirano
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Thrift Wiki" for change notification.
The "ThriftUsageObjectiveC-new" page has been changed by satoshihirano:
http://wiki.apache.org/thrift/ThriftUsageObjectiveC-new?action=diff&rev1=13&rev2=14
Comment:
progress 60% for 0.9.0
{{{#!wiki note
'''When you modify Thrift'''
- Use ''retain_stub'' instead of ''retain'' and use ''release_stub'' instead of ''release'' inside the runtime library. The thrift compiler should generate ''retain_stub/release_stub'' instead of retain/release''. [[http://svn.apache.org/viewvc/thrift/trunk/lib/cocoa/src/TObjective-C.h?view=markup|See this for detail]].
+ Use ''retain_stub'' instead of ''retain'' and use ''release_stub'' instead of ''release'' inside the runtime library. The thrift compiler should generate ''retain_stub/release_stub'' instead of ''retain/release''. [[http://svn.apache.org/viewvc/thrift/trunk/lib/cocoa/src/TObjective-C.h?view=markup|See this for detail]].
}}}
- == Static Library vs Adding Code to Your Project ==
+ == Static Library vs Adding Library to Your Project ==
+
+ There are two options to link the runtime library of Thrift with your project. If your project is small, you can add the source files of the runtime library into your project. Otherwise, if you use Thrift within several sub projects, you must make a static library, libthrift.a, from the source files of the runtime library. Since ways to make a runtime library is a bit complicated and exceeds this guide, please consult other sites.
+
+ The location of the runtime library: lib/cocoa/src
+
+ The rest of this guide assumes we directly add the runtime library into our sample project and we don't make the static library.
== Binary type ==
- Thrift supports ''binary'' type. Objective-C
+ Thrift supports ''binary'' type as well as ''string'' type. Thrift for Objective-C supports both. ''binary'' is a variant of ''string''.
+
+ ''string'' is converted into UTF-8 encoding before sending. The UTF-8 encoding is converted to NSString* after receiving.
+
+ ''binary'' omits the encoding conversion. Give NSData* for sending. NSData* is passed when receiving.
+
+ {{{#!wiki note
+ Note all language support do encoding conversion. For example Python does not do encoding conversion. (Thus, you need to do conversion by yourself).
+ }}}
+
= Getting started =
- This guide will walk you through creating an application that uses Thrift with a Java server and a client written in Objective-C. It is assumed that you will have a basic knowledge of Objective-C and Xcode to complete this tutorial.
+ This guide will walk you through creating a bulletin board application that uses Thrift with an iOS client 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.
== Requirements ==
Make sure that your system meets the requirements as noted in ThriftRequirements
- * Thrift 0.2.0
- * Mac OS X 10.5+
+ * Thrift 0.9.0+ (0.8.0-dev+)
+ * iOS 4+
- * Xcode 3.0+
+ * 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)
- == Building Thrift cocoa as a framework ==
- It is relatively straight forward to build the thrift cocoa library sources as a framework. As of Thrift 0.2.0 this is not done automatically. Hopefully it will in future releases.
-
- 1. Download Thrift 0.2.0 and install. Follow the instructions at ThriftInstallationMacOSX to install on Mac OS X.
- 2. Download the Thrift.framework project for XCode [[attachment:ThriftFramework.zip]]
- 3. Open the Xcode project file and compile.
-
== Creating the Thrift file ==
- We will use the sample thrift file described on the main page of the Thrift project.
+ We will use a sample thrift IDL file, ''idl.thrift''. get() method returns a list of struct so that we demonstrate how to send/receive an array of structs in Objective-C.
- * Create a new directory to contain the project sources.
- * Create a new file called 'profile.thrift'
-
{{{
- struct UserProfile {
- 1: i32 uid,
+ struct Message {
+ 1: string text,
- 2: string name,
+ 2: string date
- 3: string blurb
}
- service UserStorage {
- void store(1: UserProfile user),
- UserProfile retrieve(1: i32 uid)
- }
+
+ service BulletinBoard {
+ void add(1: Message msg),
+ list<Message> get()
- }}}
+ }}}}
* Run the thrift compiler to build project sources.
{{{
- thrift --gen java profile.thrift
+ thrift --gen java idl.thrift
- thrift --gen cocoa profile.thrift
+ thrift --gen cocoa idl.thrift
}}}
-
- == Creating the Java Server ==
-
- * cd into the gen-java directory
- * make sure that your classpath is properly setup. You will need to ensure that libthrift.jar, slf4j-api, and slf4j-simple are in your classpath.
- * create the file UserStorageImpl.java
-
- {{{
- import org.apache.thrift.TException;
- import java.util.HashMap;
-
- class UserStorageImpl implements UserStorage.Iface {
- private HashMap<Integer, UserProfile> profiles;
-
- public UserStorageImpl() {
- profiles = new HashMap<Integer, UserProfile>();
- }
-
- @Override
- public void store(UserProfile user) throws TException {
- long time = System.currentTimeMillis();
- System.out.println("store() called: " + time);
- System.out.println("UID: " + user.uid);
- System.out.println("Name: " + user.name);
- System.out.println("Blurb: " + user.blurb);
- profiles.put(new Integer(user.uid), user);
- }
-
- @Override
- public UserProfile retrieve(int uid) throws TException {
- return profiles.get(new Integer(uid));
- }
- }
- }}}
-
- * create a file called 'Server.java'
-
- {{{
- import java.io.IOException;
- import org.apache.thrift.protocol.TBinaryProtocol;
- import org.apache.thrift.protocol.TBinaryProtocol.Factory;
- import org.apache.thrift.server.TServer;
- import org.apache.thrift.server.TThreadPoolServer;
- import org.apache.thrift.transport.TServerSocket;
- import org.apache.thrift.transport.TTransportException;
-
- public class Server {
-
- private void start() {
- try {
- TServerSocket serverTransport = new TServerSocket(7911);
- UserStorage.Processor processor = new UserStorage.Processor(new UserStorageImpl());
- Factory protFactory = new TBinaryProtocol.Factory(true, true);
-
- TServer server = new TThreadPoolServer(processor, serverTransport, protFactory);
-
- System.out.println("Starting server on port 7911 ...");
- server.serve();
- } catch (TTransportException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public static void main(String args[]) {
- Server srv = new Server();
- srv.start();
- }
- }
- }}}
-
- * compile the classes
-
- {{{
- javac *.java
- }}}
-
- * run the server
-
- {{{
- java Server
- }}}
-
- * This will run a server that implements the service UserStorage on port 7911. The service simply stores UserProfile structures in a HashMap and retrieves them when requested.
== Create the Objective-C client ==
- The objective-c client is a simple cocoa application that allows the user to fill out the values of a UserProfiel structure and store/retrieve them from the server.
+ The objective-c client is a simple cocoa application that allows the user to fill out a text field add/get them from the server.
+ * Create a new iOS single view project.
+ * Product name is ''myThriftApp''.
+ * Check to ''Use Automatic Reference Counting''
+ * Add the runtime library.
+ * group.Right click on myThriftApp and Add files to "myThirtApp". Choose "thrift-x.x.x/lib/cocoa/src".
+ * rename group name from ''src'' to ''thrift''.
+ * Setup header search path in build settings.
+ * Always Search User Path: YES
+ * Framework Search Paths: add ''$(SRCROOT)'' and ''$(inherited)''
+ * Add generated files.
+ * group.Right click on myThriftApp and Add files to "myThirtApp". Choose "gen-cocoa".
- * Create a new cocoa project.
- * Right click on the classes group and Add existing files. Browse to the gen-cocoa directory and add the files profile.h and profile.m
- * Change the import lines in profile.h and profile.m from:
-
- {{{
- #import <TProtocol.h>
- #import <TApplicationException.h>
- #import <TProtocolUtil.h>
- #import <TProcessor.h>
- }}}
-
- to
-
- {{{
- #import <Thrift/TProtocol.h>
- #import <Thrift/TApplicationException.h>
- #import <Thrift/TProtocolUtil.h>
- #import <Thrift/TProcessor.h>
- }}}
* Copy the following text to the app delegate header file
@@ -283, +194 @@
You can download the Xcode project for this step: [[attachment:ThriftObjectiveCClient.zip]]
+ == Creating the Java Server ==
+
+ * cd into the gen-java directory
+ * make sure that your classpath is properly setup. You will need to ensure that libthrift.jar, slf4j-api, and slf4j-simple are in your classpath.
+ * create the file UserStorageImpl.java
+
+ {{{
+ import org.apache.thrift.TException;
+ import java.util.HashMap;
+
+ class UserStorageImpl implements UserStorage.Iface {
+ private HashMap<Integer, UserProfile> profiles;
+
+ public UserStorageImpl() {
+ profiles = new HashMap<Integer, UserProfile>();
+ }
+
+ @Override
+ public void store(UserProfile user) throws TException {
+ long time = System.currentTimeMillis();
+ System.out.println("store() called: " + time);
+ System.out.println("UID: " + user.uid);
+ System.out.println("Name: " + user.name);
+ System.out.println("Blurb: " + user.blurb);
+ profiles.put(new Integer(user.uid), user);
+ }
+
+ @Override
+ public UserProfile retrieve(int uid) throws TException {
+ return profiles.get(new Integer(uid));
+ }
+ }
+ }}}
+
+ * create a file called 'Server.java'
+
+ {{{
+ import java.io.IOException;
+ import org.apache.thrift.protocol.TBinaryProtocol;
+ import org.apache.thrift.protocol.TBinaryProtocol.Factory;
+ import org.apache.thrift.server.TServer;
+ import org.apache.thrift.server.TThreadPoolServer;
+ import org.apache.thrift.transport.TServerSocket;
+ import org.apache.thrift.transport.TTransportException;
+
+ public class Server {
+
+ private void start() {
+ try {
+ TServerSocket serverTransport = new TServerSocket(7911);
+ UserStorage.Processor processor = new UserStorage.Processor(new UserStorageImpl());
+ Factory protFactory = new TBinaryProtocol.Factory(true, true);
+
+ TServer server = new TThreadPoolServer(processor, serverTransport, protFactory);
+
+ System.out.println("Starting server on port 7911 ...");
+ server.serve();
+ } catch (TTransportException e) {
+ e.printStackTrace();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static void main(String args[]) {
+ Server srv = new Server();
+ srv.start();
+ }
+ }
+ }}}
+
+ * compile the classes
+
+ {{{
+ javac *.java
+ }}}
+
+ * run the server
+
+ {{{
+ java Server
+ }}}
+
+ * This will run a server that implements the service UserStorage on port 7911. The service simply stores UserProfile structures in a HashMap and retrieves them when requested.
+
+
+
= Asynchronous Client =