コード例 #1
0
ファイル: trancam.go プロジェクト: Digaku/ansvia-lessons
func main() {

	var trans thrift.TTransport

	var host = "127.0.0.1"
	var port = 7366

	addr, err := net.ResolveTCPAddr("tcp", fmt.Sprint(host, ":", port))
	if err != nil {
		fmt.Fprint(os.Stderr, "Error resolving address", err.Error())
		os.Exit(1)
	}
	//	trans, err = thrift.NewTNonblockingSocketAddr(addr)
	trans = thrift.NewTSocketAddr(addr)

	defer trans.Close()

	var protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()

	client := trancam.NewTrancamServerClientFactory(trans, protocolFactory)
	if err = trans.Open(); err != nil {
		fmt.Fprint(os.Stderr, "Error opening socket ", host, ":", port, " ", err)
		os.Exit(1)
	}

	client.Ping(&trancam.Ping{2194})
	rv, _ := client.Mul(50, 5)
	fmt.Println("rv: ", rv)

}
コード例 #2
0
ファイル: trancam-bidi.go プロジェクト: Digaku/ansvia-lessons
func main() {

	var trans thrift.TTransport

	var host = "127.0.0.1"
	var port = 7366

	addr, err := net.ResolveTCPAddr("tcp", fmt.Sprint(host, ":", port))
	if err != nil {
		fmt.Fprint(os.Stderr, "Error resolving address", err.Error())
		os.Exit(1)
	}
	//	trans, err = thrift.NewTNonblockingSocketAddr(addr)
	trans = thrift.NewTSocketAddr(addr)

	trans.Open()

	defer trans.Close()

	framedTransport := thrift.NewTFramedTransport(trans)
	var protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
	protocol := protocolFactory.GetProtocol(framedTransport)

	processor := trancam.NewTrancamServerProcessor(NewTrancamHandler())

	//go func(){
	for {
		_, err := processor.Process(protocol, protocol)
		if err != nil {
			fmt.Println("error ", err)
			break
		}
	}
	//}()
}
コード例 #3
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 help bool
	var parsedUrl url.URL
	var trans thrift.TTransport
	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.BoolVar(&help, "help", false, "See usage string")
	flag.Parse()
	if help || flag.NArg() == 0 {
		flag.Usage()
	}

	if len(urlString) > 0 {
		parsedUrl, err := url.Parse(urlString)
		if err != nil {
			fmt.Fprint(os.Stderr, "Error parsing URL: ", err.Error(), "\n")
			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.Fprint(os.Stderr, "Error parsing URL: ", err.Error(), "\n")
			flag.Usage()
		}
	}

	cmd := flag.Arg(0)
	var err error
	if useHttp {
		trans, err = thrift.NewTHttpClient(parsedUrl.String())
	} else {
		addr, err := net.ResolveTCPAddr("tcp", fmt.Sprint(host, ":", port))
		if err != nil {
			fmt.Fprint(os.Stderr, "Error resolving address", err.Error())
			os.Exit(1)
		}
		trans, err = thrift.NewTNonblockingSocketAddr(addr)
		if framed {
			trans = thrift.NewTFramedTransport(trans)
		}
	}
	if err != nil {
		fmt.Fprint(os.Stderr, "Error creating transport", err.Error())
		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.Fprint(os.Stderr, "Invalid protocol specified: ", protocol, "\n")
		Usage()
		os.Exit(1)
	}
	client := trancam.NewTrancamServerClientFactory(trans, protocolFactory)
	if err = trans.Open(); err != nil {
		fmt.Fprint(os.Stderr, "Error opening socket to ", host, ":", port, " ", err.Error())
		os.Exit(1)
	}

	switch cmd {
	case "ping":
		if flag.NArg()-1 != 1 {
			fmt.Fprint(os.Stderr, "Ping requires 1 args\n")
			flag.Usage()
		}
		arg27 := flag.Arg(1)
		mbTrans28 := thrift.NewTMemoryBufferLen(len(arg27))
		defer mbTrans28.Close()
		_, err29 := mbTrans28.WriteString(arg27)
		if err29 != nil {
			Usage()
			return
		}
		factory30 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt31 := factory30.GetProtocol(mbTrans28)
		argvalue0 := trancam.NewPing()
		err32 := argvalue0.Read(jsProt31)
		if err32 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		fmt.Print(client.Ping(value0))
		fmt.Print("\n")
		break
	case "mul":
		if flag.NArg()-1 != 2 {
			fmt.Fprint(os.Stderr, "Mul requires 2 args\n")
			flag.Usage()
		}
		tmp0, err33 := (strconv.Atoi(flag.Arg(1)))
		if err33 != nil {
			Usage()
			return
		}
		argvalue0 := int32(tmp0)
		value0 := argvalue0
		tmp1, err34 := (strconv.Atoi(flag.Arg(2)))
		if err34 != nil {
			Usage()
			return
		}
		argvalue1 := int32(tmp1)
		value1 := argvalue1
		fmt.Print(client.Mul(value0, value1))
		fmt.Print("\n")
		break
	case "":
		Usage()
		break
	default:
		fmt.Fprint(os.Stderr, "Invalid function ", cmd, "\n")
	}
}