blob: 63f7b080e5cb652e2a2ad8b88c0726769b3f57c5 [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
David Reiss57b4d9a2008-06-10 22:58:39 +000020-module(test_server).
21
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
24-include("thriftTest_types.hrl").
25
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},
55 {service, thriftTest_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
84handle_function(testStruct,
85 {Struct = #xtruct{string_thing = String,
86 byte_thing = Byte,
87 i32_thing = I32,
88 i64_thing = I64}})
David Reissa59191b2008-06-11 01:16:09 +000089when is_binary(String),
David Reiss57b4d9a2008-06-10 22:58:39 +000090 is_integer(Byte),
91 is_integer(I32),
92 is_integer(I64) ->
93 io:format("testStruct: ~p~n", [Struct]),
94 {reply, Struct};
95
96handle_function(testNest,
97 {Nest}) when is_record(Nest, xtruct2),
98 is_record(Nest#xtruct2.struct_thing, xtruct) ->
99 io:format("testNest: ~p~n", [Nest]),
100 {reply, Nest};
101
102handle_function(testMap, {Map}) ->
103 io:format("testMap: ~p~n", [dict:to_list(Map)]),
104 {reply, Map};
105
106handle_function(testSet, {Set}) ->
107 true = sets:is_set(Set),
108 io:format("testSet: ~p~n", [sets:to_list(Set)]),
109 {reply, Set};
110
111handle_function(testList, {List}) when is_list(List) ->
112 io:format("testList: ~p~n", [List]),
113 {reply, List};
114
115handle_function(testEnum, {Enum}) when is_integer(Enum) ->
116 io:format("testEnum: ~p~n", [Enum]),
117 {reply, Enum};
118
119handle_function(testTypedef, {UserID}) when is_integer(UserID) ->
120 io:format("testTypedef: ~p~n", [UserID]),
121 {reply, UserID};
122
123handle_function(testMapMap, {Hello}) ->
124 io:format("testMapMap: ~p~n", [Hello]),
125
126 PosList = [{I, I} || I <- lists:seq(1, 5)],
127 NegList = [{-I, -I} || I <- lists:seq(1, 5)],
128
129 MapMap = dict:from_list([{4, dict:from_list(PosList)},
130 {-4, dict:from_list(NegList)}]),
131 {reply, MapMap};
132
133handle_function(testInsanity, {Insanity}) when is_record(Insanity, insanity) ->
David Reisscb137292008-06-11 01:16:15 +0000134 Hello = #xtruct{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000135 byte_thing = 2,
136 i32_thing = 2,
137 i64_thing = 2},
138
David Reisscb137292008-06-11 01:16:15 +0000139 Goodbye = #xtruct{string_thing = <<"Goodbye4">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000140 byte_thing = 4,
141 i32_thing = 4,
142 i64_thing = 4},
143 Crazy = #insanity{
Anthony F. Molinaro5a9ca882011-03-24 16:46:22 +0000144 userMap = dict:from_list([{?thriftTest_Numberz_EIGHT, 8}]),
David Reiss57b4d9a2008-06-10 22:58:39 +0000145 xtructs = [Goodbye]
146 },
147
148 Looney = #insanity{
Anthony F. Molinaro5a9ca882011-03-24 16:46:22 +0000149 userMap = dict:from_list([{?thriftTest_Numberz_FIVE, 5}]),
David Reiss57b4d9a2008-06-10 22:58:39 +0000150 xtructs = [Hello]
151 },
152
Anthony F. Molinaro5a9ca882011-03-24 16:46:22 +0000153 FirstMap = dict:from_list([{?thriftTest_Numberz_TWO, Crazy},
154 {?thriftTest_Numberz_THREE, Crazy}]),
David Reiss57b4d9a2008-06-10 22:58:39 +0000155
Anthony F. Molinaro5a9ca882011-03-24 16:46:22 +0000156 SecondMap = dict:from_list([{?thriftTest_Numberz_SIX, Looney}]),
David Reiss8f943142010-08-30 22:05:01 +0000157
David Reiss57b4d9a2008-06-10 22:58:39 +0000158 Insane = dict:from_list([{1, FirstMap},
159 {2, SecondMap}]),
David Reiss8f943142010-08-30 22:05:01 +0000160
David Reiss57b4d9a2008-06-10 22:58:39 +0000161 io:format("Return = ~p~n", [Insane]),
David Reiss8f943142010-08-30 22:05:01 +0000162
David Reiss57b4d9a2008-06-10 22:58:39 +0000163 {reply, Insane};
164
165handle_function(testMulti, Args = {Arg0, Arg1, Arg2, _Arg3, Arg4, Arg5})
166 when is_integer(Arg0),
167 is_integer(Arg1),
168 is_integer(Arg2),
169 is_integer(Arg4),
170 is_integer(Arg5) ->
171
172 io:format("testMulti(~p)~n", [Args]),
David Reisscb137292008-06-11 01:16:15 +0000173 {reply, #xtruct{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000174 byte_thing = Arg0,
175 i32_thing = Arg1,
176 i64_thing = Arg2}};
177
David Reissa59191b2008-06-11 01:16:09 +0000178handle_function(testException, {String}) when is_binary(String) ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000179 io:format("testException(~p)~n", [String]),
180 case String of
David Reisscb137292008-06-11 01:16:15 +0000181 <<"Xception">> ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000182 throw(#xception{errorCode = 1001,
David Reissa94514f2010-08-30 22:05:16 +0000183 message = String});
David Reiss57b4d9a2008-06-10 22:58:39 +0000184 _ ->
185 ok
186 end;
187
188handle_function(testMultiException, {Arg0, Arg1}) ->
189 io:format("testMultiException(~p, ~p)~n", [Arg0, Arg1]),
190 case Arg0 of
David Reisscb137292008-06-11 01:16:15 +0000191 <<"Xception">> ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000192 throw(#xception{errorCode = 1001,
David Reisscb137292008-06-11 01:16:15 +0000193 message = <<"This is an Xception">>});
194 <<"Xception2">> ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000195 throw(#xception2{errorCode = 2002,
196 struct_thing =
David Reisscb137292008-06-11 01:16:15 +0000197 #xtruct{string_thing = <<"This is an Xception2">>}});
David Reiss57b4d9a2008-06-10 22:58:39 +0000198 _ ->
199 {reply, #xtruct{string_thing = Arg1}}
David Reisscdf8d192008-06-11 00:57:35 +0000200 end;
201
David Reiss6ce401d2009-03-24 20:01:58 +0000202handle_function(testOneway, {Seconds}) ->
David Reisscdf8d192008-06-11 00:57:35 +0000203 timer:sleep(1000 * Seconds),
204 ok.