blob: f504c734aa27960abae323bbe5e7fcf7417ca4d4 [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 Sukegawab31f0902015-11-01 17:00:34 +090058 "--protocol=" ++ Proto ->
59 Opts#options{server_opts = [{protocol, list_to_atom(Proto)} | Opts#options.server_opts]};
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090060 _Else ->
61 erlang:error({bad_arg, Head})
David Reissa508b332010-08-30 22:05:41 +000062 end,
63 parse_args(Rest, NewOpts).
64
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090065start() -> start(init:get_plain_arguments()).
66start(Args) ->
David Reissa508b332010-08-30 22:05:41 +000067 #options{port = Port, server_opts = ServerOpts} = parse_args(Args),
68 spawn(fun() -> start_link(Port, ServerOpts), receive after infinity -> ok end end).
69
70start_link(Port, ServerOpts) ->
David Reiss6204bb12010-08-30 22:05:17 +000071 thrift_socket_server:start([{handler, ?MODULE},
Roger Meier4702fe62015-02-15 21:17:30 +010072 {service, thrift_test_thrift},
David Reissa508b332010-08-30 22:05:41 +000073 {port, Port}] ++
74 ServerOpts).
David Reiss57b4d9a2008-06-10 22:58:39 +000075
76
77handle_function(testVoid, {}) ->
78 io:format("testVoid~n"),
79 ok;
80
David Reissa59191b2008-06-11 01:16:09 +000081handle_function(testString, {S}) when is_binary(S) ->
David Reiss57b4d9a2008-06-10 22:58:39 +000082 io:format("testString: ~p~n", [S]),
83 {reply, S};
84
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090085handle_function(testBool, {B}) when is_boolean(B) ->
86 io:format("testBool: ~p~n", [B]),
87 {reply, B};
88
David Reiss57b4d9a2008-06-10 22:58:39 +000089handle_function(testByte, {I8}) when is_integer(I8) ->
90 io:format("testByte: ~p~n", [I8]),
91 {reply, I8};
92
93handle_function(testI32, {I32}) when is_integer(I32) ->
94 io:format("testI32: ~p~n", [I32]),
95 {reply, I32};
96
97handle_function(testI64, {I64}) when is_integer(I64) ->
98 io:format("testI64: ~p~n", [I64]),
99 {reply, I64};
100
101handle_function(testDouble, {Double}) when is_float(Double) ->
102 io:format("testDouble: ~p~n", [Double]),
103 {reply, Double};
104
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100105handle_function(testBinary, {S}) when is_binary(S) ->
106 io:format("testBinary: ~p~n", [S]),
107 {reply, S};
108
David Reiss57b4d9a2008-06-10 22:58:39 +0000109handle_function(testStruct,
Jens Geyer6d15c302014-10-02 10:03:09 +0200110 {Struct = #'Xtruct'{string_thing = String,
David Reiss57b4d9a2008-06-10 22:58:39 +0000111 byte_thing = Byte,
112 i32_thing = I32,
113 i64_thing = I64}})
David Reissa59191b2008-06-11 01:16:09 +0000114when is_binary(String),
David Reiss57b4d9a2008-06-10 22:58:39 +0000115 is_integer(Byte),
116 is_integer(I32),
117 is_integer(I64) ->
118 io:format("testStruct: ~p~n", [Struct]),
119 {reply, Struct};
120
121handle_function(testNest,
Jens Geyer6d15c302014-10-02 10:03:09 +0200122 {Nest}) when is_record(Nest, 'Xtruct2'),
123 is_record(Nest#'Xtruct2'.struct_thing, 'Xtruct') ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000124 io:format("testNest: ~p~n", [Nest]),
125 {reply, Nest};
126
127handle_function(testMap, {Map}) ->
128 io:format("testMap: ~p~n", [dict:to_list(Map)]),
129 {reply, Map};
130
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900131handle_function(testStringMap, {Map}) ->
132 io:format("testStringMap: ~p~n", [dict:to_list(Map)]),
133 {reply, Map};
134
David Reiss57b4d9a2008-06-10 22:58:39 +0000135handle_function(testSet, {Set}) ->
136 true = sets:is_set(Set),
137 io:format("testSet: ~p~n", [sets:to_list(Set)]),
138 {reply, Set};
139
140handle_function(testList, {List}) when is_list(List) ->
141 io:format("testList: ~p~n", [List]),
142 {reply, List};
143
144handle_function(testEnum, {Enum}) when is_integer(Enum) ->
145 io:format("testEnum: ~p~n", [Enum]),
146 {reply, Enum};
147
148handle_function(testTypedef, {UserID}) when is_integer(UserID) ->
149 io:format("testTypedef: ~p~n", [UserID]),
150 {reply, UserID};
151
152handle_function(testMapMap, {Hello}) ->
153 io:format("testMapMap: ~p~n", [Hello]),
154
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900155 PosList = [{I, I} || I <- lists:seq(1, 4)],
156 NegList = [{-I, -I} || I <- lists:seq(1, 4)],
David Reiss57b4d9a2008-06-10 22:58:39 +0000157
158 MapMap = dict:from_list([{4, dict:from_list(PosList)},
159 {-4, dict:from_list(NegList)}]),
160 {reply, MapMap};
161
Jens Geyer6d15c302014-10-02 10:03:09 +0200162handle_function(testInsanity, {Insanity}) when is_record(Insanity, 'Insanity') ->
163 Hello = #'Xtruct'{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000164 byte_thing = 2,
165 i32_thing = 2,
166 i64_thing = 2},
167
Jens Geyer6d15c302014-10-02 10:03:09 +0200168 Goodbye = #'Xtruct'{string_thing = <<"Goodbye4">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000169 byte_thing = 4,
170 i32_thing = 4,
171 i64_thing = 4},
Jens Geyer6d15c302014-10-02 10:03:09 +0200172 Crazy = #'Insanity'{
173 userMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_EIGHT, 8}]),
David Reiss57b4d9a2008-06-10 22:58:39 +0000174 xtructs = [Goodbye]
175 },
176
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900177 Looney = #'Insanity'{},
David Reiss57b4d9a2008-06-10 22:58:39 +0000178
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900179 FirstMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_TWO, Insanity},
180 {?THRIFT_TEST_NUMBERZ_THREE, Insanity}]),
David Reiss57b4d9a2008-06-10 22:58:39 +0000181
Jens Geyer6d15c302014-10-02 10:03:09 +0200182 SecondMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_SIX, Looney}]),
David Reiss8f943142010-08-30 22:05:01 +0000183
David Reiss57b4d9a2008-06-10 22:58:39 +0000184 Insane = dict:from_list([{1, FirstMap},
185 {2, SecondMap}]),
David Reiss8f943142010-08-30 22:05:01 +0000186
David Reiss57b4d9a2008-06-10 22:58:39 +0000187 io:format("Return = ~p~n", [Insane]),
David Reiss8f943142010-08-30 22:05:01 +0000188
David Reiss57b4d9a2008-06-10 22:58:39 +0000189 {reply, Insane};
190
191handle_function(testMulti, Args = {Arg0, Arg1, Arg2, _Arg3, Arg4, Arg5})
192 when is_integer(Arg0),
193 is_integer(Arg1),
194 is_integer(Arg2),
195 is_integer(Arg4),
196 is_integer(Arg5) ->
197
198 io:format("testMulti(~p)~n", [Args]),
Jens Geyer6d15c302014-10-02 10:03:09 +0200199 {reply, #'Xtruct'{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000200 byte_thing = Arg0,
201 i32_thing = Arg1,
202 i64_thing = Arg2}};
203
David Reissa59191b2008-06-11 01:16:09 +0000204handle_function(testException, {String}) when is_binary(String) ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000205 io:format("testException(~p)~n", [String]),
206 case String of
David Reisscb137292008-06-11 01:16:15 +0000207 <<"Xception">> ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200208 throw(#'Xception'{errorCode = 1001,
David Reissa94514f2010-08-30 22:05:16 +0000209 message = String});
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900210 <<"TException">> ->
211 throw({?TApplicationException_Structure});
David Reiss57b4d9a2008-06-10 22:58:39 +0000212 _ ->
213 ok
214 end;
215
216handle_function(testMultiException, {Arg0, Arg1}) ->
217 io:format("testMultiException(~p, ~p)~n", [Arg0, Arg1]),
218 case Arg0 of
David Reisscb137292008-06-11 01:16:15 +0000219 <<"Xception">> ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200220 throw(#'Xception'{errorCode = 1001,
David Reisscb137292008-06-11 01:16:15 +0000221 message = <<"This is an Xception">>});
222 <<"Xception2">> ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200223 throw(#'Xception2'{errorCode = 2002,
David Reiss57b4d9a2008-06-10 22:58:39 +0000224 struct_thing =
Jens Geyer6d15c302014-10-02 10:03:09 +0200225 #'Xtruct'{string_thing = <<"This is an Xception2">>}});
David Reiss57b4d9a2008-06-10 22:58:39 +0000226 _ ->
Jens Geyer6d15c302014-10-02 10:03:09 +0200227 {reply, #'Xtruct'{string_thing = Arg1}}
David Reisscdf8d192008-06-11 00:57:35 +0000228 end;
229
David Reiss6ce401d2009-03-24 20:01:58 +0000230handle_function(testOneway, {Seconds}) ->
alisdair sullivan7bdba5c2014-09-30 22:03:34 -0700231 io:format("testOneway: ~p~n", [Seconds]),
David Reisscdf8d192008-06-11 00:57:35 +0000232 timer:sleep(1000 * Seconds),
233 ok.