David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 1 | -module(test_server). |
| 2 | |
| 3 | -export([start_link/1, handle_function/2]). |
| 4 | |
| 5 | -include("thriftTest_types.hrl"). |
| 6 | |
| 7 | start_link(Port) -> |
| 8 | thrift_server:start_link(Port, thriftTest_thrift, ?MODULE). |
| 9 | |
| 10 | |
| 11 | handle_function(testVoid, {}) -> |
| 12 | io:format("testVoid~n"), |
| 13 | ok; |
| 14 | |
David Reiss | a59191b | 2008-06-11 01:16:09 +0000 | [diff] [blame] | 15 | handle_function(testString, {S}) when is_binary(S) -> |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 16 | io:format("testString: ~p~n", [S]), |
| 17 | {reply, S}; |
| 18 | |
| 19 | handle_function(testByte, {I8}) when is_integer(I8) -> |
| 20 | io:format("testByte: ~p~n", [I8]), |
| 21 | {reply, I8}; |
| 22 | |
| 23 | handle_function(testI32, {I32}) when is_integer(I32) -> |
| 24 | io:format("testI32: ~p~n", [I32]), |
| 25 | {reply, I32}; |
| 26 | |
| 27 | handle_function(testI64, {I64}) when is_integer(I64) -> |
| 28 | io:format("testI64: ~p~n", [I64]), |
| 29 | {reply, I64}; |
| 30 | |
| 31 | handle_function(testDouble, {Double}) when is_float(Double) -> |
| 32 | io:format("testDouble: ~p~n", [Double]), |
| 33 | {reply, Double}; |
| 34 | |
| 35 | handle_function(testStruct, |
| 36 | {Struct = #xtruct{string_thing = String, |
| 37 | byte_thing = Byte, |
| 38 | i32_thing = I32, |
| 39 | i64_thing = I64}}) |
David Reiss | a59191b | 2008-06-11 01:16:09 +0000 | [diff] [blame] | 40 | when is_binary(String), |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 41 | is_integer(Byte), |
| 42 | is_integer(I32), |
| 43 | is_integer(I64) -> |
| 44 | io:format("testStruct: ~p~n", [Struct]), |
| 45 | {reply, Struct}; |
| 46 | |
| 47 | handle_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 | |
| 53 | handle_function(testMap, {Map}) -> |
| 54 | io:format("testMap: ~p~n", [dict:to_list(Map)]), |
| 55 | {reply, Map}; |
| 56 | |
| 57 | handle_function(testSet, {Set}) -> |
| 58 | true = sets:is_set(Set), |
| 59 | io:format("testSet: ~p~n", [sets:to_list(Set)]), |
| 60 | {reply, Set}; |
| 61 | |
| 62 | handle_function(testList, {List}) when is_list(List) -> |
| 63 | io:format("testList: ~p~n", [List]), |
| 64 | {reply, List}; |
| 65 | |
| 66 | handle_function(testEnum, {Enum}) when is_integer(Enum) -> |
| 67 | io:format("testEnum: ~p~n", [Enum]), |
| 68 | {reply, Enum}; |
| 69 | |
| 70 | handle_function(testTypedef, {UserID}) when is_integer(UserID) -> |
| 71 | io:format("testTypedef: ~p~n", [UserID]), |
| 72 | {reply, UserID}; |
| 73 | |
| 74 | handle_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 | |
| 84 | handle_function(testInsanity, {Insanity}) when is_record(Insanity, insanity) -> |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 85 | Hello = #xtruct{string_thing = <<"Hello2">>, |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 86 | byte_thing = 2, |
| 87 | i32_thing = 2, |
| 88 | i64_thing = 2}, |
| 89 | |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 90 | Goodbye = #xtruct{string_thing = <<"Goodbye4">>, |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 91 | 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 | |
| 116 | handle_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 Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 124 | {reply, #xtruct{string_thing = <<"Hello2">>, |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 125 | byte_thing = Arg0, |
| 126 | i32_thing = Arg1, |
| 127 | i64_thing = Arg2}}; |
| 128 | |
David Reiss | a59191b | 2008-06-11 01:16:09 +0000 | [diff] [blame] | 129 | handle_function(testException, {String}) when is_binary(String) -> |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 130 | io:format("testException(~p)~n", [String]), |
| 131 | case String of |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 132 | <<"Xception">> -> |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 133 | throw(#xception{errorCode = 1001, |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 134 | message = <<"This is an Xception">>}); |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 135 | _ -> |
| 136 | ok |
| 137 | end; |
| 138 | |
| 139 | handle_function(testMultiException, {Arg0, Arg1}) -> |
| 140 | io:format("testMultiException(~p, ~p)~n", [Arg0, Arg1]), |
| 141 | case Arg0 of |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 142 | <<"Xception">> -> |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 143 | throw(#xception{errorCode = 1001, |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 144 | message = <<"This is an Xception">>}); |
| 145 | <<"Xception2">> -> |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 146 | throw(#xception2{errorCode = 2002, |
| 147 | struct_thing = |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 148 | #xtruct{string_thing = <<"This is an Xception2">>}}); |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 149 | _ -> |
| 150 | {reply, #xtruct{string_thing = Arg1}} |
David Reiss | cdf8d19 | 2008-06-11 00:57:35 +0000 | [diff] [blame] | 151 | end; |
| 152 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 153 | handle_function(testOneway, {Seconds}) -> |
David Reiss | cdf8d19 | 2008-06-11 00:57:35 +0000 | [diff] [blame] | 154 | timer:sleep(1000 * Seconds), |
| 155 | ok. |