blob: cd439ccdf6f8f5875d32540a384518f6a5336352 [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
22-export([start_link/1, handle_function/2]).
23
24-include("thriftTest_types.hrl").
25
26start_link(Port) ->
27 thrift_server:start_link(Port, thriftTest_thrift, ?MODULE).
28
29
30handle_function(testVoid, {}) ->
31 io:format("testVoid~n"),
32 ok;
33
David Reissa59191b2008-06-11 01:16:09 +000034handle_function(testString, {S}) when is_binary(S) ->
David Reiss57b4d9a2008-06-10 22:58:39 +000035 io:format("testString: ~p~n", [S]),
36 {reply, S};
37
38handle_function(testByte, {I8}) when is_integer(I8) ->
39 io:format("testByte: ~p~n", [I8]),
40 {reply, I8};
41
42handle_function(testI32, {I32}) when is_integer(I32) ->
43 io:format("testI32: ~p~n", [I32]),
44 {reply, I32};
45
46handle_function(testI64, {I64}) when is_integer(I64) ->
47 io:format("testI64: ~p~n", [I64]),
48 {reply, I64};
49
50handle_function(testDouble, {Double}) when is_float(Double) ->
51 io:format("testDouble: ~p~n", [Double]),
52 {reply, Double};
53
54handle_function(testStruct,
55 {Struct = #xtruct{string_thing = String,
56 byte_thing = Byte,
57 i32_thing = I32,
58 i64_thing = I64}})
David Reissa59191b2008-06-11 01:16:09 +000059when is_binary(String),
David Reiss57b4d9a2008-06-10 22:58:39 +000060 is_integer(Byte),
61 is_integer(I32),
62 is_integer(I64) ->
63 io:format("testStruct: ~p~n", [Struct]),
64 {reply, Struct};
65
66handle_function(testNest,
67 {Nest}) when is_record(Nest, xtruct2),
68 is_record(Nest#xtruct2.struct_thing, xtruct) ->
69 io:format("testNest: ~p~n", [Nest]),
70 {reply, Nest};
71
72handle_function(testMap, {Map}) ->
73 io:format("testMap: ~p~n", [dict:to_list(Map)]),
74 {reply, Map};
75
76handle_function(testSet, {Set}) ->
77 true = sets:is_set(Set),
78 io:format("testSet: ~p~n", [sets:to_list(Set)]),
79 {reply, Set};
80
81handle_function(testList, {List}) when is_list(List) ->
82 io:format("testList: ~p~n", [List]),
83 {reply, List};
84
85handle_function(testEnum, {Enum}) when is_integer(Enum) ->
86 io:format("testEnum: ~p~n", [Enum]),
87 {reply, Enum};
88
89handle_function(testTypedef, {UserID}) when is_integer(UserID) ->
90 io:format("testTypedef: ~p~n", [UserID]),
91 {reply, UserID};
92
93handle_function(testMapMap, {Hello}) ->
94 io:format("testMapMap: ~p~n", [Hello]),
95
96 PosList = [{I, I} || I <- lists:seq(1, 5)],
97 NegList = [{-I, -I} || I <- lists:seq(1, 5)],
98
99 MapMap = dict:from_list([{4, dict:from_list(PosList)},
100 {-4, dict:from_list(NegList)}]),
101 {reply, MapMap};
102
103handle_function(testInsanity, {Insanity}) when is_record(Insanity, insanity) ->
David Reisscb137292008-06-11 01:16:15 +0000104 Hello = #xtruct{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000105 byte_thing = 2,
106 i32_thing = 2,
107 i64_thing = 2},
108
David Reisscb137292008-06-11 01:16:15 +0000109 Goodbye = #xtruct{string_thing = <<"Goodbye4">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000110 byte_thing = 4,
111 i32_thing = 4,
112 i64_thing = 4},
113 Crazy = #insanity{
114 userMap = dict:from_list([{?thriftTest_EIGHT, 8}]),
115 xtructs = [Goodbye]
116 },
117
118 Looney = #insanity{
119 userMap = dict:from_list([{?thriftTest_FIVE, 5}]),
120 xtructs = [Hello]
121 },
122
123 FirstMap = dict:from_list([{?thriftTest_TWO, Crazy},
124 {?thriftTest_THREE, Crazy}]),
125
126 SecondMap = dict:from_list([{?thriftTest_SIX, Looney}]),
127
128 Insane = dict:from_list([{1, FirstMap},
129 {2, SecondMap}]),
130
131 io:format("Return = ~p~n", [Insane]),
132
133 {reply, Insane};
134
135handle_function(testMulti, Args = {Arg0, Arg1, Arg2, _Arg3, Arg4, Arg5})
136 when is_integer(Arg0),
137 is_integer(Arg1),
138 is_integer(Arg2),
139 is_integer(Arg4),
140 is_integer(Arg5) ->
141
142 io:format("testMulti(~p)~n", [Args]),
David Reisscb137292008-06-11 01:16:15 +0000143 {reply, #xtruct{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000144 byte_thing = Arg0,
145 i32_thing = Arg1,
146 i64_thing = Arg2}};
147
David Reissa59191b2008-06-11 01:16:09 +0000148handle_function(testException, {String}) when is_binary(String) ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000149 io:format("testException(~p)~n", [String]),
150 case String of
David Reisscb137292008-06-11 01:16:15 +0000151 <<"Xception">> ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000152 throw(#xception{errorCode = 1001,
David Reisscb137292008-06-11 01:16:15 +0000153 message = <<"This is an Xception">>});
David Reiss57b4d9a2008-06-10 22:58:39 +0000154 _ ->
155 ok
156 end;
157
158handle_function(testMultiException, {Arg0, Arg1}) ->
159 io:format("testMultiException(~p, ~p)~n", [Arg0, Arg1]),
160 case Arg0 of
David Reisscb137292008-06-11 01:16:15 +0000161 <<"Xception">> ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000162 throw(#xception{errorCode = 1001,
David Reisscb137292008-06-11 01:16:15 +0000163 message = <<"This is an Xception">>});
164 <<"Xception2">> ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000165 throw(#xception2{errorCode = 2002,
166 struct_thing =
David Reisscb137292008-06-11 01:16:15 +0000167 #xtruct{string_thing = <<"This is an Xception2">>}});
David Reiss57b4d9a2008-06-10 22:58:39 +0000168 _ ->
169 {reply, #xtruct{string_thing = Arg1}}
David Reisscdf8d192008-06-11 00:57:35 +0000170 end;
171
David Reiss6ce401d2009-03-24 20:01:58 +0000172handle_function(testOneway, {Seconds}) ->
David Reisscdf8d192008-06-11 00:57:35 +0000173 timer:sleep(1000 * Seconds),
174 ok.