blob: 51457f50ff5a5359a921fa6439318053fc5eea54 [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
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090022-export([start/0, start/1, start_link/2, handle_function/2]).
David Reiss57b4d9a2008-06-10 22:58:39 +000023
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090024-include("thrift_constants.hrl").
Jens Geyer6d15c302014-10-02 10:03:09 +020025-include("gen-erl/thrift_test_types.hrl").
David Reiss57b4d9a2008-06-10 22:58:39 +000026
David Reissa508b332010-08-30 22:05:41 +000027-record(options, {port = 9090,
28 server_opts = []}).
29
30parse_args(Args) -> parse_args(Args, #options{}).
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090031parse_args([], Opts) ->
32 Opts;
David Reissa508b332010-08-30 22:05:41 +000033parse_args([Head | Rest], Opts) ->
34 NewOpts =
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090035 case Head of
36 "--port=" ++ Port ->
37 case string:to_integer(Port) of
38 {IntPort,_} when is_integer(IntPort) ->
39 Opts#options{port = IntPort};
40 _Else ->
41 erlang:error({bad_arg, Head})
42 end;
43 "--transport=" ++ Trans ->
44 case Trans of
David Reissa508b332010-08-30 22:05:41 +000045 "framed" ->
46 Opts#options{server_opts = [{framed, true} | Opts#options.server_opts]};
David Reissa508b332010-08-30 22:05:41 +000047 _Else ->
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090048 Opts
49 end;
50 "--protocol=" ++ _ -> Opts;
51 _Else ->
52 erlang:error({bad_arg, Head})
David Reissa508b332010-08-30 22:05:41 +000053 end,
54 parse_args(Rest, NewOpts).
55
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090056start() -> start(init:get_plain_arguments()).
57start(Args) ->
David Reissa508b332010-08-30 22:05:41 +000058 #options{port = Port, server_opts = ServerOpts} = parse_args(Args),
59 spawn(fun() -> start_link(Port, ServerOpts), receive after infinity -> ok end end).
60
61start_link(Port, ServerOpts) ->
David Reiss6204bb12010-08-30 22:05:17 +000062 thrift_socket_server:start([{handler, ?MODULE},
Roger Meier4702fe62015-02-15 21:17:30 +010063 {service, thrift_test_thrift},
David Reissa508b332010-08-30 22:05:41 +000064 {port, Port}] ++
65 ServerOpts).
David Reiss57b4d9a2008-06-10 22:58:39 +000066
67
68handle_function(testVoid, {}) ->
69 io:format("testVoid~n"),
70 ok;
71
David Reissa59191b2008-06-11 01:16:09 +000072handle_function(testString, {S}) when is_binary(S) ->
David Reiss57b4d9a2008-06-10 22:58:39 +000073 io:format("testString: ~p~n", [S]),
74 {reply, S};
75
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090076handle_function(testBool, {B}) when is_boolean(B) ->
77 io:format("testBool: ~p~n", [B]),
78 {reply, B};
79
David Reiss57b4d9a2008-06-10 22:58:39 +000080handle_function(testByte, {I8}) when is_integer(I8) ->
81 io:format("testByte: ~p~n", [I8]),
82 {reply, I8};
83
84handle_function(testI32, {I32}) when is_integer(I32) ->
85 io:format("testI32: ~p~n", [I32]),
86 {reply, I32};
87
88handle_function(testI64, {I64}) when is_integer(I64) ->
89 io:format("testI64: ~p~n", [I64]),
90 {reply, I64};
91
92handle_function(testDouble, {Double}) when is_float(Double) ->
93 io:format("testDouble: ~p~n", [Double]),
94 {reply, Double};
95
Jens Geyer8bcfdd92014-12-14 03:14:26 +010096handle_function(testBinary, {S}) when is_binary(S) ->
97 io:format("testBinary: ~p~n", [S]),
98 {reply, S};
99
David Reiss57b4d9a2008-06-10 22:58:39 +0000100handle_function(testStruct,
Jens Geyer6d15c302014-10-02 10:03:09 +0200101 {Struct = #'Xtruct'{string_thing = String,
David Reiss57b4d9a2008-06-10 22:58:39 +0000102 byte_thing = Byte,
103 i32_thing = I32,
104 i64_thing = I64}})
David Reissa59191b2008-06-11 01:16:09 +0000105when is_binary(String),
David Reiss57b4d9a2008-06-10 22:58:39 +0000106 is_integer(Byte),
107 is_integer(I32),
108 is_integer(I64) ->
109 io:format("testStruct: ~p~n", [Struct]),
110 {reply, Struct};
111
112handle_function(testNest,
Jens Geyer6d15c302014-10-02 10:03:09 +0200113 {Nest}) when is_record(Nest, 'Xtruct2'),
114 is_record(Nest#'Xtruct2'.struct_thing, 'Xtruct') ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000115 io:format("testNest: ~p~n", [Nest]),
116 {reply, Nest};
117
118handle_function(testMap, {Map}) ->
119 io:format("testMap: ~p~n", [dict:to_list(Map)]),
120 {reply, Map};
121
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900122handle_function(testStringMap, {Map}) ->
123 io:format("testStringMap: ~p~n", [dict:to_list(Map)]),
124 {reply, Map};
125
David Reiss57b4d9a2008-06-10 22:58:39 +0000126handle_function(testSet, {Set}) ->
127 true = sets:is_set(Set),
128 io:format("testSet: ~p~n", [sets:to_list(Set)]),
129 {reply, Set};
130
131handle_function(testList, {List}) when is_list(List) ->
132 io:format("testList: ~p~n", [List]),
133 {reply, List};
134
135handle_function(testEnum, {Enum}) when is_integer(Enum) ->
136 io:format("testEnum: ~p~n", [Enum]),
137 {reply, Enum};
138
139handle_function(testTypedef, {UserID}) when is_integer(UserID) ->
140 io:format("testTypedef: ~p~n", [UserID]),
141 {reply, UserID};
142
143handle_function(testMapMap, {Hello}) ->
144 io:format("testMapMap: ~p~n", [Hello]),
145
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900146 PosList = [{I, I} || I <- lists:seq(1, 4)],
147 NegList = [{-I, -I} || I <- lists:seq(1, 4)],
David Reiss57b4d9a2008-06-10 22:58:39 +0000148
149 MapMap = dict:from_list([{4, dict:from_list(PosList)},
150 {-4, dict:from_list(NegList)}]),
151 {reply, MapMap};
152
Jens Geyer6d15c302014-10-02 10:03:09 +0200153handle_function(testInsanity, {Insanity}) when is_record(Insanity, 'Insanity') ->
154 Hello = #'Xtruct'{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000155 byte_thing = 2,
156 i32_thing = 2,
157 i64_thing = 2},
158
Jens Geyer6d15c302014-10-02 10:03:09 +0200159 Goodbye = #'Xtruct'{string_thing = <<"Goodbye4">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000160 byte_thing = 4,
161 i32_thing = 4,
162 i64_thing = 4},
Jens Geyer6d15c302014-10-02 10:03:09 +0200163 Crazy = #'Insanity'{
164 userMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_EIGHT, 8}]),
David Reiss57b4d9a2008-06-10 22:58:39 +0000165 xtructs = [Goodbye]
166 },
167
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900168 Looney = #'Insanity'{},
David Reiss57b4d9a2008-06-10 22:58:39 +0000169
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900170 FirstMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_TWO, Insanity},
171 {?THRIFT_TEST_NUMBERZ_THREE, Insanity}]),
David Reiss57b4d9a2008-06-10 22:58:39 +0000172
Jens Geyer6d15c302014-10-02 10:03:09 +0200173 SecondMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_SIX, Looney}]),
David Reiss8f943142010-08-30 22:05:01 +0000174
David Reiss57b4d9a2008-06-10 22:58:39 +0000175 Insane = dict:from_list([{1, FirstMap},
176 {2, SecondMap}]),
David Reiss8f943142010-08-30 22:05:01 +0000177
David Reiss57b4d9a2008-06-10 22:58:39 +0000178 io:format("Return = ~p~n", [Insane]),
David Reiss8f943142010-08-30 22:05:01 +0000179
David Reiss57b4d9a2008-06-10 22:58:39 +0000180 {reply, Insane};
181
182handle_function(testMulti, Args = {Arg0, Arg1, Arg2, _Arg3, Arg4, Arg5})
183 when is_integer(Arg0),
184 is_integer(Arg1),
185 is_integer(Arg2),
186 is_integer(Arg4),
187 is_integer(Arg5) ->
188
189 io:format("testMulti(~p)~n", [Args]),
Jens Geyer6d15c302014-10-02 10:03:09 +0200190 {reply, #'Xtruct'{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000191 byte_thing = Arg0,
192 i32_thing = Arg1,
193 i64_thing = Arg2}};
194
David Reissa59191b2008-06-11 01:16:09 +0000195handle_function(testException, {String}) when is_binary(String) ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000196 io:format("testException(~p)~n", [String]),
197 case String of
David Reisscb137292008-06-11 01:16:15 +0000198 <<"Xception">> ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200199 throw(#'Xception'{errorCode = 1001,
David Reissa94514f2010-08-30 22:05:16 +0000200 message = String});
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900201 <<"TException">> ->
202 throw({?TApplicationException_Structure});
David Reiss57b4d9a2008-06-10 22:58:39 +0000203 _ ->
204 ok
205 end;
206
207handle_function(testMultiException, {Arg0, Arg1}) ->
208 io:format("testMultiException(~p, ~p)~n", [Arg0, Arg1]),
209 case Arg0 of
David Reisscb137292008-06-11 01:16:15 +0000210 <<"Xception">> ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200211 throw(#'Xception'{errorCode = 1001,
David Reisscb137292008-06-11 01:16:15 +0000212 message = <<"This is an Xception">>});
213 <<"Xception2">> ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200214 throw(#'Xception2'{errorCode = 2002,
David Reiss57b4d9a2008-06-10 22:58:39 +0000215 struct_thing =
Jens Geyer6d15c302014-10-02 10:03:09 +0200216 #'Xtruct'{string_thing = <<"This is an Xception2">>}});
David Reiss57b4d9a2008-06-10 22:58:39 +0000217 _ ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200218 {reply, #'Xtruct'{string_thing = Arg1}}
David Reisscdf8d192008-06-11 00:57:35 +0000219 end;
220
David Reiss6ce401d2009-03-24 20:01:58 +0000221handle_function(testOneway, {Seconds}) ->
alisdair sullivan7bdba5c2014-09-30 22:03:34 -0700222 io:format("testOneway: ~p~n", [Seconds]),
David Reisscdf8d192008-06-11 00:57:35 +0000223 timer:sleep(1000 * Seconds),
224 ok.