Beispiel #1
0
// ValidateAddPeer is the validation function for AddPeer to invoke the rpc
// server call
func ValidateAddPeer(p *peer.PeerAddRequest) (*services.RPCPeerAddResp, error) {
	args := &services.RPCPeerAddReq{Name: new(string), Addresses: p.Addresses}
	*args.Name = p.Name

	rsp := new(services.RPCPeerAddResp)
	//TODO : port 9876 is hardcoded for now, can be made configurable
	remoteAddress := fmt.Sprintf("%s:%s", p.Name, "9876")
	rpcConn, e := net.Dial("tcp", remoteAddress)
	if e != nil {
		log.WithField("error", e).Error("net.Dial() call failed")
		opRet = -1
		opError = e.Error()
		rsp.OpRet = &opRet
		rsp.OpError = &opError
		return rsp, e
	}
	client := rpc.NewClientWithCodec(pbcodec.NewClientCodec(rpcConn))
	defer client.Close()

	e = client.Call("PeerService.ValidateAdd", args, rsp)
	if e != nil {
		log.Error("Failed to execute PeerService.Validate() rpc call")
		opRet = -1
		opError = e.Error()
		rsp.OpRet = &opRet
		rsp.OpError = &opError
		return rsp, e
	}
	return rsp, nil
}
Beispiel #2
0
func main() {
	args := &calc.CalcReq{Op: new(int32), A: new(int32), B: new(int32)}
	*args.Op = 100
	*args.A = 100
	*args.B = 3
	rsp := new(calc.CalcRsp)

	c, e := net.Dial("tcp", "localhost:9876")
	if e != nil {
		panic(e)
	}

	client := rpc.NewClientWithCodec(pbcodec.NewClientCodec(c))

	e = client.Call("Calculator.Calculate", args, rsp)
	if e == nil {
		log.Println("Result: ", *rsp.Ret)
	} else {
		log.Print("error calling Calculate ", e)
	}

	client.Close()
}