Esempio n. 1
0
func (client *Client) invokeChanInternal(
	leverURL *core.LeverURL, args ...interface{}) (stream Stream, err error) {
	if !IsChanMethod(leverURL.Method) {
		return nil, fmt.Errorf(
			"Use Invoke / InvokeURL for non-streaming methods")
	}
	if leverURL.Environment == "" && OwnEnvironment == "" {
		return nil, fmt.Errorf(
			"Environment not specified and cannot be deduced")
	}
	if leverURL.Environment == "" {
		leverURL.Environment = OwnEnvironment
	}

	var dialTo string
	if client.ForceHost != "" {
		dialTo = client.ForceHost
	} else {
		if (core.IsInternalEnvironment(leverURL.Environment) ||
			leverURL.Environment == OwnEnvironment) &&
			internalRPCGateway != "" {
			dialTo = internalRPCGateway
		} else {
			dialTo = leverURL.Environment
		}
	}
	conn, err := client.conns.Dial(dialTo)
	if err != nil {
		return nil, err
	}

	rpc := &core.RPC{}
	if len(args) == 1 {
		byteArgs, ok := args[0].([]byte)
		if ok {
			// Byte args case.
			rpc.ArgsOneof = &core.RPC_ByteArgs{
				ByteArgs: byteArgs,
			}
		}
	}
	if rpc.ArgsOneof == nil {
		// Non-byte args case.
		var encdArgs *core.JSONArray
		encdArgs, err = encodeArgs(args)
		if err != nil {
			return nil, err
		}
		rpc.ArgsOneof = &core.RPC_Args{
			Args: encdArgs,
		}
	}
	grpcStream, err := core.SendStreamingLeverRPC(
		conn, context.Background(), leverURL)
	if err != nil {
		return nil, err
	}

	err = grpcStream.Send(&core.StreamMessage{
		MessageOneof: &core.StreamMessage_Rpc{
			Rpc: rpc,
		},
	})
	if err != nil {
		grpcStream.CloseSend()
		return nil, err
	}
	// First message received must be empty.
	fstMsg, err := grpcStream.Recv()
	if err != nil {
		return nil, err
	}
	if fstMsg.GetMessageOneof() != nil {
		return nil, fmt.Errorf("First server message needs to be empty")
	}
	return newClientStream(grpcStream), nil
}
Esempio n. 2
0
func (client *Client) invokeInternal(
	replyObj interface{}, leverURL *core.LeverURL,
	args ...interface{}) (err error) {
	if IsChanMethod(leverURL.Method) {
		return fmt.Errorf(
			"Use InvokeChan / InvokeChanURL for streaming methods")
	}
	if leverURL.Environment == "" && OwnEnvironment == "" {
		return fmt.Errorf(
			"Environment not specified and cannot be deduced")
	}
	if leverURL.Environment == "" {
		leverURL.Environment = OwnEnvironment
	}

	var dialTo string
	if client.ForceHost != "" {
		dialTo = client.ForceHost
	} else {
		if (core.IsInternalEnvironment(leverURL.Environment) ||
			leverURL.Environment == OwnEnvironment) &&
			internalRPCGateway != "" {
			dialTo = internalRPCGateway
		} else {
			dialTo = leverURL.Environment
		}
	}
	conn, err := client.conns.Dial(dialTo)
	if err != nil {
		return err
	}

	rpc := &core.RPC{}
	if len(args) == 1 {
		byteArgs, ok := args[0].([]byte)
		if ok {
			// Byte args case.
			rpc.ArgsOneof = &core.RPC_ByteArgs{
				ByteArgs: byteArgs,
			}
		}
	}
	if rpc.ArgsOneof == nil {
		// Non-byte args case.
		var encdArgs *core.JSONArray
		encdArgs, err = encodeArgs(args)
		if err != nil {
			return err
		}
		rpc.ArgsOneof = &core.RPC_Args{
			Args: encdArgs,
		}
	}
	reply, err := core.SendLeverRPC(
		conn, context.Background(), leverURL, rpc)
	if err != nil {
		return err
	}

	if reply.GetResultOneof() == nil {
		return fmt.Errorf("Received nil result oneof")
	}
	switch result := reply.GetResultOneof().(type) {
	case *core.RPCReply_ByteResult:
		byteReplyObj, ok := replyObj.(*[]byte)
		if !ok {
			return fmt.Errorf(
				"replyObj needs to be type *[]byte when RPC returns bytes")
		}
		*byteReplyObj = result.ByteResult
		return nil
	case *core.RPCReply_ByteError:
		return &RemoteByteError{Err: result.ByteError}
	case *core.RPCReply_Result:
		return decodeArgAsValue(result.Result, replyObj)
	case *core.RPCReply_Error:
		return &RemoteError{Err: buildDataForJSON(result.Error)}
	default:
		return fmt.Errorf("Invalid type")
	}
}