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
10 %% http://www.apache.org/licenses/LICENSE-2.0
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
20 -module(test_thrift_server).
22 -export([start/0, start/1, start_link/2, handle_function/2]).
24 -include("thrift_constants.hrl").
25 -include("gen-erl/thrift_test_types.hrl").
27 -record(options, {port = 9090,
30 parse_args(Args) -> parse_args(Args, #options{}).
31 parse_args([], Opts) ->
33 parse_args([Head | Rest], Opts) ->
37 case string:to_integer(Port) of
38 {IntPort,_} when is_integer(IntPort) ->
39 Opts#options{port = IntPort};
41 erlang:error({bad_arg, Head})
43 "--transport=" ++ Trans ->
46 Opts#options{server_opts = [{framed, true} | Opts#options.server_opts]};
54 {certfile, "../keys/server.pem"}
55 ,{keyfile, "../keys/server.key"}
57 Opts#options{server_opts = [{ssltransport, true} | [SslOptions | Opts#options.server_opts]]};
58 "--protocol=" ++ Proto ->
59 Opts#options{server_opts = [{protocol, list_to_atom(Proto)} | Opts#options.server_opts]};
61 erlang:error({bad_arg, Head})
63 parse_args(Rest, NewOpts).
65 start() -> start(init:get_plain_arguments()).
67 #options{port = Port, server_opts = ServerOpts} = parse_args(Args),
68 spawn(fun() -> start_link(Port, ServerOpts), receive after infinity -> ok end end).
70 start_link(Port, ServerOpts) ->
71 thrift_socket_server:start([{handler, ?MODULE},
72 {service, thrift_test_thrift},
77 handle_function(testVoid, {}) ->
78 io:format("testVoid~n"),
81 handle_function(testString, {S}) when is_binary(S) ->
82 io:format("testString: ~p~n", [S]),
85 handle_function(testBool, {B}) when is_boolean(B) ->
86 io:format("testBool: ~p~n", [B]),
89 handle_function(testByte, {I8}) when is_integer(I8) ->
90 io:format("testByte: ~p~n", [I8]),
93 handle_function(testI32, {I32}) when is_integer(I32) ->
94 io:format("testI32: ~p~n", [I32]),
97 handle_function(testI64, {I64}) when is_integer(I64) ->
98 io:format("testI64: ~p~n", [I64]),
101 handle_function(testDouble, {Double}) when is_float(Double) ->
102 io:format("testDouble: ~p~n", [Double]),
105 handle_function(testBinary, {S}) when is_binary(S) ->
106 io:format("testBinary: ~p~n", [S]),
109 handle_function(testStruct,
110 {Struct = #'thrift.test.Xtruct'{string_thing = String,
114 when is_binary(String),
118 io:format("testStruct: ~p~n", [Struct]),
121 handle_function(testNest,
122 {Nest}) when is_record(Nest, 'thrift.test.Xtruct2'),
123 is_record(Nest#'thrift.test.Xtruct2'.struct_thing, 'thrift.test.Xtruct') ->
124 io:format("testNest: ~p~n", [Nest]),
127 handle_function(testMap, {Map}) ->
128 io:format("testMap: ~p~n", [dict:to_list(Map)]),
131 handle_function(testStringMap, {Map}) ->
132 io:format("testStringMap: ~p~n", [dict:to_list(Map)]),
135 handle_function(testSet, {Set}) ->
136 true = sets:is_set(Set),
137 io:format("testSet: ~p~n", [sets:to_list(Set)]),
140 handle_function(testList, {List}) when is_list(List) ->
141 io:format("testList: ~p~n", [List]),
144 handle_function(testEnum, {Enum}) when is_integer(Enum) ->
145 io:format("testEnum: ~p~n", [Enum]),
148 handle_function(testTypedef, {UserID}) when is_integer(UserID) ->
149 io:format("testTypedef: ~p~n", [UserID]),
152 handle_function(testMapMap, {Hello}) ->
153 io:format("testMapMap: ~p~n", [Hello]),
155 PosList = [{I, I} || I <- lists:seq(1, 4)],
156 NegList = [{-I, -I} || I <- lists:seq(1, 4)],
158 MapMap = dict:from_list([{4, dict:from_list(PosList)},
159 {-4, dict:from_list(NegList)}]),
162 handle_function(testInsanity, {Insanity}) when is_record(Insanity, 'thrift.test.Insanity') ->
163 Hello = #'thrift.test.Xtruct'{string_thing = <<"Hello2">>,
168 Goodbye = #'thrift.test.Xtruct'{string_thing = <<"Goodbye4">>,
172 Crazy = #'thrift.test.Insanity'{
173 userMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_EIGHT, 8}]),
177 Looney = #'thrift.test.Insanity'{},
179 FirstMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_TWO, Insanity},
180 {?THRIFT_TEST_NUMBERZ_THREE, Insanity}]),
182 SecondMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_SIX, Looney}]),
184 Insane = dict:from_list([{1, FirstMap},
187 io:format("Return = ~p~n", [Insane]),
191 handle_function(testMulti, Args = {Arg0, Arg1, Arg2, _Arg3, Arg4, Arg5})
192 when is_integer(Arg0),
198 io:format("testMulti(~p)~n", [Args]),
199 {reply, #'thrift.test.Xtruct'{string_thing = <<"Hello2">>,
204 handle_function(testException, {String}) when is_binary(String) ->
205 io:format("testException(~p)~n", [String]),
208 throw(#'thrift.test.Xception'{errorCode = 1001,
211 throw({?TApplicationException_Structure});
216 handle_function(testMultiException, {Arg0, Arg1}) ->
217 io:format("testMultiException(~p, ~p)~n", [Arg0, Arg1]),
220 throw(#'thrift.test.Xception'{errorCode = 1001,
221 message = <<"This is an Xception">>});
223 throw(#'thrift.test.Xception2'{errorCode = 2002,
225 #'thrift.test.Xtruct'{string_thing = <<"This is an Xception2">>}});
227 {reply, #'thrift.test.Xtruct'{string_thing = Arg1}}
230 handle_function(testOneway, {Seconds}) ->
231 io:format("testOneway: ~p~n", [Seconds]),
232 timer:sleep(1000 * Seconds),