Example #1
1
func Client() {
	var err error
	var c net.Conn
	c, err = net.DialTimeout("tcp", "127.0.0.1:1234", 1000*1000*1000*30)
	if err != nil {
		log.Fatal("dialing:", err)
	}

	var client *rpc.Client
	client = jsonrpc.NewClient(c)

	// 同步
	var args *Args = &Args{7, 8}
	var reply *Data = new(Data)
	client.Call("Arith.Plus", args, reply)
	fmt.Println(reply)

	// 异步
	args.A = 1
	args.B = 2
	var call *rpc.Call
	call = client.Go("Arith.Plus", args, reply, nil)
	var doneCall *rpc.Call
	doneCall = <-call.Done
	fmt.Println(doneCall.Args, doneCall.Reply)
	fmt.Println(args, reply)

	client.Close()
	c.Close()
}
Example #2
1
func testEchoClientAsync(t *testing.T, client *rpc.Client) {
	// EchoService.Echo
	args := &message.EchoRequest{Msg: "Hello, Protobuf-RPC"}
	reply := &message.EchoResponse{}
	echoCall := client.Go("EchoService.Echo", args, reply, nil)

	// EchoService.Echo reply
	echoCall = <-echoCall.Done
	if echoCall.Error != nil {
		t.Fatalf(`EchoService.Echo: %v`, echoCall.Error)
	}
	if echoCall.Reply.(*message.EchoResponse).GetMsg() != args.GetMsg() {
		t.Fatalf(`EchoService.Echo: expected = "%s", got = "%s"`,
			args.GetMsg(),
			echoCall.Reply.(*message.EchoResponse).GetMsg(),
		)
	}
}
func (pn *paxosNode) accept(client *rpc.Client, args *paxosrpc.AcceptArgs, reply *paxosrpc.AcceptReply, reqChan chan voteRequest) {
	finChan := make(chan *rpc.Call, pn.numNodes)
	client.Go("PaxosNode.RecvAccept", args, reply, finChan)
	call := <-finChan
	if call.Reply.(*paxosrpc.AcceptReply).Status == paxosrpc.OK {
		reqChan <- voteRequest{vote: true}
	} else {
		reqChan <- voteRequest{vote: false}
	}
}
Example #4
0
func ApiQuery(w http.ResponseWriter, r *http.Request) {

	defer func() {
		//r.Body.Close()
	}()

	//_, err := ioutil.ReadAll(r.Body)
	//if err != nil {
	//    return
	//}

	var err error

	status := make(chan int, 2)

	go func() {

		var sock *rpc.Client
		if sock, err = rpc.DialHTTP("tcp", "localhost:9601"); err != nil {
			return
		}
		defer sock.Close()

		//r  := new(CommandReply)
		var rsp CommandReply
		rs := sock.Go("Command.Query", "nil", &rsp, nil)

		select {
		case <-rs.Done:
			status <- 1
		case <-time.After(3e9):
			status <- 9
		}

		//runtime.Goexit()
		return
	}()

	for {
		select {
		case <-status:
			goto L
		case <-time.After(3e9):
			goto L
		}
	}

L:
	//io.WriteString(w, "{\"status\": \"OK\"}")
	close(status)

	return
}
func (pn *paxosNode) prepare(client *rpc.Client, args *paxosrpc.PrepareArgs, reply *paxosrpc.PrepareReply, reqChan chan voteRequest) {
	finChan := make(chan *rpc.Call, pn.numNodes)
	client.Go("PaxosNode.RecvPrepare", args, reply, finChan)
	call := <-finChan

	pReply := call.Reply.(*paxosrpc.PrepareReply)
	if pReply.Status == paxosrpc.OK {
		reqChan <- voteRequest{true, pReply.N_a, pReply.V_a}
	} else {
		reqChan <- voteRequest{false, pReply.N_a, pReply.V_a}
	}
}
Example #6
0
func GetWin32Service(client *rpc.Client) {
	var reply []Win32_Service
	args := "test"

	call := client.Go("Win32_Service.GetWin32Service", args, &reply, nil)

	replyCall := <-call.Done
	if replyCall.Error != nil {
		log.Fatal("GetWin32Service.replyCall.Error:", replyCall.Error)
	}
	for i, v := range reply {
		fmt.Println(i, v.Name, v.State)
	}
}
Example #7
0
func GetWin32Process(client *rpc.Client) {
	var reply []Win32_Process
	args := "test"

	call := client.Go("Win32_Process.GetWin32Process", args, &reply, nil)

	replyCall := <-call.Done
	if replyCall.Error != nil {
		log.Fatal("GetWin32Process.replyCall.Error:", replyCall.Error)
	}
	for i, v := range reply {
		fmt.Println(i, v.Name)
	}
}
Example #8
0
func rpc_call(client *rpc.Client, method string, args interface{},
	reply interface{}, timeout time.Duration) error {
	done := make(chan *rpc.Call, 1)
	client.Go(method, args, reply, done)
	select {
	case <-time.After(timeout):
		return errors.New("i/o timeout[rpc]")
	case call := <-done:
		if call.Error == nil {
			return nil
		} else {
			return call.Error
		}
	}
}
Example #9
0
// MakeAsyncCall calls a function at a remote peer 'callee' asynchronously. The three last arguments
// are identical to that of net/rpc's '(client *Client) Go' function.
func (rpcServ *RPCService) MakeAsyncCall(callee *Peer, call string, args interface{}, result interface{}) *rpc.Call {
	if callee == nil {
		return nil
	}
	// Check if there is already a connection
	var client *rpc.Client

	rpcServ.RLock()
	client = rpcServ.clientMap[callee.Address]
	rpcServ.RUnlock()

	// Open if not
	var err error
	if client == nil {
		client, err = rpcServ.rpcConnect(callee)
		if err != nil {
			fmt.Println("RPC Connect failed!")
			return nil
		}
	}
	asyncCall := client.Go("Node."+call, args, result, nil)

	return asyncCall
}
Example #10
0
func testArithClientAsync(t *testing.T, client *rpc.Client) {
	done := make(chan *rpc.Call, 16)
	callInfoList := []struct {
		method string
		args   *msg.ArithRequest
		reply  *msg.ArithResponse
		err    error
	}{
		{
			"ArithService.Add",
			&msg.ArithRequest{A: proto.Int32(1), B: proto.Int32(2)},
			&msg.ArithResponse{C: proto.Int32(3)},
			nil,
		},
		{
			"ArithService.Mul",
			&msg.ArithRequest{A: proto.Int32(2), B: proto.Int32(3)},
			&msg.ArithResponse{C: proto.Int32(6)},
			nil,
		},
		{
			"ArithService.Div",
			&msg.ArithRequest{A: proto.Int32(13), B: proto.Int32(5)},
			&msg.ArithResponse{C: proto.Int32(2)},
			nil,
		},
		{
			"ArithService.Div",
			&msg.ArithRequest{A: proto.Int32(1), B: proto.Int32(0)},
			&msg.ArithResponse{},
			errors.New("divide by zero"),
		},
		{
			"ArithService.Error",
			&msg.ArithRequest{A: proto.Int32(1), B: proto.Int32(2)},
			&msg.ArithResponse{},
			errors.New("ArithError"),
		},
	}

	// GoCall list
	calls := make([]*rpc.Call, len(callInfoList))
	for i := 0; i < len(calls); i++ {
		calls[i] = client.Go(callInfoList[i].method,
			callInfoList[i].args, callInfoList[i].reply,
			done,
		)
	}
	for i := 0; i < len(calls); i++ {
		<-calls[i].Done
	}

	// check result
	for i := 0; i < len(calls); i++ {
		if callInfoList[i].err != nil {
			if calls[i].Error.Error() != callInfoList[i].err.Error() {
				t.Fatalf(`%s: expected %v, Got = %v`,
					callInfoList[i].method,
					callInfoList[i].err,
					calls[i].Error,
				)
			}
			continue
		}

		got := calls[i].Reply.(*msg.ArithResponse).GetC()
		expected := callInfoList[i].reply.GetC()
		if got != expected {
			t.Fatalf(`%v: expected %v, Got = %v`,
				callInfoList[i].method, got, expected,
			)
		}
	}
}
func (pn *paxosNode) commit(client *rpc.Client, args *paxosrpc.CommitArgs, reply *paxosrpc.CommitReply, reqChan chan voteRequest) {
	finChan := make(chan *rpc.Call, pn.numNodes)
	go client.Go("PaxosNode.RecvCommit", args, reply, finChan)
	<-finChan
	reqChan <- voteRequest{vote: true}
}