David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | %% |
| 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 Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 20 | -module(test_server). |
| 21 | |
| 22 | -export([start_link/1, handle_function/2]). |
| 23 | |
| 24 | -include("thriftTest_types.hrl"). |
| 25 | |
| 26 | start_link(Port) -> |
| 27 | thrift_server:start_link(Port, thriftTest_thrift, ?MODULE). |
| 28 | |
| 29 | |
| 30 | handle_function(testVoid, {}) -> |
| 31 | io:format("testVoid~n"), |
| 32 | ok; |
| 33 | |
David Reiss | a59191b | 2008-06-11 01:16:09 +0000 | [diff] [blame] | 34 | handle_function(testString, {S}) when is_binary(S) -> |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 35 | io:format("testString: ~p~n", [S]), |
| 36 | {reply, S}; |
| 37 | |
| 38 | handle_function(testByte, {I8}) when is_integer(I8) -> |
| 39 | io:format("testByte: ~p~n", [I8]), |
| 40 | {reply, I8}; |
| 41 | |
| 42 | handle_function(testI32, {I32}) when is_integer(I32) -> |
| 43 | io:format("testI32: ~p~n", [I32]), |
| 44 | {reply, I32}; |
| 45 | |
| 46 | handle_function(testI64, {I64}) when is_integer(I64) -> |
| 47 | io:format("testI64: ~p~n", [I64]), |
| 48 | {reply, I64}; |
| 49 | |
| 50 | handle_function(testDouble, {Double}) when is_float(Double) -> |
| 51 | io:format("testDouble: ~p~n", [Double]), |
| 52 | {reply, Double}; |
| 53 | |
| 54 | handle_function(testStruct, |
| 55 | {Struct = #xtruct{string_thing = String, |
| 56 | byte_thing = Byte, |
| 57 | i32_thing = I32, |
| 58 | i64_thing = I64}}) |
David Reiss | a59191b | 2008-06-11 01:16:09 +0000 | [diff] [blame] | 59 | when is_binary(String), |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 60 | is_integer(Byte), |
| 61 | is_integer(I32), |
| 62 | is_integer(I64) -> |
| 63 | io:format("testStruct: ~p~n", [Struct]), |
| 64 | {reply, Struct}; |
| 65 | |
| 66 | handle_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 | |
| 72 | handle_function(testMap, {Map}) -> |
| 73 | io:format("testMap: ~p~n", [dict:to_list(Map)]), |
| 74 | {reply, Map}; |
| 75 | |
| 76 | handle_function(testSet, {Set}) -> |
| 77 | true = sets:is_set(Set), |
| 78 | io:format("testSet: ~p~n", [sets:to_list(Set)]), |
| 79 | {reply, Set}; |
| 80 | |
| 81 | handle_function(testList, {List}) when is_list(List) -> |
| 82 | io:format("testList: ~p~n", [List]), |
| 83 | {reply, List}; |
| 84 | |
| 85 | handle_function(testEnum, {Enum}) when is_integer(Enum) -> |
| 86 | io:format("testEnum: ~p~n", [Enum]), |
| 87 | {reply, Enum}; |
| 88 | |
| 89 | handle_function(testTypedef, {UserID}) when is_integer(UserID) -> |
| 90 | io:format("testTypedef: ~p~n", [UserID]), |
| 91 | {reply, UserID}; |
| 92 | |
| 93 | handle_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 | |
| 103 | handle_function(testInsanity, {Insanity}) when is_record(Insanity, insanity) -> |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 104 | Hello = #xtruct{string_thing = <<"Hello2">>, |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 105 | byte_thing = 2, |
| 106 | i32_thing = 2, |
| 107 | i64_thing = 2}, |
| 108 | |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 109 | Goodbye = #xtruct{string_thing = <<"Goodbye4">>, |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 110 | 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 | |
| 135 | handle_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 Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 143 | {reply, #xtruct{string_thing = <<"Hello2">>, |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 144 | byte_thing = Arg0, |
| 145 | i32_thing = Arg1, |
| 146 | i64_thing = Arg2}}; |
| 147 | |
David Reiss | a59191b | 2008-06-11 01:16:09 +0000 | [diff] [blame] | 148 | handle_function(testException, {String}) when is_binary(String) -> |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 149 | io:format("testException(~p)~n", [String]), |
| 150 | case String of |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 151 | <<"Xception">> -> |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 152 | throw(#xception{errorCode = 1001, |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 153 | message = <<"This is an Xception">>}); |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 154 | _ -> |
| 155 | ok |
| 156 | end; |
| 157 | |
| 158 | handle_function(testMultiException, {Arg0, Arg1}) -> |
| 159 | io:format("testMultiException(~p, ~p)~n", [Arg0, Arg1]), |
| 160 | case Arg0 of |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 161 | <<"Xception">> -> |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 162 | throw(#xception{errorCode = 1001, |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 163 | message = <<"This is an Xception">>}); |
| 164 | <<"Xception2">> -> |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 165 | throw(#xception2{errorCode = 2002, |
| 166 | struct_thing = |
David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 167 | #xtruct{string_thing = <<"This is an Xception2">>}}); |
David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 168 | _ -> |
| 169 | {reply, #xtruct{string_thing = Arg1}} |
David Reiss | cdf8d19 | 2008-06-11 00:57:35 +0000 | [diff] [blame] | 170 | end; |
| 171 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 172 | handle_function(testOneway, {Seconds}) -> |
David Reiss | cdf8d19 | 2008-06-11 00:57:35 +0000 | [diff] [blame] | 173 | timer:sleep(1000 * Seconds), |
| 174 | ok. |