David Reiss | 3f660a4 | 2010-08-30 22:05:29 +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 | |
| 20 | -module(thrift_client_util). |
| 21 | |
| 22 | -export([new/4]). |
| 23 | |
| 24 | %% |
| 25 | %% Splits client options into client, protocol, and transport options |
| 26 | %% |
| 27 | %% split_options([Options...]) -> {ProtocolOptions, TransportOptions} |
| 28 | %% |
| 29 | split_options(Options) -> |
| 30 | split_options(Options, [], []). |
| 31 | |
| 32 | split_options([], ProtoIn, TransIn) -> |
| 33 | {ProtoIn, TransIn}; |
| 34 | |
| 35 | split_options([Opt = {OptKey, _} | Rest], ProtoIn, TransIn) |
| 36 | when OptKey =:= strict_read; |
| 37 | OptKey =:= strict_write -> |
| 38 | split_options(Rest, [Opt | ProtoIn], TransIn); |
| 39 | |
| 40 | split_options([Opt = {OptKey, _} | Rest], ProtoIn, TransIn) |
| 41 | when OptKey =:= framed; |
| 42 | OptKey =:= connect_timeout; |
| 43 | OptKey =:= sockopts -> |
| 44 | split_options(Rest, ProtoIn, [Opt | TransIn]). |
| 45 | |
| 46 | |
| 47 | %% Client constructor for the common-case of socket transports |
| 48 | %% with the binary protocol |
| 49 | new(Host, Port, Service, Options) |
| 50 | when is_integer(Port), is_atom(Service), is_list(Options) -> |
| 51 | {ProtoOpts, TransOpts} = split_options(Options), |
| 52 | |
| 53 | {ok, TransportFactory} = |
| 54 | thrift_socket_transport:new_transport_factory(Host, Port, TransOpts), |
| 55 | |
| 56 | {ok, ProtocolFactory} = thrift_binary_protocol:new_protocol_factory( |
| 57 | TransportFactory, ProtoOpts), |
| 58 | |
Jens Geyer | a6b328f | 2014-03-18 23:51:23 +0200 | [diff] [blame] | 59 | case ProtocolFactory() of |
| 60 | {ok, Protocol} -> |
| 61 | thrift_client:new(Protocol, Service); |
| 62 | {error, Error} -> |
| 63 | {error, Error} |
| 64 | end. |