blob: 1f43b72ffbd93db685f35b411d519c946ef5aebc [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001%%
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
Bosky43509df2015-01-04 23:14:11 +053020-module(test_thrift_server).
David Reiss57b4d9a2008-06-10 22:58:39 +000021
David Reissa508b332010-08-30 22:05:41 +000022-export([go/0, go/1, start_link/2, handle_function/2]).
David Reiss57b4d9a2008-06-10 22:58:39 +000023
Jens Geyer6d15c302014-10-02 10:03:09 +020024-include("gen-erl/thrift_test_types.hrl").
David Reiss57b4d9a2008-06-10 22:58:39 +000025
David Reissa508b332010-08-30 22:05:41 +000026-record(options, {port = 9090,
27 server_opts = []}).
28
29parse_args(Args) -> parse_args(Args, #options{}).
30parse_args([], Opts) -> Opts;
31parse_args([Head | Rest], Opts) ->
32 NewOpts =
33 case catch list_to_integer(Head) of
34 Port when is_integer(Port) ->
35 Opts#options{port = Port};
36 _Else ->
37 case Head of
38 "framed" ->
39 Opts#options{server_opts = [{framed, true} | Opts#options.server_opts]};
40 "" ->
41 Opts;
42 _Else ->
43 erlang:error({bad_arg, Head})
44 end
45 end,
46 parse_args(Rest, NewOpts).
47
48go() -> go([]).
49go(Args) ->
50 #options{port = Port, server_opts = ServerOpts} = parse_args(Args),
51 spawn(fun() -> start_link(Port, ServerOpts), receive after infinity -> ok end end).
52
53start_link(Port, ServerOpts) ->
David Reiss6204bb12010-08-30 22:05:17 +000054 thrift_socket_server:start([{handler, ?MODULE},
Roger Meier4702fe62015-02-15 21:17:30 +010055 {service, thrift_test_thrift},
David Reissa508b332010-08-30 22:05:41 +000056 {port, Port}] ++
57 ServerOpts).
David Reiss57b4d9a2008-06-10 22:58:39 +000058
59
60handle_function(testVoid, {}) ->
61 io:format("testVoid~n"),
62 ok;
63
David Reissa59191b2008-06-11 01:16:09 +000064handle_function(testString, {S}) when is_binary(S) ->
David Reiss57b4d9a2008-06-10 22:58:39 +000065 io:format("testString: ~p~n", [S]),
66 {reply, S};
67
68handle_function(testByte, {I8}) when is_integer(I8) ->
69 io:format("testByte: ~p~n", [I8]),
70 {reply, I8};
71
72handle_function(testI32, {I32}) when is_integer(I32) ->
73 io:format("testI32: ~p~n", [I32]),
74 {reply, I32};
75
76handle_function(testI64, {I64}) when is_integer(I64) ->
77 io:format("testI64: ~p~n", [I64]),
78 {reply, I64};
79
80handle_function(testDouble, {Double}) when is_float(Double) ->
81 io:format("testDouble: ~p~n", [Double]),
82 {reply, Double};
83
Jens Geyer8bcfdd92014-12-14 03:14:26 +010084handle_function(testBinary, {S}) when is_binary(S) ->
85 io:format("testBinary: ~p~n", [S]),
86 {reply, S};
87
David Reiss57b4d9a2008-06-10 22:58:39 +000088handle_function(testStruct,
Jens Geyer6d15c302014-10-02 10:03:09 +020089 {Struct = #'Xtruct'{string_thing = String,
David Reiss57b4d9a2008-06-10 22:58:39 +000090 byte_thing = Byte,
91 i32_thing = I32,
92 i64_thing = I64}})
David Reissa59191b2008-06-11 01:16:09 +000093when is_binary(String),
David Reiss57b4d9a2008-06-10 22:58:39 +000094 is_integer(Byte),
95 is_integer(I32),
96 is_integer(I64) ->
97 io:format("testStruct: ~p~n", [Struct]),
98 {reply, Struct};
99
100handle_function(testNest,
Jens Geyer6d15c302014-10-02 10:03:09 +0200101 {Nest}) when is_record(Nest, 'Xtruct2'),
102 is_record(Nest#'Xtruct2'.struct_thing, 'Xtruct') ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000103 io:format("testNest: ~p~n", [Nest]),
104 {reply, Nest};
105
106handle_function(testMap, {Map}) ->
107 io:format("testMap: ~p~n", [dict:to_list(Map)]),
108 {reply, Map};
109
110handle_function(testSet, {Set}) ->
111 true = sets:is_set(Set),
112 io:format("testSet: ~p~n", [sets:to_list(Set)]),
113 {reply, Set};
114
115handle_function(testList, {List}) when is_list(List) ->
116 io:format("testList: ~p~n", [List]),
117 {reply, List};
118
119handle_function(testEnum, {Enum}) when is_integer(Enum) ->
120 io:format("testEnum: ~p~n", [Enum]),
121 {reply, Enum};
122
123handle_function(testTypedef, {UserID}) when is_integer(UserID) ->
124 io:format("testTypedef: ~p~n", [UserID]),
125 {reply, UserID};
126
127handle_function(testMapMap, {Hello}) ->
128 io:format("testMapMap: ~p~n", [Hello]),
129
130 PosList = [{I, I} || I <- lists:seq(1, 5)],
131 NegList = [{-I, -I} || I <- lists:seq(1, 5)],
132
133 MapMap = dict:from_list([{4, dict:from_list(PosList)},
134 {-4, dict:from_list(NegList)}]),
135 {reply, MapMap};
136
Jens Geyer6d15c302014-10-02 10:03:09 +0200137handle_function(testInsanity, {Insanity}) when is_record(Insanity, 'Insanity') ->
138 Hello = #'Xtruct'{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000139 byte_thing = 2,
140 i32_thing = 2,
141 i64_thing = 2},
142
Jens Geyer6d15c302014-10-02 10:03:09 +0200143 Goodbye = #'Xtruct'{string_thing = <<"Goodbye4">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000144 byte_thing = 4,
145 i32_thing = 4,
146 i64_thing = 4},
Jens Geyer6d15c302014-10-02 10:03:09 +0200147 Crazy = #'Insanity'{
148 userMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_EIGHT, 8}]),
David Reiss57b4d9a2008-06-10 22:58:39 +0000149 xtructs = [Goodbye]
150 },
151
Jens Geyer6d15c302014-10-02 10:03:09 +0200152 Looney = #'Insanity'{
153 userMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_FIVE, 5}]),
David Reiss57b4d9a2008-06-10 22:58:39 +0000154 xtructs = [Hello]
155 },
156
Jens Geyer6d15c302014-10-02 10:03:09 +0200157 FirstMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_TWO, Crazy},
158 {?THRIFT_TEST_NUMBERZ_THREE, Crazy}]),
David Reiss57b4d9a2008-06-10 22:58:39 +0000159
Jens Geyer6d15c302014-10-02 10:03:09 +0200160 SecondMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_SIX, Looney}]),
David Reiss8f943142010-08-30 22:05:01 +0000161
David Reiss57b4d9a2008-06-10 22:58:39 +0000162 Insane = dict:from_list([{1, FirstMap},
163 {2, SecondMap}]),
David Reiss8f943142010-08-30 22:05:01 +0000164
David Reiss57b4d9a2008-06-10 22:58:39 +0000165 io:format("Return = ~p~n", [Insane]),
David Reiss8f943142010-08-30 22:05:01 +0000166
David Reiss57b4d9a2008-06-10 22:58:39 +0000167 {reply, Insane};
168
169handle_function(testMulti, Args = {Arg0, Arg1, Arg2, _Arg3, Arg4, Arg5})
170 when is_integer(Arg0),
171 is_integer(Arg1),
172 is_integer(Arg2),
173 is_integer(Arg4),
174 is_integer(Arg5) ->
175
176 io:format("testMulti(~p)~n", [Args]),
Jens Geyer6d15c302014-10-02 10:03:09 +0200177 {reply, #'Xtruct'{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000178 byte_thing = Arg0,
179 i32_thing = Arg1,
180 i64_thing = Arg2}};
181
David Reissa59191b2008-06-11 01:16:09 +0000182handle_function(testException, {String}) when is_binary(String) ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000183 io:format("testException(~p)~n", [String]),
184 case String of
David Reisscb137292008-06-11 01:16:15 +0000185 <<"Xception">> ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200186 throw(#'Xception'{errorCode = 1001,
David Reissa94514f2010-08-30 22:05:16 +0000187 message = String});
David Reiss57b4d9a2008-06-10 22:58:39 +0000188 _ ->
189 ok
190 end;
191
192handle_function(testMultiException, {Arg0, Arg1}) ->
193 io:format("testMultiException(~p, ~p)~n", [Arg0, Arg1]),
194 case Arg0 of
David Reisscb137292008-06-11 01:16:15 +0000195 <<"Xception">> ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200196 throw(#'Xception'{errorCode = 1001,
David Reisscb137292008-06-11 01:16:15 +0000197 message = <<"This is an Xception">>});
198 <<"Xception2">> ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200199 throw(#'Xception2'{errorCode = 2002,
David Reiss57b4d9a2008-06-10 22:58:39 +0000200 struct_thing =
Jens Geyer6d15c302014-10-02 10:03:09 +0200201 #'Xtruct'{string_thing = <<"This is an Xception2">>}});
David Reiss57b4d9a2008-06-10 22:58:39 +0000202 _ ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200203 {reply, #'Xtruct'{string_thing = Arg1}}
David Reisscdf8d192008-06-11 00:57:35 +0000204 end;
205
David Reiss6ce401d2009-03-24 20:01:58 +0000206handle_function(testOneway, {Seconds}) ->
David Reisscdf8d192008-06-11 00:57:35 +0000207 timer:sleep(1000 * Seconds),
208 ok.