]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/test/erl/src/test_thrift_server.erl
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / test / erl / src / test_thrift_server.erl
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
20 -module(test_thrift_server).
21
22 -export([start/0, start/1, start_link/2, handle_function/2]).
23
24 -include("thrift_constants.hrl").
25 -include("gen-erl/thrift_test_types.hrl").
26
27 -record(options, {port = 9090,
28 server_opts = []}).
29
30 parse_args(Args) -> parse_args(Args, #options{}).
31 parse_args([], Opts) ->
32 Opts;
33 parse_args([Head | Rest], Opts) ->
34 NewOpts =
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
45 "framed" ->
46 Opts#options{server_opts = [{framed, true} | Opts#options.server_opts]};
47 _Else ->
48 Opts
49 end;
50 "--ssl" ->
51 ssl:start(),
52 SslOptions =
53 {ssloptions, [
54 {certfile, "../keys/server.pem"}
55 ,{keyfile, "../keys/server.key"}
56 ]},
57 Opts#options{server_opts = [{ssltransport, true} | [SslOptions | Opts#options.server_opts]]};
58 "--protocol=" ++ Proto ->
59 Opts#options{server_opts = [{protocol, list_to_atom(Proto)} | Opts#options.server_opts]};
60 _Else ->
61 erlang:error({bad_arg, Head})
62 end,
63 parse_args(Rest, NewOpts).
64
65 start() -> start(init:get_plain_arguments()).
66 start(Args) ->
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) ->
71 thrift_socket_server:start([{handler, ?MODULE},
72 {service, thrift_test_thrift},
73 {port, Port}] ++
74 ServerOpts).
75
76
77 handle_function(testVoid, {}) ->
78 io:format("testVoid~n"),
79 ok;
80
81 handle_function(testString, {S}) when is_binary(S) ->
82 io:format("testString: ~p~n", [S]),
83 {reply, S};
84
85 handle_function(testBool, {B}) when is_boolean(B) ->
86 io:format("testBool: ~p~n", [B]),
87 {reply, B};
88
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
105 handle_function(testBinary, {S}) when is_binary(S) ->
106 io:format("testBinary: ~p~n", [S]),
107 {reply, S};
108
109 handle_function(testStruct,
110 {Struct = #'thrift.test.Xtruct'{string_thing = String,
111 byte_thing = Byte,
112 i32_thing = I32,
113 i64_thing = I64}})
114 when is_binary(String),
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,
122 {Nest}) when is_record(Nest, 'thrift.test.Xtruct2'),
123 is_record(Nest#'thrift.test.Xtruct2'.struct_thing, 'thrift.test.Xtruct') ->
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
131 handle_function(testStringMap, {Map}) ->
132 io:format("testStringMap: ~p~n", [dict:to_list(Map)]),
133 {reply, Map};
134
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
155 PosList = [{I, I} || I <- lists:seq(1, 4)],
156 NegList = [{-I, -I} || I <- lists:seq(1, 4)],
157
158 MapMap = dict:from_list([{4, dict:from_list(PosList)},
159 {-4, dict:from_list(NegList)}]),
160 {reply, MapMap};
161
162 handle_function(testInsanity, {Insanity}) when is_record(Insanity, 'thrift.test.Insanity') ->
163 Hello = #'thrift.test.Xtruct'{string_thing = <<"Hello2">>,
164 byte_thing = 2,
165 i32_thing = 2,
166 i64_thing = 2},
167
168 Goodbye = #'thrift.test.Xtruct'{string_thing = <<"Goodbye4">>,
169 byte_thing = 4,
170 i32_thing = 4,
171 i64_thing = 4},
172 Crazy = #'thrift.test.Insanity'{
173 userMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_EIGHT, 8}]),
174 xtructs = [Goodbye]
175 },
176
177 Looney = #'thrift.test.Insanity'{},
178
179 FirstMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_TWO, Insanity},
180 {?THRIFT_TEST_NUMBERZ_THREE, Insanity}]),
181
182 SecondMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_SIX, Looney}]),
183
184 Insane = dict:from_list([{1, FirstMap},
185 {2, SecondMap}]),
186
187 io:format("Return = ~p~n", [Insane]),
188
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]),
199 {reply, #'thrift.test.Xtruct'{string_thing = <<"Hello2">>,
200 byte_thing = Arg0,
201 i32_thing = Arg1,
202 i64_thing = Arg2}};
203
204 handle_function(testException, {String}) when is_binary(String) ->
205 io:format("testException(~p)~n", [String]),
206 case String of
207 <<"Xception">> ->
208 throw(#'thrift.test.Xception'{errorCode = 1001,
209 message = String});
210 <<"TException">> ->
211 throw({?TApplicationException_Structure});
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
219 <<"Xception">> ->
220 throw(#'thrift.test.Xception'{errorCode = 1001,
221 message = <<"This is an Xception">>});
222 <<"Xception2">> ->
223 throw(#'thrift.test.Xception2'{errorCode = 2002,
224 struct_thing =
225 #'thrift.test.Xtruct'{string_thing = <<"This is an Xception2">>}});
226 _ ->
227 {reply, #'thrift.test.Xtruct'{string_thing = Arg1}}
228 end;
229
230 handle_function(testOneway, {Seconds}) ->
231 io:format("testOneway: ~p~n", [Seconds]),
232 timer:sleep(1000 * Seconds),
233 ok.