func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) 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
	}
	if transport == nil {
		return fmt.Errorf("Error opening socket, got nil transport. Is server available?")
	}
	transport = transportFactory.GetTransport(transport)
	if transport == nil {
		return fmt.Errorf("Error from transportFactory.GetTransport(), got nil transport. Is server available?")
	}

	err = transport.Open()
	if err != nil {
		return err
	}
	defer transport.Close()

	return handleClient(tutorial.NewCalculatorClientFactory(transport, protocolFactory))
}
func RunClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) 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(addrservice.NewAddressServiceClientFactory(transport, protocolFactory))
}
Esempio n. 3
0
func RunThriftService(thriftFunc ThriftFunc) (map[string]string, error) {

	//addr := flag.String("addr", "localhost:9090", "Address to listen to")
	//secure := flag.Bool("secure", false, "Use tls secure transport")
	//flag.Parse()
	addr := "localhost:9090"
	secure := true

	var protocolFactory thrift.TProtocolFactory
	protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()

	var transportFactory thrift.TTransportFactory
	transportFactory = thrift.NewTTransportFactory()

	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 nil, err
	}
	transport = transportFactory.GetTransport(transport)
	defer transport.Close()
	if err := transport.Open(); err != nil {
		return nil, err
	}
	client := thriftlib.NewBaseServiceClientFactory(transport, protocolFactory)

	return thriftFunc(client)
}
Esempio n. 4
0
func RunThrifter(index int, dataChan chan *demoThrift.RequestInfo) {
	var err error = nil
	var data *demoThrift.RequestInfo

	for {
		if err != nil { // wait for a while when retry
			time.Sleep(time.Duration(30) * time.Second)
		}

		secure := false
		protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

		//transportFactory := thrift.NewTTransportFactory()
		var transportFactory thrift.TTransportFactory
		//transportFactory = thrift.NewTBufferedTransportFactory(8192)
		transportFactory = thrift.NewTTransportFactory()
		//transportFactory = thrift.NewTFramedTransportFactory(transportFactory)

		var transport thrift.TTransport
		if secure {
			cfg := new(tls.Config)
			cfg.InsecureSkipVerify = true
			transport, err = thrift.NewTSSLSocket(ThrifterAddr, cfg)
		} else {
			transport, err = thrift.NewTSocket(ThrifterAddr)
		}
		if err != nil {
			Log.Error("[No.%d sender] Error opening socket: %s", index, err)
			continue
		}

		transport = transportFactory.GetTransport(transport)
		if err = transport.Open(); err != nil {
			Log.Error("[No.%d sender] Error opening transport: %s", index, err)
			continue
		}

		client := demoThrift.NewDemoThriftClientFactory(transport, protocolFactory)
		if client == nil {
			Log.Error("[No.%d sender] Error new unify-client", index)
			transport.Close()
			continue
		}
		Log.Info("[No.%d sender] connected to judge center", index)

		// loop for sending
		for {
			if data == nil {
				data = <-dataChan
			}
			//Log.Info("No.%d sender work!", index)

			var result string
			// retry times
			for i := 0; i < 2; i++ {
				result, err = client.SendtoParser(data)
				if err != nil {
					Log.Error("[No.%d sender] client.SendtoParser failed, result: %s, err: %s", index, result, err.Error())
					time.Sleep(time.Duration(5) * time.Second)
					continue
				}
				data = nil
				Log.Info("[No.%d sender] get result: %s", index, result)
				break
			}

			if err != nil {
				// reconnect
				transport.Close()
				Log.Info("[No.%d sender] try to reconnect, err: %s", index, err.Error())
				break
			}
		}
	}
}