]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | package common | |
21 | ||
22 | import ( | |
23 | "compress/zlib" | |
24 | "crypto/tls" | |
25 | "flag" | |
26 | "fmt" | |
27 | "gen/thrifttest" | |
28 | "net/http" | |
29 | "thrift" | |
30 | ) | |
31 | ||
32 | var debugClientProtocol bool | |
33 | ||
34 | func init() { | |
35 | flag.BoolVar(&debugClientProtocol, "debug_client_protocol", false, "turn client protocol trace on") | |
36 | } | |
37 | ||
38 | func StartClient( | |
39 | host string, | |
40 | port int64, | |
41 | domain_socket string, | |
42 | transport string, | |
43 | protocol string, | |
44 | ssl bool) (client *thrifttest.ThriftTestClient, trans thrift.TTransport, err error) { | |
45 | ||
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 | case "header": | |
59 | protocolFactory = thrift.NewTHeaderProtocolFactory() | |
60 | default: | |
61 | return nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol) | |
62 | } | |
63 | if debugClientProtocol { | |
64 | protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "client:") | |
65 | } | |
66 | if ssl { | |
67 | trans, err = thrift.NewTSSLSocket(hostPort, &tls.Config{InsecureSkipVerify: true}) | |
68 | } else { | |
69 | if domain_socket != "" { | |
70 | trans, err = thrift.NewTSocket(domain_socket) | |
71 | } else { | |
72 | trans, err = thrift.NewTSocket(hostPort) | |
73 | } | |
74 | } | |
75 | if err != nil { | |
76 | return nil, nil, err | |
77 | } | |
78 | switch transport { | |
79 | case "http": | |
80 | if ssl { | |
81 | tr := &http.Transport{ | |
82 | TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | |
83 | } | |
84 | client := &http.Client{Transport: tr} | |
85 | trans, err = thrift.NewTHttpPostClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client}) | |
86 | fmt.Println(hostPort) | |
87 | } else { | |
88 | trans, err = thrift.NewTHttpPostClient(fmt.Sprintf("http://%s/", hostPort)) | |
89 | } | |
90 | case "framed": | |
91 | trans = thrift.NewTFramedTransport(trans) | |
92 | case "buffered": | |
93 | trans = thrift.NewTBufferedTransport(trans, 8192) | |
94 | case "zlib": | |
95 | trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression) | |
96 | case "": | |
97 | trans = trans | |
98 | default: | |
99 | return nil, nil, fmt.Errorf("Invalid transport specified %s", transport) | |
100 | } | |
101 | if err != nil { | |
102 | return nil, nil, err | |
103 | } | |
104 | if err = trans.Open(); err != nil { | |
105 | return nil, nil, err | |
106 | } | |
107 | iprot := protocolFactory.GetProtocol(trans) | |
108 | oprot := protocolFactory.GetProtocol(trans) | |
109 | client = thrifttest.NewThriftTestClient(thrift.NewTStandardClient(iprot, oprot)) | |
110 | return | |
111 | } |