예제 #1
0
파일: main.go 프로젝트: viney/gothrift
func main() {
	defer os.Exit(1)

	socket, err := thrift.NewTSocket(remoteAddr)
	if err != nil {
		fmt.Println("thrift.NewTSocket: ", err)
		return
	}
	defer socket.Close()

	transport := transportFactory.GetTransport(socket)

	client := rpc.NewRpcServiceClientFactory(transport, protocolFactory)

	// open
	if err := socket.Open(); err != nil {
		fmt.Println("socket.Open: ", err)
		return
	}

	res, err := client.FindAll(1, "123456", map[string]string{"name": "viney", "email": "*****@*****.**"})
	if err != nil {
		fmt.Println("client.FindAll:", err)
		return
	}

	fmt.Println("res: ", res)
}
예제 #2
0
func client(protocolFactory thrift.TProtocolFactory) {
	trans, err := thrift.NewTSocket(hostPort)
	if err != nil {
		log.Fatalf("Unable to create server socket: %s", err)
	}
	btrans := thrift.NewTBufferedTransport(trans, 2048)
	client := stress.NewServiceClientFactory(btrans, protocolFactory)
	err = trans.Open()
	if err != nil {
		log.Fatalf("Unable to open connection: %s", err)
	}
	ready.Done()
	ready.Wait()
	switch callType {
	case echoVoid:
		for i := 0; i < *loop; i++ {
			client.EchoVoid()
			atomic.AddInt64(&clicounter, 1)
		}
	case echoByte:
		for i := 0; i < *loop; i++ {
			client.EchoByte(42)
			atomic.AddInt64(&clicounter, 1)
		}
	case echoI32:
		for i := 0; i < *loop; i++ {
			client.EchoI32(4242)
			atomic.AddInt64(&clicounter, 1)
		}
	case echoI64:
		for i := 0; i < *loop; i++ {
			client.EchoI64(424242)
			atomic.AddInt64(&clicounter, 1)
		}
	case echoString:
		for i := 0; i < *loop; i++ {
			client.EchoString("TestString")
			atomic.AddInt64(&clicounter, 1)
		}
	case echiList:
		l := []int8{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8}
		for i := 0; i < *loop; i++ {
			client.EchoList(l)
			atomic.AddInt64(&clicounter, 1)
		}
	case echoSet:
		s := map[int8]struct{}{-10: {}, -9: {}, -8: {}, -7: {}, -6: {}, -5: {}, -4: {}, -3: {}, -2: {}, -1: {}, 0: {}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}}
		for i := 0; i < *loop; i++ {
			client.EchoSet(s)
			atomic.AddInt64(&clicounter, 1)
		}
	case echoMap:
		m := map[int8]int8{-10: 10, -9: 9, -8: 8, -7: 7, -6: 6, -5: 5, -4: 4, -3: 3, -2: 2, -1: 1, 0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8}
		for i := 0; i < *loop; i++ {
			client.EchoMap(m)
			atomic.AddInt64(&clicounter, 1)
		}
	}

	done.Done()
}
예제 #3
0
파일: client.go 프로젝트: CloudTechs/thrift
func StartClient(
	host string,
	port int64,
	domain_socket string,
	transport string,
	protocol string,
	ssl bool) (client *thrifttest.ThriftTestClient, err error) {

	hostPort := fmt.Sprintf("%s:%d", host, port)

	var protocolFactory thrift.TProtocolFactory
	switch protocol {
	case "compact":
		protocolFactory = thrift.NewTCompactProtocolFactory()
	case "simplejson":
		protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
	case "json":
		protocolFactory = thrift.NewTJSONProtocolFactory()
	case "binary":
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
	default:
		return nil, fmt.Errorf("Invalid protocol specified %s", protocol)
	}
	if debugClientProtocol {
		protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "client:")
	}
	var trans thrift.TTransport
	if ssl {
		trans, err = thrift.NewTSSLSocket(hostPort, &tls.Config{InsecureSkipVerify: true})
	} else {
		if domain_socket != "" {
			trans, err = thrift.NewTSocket(domain_socket)
		} else {
			trans, err = thrift.NewTSocket(hostPort)
		}
	}
	if err != nil {
		return nil, err
	}
	switch transport {
	case "http":
		trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/service", hostPort))
		if err != nil {
			return nil, err
		}
	case "framed":
		trans = thrift.NewTFramedTransport(trans)
	case "buffered":
		trans = thrift.NewTBufferedTransport(trans, 8192)
	case "":
		trans = trans
	default:
		return nil, fmt.Errorf("Invalid transport specified %s", transport)
	}

	if err = trans.Open(); err != nil {
		return nil, err
	}
	client = thrifttest.NewThriftTestClientFactory(trans, protocolFactory)
	return
}
예제 #4
0
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 := thrifttest.NewThriftTestClientFactory(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 "testVoid":
		if flag.NArg()-1 != 0 {
			fmt.Fprintln(os.Stderr, "TestVoid requires 0 args")
			flag.Usage()
		}
		fmt.Print(client.TestVoid())
		fmt.Print("\n")
		break
	case "testString":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestString requires 1 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1)
		value0 := argvalue0
		fmt.Print(client.TestString(value0))
		fmt.Print("\n")
		break
	case "testBool":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestBool requires 1 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1) == "true"
		value0 := argvalue0
		fmt.Print(client.TestBool(value0))
		fmt.Print("\n")
		break
	case "testByte":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestByte requires 1 args")
			flag.Usage()
		}
		tmp0, err100 := (strconv.Atoi(flag.Arg(1)))
		if err100 != nil {
			Usage()
			return
		}
		argvalue0 := byte(tmp0)
		value0 := argvalue0
		fmt.Print(client.TestByte(value0))
		fmt.Print("\n")
		break
	case "testI32":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestI32 requires 1 args")
			flag.Usage()
		}
		tmp0, err101 := (strconv.Atoi(flag.Arg(1)))
		if err101 != nil {
			Usage()
			return
		}
		argvalue0 := int32(tmp0)
		value0 := argvalue0
		fmt.Print(client.TestI32(value0))
		fmt.Print("\n")
		break
	case "testI64":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestI64 requires 1 args")
			flag.Usage()
		}
		argvalue0, err102 := (strconv.ParseInt(flag.Arg(1), 10, 64))
		if err102 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		fmt.Print(client.TestI64(value0))
		fmt.Print("\n")
		break
	case "testDouble":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestDouble requires 1 args")
			flag.Usage()
		}
		argvalue0, err103 := (strconv.ParseFloat(flag.Arg(1), 64))
		if err103 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		fmt.Print(client.TestDouble(value0))
		fmt.Print("\n")
		break
	case "testBinary":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestBinary requires 1 args")
			flag.Usage()
		}
		argvalue0 := []byte(flag.Arg(1))
		value0 := argvalue0
		fmt.Print(client.TestBinary(value0))
		fmt.Print("\n")
		break
	case "testStruct":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestStruct requires 1 args")
			flag.Usage()
		}
		arg105 := flag.Arg(1)
		mbTrans106 := thrift.NewTMemoryBufferLen(len(arg105))
		defer mbTrans106.Close()
		_, err107 := mbTrans106.WriteString(arg105)
		if err107 != nil {
			Usage()
			return
		}
		factory108 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt109 := factory108.GetProtocol(mbTrans106)
		argvalue0 := thrifttest.NewXtruct()
		err110 := argvalue0.Read(jsProt109)
		if err110 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		fmt.Print(client.TestStruct(value0))
		fmt.Print("\n")
		break
	case "testNest":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestNest requires 1 args")
			flag.Usage()
		}
		arg111 := flag.Arg(1)
		mbTrans112 := thrift.NewTMemoryBufferLen(len(arg111))
		defer mbTrans112.Close()
		_, err113 := mbTrans112.WriteString(arg111)
		if err113 != nil {
			Usage()
			return
		}
		factory114 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt115 := factory114.GetProtocol(mbTrans112)
		argvalue0 := thrifttest.NewXtruct2()
		err116 := argvalue0.Read(jsProt115)
		if err116 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		fmt.Print(client.TestNest(value0))
		fmt.Print("\n")
		break
	case "testMap":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestMap requires 1 args")
			flag.Usage()
		}
		arg117 := flag.Arg(1)
		mbTrans118 := thrift.NewTMemoryBufferLen(len(arg117))
		defer mbTrans118.Close()
		_, err119 := mbTrans118.WriteString(arg117)
		if err119 != nil {
			Usage()
			return
		}
		factory120 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt121 := factory120.GetProtocol(mbTrans118)
		containerStruct0 := thrifttest.NewThriftTestTestMapArgs()
		err122 := containerStruct0.ReadField1(jsProt121)
		if err122 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.Thing
		value0 := argvalue0
		fmt.Print(client.TestMap(value0))
		fmt.Print("\n")
		break
	case "testStringMap":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestStringMap requires 1 args")
			flag.Usage()
		}
		arg123 := flag.Arg(1)
		mbTrans124 := thrift.NewTMemoryBufferLen(len(arg123))
		defer mbTrans124.Close()
		_, err125 := mbTrans124.WriteString(arg123)
		if err125 != nil {
			Usage()
			return
		}
		factory126 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt127 := factory126.GetProtocol(mbTrans124)
		containerStruct0 := thrifttest.NewThriftTestTestStringMapArgs()
		err128 := containerStruct0.ReadField1(jsProt127)
		if err128 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.Thing
		value0 := argvalue0
		fmt.Print(client.TestStringMap(value0))
		fmt.Print("\n")
		break
	case "testSet":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestSet requires 1 args")
			flag.Usage()
		}
		arg129 := flag.Arg(1)
		mbTrans130 := thrift.NewTMemoryBufferLen(len(arg129))
		defer mbTrans130.Close()
		_, err131 := mbTrans130.WriteString(arg129)
		if err131 != nil {
			Usage()
			return
		}
		factory132 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt133 := factory132.GetProtocol(mbTrans130)
		containerStruct0 := thrifttest.NewThriftTestTestSetArgs()
		err134 := containerStruct0.ReadField1(jsProt133)
		if err134 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.Thing
		value0 := argvalue0
		fmt.Print(client.TestSet(value0))
		fmt.Print("\n")
		break
	case "testList":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestList requires 1 args")
			flag.Usage()
		}
		arg135 := flag.Arg(1)
		mbTrans136 := thrift.NewTMemoryBufferLen(len(arg135))
		defer mbTrans136.Close()
		_, err137 := mbTrans136.WriteString(arg135)
		if err137 != nil {
			Usage()
			return
		}
		factory138 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt139 := factory138.GetProtocol(mbTrans136)
		containerStruct0 := thrifttest.NewThriftTestTestListArgs()
		err140 := containerStruct0.ReadField1(jsProt139)
		if err140 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.Thing
		value0 := argvalue0
		fmt.Print(client.TestList(value0))
		fmt.Print("\n")
		break
	case "testEnum":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestEnum requires 1 args")
			flag.Usage()
		}
		tmp0, err := (strconv.Atoi(flag.Arg(1)))
		if err != nil {
			Usage()
			return
		}
		argvalue0 := thrifttest.Numberz(tmp0)
		value0 := argvalue0
		fmt.Print(client.TestEnum(value0))
		fmt.Print("\n")
		break
	case "testTypedef":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestTypedef requires 1 args")
			flag.Usage()
		}
		argvalue0, err141 := (strconv.ParseInt(flag.Arg(1), 10, 64))
		if err141 != nil {
			Usage()
			return
		}
		value0 := thrifttest.UserId(argvalue0)
		fmt.Print(client.TestTypedef(value0))
		fmt.Print("\n")
		break
	case "testMapMap":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestMapMap requires 1 args")
			flag.Usage()
		}
		tmp0, err142 := (strconv.Atoi(flag.Arg(1)))
		if err142 != nil {
			Usage()
			return
		}
		argvalue0 := int32(tmp0)
		value0 := argvalue0
		fmt.Print(client.TestMapMap(value0))
		fmt.Print("\n")
		break
	case "testInsanity":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestInsanity requires 1 args")
			flag.Usage()
		}
		arg143 := flag.Arg(1)
		mbTrans144 := thrift.NewTMemoryBufferLen(len(arg143))
		defer mbTrans144.Close()
		_, err145 := mbTrans144.WriteString(arg143)
		if err145 != nil {
			Usage()
			return
		}
		factory146 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt147 := factory146.GetProtocol(mbTrans144)
		argvalue0 := thrifttest.NewInsanity()
		err148 := argvalue0.Read(jsProt147)
		if err148 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		fmt.Print(client.TestInsanity(value0))
		fmt.Print("\n")
		break
	case "testMulti":
		if flag.NArg()-1 != 6 {
			fmt.Fprintln(os.Stderr, "TestMulti requires 6 args")
			flag.Usage()
		}
		tmp0, err149 := (strconv.Atoi(flag.Arg(1)))
		if err149 != nil {
			Usage()
			return
		}
		argvalue0 := byte(tmp0)
		value0 := argvalue0
		tmp1, err150 := (strconv.Atoi(flag.Arg(2)))
		if err150 != nil {
			Usage()
			return
		}
		argvalue1 := int32(tmp1)
		value1 := argvalue1
		argvalue2, err151 := (strconv.ParseInt(flag.Arg(3), 10, 64))
		if err151 != nil {
			Usage()
			return
		}
		value2 := argvalue2
		arg152 := flag.Arg(4)
		mbTrans153 := thrift.NewTMemoryBufferLen(len(arg152))
		defer mbTrans153.Close()
		_, err154 := mbTrans153.WriteString(arg152)
		if err154 != nil {
			Usage()
			return
		}
		factory155 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt156 := factory155.GetProtocol(mbTrans153)
		containerStruct3 := thrifttest.NewThriftTestTestMultiArgs()
		err157 := containerStruct3.ReadField4(jsProt156)
		if err157 != nil {
			Usage()
			return
		}
		argvalue3 := containerStruct3.Arg3
		value3 := argvalue3
		tmp4, err := (strconv.Atoi(flag.Arg(5)))
		if err != nil {
			Usage()
			return
		}
		argvalue4 := thrifttest.Numberz(tmp4)
		value4 := argvalue4
		argvalue5, err158 := (strconv.ParseInt(flag.Arg(6), 10, 64))
		if err158 != nil {
			Usage()
			return
		}
		value5 := thrifttest.UserId(argvalue5)
		fmt.Print(client.TestMulti(value0, value1, value2, value3, value4, value5))
		fmt.Print("\n")
		break
	case "testException":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestException requires 1 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1)
		value0 := argvalue0
		fmt.Print(client.TestException(value0))
		fmt.Print("\n")
		break
	case "testMultiException":
		if flag.NArg()-1 != 2 {
			fmt.Fprintln(os.Stderr, "TestMultiException requires 2 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1)
		value0 := argvalue0
		argvalue1 := flag.Arg(2)
		value1 := argvalue1
		fmt.Print(client.TestMultiException(value0, value1))
		fmt.Print("\n")
		break
	case "testOneway":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TestOneway requires 1 args")
			flag.Usage()
		}
		tmp0, err162 := (strconv.Atoi(flag.Arg(1)))
		if err162 != nil {
			Usage()
			return
		}
		argvalue0 := int32(tmp0)
		value0 := argvalue0
		fmt.Print(client.TestOneway(value0))
		fmt.Print("\n")
		break
	case "":
		Usage()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
	}
}
예제 #5
0
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 := thrifttest.NewSecondServiceClientFactory(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 "blahBlah":
		if flag.NArg()-1 != 0 {
			fmt.Fprintln(os.Stderr, "BlahBlah requires 0 args")
			flag.Usage()
		}
		fmt.Print(client.BlahBlah())
		fmt.Print("\n")
		break
	case "secondtestString":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "SecondtestString requires 1 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1)
		value0 := argvalue0
		fmt.Print(client.SecondtestString(value0))
		fmt.Print("\n")
		break
	case "":
		Usage()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
	}
}
예제 #6
0
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 := stress.NewServiceClientFactory(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 "echoVoid":
		if flag.NArg()-1 != 0 {
			fmt.Fprintln(os.Stderr, "EchoVoid requires 0 args")
			flag.Usage()
		}
		fmt.Print(client.EchoVoid())
		fmt.Print("\n")
		break
	case "echoByte":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "EchoByte requires 1 args")
			flag.Usage()
		}
		tmp0, err26 := (strconv.Atoi(flag.Arg(1)))
		if err26 != nil {
			Usage()
			return
		}
		argvalue0 := byte(tmp0)
		value0 := argvalue0
		fmt.Print(client.EchoByte(value0))
		fmt.Print("\n")
		break
	case "echoI32":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "EchoI32 requires 1 args")
			flag.Usage()
		}
		tmp0, err27 := (strconv.Atoi(flag.Arg(1)))
		if err27 != nil {
			Usage()
			return
		}
		argvalue0 := int32(tmp0)
		value0 := argvalue0
		fmt.Print(client.EchoI32(value0))
		fmt.Print("\n")
		break
	case "echoI64":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "EchoI64 requires 1 args")
			flag.Usage()
		}
		argvalue0, err28 := (strconv.ParseInt(flag.Arg(1), 10, 64))
		if err28 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		fmt.Print(client.EchoI64(value0))
		fmt.Print("\n")
		break
	case "echoString":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "EchoString requires 1 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1)
		value0 := argvalue0
		fmt.Print(client.EchoString(value0))
		fmt.Print("\n")
		break
	case "echoList":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "EchoList requires 1 args")
			flag.Usage()
		}
		arg30 := flag.Arg(1)
		mbTrans31 := thrift.NewTMemoryBufferLen(len(arg30))
		defer mbTrans31.Close()
		_, err32 := mbTrans31.WriteString(arg30)
		if err32 != nil {
			Usage()
			return
		}
		factory33 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt34 := factory33.GetProtocol(mbTrans31)
		containerStruct0 := stress.NewServiceEchoListArgs()
		err35 := containerStruct0.ReadField1(jsProt34)
		if err35 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.Arg
		value0 := argvalue0
		fmt.Print(client.EchoList(value0))
		fmt.Print("\n")
		break
	case "echoSet":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "EchoSet requires 1 args")
			flag.Usage()
		}
		arg36 := flag.Arg(1)
		mbTrans37 := thrift.NewTMemoryBufferLen(len(arg36))
		defer mbTrans37.Close()
		_, err38 := mbTrans37.WriteString(arg36)
		if err38 != nil {
			Usage()
			return
		}
		factory39 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt40 := factory39.GetProtocol(mbTrans37)
		containerStruct0 := stress.NewServiceEchoSetArgs()
		err41 := containerStruct0.ReadField1(jsProt40)
		if err41 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.Arg
		value0 := argvalue0
		fmt.Print(client.EchoSet(value0))
		fmt.Print("\n")
		break
	case "echoMap":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "EchoMap requires 1 args")
			flag.Usage()
		}
		arg42 := flag.Arg(1)
		mbTrans43 := thrift.NewTMemoryBufferLen(len(arg42))
		defer mbTrans43.Close()
		_, err44 := mbTrans43.WriteString(arg42)
		if err44 != nil {
			Usage()
			return
		}
		factory45 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt46 := factory45.GetProtocol(mbTrans43)
		containerStruct0 := stress.NewServiceEchoMapArgs()
		err47 := containerStruct0.ReadField1(jsProt46)
		if err47 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.Arg
		value0 := argvalue0
		fmt.Print(client.EchoMap(value0))
		fmt.Print("\n")
		break
	case "":
		Usage()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
	}
}