Created
March 24, 2015 15:36
-
-
Save developerworks/87889ba58c7395bc1e46 to your computer and use it in GitHub Desktop.
Thrift client and server example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(hello_client). | |
-include("hello_thrift.hrl"). | |
-include("hello_constants.hrl"). | |
-export([ | |
test/0 | |
]). | |
p(X) -> | |
io:format("in the p() ~w~n", [X]), | |
ok. | |
test() -> | |
{ok, Client0} = thrift_client_util:new(?HELLO_SERVER_HOST, ?HELLO_SERVER_PORT, hello_thrift, [{framed, true}]), | |
io:format("~n Client0: ~p~n", [Client0]), | |
{Client1, Res} = thrift_client:call(Client0, say, [<<"World!">>]), | |
io:format(" the Res is ~p~n", [Res]), | |
io:format("~n Client1: ~p~n", [Client1]), | |
p(Res), | |
io:format("The Client0 = Client1: ~p~n", [Client0 == Client1]), | |
thrift_client:close(Client1), | |
ok. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(hello_server). | |
-include("hello_thrift.hrl"). | |
-include("hello_constants.hrl"). | |
-define(SERVER_NAME, ?MODULE). | |
-export([ | |
start/0, | |
handle_function/2, | |
%% say/1, | |
stop/0 | |
]). | |
%% debug(Info) -> | |
%% io:format("Debug info: ~s~n", [Info]). | |
%% say(Name) -> | |
%% io:format("~n Line:~p ", [?LINE]), | |
%% Sentence = "Hello," ++ Name, | |
%% debug(Sentence), | |
%% BinSentence = list_to_binary(Sentence), | |
%% BinSentence. | |
%% | |
%% walk(Name) -> | |
%% list_to_binary(Name). | |
start() -> | |
start(?HELLO_SERVER_PORT). | |
start(Port) -> | |
Handler = ?MODULE, | |
thrift_socket_server:start([ | |
{handler, Handler}, | |
{service, hello_thrift}, | |
{port, Port}, | |
{framed, true}, | |
{name, ?SERVER_NAME}, | |
{ip, ?HELLO_SERVER_HOST} | |
]). | |
stop() -> | |
thrift_socket_server:stop(?SERVER_NAME), | |
ok. | |
handle_function(say, {Name}) -> | |
io:format("Args: ~p~n", [Name]), | |
{reply, "Hello"}; | |
handle_function(walk, {Name}) -> | |
io:format("Args: ~p~n", [Name]), | |
{reply, "Walk"}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment