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