Beispiel #1
0
func runClient(transportFactory thrift.TTransportFactory,
	protocolFactory thrift.TProtocolFactory, addr string, secure bool) error {

	opts := nats.DefaultOptions
	opts.Servers = []string{addr}
	opts.Secure = secure
	conn, err := opts.Connect()
	if err != nil {
		return err
	}
	defer conn.Close()

	transport, err := thrift_nats.NATSTransportFactory(conn, "foo", time.Second, time.Second)
	if err != nil {
		return err
	}
	transport = transportFactory.GetTransport(transport)

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

	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
	}
	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))
}
Beispiel #3
0
func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string) error {
	var transport thrift.TTransport
	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(tutorial.NewCalculatorClientFactory(transport, protocolFactory))
}
Beispiel #4
0
func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) error {
	var transport thrift.TTransport
	var err error

	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(awesome_service.NewAwesomeServiceClientFactory(transport, protocolFactory))
}
Beispiel #5
0
func NewThriftRequestExec(tFac thrift.TTransportFactory, clientExec ThriftClientExecutor, timeout time.Duration, hosts ...string) bender.RequestExecutor {
	return func(_ int64, request interface{}) (interface{}, error) {
		addr := hosts[rand.Intn(len(hosts))]
		socket, err := thrift.NewTSocketTimeout(addr, timeout)
		if err != nil {
			return nil, err
		}
		defer socket.Close()

		transport := tFac.GetTransport(socket)
		if err := transport.Open(); err != nil {
			return nil, err
		}
		defer transport.Close()

		return clientExec(request, transport)
	}
}
Beispiel #6
0
func Update(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, variable *person.Person) (*person.Person, error) {
	socket, err := thrift.NewTSocket("localhost:4000")
	if err != nil {
		log.Println(err)
	}
	transport := transportFactory.GetTransport(socket)
	client := person.NewPersonServiceClientFactory(transport, protocolFactory)
	defer client.Transport.Close()

	if err := client.Transport.Open(); err != nil {
		log.Println(err)
	}

	result, err := client.Update(variable)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, nil
}
Beispiel #7
0
func Delete(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, id int64) error {
	socket, err := thrift.NewTSocket("localhost:4000")
	if err != nil {
		log.Println(err)
	}
	transport := transportFactory.GetTransport(socket)
	client := person.NewPersonServiceClientFactory(transport, protocolFactory)
	defer client.Transport.Close()

	if err := client.Transport.Open(); err != nil {
		log.Println(err)
	}

	err = client.Destroy(id)
	if err != nil {
		log.Println(err)
		return err
	}
	return nil
}
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))
}
Beispiel #9
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)
}
Beispiel #10
0
func testHappyPath(t *testing.T, proto thrift.TProtocolFactory, buffered, framed bool) {
	options := nats.DefaultOptions
	options.Servers = []string{fmt.Sprintf("nats://localhost:%d", port)}
	serverConn, err := options.Connect()
	if err != nil {
		t.Fatal("Failed to connect to gnatsd", err)
	}
	defer serverConn.Close()
	clientConn, err := options.Connect()
	if err != nil {
		t.Fatal("Failed to connect to gnatsd", err)
	}
	defer clientConn.Close()

	var transportFactory thrift.TTransportFactory
	if buffered {
		transportFactory = thrift.NewTBufferedTransportFactory(8192)
	} else {
		transportFactory = thrift.NewTTransportFactory()
	}

	if framed {
		transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
	}

	processor := tutorial.NewCalculatorProcessor(NewCalculatorHandler())
	server := thrift_nats.NewNATSServer(serverConn, subject, -1, -1, processor,
		transportFactory, proto)

	wait := make(chan bool)
	go func() {
		wait <- true
		if err := server.Serve(); err != nil {
			t.Fatal("Failed to start thrift server", err)
		}
	}()
	<-wait
	defer server.Stop()

	// Give the server a chance to start.
	time.Sleep(500 * time.Millisecond)

	transport, err := thrift_nats.NATSTransportFactory(clientConn, subject, time.Second, -1)
	if err != nil {
		t.Fatal("Failed to create NATS transport", err)
	}
	transport = transportFactory.GetTransport(transport)
	if err := transport.Open(); err != nil {
		t.Fatal("Failed to open transport", err)
	}
	defer transport.Close()

	client := tutorial.NewCalculatorClientFactory(transport, proto)
	if err := client.Ping(); err != nil {
		t.Fatal("Ping failed", err)
	}
	if err := client.Zip(); err != nil {
		t.Fatal("Zip failed", err)
	}
	sum, err := client.Add(1, 1)
	if err != nil {
		t.Fatal("Sum failed", err)
	}
	if sum != 2 {
		t.Fatalf("Expected Sum result 2, got %d", sum)
	}

	work := tutorial.NewWork()
	work.Op = tutorial.Operation_DIVIDE
	work.Num1 = 8
	work.Num2 = 4
	quotient, err := client.Calculate(1, work)
	if err != nil {
		t.Fatal("Calculate failed", err)
	}
	if sum != 2 {
		t.Fatalf("Expected Calculate result 4, got %d", quotient)
	}

	work.Op = tutorial.Operation_DIVIDE
	work.Num1 = 1
	work.Num2 = 0
	_, err = client.Calculate(2, work)
	switch v := err.(type) {
	case *tutorial.InvalidOperation:
		break
	default:
		t.Fatalf("Expected Calculate to return InvalidOperation, got %s", v)
	}

	log, err := client.GetStruct(1)
	if err != nil {
		t.Fatal("GetStruct failed", err)
	}
	if log.Value != "2" {
		t.Fatalf("Expected struct value `2`, got `%s`", log.Value)
	}

	log, err = client.GetStruct(2)
	if err != nil {
		t.Fatal("GetStruct failed", err)
	}
	if log != nil {
		t.Fatalf("Expected nil struct, got `%s`", log)
	}
}
Beispiel #11
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
			}
		}
	}
}