blob: 45143b7601066b9c910c64d961091535610dcb08 [file] [log] [blame]
David Reiss57b4d9a2008-06-10 22:58:39 +00001-module(test_server).
2
3-export([start_link/1, handle_function/2]).
4
5-include("thriftTest_types.hrl").
6
7start_link(Port) ->
8 thrift_server:start_link(Port, thriftTest_thrift, ?MODULE).
9
10
11handle_function(testVoid, {}) ->
12 io:format("testVoid~n"),
13 ok;
14
David Reissa59191b2008-06-11 01:16:09 +000015handle_function(testString, {S}) when is_binary(S) ->
David Reiss57b4d9a2008-06-10 22:58:39 +000016 io:format("testString: ~p~n", [S]),
17 {reply, S};
18
19handle_function(testByte, {I8}) when is_integer(I8) ->
20 io:format("testByte: ~p~n", [I8]),
21 {reply, I8};
22
23handle_function(testI32, {I32}) when is_integer(I32) ->
24 io:format("testI32: ~p~n", [I32]),
25 {reply, I32};
26
27handle_function(testI64, {I64}) when is_integer(I64) ->
28 io:format("testI64: ~p~n", [I64]),
29 {reply, I64};
30
31handle_function(testDouble, {Double}) when is_float(Double) ->
32 io:format("testDouble: ~p~n", [Double]),
33 {reply, Double};
34
35handle_function(testStruct,
36 {Struct = #xtruct{string_thing = String,
37 byte_thing = Byte,
38 i32_thing = I32,
39 i64_thing = I64}})
David Reissa59191b2008-06-11 01:16:09 +000040when is_binary(String),
David Reiss57b4d9a2008-06-10 22:58:39 +000041 is_integer(Byte),
42 is_integer(I32),
43 is_integer(I64) ->
44 io:format("testStruct: ~p~n", [Struct]),
45 {reply, Struct};
46
47handle_function(testNest,
48 {Nest}) when is_record(Nest, xtruct2),
49 is_record(Nest#xtruct2.struct_thing, xtruct) ->
50 io:format("testNest: ~p~n", [Nest]),
51 {reply, Nest};
52
53handle_function(testMap, {Map}) ->
54 io:format("testMap: ~p~n", [dict:to_list(Map)]),
55 {reply, Map};
56
57handle_function(testSet, {Set}) ->
58 true = sets:is_set(Set),
59 io:format("testSet: ~p~n", [sets:to_list(Set)]),
60 {reply, Set};
61
62handle_function(testList, {List}) when is_list(List) ->
63 io:format("testList: ~p~n", [List]),
64 {reply, List};
65
66handle_function(testEnum, {Enum}) when is_integer(Enum) ->
67 io:format("testEnum: ~p~n", [Enum]),
68 {reply, Enum};
69
70handle_function(testTypedef, {UserID}) when is_integer(UserID) ->
71 io:format("testTypedef: ~p~n", [UserID]),
72 {reply, UserID};
73
74handle_function(testMapMap, {Hello}) ->
75 io:format("testMapMap: ~p~n", [Hello]),
76
77 PosList = [{I, I} || I <- lists:seq(1, 5)],
78 NegList = [{-I, -I} || I <- lists:seq(1, 5)],
79
80 MapMap = dict:from_list([{4, dict:from_list(PosList)},
81 {-4, dict:from_list(NegList)}]),
82 {reply, MapMap};
83
84handle_function(testInsanity, {Insanity}) when is_record(Insanity, insanity) ->
David Reisscb137292008-06-11 01:16:15 +000085 Hello = #xtruct{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +000086 byte_thing = 2,
87 i32_thing = 2,
88 i64_thing = 2},
89
David Reisscb137292008-06-11 01:16:15 +000090 Goodbye = #xtruct{string_thing = <<"Goodbye4">>,
David Reiss57b4d9a2008-06-10 22:58:39 +000091 byte_thing = 4,
92 i32_thing = 4,
93 i64_thing = 4},
94 Crazy = #insanity{
95 userMap = dict:from_list([{?thriftTest_EIGHT, 8}]),
96 xtructs = [Goodbye]
97 },
98
99 Looney = #insanity{
100 userMap = dict:from_list([{?thriftTest_FIVE, 5}]),
101 xtructs = [Hello]
102 },
103
104 FirstMap = dict:from_list([{?thriftTest_TWO, Crazy},
105 {?thriftTest_THREE, Crazy}]),
106
107 SecondMap = dict:from_list([{?thriftTest_SIX, Looney}]),
108
109 Insane = dict:from_list([{1, FirstMap},
110 {2, SecondMap}]),
111
112 io:format("Return = ~p~n", [Insane]),
113
114 {reply, Insane};
115
116handle_function(testMulti, Args = {Arg0, Arg1, Arg2, _Arg3, Arg4, Arg5})
117 when is_integer(Arg0),
118 is_integer(Arg1),
119 is_integer(Arg2),
120 is_integer(Arg4),
121 is_integer(Arg5) ->
122
123 io:format("testMulti(~p)~n", [Args]),
David Reisscb137292008-06-11 01:16:15 +0000124 {reply, #xtruct{string_thing = <<"Hello2">>,
David Reiss57b4d9a2008-06-10 22:58:39 +0000125 byte_thing = Arg0,
126 i32_thing = Arg1,
127 i64_thing = Arg2}};
128
David Reissa59191b2008-06-11 01:16:09 +0000129handle_function(testException, {String}) when is_binary(String) ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000130 io:format("testException(~p)~n", [String]),
131 case String of
David Reisscb137292008-06-11 01:16:15 +0000132 <<"Xception">> ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000133 throw(#xception{errorCode = 1001,
David Reisscb137292008-06-11 01:16:15 +0000134 message = <<"This is an Xception">>});
David Reiss57b4d9a2008-06-10 22:58:39 +0000135 _ ->
136 ok
137 end;
138
139handle_function(testMultiException, {Arg0, Arg1}) ->
140 io:format("testMultiException(~p, ~p)~n", [Arg0, Arg1]),
141 case Arg0 of
David Reisscb137292008-06-11 01:16:15 +0000142 <<"Xception">> ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000143 throw(#xception{errorCode = 1001,
David Reisscb137292008-06-11 01:16:15 +0000144 message = <<"This is an Xception">>});
145 <<"Xception2">> ->
David Reiss57b4d9a2008-06-10 22:58:39 +0000146 throw(#xception2{errorCode = 2002,
147 struct_thing =
David Reisscb137292008-06-11 01:16:15 +0000148 #xtruct{string_thing = <<"This is an Xception2">>}});
David Reiss57b4d9a2008-06-10 22:58:39 +0000149 _ ->
150 {reply, #xtruct{string_thing = Arg1}}
David Reisscdf8d192008-06-11 00:57:35 +0000151 end;
152
David Reiss6ce401d2009-03-24 20:01:58 +0000153handle_function(testOneway, {Seconds}) ->
David Reisscdf8d192008-06-11 00:57:35 +0000154 timer:sleep(1000 * Seconds),
155 ok.