blob: 6797dd81022be95fb96c69e18d5789ae5d335951 [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 Reissf32d0fb2010-08-30 22:05:00 +000022-export([start_link/1, handle_function/2]).
David Reiss57b4d9a2008-06-10 22:58:39 +000023
24-include("thriftTest_types.hrl").
25
David Reissf32d0fb2010-08-30 22:05:00 +000026start_link(Port) ->
David Reiss6204bb12010-08-30 22:05:17 +000027 thrift_socket_server:start([{handler, ?MODULE},
28 {service, thriftTest_thrift},
29 {port, Port}]).
David Reiss57b4d9a2008-06-10 22:58:39 +000030
31
32handle_function(testVoid, {}) ->
33 io:format("testVoid~n"),
34 ok;
35
David Reissa59191b2008-06-11 01:16:09 +000036handle_function(testString, {S}) when is_binary(S) ->
David Reiss57b4d9a2008-06-10 22:58:39 +000037 io:format("testString: ~p~n", [S]),
38 {reply, S};
39
40handle_function(testByte, {I8}) when is_integer(I8) ->
41 io:format("testByte: ~p~n", [I8]),
42 {reply, I8};
43
44handle_function(testI32, {I32}) when is_integer(I32) ->
45 io:format("testI32: ~p~n", [I32]),
46 {reply, I32};
47
48handle_function(testI64, {I64}) when is_integer(I64) ->
49 io:format("testI64: ~p~n", [I64]),
50 {reply, I64};
51
52handle_function(testDouble, {Double}) when is_float(Double) ->
53 io:format("testDouble: ~p~n", [Double]),
54 {reply, Double};
55
56handle_function(testStruct,
57 {Struct = #xtruct{string_thing = String,
58 byte_thing = Byte,
59 i32_thing = I32,
60 i64_thing = I64}})
David Reissa59191b2008-06-11 01:16:09 +000061when is_binary(String),
David Reiss57b4d9a2008-06-10 22:58:39 +000062 is_integer(Byte),
63 is_integer(I32),
64 is_integer(I64) ->
65 io:format("testStruct: ~p~n", [Struct]),
66 {reply, Struct};
67
68handle_function(testNest,
69 {Nest}) when is_record(Nest, xtruct2),
70 is_record(Nest#xtruct2.struct_thing, xtruct) ->
71 io:format("testNest: ~p~n", [Nest]),
72 {reply, Nest};
73
74handle_function(testMap, {Map}) ->
75 io:format("testMap: ~p~n", [dict:to_list(Map)]),
76 {reply, Map};
77
78handle_function(testSet, {Set}) ->
79 true = sets:is_set(Set),
80 io:format("testSet: ~p~n", [sets:to_list(Set)]),
81 {reply, Set};
82
83handle_function(testList, {List}) when is_list(List) ->
84 io:format("testList: ~p~n", [List]),
85 {reply, List};
86
87handle_function(testEnum, {Enum}) when is_integer(Enum) ->
88 io:format("testEnum: ~p~n", [Enum]),
89 {reply, Enum};
90
91handle_function(testTypedef, {UserID}) when is_integer(UserID) ->
92 io:format("testTypedef: ~p~n", [UserID]),
93 {reply, UserID};
94
95handle_function(testMapMap, {Hello}) ->
96 io:format("testMapMap: ~p~n", [Hello]),
97
98 PosList = [{I, I} || I <- lists:seq(1, 5)],
99 NegList = [{-I, -I} || I <- lists:seq(1, 5)],
100
101 MapMap = dict:from_list([{4, dict:from_list(PosList)},
102 {-4, dict:from_list(NegList)}]),
103 {reply, MapMap};
104
105handle_function(testInsanity, {Insanity}) when is_record(Insanity, insanity) ->
David Reisscb137292008-06-11 01:16:15 +0000106 Hello = #xtruct{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000107 byte_thing = 2,
108 i32_thing = 2,
109 i64_thing = 2},
110
David Reisscb137292008-06-11 01:16:15 +0000111 Goodbye = #xtruct{string_thing = <<"Goodbye4">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000112 byte_thing = 4,
113 i32_thing = 4,
114 i64_thing = 4},
115 Crazy = #insanity{
116 userMap = dict:from_list([{?thriftTest_EIGHT, 8}]),
117 xtructs = [Goodbye]
118 },
119
120 Looney = #insanity{
121 userMap = dict:from_list([{?thriftTest_FIVE, 5}]),
122 xtructs = [Hello]
123 },
124
125 FirstMap = dict:from_list([{?thriftTest_TWO, Crazy},
126 {?thriftTest_THREE, Crazy}]),
127
128 SecondMap = dict:from_list([{?thriftTest_SIX, Looney}]),
David Reiss8f943142010-08-30 22:05:01 +0000129
David Reiss57b4d9a2008-06-10 22:58:39 +0000130 Insane = dict:from_list([{1, FirstMap},
131 {2, SecondMap}]),
David Reiss8f943142010-08-30 22:05:01 +0000132
David Reiss57b4d9a2008-06-10 22:58:39 +0000133 io:format("Return = ~p~n", [Insane]),
David Reiss8f943142010-08-30 22:05:01 +0000134
David Reiss57b4d9a2008-06-10 22:58:39 +0000135 {reply, Insane};
136
137handle_function(testMulti, Args = {Arg0, Arg1, Arg2, _Arg3, Arg4, Arg5})
138 when is_integer(Arg0),
139 is_integer(Arg1),
140 is_integer(Arg2),
141 is_integer(Arg4),
142 is_integer(Arg5) ->
143
144 io:format("testMulti(~p)~n", [Args]),
David Reisscb137292008-06-11 01:16:15 +0000145 {reply, #xtruct{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000146 byte_thing = Arg0,
147 i32_thing = Arg1,
148 i64_thing = Arg2}};
149
David Reissa59191b2008-06-11 01:16:09 +0000150handle_function(testException, {String}) when is_binary(String) ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000151 io:format("testException(~p)~n", [String]),
152 case String of
David Reisscb137292008-06-11 01:16:15 +0000153 <<"Xception">> ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000154 throw(#xception{errorCode = 1001,
David Reissa94514f2010-08-30 22:05:16 +0000155 message = String});
David Reiss57b4d9a2008-06-10 22:58:39 +0000156 _ ->
157 ok
158 end;
159
160handle_function(testMultiException, {Arg0, Arg1}) ->
161 io:format("testMultiException(~p, ~p)~n", [Arg0, Arg1]),
162 case Arg0 of
David Reisscb137292008-06-11 01:16:15 +0000163 <<"Xception">> ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000164 throw(#xception{errorCode = 1001,
David Reisscb137292008-06-11 01:16:15 +0000165 message = <<"This is an Xception">>});
166 <<"Xception2">> ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000167 throw(#xception2{errorCode = 2002,
168 struct_thing =
David Reisscb137292008-06-11 01:16:15 +0000169 #xtruct{string_thing = <<"This is an Xception2">>}});
David Reiss57b4d9a2008-06-10 22:58:39 +0000170 _ ->
171 {reply, #xtruct{string_thing = Arg1}}
David Reisscdf8d192008-06-11 00:57:35 +0000172 end;
173
David Reiss6ce401d2009-03-24 20:01:58 +0000174handle_function(testOneway, {Seconds}) ->
David Reisscdf8d192008-06-11 00:57:35 +0000175 timer:sleep(1000 * Seconds),
176 ok.