blob: 884eb9e60b12b6a4a985deb4ac62826d45c96332 [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;
David Robakowskia7d6a972013-08-07 05:51:00 +020050 "--ssl" ->
51 ssl:start(),
52 SslOptions =
53 {ssloptions, [
54 {certfile, "../keys/server.crt"}
55 ,{keyfile, "../keys/server.key"}
56 ]},
57 Opts#options{server_opts = [{ssltransport, true} | [SslOptions | Opts#options.server_opts]]};
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090058 "--protocol=" ++ _ -> Opts;
59 _Else ->
60 erlang:error({bad_arg, Head})
David Reissa508b332010-08-30 22:05:41 +000061 end,
62 parse_args(Rest, NewOpts).
63
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090064start() -> start(init:get_plain_arguments()).
65start(Args) ->
David Reissa508b332010-08-30 22:05:41 +000066 #options{port = Port, server_opts = ServerOpts} = parse_args(Args),
67 spawn(fun() -> start_link(Port, ServerOpts), receive after infinity -> ok end end).
68
69start_link(Port, ServerOpts) ->
David Reiss6204bb12010-08-30 22:05:17 +000070 thrift_socket_server:start([{handler, ?MODULE},
Roger Meier4702fe62015-02-15 21:17:30 +010071 {service, thrift_test_thrift},
David Reissa508b332010-08-30 22:05:41 +000072 {port, Port}] ++
73 ServerOpts).
David Reiss57b4d9a2008-06-10 22:58:39 +000074
75
76handle_function(testVoid, {}) ->
77 io:format("testVoid~n"),
78 ok;
79
David Reissa59191b2008-06-11 01:16:09 +000080handle_function(testString, {S}) when is_binary(S) ->
David Reiss57b4d9a2008-06-10 22:58:39 +000081 io:format("testString: ~p~n", [S]),
82 {reply, S};
83
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090084handle_function(testBool, {B}) when is_boolean(B) ->
85 io:format("testBool: ~p~n", [B]),
86 {reply, B};
87
David Reiss57b4d9a2008-06-10 22:58:39 +000088handle_function(testByte, {I8}) when is_integer(I8) ->
89 io:format("testByte: ~p~n", [I8]),
90 {reply, I8};
91
92handle_function(testI32, {I32}) when is_integer(I32) ->
93 io:format("testI32: ~p~n", [I32]),
94 {reply, I32};
95
96handle_function(testI64, {I64}) when is_integer(I64) ->
97 io:format("testI64: ~p~n", [I64]),
98 {reply, I64};
99
100handle_function(testDouble, {Double}) when is_float(Double) ->
101 io:format("testDouble: ~p~n", [Double]),
102 {reply, Double};
103
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100104handle_function(testBinary, {S}) when is_binary(S) ->
105 io:format("testBinary: ~p~n", [S]),
106 {reply, S};
107
David Reiss57b4d9a2008-06-10 22:58:39 +0000108handle_function(testStruct,
Jens Geyer6d15c302014-10-02 10:03:09 +0200109 {Struct = #'Xtruct'{string_thing = String,
David Reiss57b4d9a2008-06-10 22:58:39 +0000110 byte_thing = Byte,
111 i32_thing = I32,
112 i64_thing = I64}})
David Reissa59191b2008-06-11 01:16:09 +0000113when is_binary(String),
David Reiss57b4d9a2008-06-10 22:58:39 +0000114 is_integer(Byte),
115 is_integer(I32),
116 is_integer(I64) ->
117 io:format("testStruct: ~p~n", [Struct]),
118 {reply, Struct};
119
120handle_function(testNest,
Jens Geyer6d15c302014-10-02 10:03:09 +0200121 {Nest}) when is_record(Nest, 'Xtruct2'),
122 is_record(Nest#'Xtruct2'.struct_thing, 'Xtruct') ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000123 io:format("testNest: ~p~n", [Nest]),
124 {reply, Nest};
125
126handle_function(testMap, {Map}) ->
127 io:format("testMap: ~p~n", [dict:to_list(Map)]),
128 {reply, Map};
129
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900130handle_function(testStringMap, {Map}) ->
131 io:format("testStringMap: ~p~n", [dict:to_list(Map)]),
132 {reply, Map};
133
David Reiss57b4d9a2008-06-10 22:58:39 +0000134handle_function(testSet, {Set}) ->
135 true = sets:is_set(Set),
136 io:format("testSet: ~p~n", [sets:to_list(Set)]),
137 {reply, Set};
138
139handle_function(testList, {List}) when is_list(List) ->
140 io:format("testList: ~p~n", [List]),
141 {reply, List};
142
143handle_function(testEnum, {Enum}) when is_integer(Enum) ->
144 io:format("testEnum: ~p~n", [Enum]),
145 {reply, Enum};
146
147handle_function(testTypedef, {UserID}) when is_integer(UserID) ->
148 io:format("testTypedef: ~p~n", [UserID]),
149 {reply, UserID};
150
151handle_function(testMapMap, {Hello}) ->
152 io:format("testMapMap: ~p~n", [Hello]),
153
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900154 PosList = [{I, I} || I <- lists:seq(1, 4)],
155 NegList = [{-I, -I} || I <- lists:seq(1, 4)],
David Reiss57b4d9a2008-06-10 22:58:39 +0000156
157 MapMap = dict:from_list([{4, dict:from_list(PosList)},
158 {-4, dict:from_list(NegList)}]),
159 {reply, MapMap};
160
Jens Geyer6d15c302014-10-02 10:03:09 +0200161handle_function(testInsanity, {Insanity}) when is_record(Insanity, 'Insanity') ->
162 Hello = #'Xtruct'{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000163 byte_thing = 2,
164 i32_thing = 2,
165 i64_thing = 2},
166
Jens Geyer6d15c302014-10-02 10:03:09 +0200167 Goodbye = #'Xtruct'{string_thing = <<"Goodbye4">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000168 byte_thing = 4,
169 i32_thing = 4,
170 i64_thing = 4},
Jens Geyer6d15c302014-10-02 10:03:09 +0200171 Crazy = #'Insanity'{
172 userMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_EIGHT, 8}]),
David Reiss57b4d9a2008-06-10 22:58:39 +0000173 xtructs = [Goodbye]
174 },
175
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900176 Looney = #'Insanity'{},
David Reiss57b4d9a2008-06-10 22:58:39 +0000177
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900178 FirstMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_TWO, Insanity},
179 {?THRIFT_TEST_NUMBERZ_THREE, Insanity}]),
David Reiss57b4d9a2008-06-10 22:58:39 +0000180
Jens Geyer6d15c302014-10-02 10:03:09 +0200181 SecondMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_SIX, Looney}]),
David Reiss8f943142010-08-30 22:05:01 +0000182
David Reiss57b4d9a2008-06-10 22:58:39 +0000183 Insane = dict:from_list([{1, FirstMap},
184 {2, SecondMap}]),
David Reiss8f943142010-08-30 22:05:01 +0000185
David Reiss57b4d9a2008-06-10 22:58:39 +0000186 io:format("Return = ~p~n", [Insane]),
David Reiss8f943142010-08-30 22:05:01 +0000187
David Reiss57b4d9a2008-06-10 22:58:39 +0000188 {reply, Insane};
189
190handle_function(testMulti, Args = {Arg0, Arg1, Arg2, _Arg3, Arg4, Arg5})
191 when is_integer(Arg0),
192 is_integer(Arg1),
193 is_integer(Arg2),
194 is_integer(Arg4),
195 is_integer(Arg5) ->
196
197 io:format("testMulti(~p)~n", [Args]),
Jens Geyer6d15c302014-10-02 10:03:09 +0200198 {reply, #'Xtruct'{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000199 byte_thing = Arg0,
200 i32_thing = Arg1,
201 i64_thing = Arg2}};
202
David Reissa59191b2008-06-11 01:16:09 +0000203handle_function(testException, {String}) when is_binary(String) ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000204 io:format("testException(~p)~n", [String]),
205 case String of
David Reisscb137292008-06-11 01:16:15 +0000206 <<"Xception">> ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200207 throw(#'Xception'{errorCode = 1001,
David Reissa94514f2010-08-30 22:05:16 +0000208 message = String});
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900209 <<"TException">> ->
210 throw({?TApplicationException_Structure});
David Reiss57b4d9a2008-06-10 22:58:39 +0000211 _ ->
212 ok
213 end;
214
215handle_function(testMultiException, {Arg0, Arg1}) ->
216 io:format("testMultiException(~p, ~p)~n", [Arg0, Arg1]),
217 case Arg0 of
David Reisscb137292008-06-11 01:16:15 +0000218 <<"Xception">> ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200219 throw(#'Xception'{errorCode = 1001,
David Reisscb137292008-06-11 01:16:15 +0000220 message = <<"This is an Xception">>});
221 <<"Xception2">> ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200222 throw(#'Xception2'{errorCode = 2002,
David Reiss57b4d9a2008-06-10 22:58:39 +0000223 struct_thing =
Jens Geyer6d15c302014-10-02 10:03:09 +0200224 #'Xtruct'{string_thing = <<"This is an Xception2">>}});
David Reiss57b4d9a2008-06-10 22:58:39 +0000225 _ ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200226 {reply, #'Xtruct'{string_thing = Arg1}}
David Reisscdf8d192008-06-11 00:57:35 +0000227 end;
228
David Reiss6ce401d2009-03-24 20:01:58 +0000229handle_function(testOneway, {Seconds}) ->
alisdair sullivan7bdba5c2014-09-30 22:03:34 -0700230 io:format("testOneway: ~p~n", [Seconds]),
David Reisscdf8d192008-06-11 00:57:35 +0000231 timer:sleep(1000 * Seconds),
232 ok.