Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 1 | package main |
Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [diff] [blame] | 2 | |
| 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 Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [diff] [blame] | 22 | import ( |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 23 | "crypto/tls" |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 24 | "flag" |
| 25 | "fmt" |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 26 | "os" |
Yuxuan 'fishy' Wang | b71f11e | 2021-03-22 15:01:00 -0700 | [diff] [blame] | 27 | |
| 28 | "github.com/apache/thrift/lib/go/thrift" |
Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func Usage() { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 32 | fmt.Fprint(os.Stderr, "Usage of ", os.Args[0], ":\n") |
| 33 | flag.PrintDefaults() |
| 34 | fmt.Fprint(os.Stderr, "\n") |
Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | func main() { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 38 | flag.Usage = Usage |
| 39 | server := flag.Bool("server", false, "Run server") |
Jens Geyer | c949514 | 2013-09-08 00:31:20 +0200 | [diff] [blame] | 40 | protocol := flag.String("P", "binary", "Specify the protocol (binary, compact, json, simplejson)") |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 41 | framed := flag.Bool("framed", false, "Use framed transport") |
Jens Geyer | 44b19df | 2013-07-26 23:05:00 +0200 | [diff] [blame] | 42 | buffered := flag.Bool("buffered", false, "Use buffered transport") |
| 43 | addr := flag.String("addr", "localhost:9090", "Address to listen to") |
Jens Geyer | 4c83595 | 2013-08-13 21:34:17 +0200 | [diff] [blame] | 44 | secure := flag.Bool("secure", false, "Use tls secure transport") |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 45 | |
| 46 | flag.Parse() |
| 47 | |
| 48 | var protocolFactory thrift.TProtocolFactory |
| 49 | switch *protocol { |
| 50 | case "compact": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 51 | protocolFactory = thrift.NewTCompactProtocolFactoryConf(nil) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 52 | case "simplejson": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 53 | protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(nil) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 54 | case "json": |
| 55 | protocolFactory = thrift.NewTJSONProtocolFactory() |
| 56 | case "binary", "": |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 57 | protocolFactory = thrift.NewTBinaryProtocolFactoryConf(nil) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 58 | default: |
| 59 | fmt.Fprint(os.Stderr, "Invalid protocol specified", protocol, "\n") |
| 60 | Usage() |
| 61 | os.Exit(1) |
| 62 | } |
Jens Geyer | 44b19df | 2013-07-26 23:05:00 +0200 | [diff] [blame] | 63 | |
| 64 | var transportFactory thrift.TTransportFactory |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 65 | cfg := &thrift.TConfiguration{ |
| 66 | TLSConfig: &tls.Config{ |
| 67 | InsecureSkipVerify: true, |
| 68 | }, |
| 69 | } |
Jens Geyer | 44b19df | 2013-07-26 23:05:00 +0200 | [diff] [blame] | 70 | if *buffered { |
| 71 | transportFactory = thrift.NewTBufferedTransportFactory(8192) |
| 72 | } else { |
| 73 | transportFactory = thrift.NewTTransportFactory() |
| 74 | } |
| 75 | |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 76 | if *framed { |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 77 | transportFactory = thrift.NewTFramedTransportFactoryConf(transportFactory, cfg) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if *server { |
Jens Geyer | 4c83595 | 2013-08-13 21:34:17 +0200 | [diff] [blame] | 81 | if err := runServer(transportFactory, protocolFactory, *addr, *secure); err != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 82 | fmt.Println("error running server:", err) |
| 83 | } |
| 84 | } else { |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame] | 85 | if err := runClient(transportFactory, protocolFactory, *addr, *secure, cfg); err != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 86 | fmt.Println("error running client:", err) |
| 87 | } |
| 88 | } |
Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [diff] [blame] | 89 | } |