blob: afac6bba708f3dfe747d3973cd4341f090be51f1 [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 (
Jens Geyer0e87c462013-06-18 22:25:07 +020023 "flag"
24 "fmt"
Jens Geyer0e87c462013-06-18 22:25:07 +020025 "os"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070026
27 "github.com/apache/thrift/lib/go/thrift"
Christian Lavoieafc6d8f2011-02-20 02:39:19 +000028)
29
30func Usage() {
Jens Geyer0e87c462013-06-18 22:25:07 +020031 fmt.Fprint(os.Stderr, "Usage of ", os.Args[0], ":\n")
32 flag.PrintDefaults()
33 fmt.Fprint(os.Stderr, "\n")
Christian Lavoieafc6d8f2011-02-20 02:39:19 +000034}
35
36func main() {
Jens Geyer0e87c462013-06-18 22:25:07 +020037 flag.Usage = Usage
38 server := flag.Bool("server", false, "Run server")
Jens Geyerc9495142013-09-08 00:31:20 +020039 protocol := flag.String("P", "binary", "Specify the protocol (binary, compact, json, simplejson)")
Jens Geyer0e87c462013-06-18 22:25:07 +020040 framed := flag.Bool("framed", false, "Use framed transport")
Jens Geyer44b19df2013-07-26 23:05:00 +020041 buffered := flag.Bool("buffered", false, "Use buffered transport")
42 addr := flag.String("addr", "localhost:9090", "Address to listen to")
Jens Geyer4c835952013-08-13 21:34:17 +020043 secure := flag.Bool("secure", false, "Use tls secure transport")
Jens Geyer0e87c462013-06-18 22:25:07 +020044
45 flag.Parse()
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 fmt.Fprint(os.Stderr, "Invalid protocol specified", protocol, "\n")
59 Usage()
60 os.Exit(1)
61 }
Jens Geyer44b19df2013-07-26 23:05:00 +020062
63 var transportFactory thrift.TTransportFactory
64 if *buffered {
65 transportFactory = thrift.NewTBufferedTransportFactory(8192)
66 } else {
67 transportFactory = thrift.NewTTransportFactory()
68 }
69
Jens Geyer0e87c462013-06-18 22:25:07 +020070 if *framed {
71 transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
72 }
73
74 if *server {
Jens Geyer4c835952013-08-13 21:34:17 +020075 if err := runServer(transportFactory, protocolFactory, *addr, *secure); err != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +020076 fmt.Println("error running server:", err)
77 }
78 } else {
Jens Geyer4c835952013-08-13 21:34:17 +020079 if err := runClient(transportFactory, protocolFactory, *addr, *secure); err != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +020080 fmt.Println("error running client:", err)
81 }
82 }
Christian Lavoieafc6d8f2011-02-20 02:39:19 +000083}