Configuring Apache Thrift for Visual Studio 2012

Summary

Yes, it’s true: you can run and compile Apache Thrift servers and clients in Visual Studio 2012! Here’s a set of steps for compiling Apache Thrift under Visual Studio 2012 and then running a C++ Server and Client pair. I’ve also included steps to build a Java client which interacts with the C++ server, to demonstrate Thrift’s cross-language capabilities.

The current version of Thrift was 0.9.1 at the time of writing.

 

Build Apache Thrift libraries for VS2012

  1. Prerequisite: Download and build the Boost libraries for VS2012: see Configuring C++ Boost Libraries for Visual Studio. Note your {boost_install_dir} for later.
  2. Download Apache Thrift AND the Thrift compiler for Windows from http://thrift.apache.org/download
  3. Download libevent, a C++ dependency for the non-blocking Thrift Server: http://libevent.org/
  4. Extract both tar.gz files once downloaded.
  5. Navigate to {thrift_install_dir}\lib\cpp and open thrift.sln with VS2012
  6. If a dialog box appears that asks to upgrade some files to VS2012, click Upgrade
  7. Open the Visual Studio 2012 Command Prompt at Start->All Programs->Microsoft Visual Studio 2012 ->Visual Studio Tools -> Visual Studio Command Prompt (2012)
  8. In the VS2012 CMD Prompt, cd into the unzipped libevent directory. You should see subdirectories like “compat”, “include”, and “test” if it is the correct directory.
  9. Run this command in the CMD prompt to build libevent for VS2012: nmake -f Makefile.nmake
  10. Note your {libevent_install_dir}.
  11. Once the command has finished, go back to VS2012 (thrift.sln), right-click libthrift’s Project Properties, and navigate to C/C++->General.
  12. Enter this information into the Additional Include Directories line:
    {boost_install_dir}\boost_1_56_0;{boost_install_dir}\boost_1_56_0\boost
  13. Right-click libthriftnb’s Project Properties, and navigate to C/C++->General.
  14. Enter this information into the Additional Include Directories line:
    {boost_install_dir}\boost_1_56_0;{libevent_install_dir};{libevent_install_dir}\include;{libevent_install_dir}\WIN32-Code;
  15. Counter-intuitive, but necessary: Remove BoostThreadFactory.cpp from the libthrift project. This file causes compilation issues and must be removed in order to continue.
  16. Trigger a Rebuild of both projects, and note the directory where libthrift.lib and libthriftnb.lib are created. This will depend on your configuration for Debug or Release.

Build your Thrift Server and Client

This section will guide you through the Apache Thrift tutorial for C++ in VS2012, found at http://thrift.apache.org/tutorial/cpp

Building and Running the C++ Thrift server

  1. Download the tutorial.thrift and shared.thrift files from Apache:
  2. Run these two Thrift compiler commands to generate C++ for both of these service definition files:
    • thrift -r --gen cpp tutorial.thrift
    • thrift -r --gen cpp shared.thrift
  3. This will generate a gen-cpp directory in the same directory that thrift.exe was run from.
  4. Copy this gen-cpp folder into a Visual Studio Project, and separate the header and source files into the appropriate folders.

    All of the gen-cpp files imported into a VC++ project.

    All of the gen-cpp files imported into a VC++ project.

  5. Next, we must fix some convenient gotchas to bring the tutorial to life.
  6. Right-click your project in the Solution Explorer and go to Properties, then C/C++->All Options, and set Additional Include Directories to:
    {boost_install_dir};{thrift_install_dir}\lib\cpp\src\thrift\windows;{thrift_install_dir}\lib\cpp\src;
  7. Then, go to Linker->All Options, and set Additional Dependencies to:
    libboost_thread-vc110-mt-gd-1_56.lib;libboost_chrono-vc110-mt-gd-1_56.lib;libthrift.lib;%(AdditionalDependencies)

    Note: the version numbers for these libraries may differ if you’ve built a different version of Boost.

  8. Click on Linker->All Options, and set Additional Library Directories to:
    {boost_install_dir}\stage\lib;{thrift_install_dir}\lib\cpp\Debug

    The second item in the path could end with \Release, if you used the Release configuration instead of Debug while compiling Thrift.

  9. Double-click Calculator_server.skeleton.cpp. We need to adjust the main() method for the Server.
  10. Place this code at the top of the int main(int argc, char **argv) method:
      	WSADATA wsaData = {};
        WORD wVersionRequested = MAKEWORD(2, 2);
        int err = WSAStartup(wVersionRequested, &wsaData);
    
  11. Trigger a rebuild. If all goes well, then your project should compile without a hitch, and then you should be able to Run the project and see your C++ Thrift server starting up!

 

Building and running the C++ Thrift client

  1. Create another VS2012 project for the Thrift C++ client.
  2. Download the CppClient.cpp file and copy its contents to a new Source file in your new Client project:
    https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob;f=tutorial/cpp/CppClient.cpp;hb=HEAD
  3. Copy the gen-cpp folder from the Server application into your Client project, and add all of the files to the project. You need two separate copies of these files, or there will be two int main() functions defined in both your server and client, which is not fun to resolve.
  4. Erase or remove the files Calculator_server.skeleton.cpp and SharedService_server.skeleton.cpp from your project.
  5. Right-click your project in the Solution Explorer and go to Properties, then C/C++->All Options, and set Additional Include Directories to:
    {boost_install_dir};{thrift_install_dir}\lib\cpp\src\thrift\windows;{thrift_install_dir}\lib\cpp\src;
  6. Then, go to Linker->All Options, and set Additional Dependencies to:
    libboost_thread-vc110-mt-gd-1_56.lib;libboost_chrono-vc110-mt-gd-1_56.lib;libthrift.lib;%(AdditionalDependencies)

     Note: the version numbers for these libraries may differ if you’ve built a different version of Boost.

  7. Then, go to Linker->All Options, and set Additional Library Directories to:
    {boost_install_dir}\stage\lib;{thrift_install_dir}\lib\cpp\Debug

    The second item in the path could end with \Release, if you used the Release configuration instead of Debug while compiling Thrift.

  8. Trigger a rebuild. If all is well, it should succeed.
  9. Ensure that your Server is running, and run your Client. Thrift lives!
Eureka! The Thrift server accepts commands and responds to them.

Eureka! The Thrift server accepts commands and responds to them.

Addendum: Building and Running a Java Client for your C++ Service

Let’s explore the cross-language power of Thrift by creating a Java client which can talk to the C++ service.

Create Thrift files, and import them into Eclipse

  1. Create a new Java Project in Eclipse.
  2. Run these two Thrift compiler commands to generate Java code for both of these service definition files:
    • thrift -r --gen java tutorial.thrift
    • thrift -r --gen java shared.thrift
  3. This will create a gen-java folder wherever thrift.exe is located. Import all of these files into your Java Project, and sort them into the correct packages: some files will go into a package named ‘tutorial’ and others will go into a package named ‘shared’.
  4. Create a Java file named JavaClient.java, and paste the contents of the sample JavaClient.java into it
    https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/java/src/JavaClient.java;hb=HEAD
  5. So many red marks! Let’s work on fixing those…

 

Build all Java libraries and compile the client

Thrift has two dependencies in Java: ant and slf4j.

  1. Download the latest Apache Ant binary from here: http://ant.apache.org/bindownload.cgi
  2. Download the latest SLF4J binary from here: http://www.slf4j.org/download.html
  3. Unzip both downloads and note their locations.
  4. We won’t be able to build Ant files until we configure Ant correctly.
    Follow Steps 1-5 here: http://ant.apache.org/manual/install.html#getting
  5. Next, download and unzip this maven-ant-tasks library, which we will need for the Thrift compilation:
    http://maven.apache.org/ant-tasks/download.cgi
  6. Copy the unzipped maven-ant-tasks-2.1.3.jar into {ant_install_directory}\lib
  7. Time to build Thrift’s Java library.
  8. Navigate to {thrift_install_dir}\lib\java
  9. Open a Command Prompt, and type ant
    If you’ve set up Ant and the maven-ant-tasks JAR correctly, you will see BUILD SUCCEEDED
  10. Copy the path for the newly-built Thrift Java library, located at “{thrift_install_dir}\lib\java\build\libthrift-0.9.1.jar”
  11. In Eclipse, right-click your project’s JRE System Library in the project explorer, and choose Build Path->Configure Build Path
  12. Click on the Libraries tab and Add External JAR, then add the Thrift Java library.
  13. Finish up by adding the SLF4J dependency to Eclipse.
  14. In Eclipse, right-click your project’s *JRE System Library in the project explorer, and choose Build Path->Configure Build Path
  15. Click on the Libraries tab and Add External JAR, then add the SLF4J jar, located at “{slf4j_install_directory}\slf4j-api-1.7.7.jar”
  16. Ensure that your Thrift server is up, and run your Java project as a Java Application through Eclipse. It works!
  17. If the program exits with a message like “Please enter ‘simple’ or ‘secure'”, then you are missing a program argument. Right-click your project, click Run->Run Configurations, and click on the Arguments tab. Add the word simple or secure to the Program Arguments dialog box, then click Run.

 

Related Links

  1. The Apache/Facebook developers were very Thrifty with their documentation, and produced a one-page tutorial: http://thrift.apache.org/tutorial/cpp
  2. A blog post with instructions for Visual Studio 2010 can be found here: http://technicaltidbit.blogspot.com/2012/12/apache-thrift-in-windows.html
Advertisements

iMacros Firefox Add-on: Download all Tagged Facebook Photos

(This script works as of December 17th, 2011. It will probably stop working as Facebook updates to the Timeline…)

I downloaded iMacros this morning. It’s a Firefox add-on that lets me automate any task on the browser. With the built-in macro recorder, I can instruct iMacros to download files, click specific links/images, and even fill out web forms. One of the things iMacros can’t do, however, is press some keyboard keys: It can enter text into forms and submit them, but it does not support arrow keys and modifiers (like the ALT key).

You can get iMacros here: https://addons.mozilla.org/en-US/firefox/addon/imacros-for-firefox/ – it’s free, but tech-supported versions cost some money.

As soon as I had iMacros downloaded, I decided to automate download of all my Facebook tagged photos. It was actually pretty straightforward:

  1. First, click a tagged photo of yourself. The URL in your address bar will look like this: https://www.facebook.com/photo.php?fbid=______&set=_____&type=1&theater
  2. Modify the URL by taking off the ending “&theater” part of it: https://www.facebook.com/photo.php?fbid=______&set=_____&type=1 – this will take us to the simpler photo interface.
  3. Run the iMacros script below after you’ve downloaded and installed the add-on from the link above. Make sure your active tab is pointing at the URL in Step 2.
VERSION BUILD=7401110 RECORDER=FX
TAB T=1
ONDOWNLOAD FOLDER=* FILE=+_{{!NOW:yyyymmdd_hhnnss}} WAIT=YES
ONDOWNLOAD FOLDER=* FILE=+_{{!NOW:yyyymmdd_hhnnss}} WAIT=YES
TAG POS=1 TYPE=A ATTR=TXT:Download
TAG POS=1 TYPE=A ATTR=TXT:Next

This script will look for the “Download” button, click it, save your photo, and then click the “Next” button.

You can set iMacros to run this in a loop and get all of your photos at once!