Exemplo n.º 1
0
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(demoThrift.NewDemoThriftClientFactory(transport, protocolFactory))
}
Exemplo n.º 2
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
			}
		}
	}
}