blob: 236ce43eaf6b4a21a3455af844234cb756b58cd8 [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"
claudemirof8ca0552016-01-10 23:31:30 -020028 "net/http"
Jens Geyerf4598682014-05-08 23:18:44 +020029 "thrift"
30)
31
32var debugClientProtocol bool
33
34func init() {
35 flag.BoolVar(&debugClientProtocol, "debug_client_protocol", false, "turn client protocol trace on")
36}
37
38func StartClient(
39 host string,
40 port int64,
41 domain_socket string,
42 transport string,
43 protocol string,
D. Can Celasun4f77ab82017-09-21 15:21:00 +020044 ssl bool) (client *thrifttest.ThriftTestClient, trans thrift.TTransport, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020045
46 hostPort := fmt.Sprintf("%s:%d", host, port)
47
48 var protocolFactory thrift.TProtocolFactory
49 switch protocol {
50 case "compact":
51 protocolFactory = thrift.NewTCompactProtocolFactory()
52 case "simplejson":
53 protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
54 case "json":
55 protocolFactory = thrift.NewTJSONProtocolFactory()
56 case "binary":
57 protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
58 default:
D. Can Celasun4f77ab82017-09-21 15:21:00 +020059 return nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol)
Jens Geyerf4598682014-05-08 23:18:44 +020060 }
61 if debugClientProtocol {
62 protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "client:")
63 }
Jens Geyerf4598682014-05-08 23:18:44 +020064 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 {
D. Can Celasun4f77ab82017-09-21 15:21:00 +020074 return nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +020075 }
76 switch transport {
77 case "http":
claudemirof8ca0552016-01-10 23:31:30 -020078 if ssl {
79 tr := &http.Transport{
80 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
81 }
82 client := &http.Client{Transport: tr}
83 trans, err = thrift.NewTHttpPostClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client})
84 fmt.Println(hostPort)
85 } else {
86 trans, err = thrift.NewTHttpPostClient(fmt.Sprintf("http://%s/", hostPort))
87 }
Jens Geyerf4598682014-05-08 23:18:44 +020088 case "framed":
89 trans = thrift.NewTFramedTransport(trans)
90 case "buffered":
91 trans = thrift.NewTBufferedTransport(trans, 8192)
Jens Geyerc6b991f2015-08-07 23:41:09 +020092 case "zlib":
93 trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression)
Jens Geyerf4598682014-05-08 23:18:44 +020094 case "":
95 trans = trans
96 default:
D. Can Celasun4f77ab82017-09-21 15:21:00 +020097 return nil, nil, fmt.Errorf("Invalid transport specified %s", transport)
Jens Geyerf4598682014-05-08 23:18:44 +020098 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +020099 if err != nil {
100 return nil, nil, err
101 }
Jens Geyerf4598682014-05-08 23:18:44 +0200102 if err = trans.Open(); err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200103 return nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +0200104 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200105 iprot := protocolFactory.GetProtocol(trans)
106 oprot := protocolFactory.GetProtocol(trans)
107 client = thrifttest.NewThriftTestClient(thrift.NewTStandardClient(iprot, oprot))
Jens Geyerf4598682014-05-08 23:18:44 +0200108 return
109}