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