예제 #1
0
func finalizer(client ClientInt) {
	log.ProtoLevel().Tag("gormethods", "client").Printf("Finalize %v, %v", client.SessionName(), client.InstanceName())
	err := client.Destroy()
	if err != nil {
		log.Error("Client finalizer failed:", err)
	}
	err = client.CloseClient()
	if err != nil {
		log.Error("Client finalizer failed:", err)
	}
}
예제 #2
0
func Example() {
	// Create a database of credentials
	owners := auth.NewPlainTextCredentials()
	// Authorize the user id
	err := owners.Add(&auth.PlainText{"id", "pass1234"})
	if err != nil {
		log.Error(err)
	}
	// Constructors
	constructor := core.NewConstructor()
	// Struct with the instances
	instances := core.NewInstances()
	// New instance of a calculator.
	err = instances.New("1", "1", reflect.ValueOf(dummy.Calculator{}), owners)
	if err != nil {
		log.Error(err)
	}
	// Export the return value 0 of BigAdd with the client ResultClient
	err = instances.Export("1", "1", &core.ToExport{Method: "BigAdd", RetvalPos: 0, Export: &core.Export{Client: &dummy.ResultClient{}}})
	if err != nil {
		log.Error(err)
	}

	// Create the server
	server := &core.Server{
		Proto:       "tcp",
		Addr:        "localhost:0",
		ConnTimeout: 30 * time.Second,
		Insts:       instances,
		Cons:        constructor,
		FifoLength:  1,
		Ttl:         24 * time.Hour,
		Cleanup:     5 * time.Minute,
	}

	// Start the server
	server.Start()
	defer server.Close()

	// Configure the client
	calc := &dummy.CalculatorClient{
		Net:         "tcp",
		Addrs:       server.Address().String(),
		Auth:        &auth.PlainText{"id", "pass1234"},
		Session:     "1",
		Instance:    "1",
		PointerRecv: true,
	}

	// Use the client API
	result := calc.Add(2, 40)
	err = calc.Error()
	if err != nil {
		log.Error(err)
	}
	fmt.Println(result)

	res := calc.BigAdd(2, 40)
	err = calc.Error()
	if err != nil {
		log.Error(err)
		return
	}
	fmt.Println(res.Get())

	err = calc.Destroy()
	if err != nil {
		log.Error(err)
	}
	//Output:
	//42
	//42
}