blob: 2b6f230f527cf6e802aa40dc559cc2568bd1d1b0 [file] [log] [blame]
Jens Geyer0e87c462013-06-18 22:25:07 +02001package main
Christian Lavoieafc6d8f2011-02-20 02:39:19 +00002
3/*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
Christian Lavoieafc6d8f2011-02-20 02:39:19 +000022import (
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070023 "crypto/tls"
Jens Geyer0e87c462013-06-18 22:25:07 +020024 "flag"
25 "fmt"
Jens Geyer0e87c462013-06-18 22:25:07 +020026 "os"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070027
28 "github.com/apache/thrift/lib/go/thrift"
Christian Lavoieafc6d8f2011-02-20 02:39:19 +000029)
30
31func Usage() {
Jens Geyer0e87c462013-06-18 22:25:07 +020032 fmt.Fprint(os.Stderr, "Usage of ", os.Args[0], ":\n")
33 flag.PrintDefaults()
34 fmt.Fprint(os.Stderr, "\n")
Christian Lavoieafc6d8f2011-02-20 02:39:19 +000035}
36
37func main() {
Jens Geyer0e87c462013-06-18 22:25:07 +020038 flag.Usage = Usage
39 server := flag.Bool("server", false, "Run server")
Jens Geyerc9495142013-09-08 00:31:20 +020040 protocol := flag.String("P", "binary", "Specify the protocol (binary, compact, json, simplejson)")
Jens Geyer0e87c462013-06-18 22:25:07 +020041 framed := flag.Bool("framed", false, "Use framed transport")
Jens Geyer44b19df2013-07-26 23:05:00 +020042 buffered := flag.Bool("buffered", false, "Use buffered transport")
43 addr := flag.String("addr", "localhost:9090", "Address to listen to")
Jens Geyer4c835952013-08-13 21:34:17 +020044 secure := flag.Bool("secure", false, "Use tls secure transport")
Jens Geyer0e87c462013-06-18 22:25:07 +020045
46 flag.Parse()
47
48 var protocolFactory thrift.TProtocolFactory
49 switch *protocol {
50 case "compact":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070051 protocolFactory = thrift.NewTCompactProtocolFactoryConf(nil)
Jens Geyer0e87c462013-06-18 22:25:07 +020052 case "simplejson":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070053 protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(nil)
Jens Geyer0e87c462013-06-18 22:25:07 +020054 case "json":
55 protocolFactory = thrift.NewTJSONProtocolFactory()
56 case "binary", "":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070057 protocolFactory = thrift.NewTBinaryProtocolFactoryConf(nil)
Jens Geyer0e87c462013-06-18 22:25:07 +020058 default:
59 fmt.Fprint(os.Stderr, "Invalid protocol specified", protocol, "\n")
60 Usage()
61 os.Exit(1)
62 }
Jens Geyer44b19df2013-07-26 23:05:00 +020063
64 var transportFactory thrift.TTransportFactory
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070065 cfg := &thrift.TConfiguration{
66 TLSConfig: &tls.Config{
67 InsecureSkipVerify: true,
68 },
69 }
Jens Geyer44b19df2013-07-26 23:05:00 +020070 if *buffered {
71 transportFactory = thrift.NewTBufferedTransportFactory(8192)
72 } else {
73 transportFactory = thrift.NewTTransportFactory()
74 }
75
Jens Geyer0e87c462013-06-18 22:25:07 +020076 if *framed {
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070077 transportFactory = thrift.NewTFramedTransportFactoryConf(transportFactory, cfg)
Jens Geyer0e87c462013-06-18 22:25:07 +020078 }
79
80 if *server {
Jens Geyer4c835952013-08-13 21:34:17 +020081 if err := runServer(transportFactory, protocolFactory, *addr, *secure); err != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +020082 fmt.Println("error running server:", err)
83 }
84 } else {
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070085 if err := runClient(transportFactory, protocolFactory, *addr, *secure, cfg); err != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +020086 fmt.Println("error running client:", err)
87 }
88 }
Christian Lavoieafc6d8f2011-02-20 02:39:19 +000089}