blob: e55dc6d5127dc59b6337c79f61832ca4cdfa1f68 [file] [log] [blame]
Jens Geyerf4598682014-05-08 23:18:44 +02001/*
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
20package common
21
22import (
Jens Geyerc6b991f2015-08-07 23:41:09 +020023 "compress/zlib"
Jens Geyerf4598682014-05-08 23:18:44 +020024 "crypto/tls"
25 "flag"
26 "fmt"
27 "gen/thrifttest"
28 "thrift"
29)
30
31var debugClientProtocol bool
32
33func init() {
34 flag.BoolVar(&debugClientProtocol, "debug_client_protocol", false, "turn client protocol trace on")
35}
36
37func StartClient(
38 host string,
39 port int64,
40 domain_socket string,
41 transport string,
42 protocol string,
43 ssl bool) (client *thrifttest.ThriftTestClient, err error) {
44
45 hostPort := fmt.Sprintf("%s:%d", host, port)
46
47 var protocolFactory thrift.TProtocolFactory
48 switch protocol {
49 case "compact":
50 protocolFactory = thrift.NewTCompactProtocolFactory()
51 case "simplejson":
52 protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
53 case "json":
54 protocolFactory = thrift.NewTJSONProtocolFactory()
55 case "binary":
56 protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
57 default:
58 return nil, fmt.Errorf("Invalid protocol specified %s", protocol)
59 }
60 if debugClientProtocol {
61 protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "client:")
62 }
63 var trans thrift.TTransport
64 if ssl {
65 trans, err = thrift.NewTSSLSocket(hostPort, &tls.Config{InsecureSkipVerify: true})
66 } else {
67 if domain_socket != "" {
68 trans, err = thrift.NewTSocket(domain_socket)
69 } else {
70 trans, err = thrift.NewTSocket(hostPort)
71 }
72 }
73 if err != nil {
74 return nil, err
75 }
76 switch transport {
77 case "http":
78 trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/service", hostPort))
79 if err != nil {
80 return nil, err
81 }
82 case "framed":
83 trans = thrift.NewTFramedTransport(trans)
84 case "buffered":
85 trans = thrift.NewTBufferedTransport(trans, 8192)
Jens Geyerc6b991f2015-08-07 23:41:09 +020086 case "zlib":
87 trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression)
88 if err != nil {
89 return nil, err
90 }
Jens Geyerf4598682014-05-08 23:18:44 +020091 case "":
92 trans = trans
93 default:
94 return nil, fmt.Errorf("Invalid transport specified %s", transport)
95 }
96
97 if err = trans.Open(); err != nil {
98 return nil, err
99 }
100 client = thrifttest.NewThriftTestClientFactory(trans, protocolFactory)
101 return
102}