blob: caea0670649f7b4f35f11016445bc20c22eed477 [file] [log] [blame]
Christopher Piro93a06642007-09-18 06:23:33 +00001Thrift Erlang Library
Christopher Piro094823a2007-07-18 00:26:12 +00002
Christopher Piro93a06642007-09-18 06:23:33 +00003README Author: Chris Piro (cpiro@facebook.com)
4Last Modified: 2007-Sep-17
Christopher Piro094823a2007-07-18 00:26:12 +00005
Christopher Piro93a06642007-09-18 06:23:33 +00006Thrift is distributed under the Thrift open source software license.
7Please see the included LICENSE file.
Christopher Piro094823a2007-07-18 00:26:12 +00008
Christopher Piro93a06642007-09-18 06:23:33 +00009Using Thrift with Erlang
10========================
Christopher Piro094823a2007-07-18 00:26:12 +000011
Christopher Piro93a06642007-09-18 06:23:33 +000012The Thrift Erlang binding is built using GNU make. Run `make' in
13lib/erl to generate the necessary .beam object files in lib/erl/ebin/.
14Although the directories are laid out much like an OTP application,
15these bindings (as you will soon discover) are not an OTP application
16proper. When starting the Erlang emulator (interpreter) you must use
17`-pa /path/to/thrift/lib/erl/ebin' to load the bindings.
Christopher Piro094823a2007-07-18 00:26:12 +000018
Christopher Piro93a06642007-09-18 06:23:33 +000019Running the Tutorial
20====================
Christopher Piro094823a2007-07-18 00:26:12 +000021
Christopher Piro93a06642007-09-18 06:23:33 +000022It is recommended to pattern your own servers after the tutorial
23included in tutorial/. Generate the gen-erl/ directory by running
24tutorial.thrift, then cd to tutorial/erl/ and run server.sh. This
25script includes the commmands necessary to compile the generated
26Erlang source, compile the tutorial server itself, and open the Erlang
27emulator. At the emulator prompt, type `server:start()' to begin
28listening for connections.
Christopher Piro094823a2007-07-18 00:26:12 +000029
Christopher Piro93a06642007-09-18 06:23:33 +000030Note that there is no tutorial client; you may use a supplied client
31in another language.
Christopher Piro094823a2007-07-18 00:26:12 +000032
Christopher Piro93a06642007-09-18 06:23:33 +000033Implementation Notes
34====================
Christopher Piro094823a2007-07-18 00:26:12 +000035
Christopher Piro93a06642007-09-18 06:23:33 +000036tExecptions and t*Factorys are straight "new" -- e.g. TF =
37tTransportFactory:new() everything else is start_new
38(i.e. gen_server:start_link) -- this spawns a process and returns a
39pid
Christopher Piro094823a2007-07-18 00:26:12 +000040
Christopher Piro93a06642007-09-18 06:23:33 +000041tErlProcessor is a shim around the generated code (which is not
42actually a gen_server). Of course tErlProcessor isn't a gen_server
43either ... thrift_oop_server is a shim to make our "Thrift objects"
44gen_servers. Maybe we should remove some layers?
Christopher Piro094823a2007-07-18 00:26:12 +000045
Christopher Piro93a06642007-09-18 06:23:33 +000046get/set never means process dictionary
Christopher Piro094823a2007-07-18 00:26:12 +000047
Christopher Piro93a06642007-09-18 06:23:33 +000048Use tErlServer and tErlAcceptor. tSimpleServer and tServerSocket as
49are present in the other bindings are incompatible by design ... the
50call trace is spastic across the process tree. tErlServer and
51tErlAcceptor follow the same model as iserve:
Christopher Piro094823a2007-07-18 00:26:12 +000052
Christopher Piro93a06642007-09-18 06:23:33 +000053 * the top level code spawns a tErlServer, which listens on a socket
54 * a tErlAcceptor is spawned and calls accept() on the listening
55socket
56 * when accept() finishes, the tErlAcceptor
57 * tells the tErlServer to spawn a new acceptor
58 * handles the requests by spawning a processor, a transport, and a
59protocol
60 * (the tricky part) when the socket closes, the protocol exits, so:
61 * the transport exits because it's the one caller of the protocol
62 * likewise, the processor exits because it's the caller of the
63transport
64 * the tErlAcceptor traps the protocol's exit and exits with an
65acceptor_done
66 * the tErlServer sees that the acceptor exited and does nothing
67since there is already another acceptor accept()ing on the listen
68socket
Christopher Piro094823a2007-07-18 00:26:12 +000069
Christopher Piro93a06642007-09-18 06:23:33 +000070For info about iserve: http://www.trapexit.org/A_fast_web_server_demonstrating_some_undocumented_Erlang_features
Christopher Piro094823a2007-07-18 00:26:12 +000071
Christopher Piro93a06642007-09-18 06:23:33 +000072Final Thoughts
73==============
Christopher Piro094823a2007-07-18 00:26:12 +000074
Christopher Piro93a06642007-09-18 06:23:33 +000075This binding is a work in progress. It's certainly less thoroughly
76tested than the other, older bindings. Despite using parts from
77otp_base it is not packaged well, nor is it an OTP application (not to
78mention its many smaller transgressions). This implementation
79intentionally patterns after the other bindings (which is why there's
80oop.erl and thrift_oop_server), but regretfully it departs from
81idiomatic Erlang. Please see the included TODO and contribute your
82improvements back to the project.