summaryrefslogtreecommitdiff
path: root/tutorial/erl
diff options
context:
space:
mode:
authorAnthony F. Molinaro <molinaro@apache.org>2011-09-18 04:57:50 +0000
committerAnthony F. Molinaro <molinaro@apache.org>2011-09-18 04:57:50 +0000
commita653067e8ca6d56116af3ed4613e6f1b26363251 (patch)
tree7c8945ca04e1aaf7ea12ae572c023328e446c99e /tutorial/erl
parent84e4a3c1819346dc5e5fcc4c4701e6c43afa1041 (diff)
downloadthrift-a653067e8ca6d56116af3ed4613e6f1b26363251.tar.gz
THRIFT-1227 - erlang implementation of thrift json protocol
git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1172199 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'tutorial/erl')
-rw-r--r--tutorial/erl/json_client.erl89
1 files changed, 89 insertions, 0 deletions
diff --git a/tutorial/erl/json_client.erl b/tutorial/erl/json_client.erl
new file mode 100644
index 000000000..524e9aea7
--- /dev/null
+++ b/tutorial/erl/json_client.erl
@@ -0,0 +1,89 @@
+%%
+%% Licensed to the Apache Software Foundation (ASF) under one
+%% or more contributor license agreements. See the NOTICE file
+%% distributed with this work for additional information
+%% regarding copyright ownership. The ASF licenses this file
+%% to you under the Apache License, Version 2.0 (the
+%% "License"); you may not use this file except in compliance
+%% with the License. You may obtain a copy of the License at
+%%
+%% http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied. See the License for the
+%% specific language governing permissions and limitations
+%% under the License.
+%%
+%% The JSON protocol over HTTP implementation was created by
+%% Peter Neumark <neumark.peter@gmail.com> based on
+%% the binary protocol + socket tutorial. Use with the same server
+%% that the Javascript tutorial uses!
+
+-module(json_client).
+
+-include("calculator_thrift.hrl").
+
+-export([t/0]).
+
+%% Client constructor for the common-case of socket transports
+%% with the binary protocol
+new_client(Host, Path, Service, _Options) ->
+ {ProtoOpts, TransOpts} = {[],[]},
+ TransportFactory = fun() -> thrift_http_transport:new(Host, Path, TransOpts) end,
+ {ok, ProtocolFactory} = thrift_json_protocol:new_protocol_factory(
+ TransportFactory, ProtoOpts),
+ {ok, Protocol} = ProtocolFactory(),
+ thrift_client:new(Protocol, Service).
+
+p(X) ->
+ io:format("~p~n", [X]),
+ ok.
+
+t() ->
+ inets:start(),
+ {ok, Client0} = new_client("127.0.0.1:8088", "/thrift/service/tutorial/",
+ calculator_thrift,
+ []),
+ {Client1, {ok, ok}} = thrift_client:call(Client0, ping, []),
+ io:format("ping~n", []),
+
+ {Client2, {ok, Sum}} = thrift_client:call(Client1, add, [1, 1]),
+ io:format("1+1=~p~n", [Sum]),
+
+ {Client3, {ok, Sum1}} = thrift_client:call(Client2, add, [1, 4]),
+ io:format("1+4=~p~n", [Sum1]),
+
+ Work = #work{op=?tutorial_Operation_SUBTRACT,
+ num1=15,
+ num2=10},
+ {Client4, {ok, Diff}} = thrift_client:call(Client3, calculate, [1, Work]),
+ io:format("15-10=~p~n", [Diff]),
+
+ {Client5, {ok, Log}} = thrift_client:call(Client4, getStruct, [1]),
+ io:format("Log: ~p~n", [Log]),
+
+ Client6 =
+ try
+ Work1 = #work{op=?tutorial_Operation_DIVIDE,
+ num1=1,
+ num2=0},
+ {ClientS1, {ok, _Quot}} = thrift_client:call(Client5, calculate, [2, Work1]),
+
+ io:format("LAME: exception handling is broken~n", []),
+ ClientS1
+ catch
+ throw:{ClientS2, Z} ->
+ io:format("Got exception where expecting - the " ++
+ "following is NOT a problem!!!~n"),
+ p(Z),
+ ClientS2
+ end,
+
+
+ {Client7, {ok, ok}} = thrift_client:call(Client6, zip, []),
+ io:format("zip~n", []),
+
+ {_Client8, ok} = thrift_client:close(Client7),
+ ok.