| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | %% | 
|  | 2 | %% Licensed to the Apache Software Foundation (ASF) under one | 
|  | 3 | %% or more contributor license agreements. See the NOTICE file | 
|  | 4 | %% distributed with this work for additional information | 
|  | 5 | %% regarding copyright ownership. The ASF licenses this file | 
|  | 6 | %% to you under the Apache License, Version 2.0 (the | 
|  | 7 | %% "License"); you may not use this file except in compliance | 
|  | 8 | %% with the License. You may obtain a copy of the License at | 
|  | 9 | %% | 
|  | 10 | %%   http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 11 | %% | 
|  | 12 | %% Unless required by applicable law or agreed to in writing, | 
|  | 13 | %% software distributed under the License is distributed on an | 
|  | 14 | %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
|  | 15 | %% KIND, either express or implied. See the License for the | 
|  | 16 | %% specific language governing permissions and limitations | 
|  | 17 | %% under the License. | 
|  | 18 | %% | 
|  | 19 |  | 
| David Reiss | 8a162a5 | 2008-06-11 01:03:16 +0000 | [diff] [blame] | 20 | -module(client). | 
|  | 21 |  | 
|  | 22 | -include("calculator_thrift.hrl"). | 
|  | 23 |  | 
|  | 24 | -export([t/0]). | 
|  | 25 |  | 
|  | 26 | p(X) -> | 
|  | 27 | io:format("~p~n", [X]), | 
|  | 28 | ok. | 
|  | 29 |  | 
|  | 30 | t() -> | 
|  | 31 | Port = 9999, | 
| David Reiss | 8a162a5 | 2008-06-11 01:03:16 +0000 | [diff] [blame] | 32 |  | 
| David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 33 | {ok, Client0} = thrift_client_util:new("127.0.0.1", | 
|  | 34 | Port, | 
|  | 35 | calculator_thrift, | 
|  | 36 | []), | 
|  | 37 |  | 
|  | 38 | {Client1, {ok, ok}} = thrift_client:call(Client0, ping, []), | 
| David Reiss | 8a162a5 | 2008-06-11 01:03:16 +0000 | [diff] [blame] | 39 | io:format("ping~n", []), | 
|  | 40 |  | 
| David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 41 | {Client2, {ok, Sum}} = thrift_client:call(Client1, add,  [1, 1]), | 
| David Reiss | 8a162a5 | 2008-06-11 01:03:16 +0000 | [diff] [blame] | 42 | io:format("1+1=~p~n", [Sum]), | 
|  | 43 |  | 
| David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 44 | {Client3, {ok, Sum1}} = thrift_client:call(Client2, add, [1, 4]), | 
| David Reiss | 8a162a5 | 2008-06-11 01:03:16 +0000 | [diff] [blame] | 45 | io:format("1+4=~p~n", [Sum1]), | 
|  | 46 |  | 
| Anthony F. Molinaro | 10a8743 | 2011-02-16 05:54:17 +0000 | [diff] [blame] | 47 | Work = #work{op=?tutorial_Operation_SUBTRACT, | 
| David Reiss | 8a162a5 | 2008-06-11 01:03:16 +0000 | [diff] [blame] | 48 | num1=15, | 
|  | 49 | num2=10}, | 
| David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 50 | {Client4, {ok, Diff}} = thrift_client:call(Client3, calculate, [1, Work]), | 
| David Reiss | 8a162a5 | 2008-06-11 01:03:16 +0000 | [diff] [blame] | 51 | io:format("15-10=~p~n", [Diff]), | 
|  | 52 |  | 
| David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 53 | {Client5, {ok, Log}} = thrift_client:call(Client4, getStruct, [1]), | 
| David Reiss | 8a162a5 | 2008-06-11 01:03:16 +0000 | [diff] [blame] | 54 | io:format("Log: ~p~n", [Log]), | 
|  | 55 |  | 
| David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 56 | Client6 = | 
|  | 57 | try | 
| Anthony F. Molinaro | 10a8743 | 2011-02-16 05:54:17 +0000 | [diff] [blame] | 58 | Work1 = #work{op=?tutorial_Operation_DIVIDE, | 
| David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 59 | num1=1, | 
|  | 60 | num2=0}, | 
|  | 61 | {ClientS1, {ok, _Quot}} = thrift_client:call(Client5, calculate, [2, Work1]), | 
| David Reiss | 8a162a5 | 2008-06-11 01:03:16 +0000 | [diff] [blame] | 62 |  | 
| David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 63 | io:format("LAME: exception handling is broken~n", []), | 
|  | 64 | ClientS1 | 
|  | 65 | catch | 
|  | 66 | throw:{ClientS2, Z} -> | 
|  | 67 | io:format("Got exception where expecting - the " ++ | 
|  | 68 | "following is NOT a problem!!!~n"), | 
|  | 69 | p(Z), | 
|  | 70 | ClientS2 | 
|  | 71 | end, | 
| David Reiss | 8a162a5 | 2008-06-11 01:03:16 +0000 | [diff] [blame] | 72 |  | 
|  | 73 |  | 
| David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 74 | {Client7, {ok, ok}} = thrift_client:call(Client6, zip, []), | 
| David Reiss | 8a162a5 | 2008-06-11 01:03:16 +0000 | [diff] [blame] | 75 | io:format("zip~n", []), | 
|  | 76 |  | 
| David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 77 | {_Client8, ok} = thrift_client:close(Client7), | 
| David Reiss | 8a162a5 | 2008-06-11 01:03:16 +0000 | [diff] [blame] | 78 | ok. |