From 8a162a5a01a0b9c6748028bcc9efd6e8dd9daf0c Mon Sep 17 00:00:00 2001 From: David Reiss Date: Wed, 11 Jun 2008 01:03:16 +0000 Subject: [PATCH] New tutorial for alterl bindings - include working client + server Test plan: ./client.sh server:start(). client:t(). git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@666458 13f79535-47bb-0310-9956-ffa450edef68 --- tutorial/alterl/client.erl | 55 +++++++++++++++++++++++++++++++++ tutorial/alterl/client.sh | 1 + tutorial/alterl/server.erl | 63 ++++++++++++++++++++++++++++++++++++++ tutorial/alterl/server.sh | 18 +++++++++++ 4 files changed, 137 insertions(+) create mode 100644 tutorial/alterl/client.erl create mode 120000 tutorial/alterl/client.sh create mode 100644 tutorial/alterl/server.erl create mode 100755 tutorial/alterl/server.sh diff --git a/tutorial/alterl/client.erl b/tutorial/alterl/client.erl new file mode 100644 index 00000000..c7a75ed8 --- /dev/null +++ b/tutorial/alterl/client.erl @@ -0,0 +1,55 @@ +-module(client). + +-include("calculator_thrift.hrl"). + +-export([t/0]). + +p(X) -> + io:format("~p~n", [X]), + ok. + +t() -> + Port = 9999, + + {ok, Client} = thrift_client:start_link("127.0.0.1", + Port, + calculator_thrift), + + thrift_client:call(Client, ping, []), + io:format("ping~n", []), + + {ok, Sum} = thrift_client:call(Client, add, [1, 1]), + io:format("1+1=~p~n", [Sum]), + + {ok, Sum1} = thrift_client:call(Client, add, [1, 4]), + io:format("1+4=~p~n", [Sum1]), + + Work = #work{op=?tutorial_SUBTRACT, + num1=15, + num2=10}, + {ok, Diff} = thrift_client:call(Client, calculate, [1, Work]), + io:format("15-10=~p~n", [Diff]), + + {ok, Log} = thrift_client:call(Client, getStruct, [1]), + io:format("Log: ~p~n", [Log]), + + try + Work1 = #work{op=?tutorial_DIVIDE, + num1=1, + num2=0}, + {ok, _Quot} = thrift_client:call(Client, calculate, [2, Work1]), + + io:format("LAME: exception handling is broken~n", []) + catch + Z -> + io:format("Got exception where expecting - the " ++ + "following is NOT a problem!!!~n"), + p(Z) + end, + + + {ok, ok} = thrift_client:call(Client, zip, []), + io:format("zip~n", []), + + ok = thrift_client:close(Client), + ok. diff --git a/tutorial/alterl/client.sh b/tutorial/alterl/client.sh new file mode 120000 index 00000000..a417e0da --- /dev/null +++ b/tutorial/alterl/client.sh @@ -0,0 +1 @@ +server.sh \ No newline at end of file diff --git a/tutorial/alterl/server.erl b/tutorial/alterl/server.erl new file mode 100644 index 00000000..bbb169cd --- /dev/null +++ b/tutorial/alterl/server.erl @@ -0,0 +1,63 @@ +-module(server). + +-include("calculator_thrift.hrl"). + +-export([start/0, start/1, handle_function/2, + stop/1, ping/0, add/2, calculate/2, getStruct/1, zip/0]). + +debug(Format, Data) -> + error_logger:info_msg(Format, Data). + +ping() -> + debug("ping()",[]), + ok. + +add(N1, N2) -> + debug("add(~p,~p)",[N1,N2]), + N1+N2. + +calculate(Logid, Work) -> + { Op, Num1, Num2 } = { Work#work.op, Work#work.num1, Work#work.num2 }, + debug("calculate(~p, {~p,~p,~p})", [Logid, Op, Num1, Num2]), + case Op of + ?tutorial_ADD -> Num1 + Num2; + ?tutorial_SUBTRACT -> Num1 - Num2; + ?tutorial_MULTIPLY -> Num1 * Num2; + + ?tutorial_DIVIDE when Num2 == 0 -> + throw(#invalidOperation{what=Op, why="Cannot divide by 0"}); + ?tutorial_DIVIDE -> + Num1 div Num2; + + _Else -> + throw(#invalidOperation{what=Op, why="Invalid operation"}) + end. + +getStruct(Key) -> + debug("getStruct(~p)", [Key]), + #sharedStruct{key=Key, value="RARG"}. + +zip() -> + debug("zip", []), + ok. + +%% + +start() -> + start(9999). + +start(Port) -> + Handler = ?MODULE, + thrift_socket_server:start([{handler, Handler}, + {service, calculator_thrift}, + {port, Port}, + {name, tutorial_server}]). + +stop(Server) -> + thrift_socket_server:stop(Server). + +handle_function(Function, Args) when is_atom(Function), is_tuple(Args) -> + case apply(?MODULE, Function, tuple_to_list(Args)) of + ok -> ok; + Reply -> {reply, Reply} + end. diff --git a/tutorial/alterl/server.sh b/tutorial/alterl/server.sh new file mode 100755 index 00000000..857d9d39 --- /dev/null +++ b/tutorial/alterl/server.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +ERL_THRIFT=../../lib/alterl + +if ! [ -d ${ERL_THRIFT}/ebin ]; then + echo "Please build the Thrift library by running \`make' in ${ERL_THRIFT}" + exit 1 +fi + +if ! [ -d ../gen-erl ]; then + echo "Please run thrift first to generate ../gen-erl/" + exit 1 +fi + + +erlc -I ${ERL_THRIFT}/include -I ../gen-erl -o ../gen-erl ../gen-erl/*.erl && + erlc -I ${ERL_THRIFT}/include -I ../gen-erl *.erl && + erl +K true -pa ${ERL_THRIFT}/ebin -pa ../gen-erl -config server.config -- 2.17.1