| 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 |  | 
| Bosky | 43509df | 2015-01-04 23:14:11 +0530 | [diff] [blame] | 20 | -module(test_thrift_server). | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 21 |  | 
| David Reiss | a508b33 | 2010-08-30 22:05:41 +0000 | [diff] [blame] | 22 | -export([go/0, go/1, start_link/2, handle_function/2]). | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 23 |  | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 24 | -include("gen-erl/thrift_test_types.hrl"). | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 25 |  | 
| David Reiss | a508b33 | 2010-08-30 22:05:41 +0000 | [diff] [blame] | 26 | -record(options, {port = 9090, | 
|  | 27 | server_opts = []}). | 
|  | 28 |  | 
|  | 29 | parse_args(Args) -> parse_args(Args, #options{}). | 
|  | 30 | parse_args([], Opts) -> Opts; | 
|  | 31 | parse_args([Head | Rest], Opts) -> | 
|  | 32 | NewOpts = | 
|  | 33 | case catch list_to_integer(Head) of | 
|  | 34 | Port when is_integer(Port) -> | 
|  | 35 | Opts#options{port = Port}; | 
|  | 36 | _Else -> | 
|  | 37 | case Head of | 
|  | 38 | "framed" -> | 
|  | 39 | Opts#options{server_opts = [{framed, true} | Opts#options.server_opts]}; | 
|  | 40 | "" -> | 
|  | 41 | Opts; | 
|  | 42 | _Else -> | 
|  | 43 | erlang:error({bad_arg, Head}) | 
|  | 44 | end | 
|  | 45 | end, | 
|  | 46 | parse_args(Rest, NewOpts). | 
|  | 47 |  | 
|  | 48 | go() -> go([]). | 
|  | 49 | go(Args) -> | 
|  | 50 | #options{port = Port, server_opts = ServerOpts} = parse_args(Args), | 
|  | 51 | spawn(fun() -> start_link(Port, ServerOpts), receive after infinity -> ok end end). | 
|  | 52 |  | 
|  | 53 | start_link(Port, ServerOpts) -> | 
| David Reiss | 6204bb1 | 2010-08-30 22:05:17 +0000 | [diff] [blame] | 54 | thrift_socket_server:start([{handler, ?MODULE}, | 
| Roger Meier | 4702fe6 | 2015-02-15 21:17:30 +0100 | [diff] [blame] | 55 | {service, thrift_test_thrift}, | 
| David Reiss | a508b33 | 2010-08-30 22:05:41 +0000 | [diff] [blame] | 56 | {port, Port}] ++ | 
|  | 57 | ServerOpts). | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 58 |  | 
|  | 59 |  | 
|  | 60 | handle_function(testVoid, {}) -> | 
|  | 61 | io:format("testVoid~n"), | 
|  | 62 | ok; | 
|  | 63 |  | 
| David Reiss | a59191b | 2008-06-11 01:16:09 +0000 | [diff] [blame] | 64 | handle_function(testString, {S}) when is_binary(S) -> | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 65 | io:format("testString: ~p~n", [S]), | 
|  | 66 | {reply, S}; | 
|  | 67 |  | 
|  | 68 | handle_function(testByte, {I8}) when is_integer(I8) -> | 
|  | 69 | io:format("testByte: ~p~n", [I8]), | 
|  | 70 | {reply, I8}; | 
|  | 71 |  | 
|  | 72 | handle_function(testI32, {I32}) when is_integer(I32) -> | 
|  | 73 | io:format("testI32: ~p~n", [I32]), | 
|  | 74 | {reply, I32}; | 
|  | 75 |  | 
|  | 76 | handle_function(testI64, {I64}) when is_integer(I64) -> | 
|  | 77 | io:format("testI64: ~p~n", [I64]), | 
|  | 78 | {reply, I64}; | 
|  | 79 |  | 
|  | 80 | handle_function(testDouble, {Double}) when is_float(Double) -> | 
|  | 81 | io:format("testDouble: ~p~n", [Double]), | 
|  | 82 | {reply, Double}; | 
|  | 83 |  | 
| Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 84 | handle_function(testBinary, {S}) when is_binary(S) -> | 
|  | 85 | io:format("testBinary: ~p~n", [S]), | 
|  | 86 | {reply, S}; | 
|  | 87 |  | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 88 | handle_function(testStruct, | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 89 | {Struct = #'Xtruct'{string_thing = String, | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 90 | byte_thing = Byte, | 
|  | 91 | i32_thing = I32, | 
|  | 92 | i64_thing = I64}}) | 
| David Reiss | a59191b | 2008-06-11 01:16:09 +0000 | [diff] [blame] | 93 | when is_binary(String), | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 94 | is_integer(Byte), | 
|  | 95 | is_integer(I32), | 
|  | 96 | is_integer(I64) -> | 
|  | 97 | io:format("testStruct: ~p~n", [Struct]), | 
|  | 98 | {reply, Struct}; | 
|  | 99 |  | 
|  | 100 | handle_function(testNest, | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 101 | {Nest}) when is_record(Nest, 'Xtruct2'), | 
|  | 102 | is_record(Nest#'Xtruct2'.struct_thing, 'Xtruct') -> | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 103 | io:format("testNest: ~p~n", [Nest]), | 
|  | 104 | {reply, Nest}; | 
|  | 105 |  | 
|  | 106 | handle_function(testMap, {Map}) -> | 
|  | 107 | io:format("testMap: ~p~n", [dict:to_list(Map)]), | 
|  | 108 | {reply, Map}; | 
|  | 109 |  | 
|  | 110 | handle_function(testSet, {Set}) -> | 
|  | 111 | true = sets:is_set(Set), | 
|  | 112 | io:format("testSet: ~p~n", [sets:to_list(Set)]), | 
|  | 113 | {reply, Set}; | 
|  | 114 |  | 
|  | 115 | handle_function(testList, {List}) when is_list(List) -> | 
|  | 116 | io:format("testList: ~p~n", [List]), | 
|  | 117 | {reply, List}; | 
|  | 118 |  | 
|  | 119 | handle_function(testEnum, {Enum}) when is_integer(Enum) -> | 
|  | 120 | io:format("testEnum: ~p~n", [Enum]), | 
|  | 121 | {reply, Enum}; | 
|  | 122 |  | 
|  | 123 | handle_function(testTypedef, {UserID}) when is_integer(UserID) -> | 
|  | 124 | io:format("testTypedef: ~p~n", [UserID]), | 
|  | 125 | {reply, UserID}; | 
|  | 126 |  | 
|  | 127 | handle_function(testMapMap, {Hello}) -> | 
|  | 128 | io:format("testMapMap: ~p~n", [Hello]), | 
|  | 129 |  | 
|  | 130 | PosList = [{I, I}   || I <- lists:seq(1, 5)], | 
|  | 131 | NegList = [{-I, -I} || I <- lists:seq(1, 5)], | 
|  | 132 |  | 
|  | 133 | MapMap = dict:from_list([{4,  dict:from_list(PosList)}, | 
|  | 134 | {-4, dict:from_list(NegList)}]), | 
|  | 135 | {reply, MapMap}; | 
|  | 136 |  | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 137 | handle_function(testInsanity, {Insanity}) when is_record(Insanity, 'Insanity') -> | 
|  | 138 | Hello = #'Xtruct'{string_thing = <<"Hello2">>, | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 139 | byte_thing = 2, | 
|  | 140 | i32_thing = 2, | 
|  | 141 | i64_thing = 2}, | 
|  | 142 |  | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 143 | Goodbye = #'Xtruct'{string_thing = <<"Goodbye4">>, | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 144 | byte_thing = 4, | 
|  | 145 | i32_thing = 4, | 
|  | 146 | i64_thing = 4}, | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 147 | Crazy = #'Insanity'{ | 
|  | 148 | userMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_EIGHT, 8}]), | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 149 | xtructs = [Goodbye] | 
|  | 150 | }, | 
|  | 151 |  | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 152 | Looney = #'Insanity'{ | 
|  | 153 | userMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_FIVE, 5}]), | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 154 | xtructs = [Hello] | 
|  | 155 | }, | 
|  | 156 |  | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 157 | FirstMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_TWO, Crazy}, | 
|  | 158 | {?THRIFT_TEST_NUMBERZ_THREE, Crazy}]), | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 159 |  | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 160 | SecondMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_SIX, Looney}]), | 
| David Reiss | 8f94314 | 2010-08-30 22:05:01 +0000 | [diff] [blame] | 161 |  | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 162 | Insane = dict:from_list([{1, FirstMap}, | 
|  | 163 | {2, SecondMap}]), | 
| David Reiss | 8f94314 | 2010-08-30 22:05:01 +0000 | [diff] [blame] | 164 |  | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 165 | io:format("Return = ~p~n", [Insane]), | 
| David Reiss | 8f94314 | 2010-08-30 22:05:01 +0000 | [diff] [blame] | 166 |  | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 167 | {reply, Insane}; | 
|  | 168 |  | 
|  | 169 | handle_function(testMulti, Args = {Arg0, Arg1, Arg2, _Arg3, Arg4, Arg5}) | 
|  | 170 | when is_integer(Arg0), | 
|  | 171 | is_integer(Arg1), | 
|  | 172 | is_integer(Arg2), | 
|  | 173 | is_integer(Arg4), | 
|  | 174 | is_integer(Arg5) -> | 
|  | 175 |  | 
|  | 176 | io:format("testMulti(~p)~n", [Args]), | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 177 | {reply, #'Xtruct'{string_thing = <<"Hello2">>, | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 178 | byte_thing = Arg0, | 
|  | 179 | i32_thing = Arg1, | 
|  | 180 | i64_thing = Arg2}}; | 
|  | 181 |  | 
| David Reiss | a59191b | 2008-06-11 01:16:09 +0000 | [diff] [blame] | 182 | handle_function(testException, {String}) when is_binary(String) -> | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 183 | io:format("testException(~p)~n", [String]), | 
|  | 184 | case String of | 
| David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 185 | <<"Xception">> -> | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 186 | throw(#'Xception'{errorCode = 1001, | 
| David Reiss | a94514f | 2010-08-30 22:05:16 +0000 | [diff] [blame] | 187 | message = String}); | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 188 | _ -> | 
|  | 189 | ok | 
|  | 190 | end; | 
|  | 191 |  | 
|  | 192 | handle_function(testMultiException, {Arg0, Arg1}) -> | 
|  | 193 | io:format("testMultiException(~p, ~p)~n", [Arg0, Arg1]), | 
|  | 194 | case Arg0 of | 
| David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 195 | <<"Xception">> -> | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 196 | throw(#'Xception'{errorCode = 1001, | 
| David Reiss | cb13729 | 2008-06-11 01:16:15 +0000 | [diff] [blame] | 197 | message = <<"This is an Xception">>}); | 
|  | 198 | <<"Xception2">> -> | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 199 | throw(#'Xception2'{errorCode = 2002, | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 200 | struct_thing = | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 201 | #'Xtruct'{string_thing = <<"This is an Xception2">>}}); | 
| David Reiss | 57b4d9a | 2008-06-10 22:58:39 +0000 | [diff] [blame] | 202 | _ -> | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 203 | {reply, #'Xtruct'{string_thing = Arg1}} | 
| David Reiss | cdf8d19 | 2008-06-11 00:57:35 +0000 | [diff] [blame] | 204 | end; | 
|  | 205 |  | 
| David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 206 | handle_function(testOneway, {Seconds}) -> | 
| David Reiss | cdf8d19 | 2008-06-11 00:57:35 +0000 | [diff] [blame] | 207 | timer:sleep(1000 * Seconds), | 
|  | 208 | ok. |