func Start() { cfg := g.Config() if !cfg.Rrd.Enable { log.Println("rrd.Start warning, not enable") return } // get rrd svr config protocol := cfg.Rrd.Protocol buffered := cfg.Rrd.Buffered framed := cfg.Rrd.Framed addr := cfg.Rrd.Listen cliTimeout := cfg.Rrd.CallTimeout // protocal factory 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: log.Fatalln("rrd.Start error, invalid protocol", protocol) } // transport factory var transportFactory thrift.TTransportFactory if buffered { transportFactory = thrift.NewTBufferedTransportFactory(10240) // 10K bytes } else { transportFactory = thrift.NewTTransportFactory() } if framed { transportFactory = thrift.NewTFramedTransportFactory(transportFactory) } // run server go func() { if err := RunServer(transportFactory, protocolFactory, addr, cliTimeout); err != nil { fmt.Println("running server error,", err) } }() }
func main() { flag.Usage = Usage server := flag.Bool("server", false, "Run server") protocol := flag.String("P", "binary", "Specify the protocol (binary, compact, json, simplejson)") framed := flag.Bool("framed", false, "Use framed transport") buffered := flag.Bool("buffered", false, "Use buffered transport") addr := flag.String("addr", "localhost:9090", "Address to listen to") secure := flag.Bool("secure", false, "Use tls secure transport") flag.Parse() 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: fmt.Fprint(os.Stderr, "Invalid protocol specified", protocol, "\n") Usage() os.Exit(1) } var transportFactory thrift.TTransportFactory if *buffered { transportFactory = thrift.NewTBufferedTransportFactory(8192) } else { transportFactory = thrift.NewTTransportFactory() } if *framed { transportFactory = thrift.NewTFramedTransportFactory(transportFactory) } if *server { if err := svr.RunServer(transportFactory, protocolFactory, *addr, *secure); err != nil { fmt.Println("error running server:", err) } } else { if err := cli.RunClient(transportFactory, protocolFactory, *addr, *secure); err != nil { fmt.Println("error running client:", err) } } }