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 ( |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 23 | "flag" |
| 24 | "fmt" |
| 25 | "git.apache.org/thrift.git/lib/go/thrift" |
| 26 | "os" |
Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | func Usage() { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 30 | fmt.Fprint(os.Stderr, "Usage of ", os.Args[0], ":\n") |
| 31 | flag.PrintDefaults() |
| 32 | fmt.Fprint(os.Stderr, "\n") |
Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | func main() { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 36 | flag.Usage = Usage |
| 37 | server := flag.Bool("server", false, "Run server") |
| 38 | protocol := flag.String("P", "binary", "Specify the protocol (binary, compact, simplejson)") |
| 39 | framed := flag.Bool("framed", false, "Use framed transport") |
Jens Geyer | 44b19df | 2013-07-26 23:05:00 +0200 | [diff] [blame] | 40 | buffered := flag.Bool("buffered", false, "Use buffered transport") |
| 41 | addr := flag.String("addr", "localhost:9090", "Address to listen to") |
Jens Geyer | 4c83595 | 2013-08-13 21:34:17 +0200 | [diff] [blame^] | 42 | secure := flag.Bool("secure", false, "Use tls secure transport") |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 43 | |
| 44 | flag.Parse() |
| 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 | fmt.Fprint(os.Stderr, "Invalid protocol specified", protocol, "\n") |
| 58 | Usage() |
| 59 | os.Exit(1) |
| 60 | } |
Jens Geyer | 44b19df | 2013-07-26 23:05:00 +0200 | [diff] [blame] | 61 | |
| 62 | var transportFactory thrift.TTransportFactory |
| 63 | if *buffered { |
| 64 | transportFactory = thrift.NewTBufferedTransportFactory(8192) |
| 65 | } else { |
| 66 | transportFactory = thrift.NewTTransportFactory() |
| 67 | } |
| 68 | |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 69 | if *framed { |
| 70 | transportFactory = thrift.NewTFramedTransportFactory(transportFactory) |
| 71 | } |
| 72 | |
| 73 | if *server { |
Jens Geyer | 4c83595 | 2013-08-13 21:34:17 +0200 | [diff] [blame^] | 74 | if err := runServer(transportFactory, protocolFactory, *addr, *secure); err != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 75 | fmt.Println("error running server:", err) |
| 76 | } |
| 77 | } else { |
Jens Geyer | 4c83595 | 2013-08-13 21:34:17 +0200 | [diff] [blame^] | 78 | if err := runClient(transportFactory, protocolFactory, *addr, *secure); err != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 79 | fmt.Println("error running client:", err) |
| 80 | } |
| 81 | } |
Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [diff] [blame] | 82 | } |