func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool, m calendar.Message) error { var transport thrift.TTransport var err error if secure { cfg := new(tls.Config) cfg.InsecureSkipVerify = true transport, err = thrift.NewTSSLSocket(addr, cfg) } else { transport, err = thrift.NewTSocket(addr) } if err != nil { fmt.Println("Error opening socket:", err) return err } transport = transportFactory.GetTransport(transport) defer transport.Close() if err := transport.Open(); err != nil { return err } return handleClient(calendar.NewMyServiceClientFactory(transport, protocolFactory), m) }
func main() { flag.Usage = Usage var host string var port int var protocol string var urlString string var framed bool var useHttp bool var parsedUrl url.URL var trans thrift.TTransport _ = strconv.Atoi _ = math.Abs flag.Usage = Usage flag.StringVar(&host, "h", "localhost", "Specify host and port") flag.IntVar(&port, "p", 9090, "Specify port") flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)") flag.StringVar(&urlString, "u", "", "Specify the url") flag.BoolVar(&framed, "framed", false, "Use framed transport") flag.BoolVar(&useHttp, "http", false, "Use http") flag.Parse() if len(urlString) > 0 { parsedUrl, err := url.Parse(urlString) if err != nil { fmt.Fprintln(os.Stderr, "Error parsing URL: ", err) flag.Usage() } host = parsedUrl.Host useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http" } else if useHttp { _, err := url.Parse(fmt.Sprint("http://", host, ":", port)) if err != nil { fmt.Fprintln(os.Stderr, "Error parsing URL: ", err) flag.Usage() } } cmd := flag.Arg(0) var err error if useHttp { trans, err = thrift.NewTHttpClient(parsedUrl.String()) } else { portStr := fmt.Sprint(port) if strings.Contains(host, ":") { host, portStr, err = net.SplitHostPort(host) if err != nil { fmt.Fprintln(os.Stderr, "error with host:", err) os.Exit(1) } } trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr)) if err != nil { fmt.Fprintln(os.Stderr, "error resolving address:", err) os.Exit(1) } if framed { trans = thrift.NewTFramedTransport(trans) } } if err != nil { fmt.Fprintln(os.Stderr, "Error creating transport", err) os.Exit(1) } defer trans.Close() var protocolFactory thrift.TProtocolFactory switch protocol { case "compact": protocolFactory = thrift.NewTCompactProtocolFactory() break case "simplejson": protocolFactory = thrift.NewTSimpleJSONProtocolFactory() break case "json": protocolFactory = thrift.NewTJSONProtocolFactory() break case "binary", "": protocolFactory = thrift.NewTBinaryProtocolFactoryDefault() break default: fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol) Usage() os.Exit(1) } client := calendar.NewMyServiceClientFactory(trans, protocolFactory) if err := trans.Open(); err != nil { fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err) os.Exit(1) } switch cmd { case "PuttoBack": if flag.NArg()-1 != 1 { fmt.Fprintln(os.Stderr, "PuttoBack requires 1 args") flag.Usage() } arg8 := flag.Arg(1) mbTrans9 := thrift.NewTMemoryBufferLen(len(arg8)) defer mbTrans9.Close() _, err10 := mbTrans9.WriteString(arg8) if err10 != nil { Usage() return } factory11 := thrift.NewTSimpleJSONProtocolFactory() jsProt12 := factory11.GetProtocol(mbTrans9) argvalue0 := calendar.NewMessage() err13 := argvalue0.Read(jsProt12) if err13 != nil { Usage() return } value0 := argvalue0 fmt.Print(client.PuttoBack(value0)) fmt.Print("\n") break case "PuttoFront": if flag.NArg()-1 != 1 { fmt.Fprintln(os.Stderr, "PuttoFront requires 1 args") flag.Usage() } arg14 := flag.Arg(1) mbTrans15 := thrift.NewTMemoryBufferLen(len(arg14)) defer mbTrans15.Close() _, err16 := mbTrans15.WriteString(arg14) if err16 != nil { Usage() return } factory17 := thrift.NewTSimpleJSONProtocolFactory() jsProt18 := factory17.GetProtocol(mbTrans15) argvalue0 := calendar.NewMessage() err19 := argvalue0.Read(jsProt18) if err19 != nil { Usage() return } value0 := argvalue0 fmt.Print(client.PuttoFront(value0)) fmt.Print("\n") break case "getLock": if flag.NArg()-1 != 1 { fmt.Fprintln(os.Stderr, "GetLock requires 1 args") flag.Usage() } argvalue0 := flag.Arg(1) value0 := argvalue0 fmt.Print(client.GetLock(value0)) fmt.Print("\n") break case "": Usage() break default: fmt.Fprintln(os.Stderr, "Invalid function ", cmd) } }