本文整理汇总了Golang中net/rpc.Client类的典型用法代码示例。如果您正苦于以下问题:Golang Client类的具体用法?Golang Client怎么用?Golang Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: iAppendToList
/**@brief append newitem to list
* @param key
* @param newitem
* @return error
*/
func (ls *Libstore) iAppendToList(key, newitem string) error {
var cli *rpc.Client
var args storageproto.PutArgs = storageproto.PutArgs{key, newitem}
var reply storageproto.PutReply
var err error
cli, err = ls.GetServer(key)
if lsplog.CheckReport(1, err) {
return err
}
//lsplog.Vlogf(0, "AppendToList args %v\n", args)
err = cli.Call("StorageRPC.AppendToList", &args, &reply)
if lsplog.CheckReport(1, err) {
return err
}
//lsplog.Vlogf(0, "AppendToList reply %v\n", reply)
if reply.Status != storageproto.OK {
return MakeErr("AppendToList()", reply.Status)
}
return nil
}
示例2: NewXZRPCClient
// NewRPCClient takes a net/rpc Client that should point to an instance...
func NewXZRPCClient(c *rpc.Client) func(method string) endpoint.Endpoint {
return func(method string) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
var (
errs = make(chan error, 1)
responses = make(chan interface{}, 1)
)
go func() {
var response XZResponse
if err := c.Call(method, request, &response); err != nil {
errs <- err
return
}
responses <- response
}()
select {
case <-ctx.Done():
return nil, context.DeadlineExceeded
case err := <-errs:
return nil, err
case response := <-responses:
return response, nil
}
}
}
}
示例3: NewStorageServer
// NewStorageServer creates and starts a new StorageServer. masterServerHostPort
// is the master storage server's host:port address. If empty, then this server
// is the master; otherwise, this server is a slave. numNodes is the total number of
// servers in the ring. port is the port number that this server should listen on.
// nodeID is a random, unsigned 32-bit ID identifying this server.
//
// This function should return only once all storage servers have joined the ring,
// and should return a non-nil error if the storage server could not be started.
func NewStorageServer(masterServerHostPort string, numNodes, port int, nodeID uint32) (StorageServer, error) {
// Set upt this server's info
serverInfo := storagerpc.Node{HostPort: fmt.Sprintf("localhost:%d", port), NodeID: nodeID}
var ss storageServer
if masterServerHostPort == "" {
// If this is the master server, set up a list of servers
var servers = make([]storagerpc.Node, numNodes)
servers[0] = serverInfo
// Create the master server
ss = storageServer{topMap: make(map[string]interface{}), nodeID: nodeID,
servers: servers, count: 1, countLock: sync.Mutex{}, keyLocks: make(map[string]chan int)}
} else {
// Try to connect to the master at most five times
args := storagerpc.RegisterArgs{ServerInfo: serverInfo}
var reply storagerpc.RegisterReply
var err error
var master *rpc.Client
for try := 1; try <= 5; try++ {
master, err = rpc.DialHTTP("tcp", masterServerHostPort)
if err == nil {
break
}
if try == 5 {
return nil, err
}
time.Sleep(time.Millisecond * 20)
}
for i := 1; i <= 5; i++ {
master.Call("StorageServer.RegisterServer", args, &reply)
if reply.Status == storagerpc.OK {
// All servers are connected, create this slave server
ss = storageServer{topMap: make(map[string]interface{}), nodeID: nodeID,
servers: reply.Servers, count: numNodes, countLock: sync.Mutex{}, keyLocks: make(map[string]chan int)}
break
}
// Wait one second, try to connect to master again
if i == 5 {
return nil, errors.New("couldn't connect to master")
}
time.Sleep(time.Millisecond * 20)
}
}
// Start listening for connections from other storageServers and libstores
rpc.RegisterName("StorageServer", &ss)
rpc.HandleHTTP()
l, e := net.Listen("tcp", serverInfo.HostPort)
if e != nil {
return nil, errors.New("Storage server couldn't start listening")
}
go http.Serve(l, nil)
return &ss, nil
}
示例4: MakeRemoteCall
// MakeRemoteCall calls a function at a remote peer 'callee' synchronously. The usage of the three
// last arguments is identical to that of net/rpc's '(client *Client) Call' function.
func (rpcServ *RPCService) MakeRemoteCall(callee *Peer, call string, args interface{},
result interface{}) error {
if callee == nil {
return nil
}
// Check if there is already a connection
var client *rpc.Client
var err error
rpcServ.RLock()
client = rpcServ.clientMap[callee.Address]
rpcServ.RUnlock()
// Open if not
if client == nil {
client, err = rpcServ.rpcConnect(callee)
if err != nil {
fmt.Println("RPC Connect failed!")
return err
}
}
err = client.Call("Node."+call, args, result)
if err != nil {
log.Print("RPC call failed, client "+callee.Address+" down? ", err)
if err == rpc.ErrShutdown || reflect.TypeOf(err) == reflect.TypeOf((*rpc.ServerError)(nil)).Elem() {
rpcServ.rpcClose(callee)
}
}
return err
}
示例5: SendPeerMessage
func (srv *ServerNode) SendPeerMessage(peerName string, api string, args interface{}, reply interface{}) error {
srv.lock.RLock()
// Find the peer
var peer *ServerPeer
peer = srv.peers[peerName]
srv.lock.RUnlock()
if peer == nil {
return ERR_PEER_NOT_FOUND
}
// Attempt to get a connection from the pool
//srv_log("Looking for connection in pool for peer %v\n", peerName)
var client *rpc.Client
var ok bool
client, ok = <-peer.connections
if !ok {
srv_log("Peer %v connection in shutdown - unable to send to peer\n", peerName)
return ERR_PEER_NOT_FOUND
}
//srv_log("Found connection - sending api call %v\n", api)
err := client.Call(api, args, reply)
if err != nil {
srv_log("Error in outbound call to %v - closing client and asking for a new connection: %v\n", peerName, err)
client.Close()
peer.broken_connections <- struct{}{}
} else {
//srv_log("Call worked - returning connection to the pool\n")
// It worked - restore the connection to the pool
peer.connections <- client
}
return err
}
示例6: RPCAddDoc
func (ps *server) RPCAddDoc(client *rpc.Client, docId, myHostPort string) error {
// Pear Server -> Pear Central: Requesting Add Fresh New Document
tries := maxTries
for tries > 0 {
// Make RPC Call to Master
args := ¢ralrpc.AddDocArgs{
DocId: docId,
HostPort: myHostPort,
}
var reply centralrpc.AddDocReply
if err := client.Call("PearCentral.AddDoc", args, &reply); err != nil {
return err
}
// common.LOGV.Println("$Call AddDoc:",reply)
// Check reply from Master
if reply.Status == centralrpc.OK {
_, ok := ps.docToServerMap[docId]
if !ok {
ps.docToServerMap[docId] = make(map[string]bool)
}
ps.docToServerMap[docId] = reply.Teammates
return nil
} else if reply.Status == centralrpc.DocExist {
common.LOGE.Println("Doc ", docId, " already Exist")
return nil
}
time.Sleep(time.Second)
tries--
}
return errors.New("RPCAddDoc Failed.")
}
示例7: statusRpcCall
func statusRpcCall(client *rpc.Client, args interface{}) {
var reply []*report.App
err := client.Call("Rpc.Status", args, &reply)
if err != nil {
log.Fatal("error:", err)
}
tabWriter := tabwriter.NewWriter(os.Stdout, 2, 2, 1, ' ', 0)
for _, appReport := range reply {
fmt.Fprintf(tabWriter, "[%s/%s:%d]\n", appReport.Name, appReport.Host, appReport.Port)
for _, instanceReport := range appReport.Instances {
if instanceReport.Active {
fmt.Fprint(tabWriter, "*\t")
} else {
fmt.Fprint(tabWriter, "\t")
}
fmt.Fprintf(tabWriter, "%d/%s:%d\t", instanceReport.Id, instanceReport.Host, instanceReport.Port)
fmt.Fprintf(tabWriter, "%s\t", instanceReport.Status)
fmt.Fprintf(tabWriter, "%s\t", time.Duration(instanceReport.SinceStatusChange)*time.Second)
fmt.Fprintf(tabWriter, "%s\n", instanceReport.Error)
}
}
tabWriter.Flush()
}
示例8: testEchoService
func testEchoService(t *testing.T, client *rpc.Client) {
var args EchoRequest
var reply EchoResponse
var err error
// EchoService.EchoTwice
args.Msg = proto.String(echoRequest)
err = client.Call("EchoService.EchoTwice", &args, &reply)
if err != nil {
t.Fatalf(`EchoService.EchoTwice: %v`, err)
}
if reply.GetMsg() != echoResponse {
t.Fatalf(
`EchoService.EchoTwice: expected = "%s", got = "%s"`,
echoResponse, reply.GetMsg(),
)
}
// EchoService.EchoTwice (Massive)
args.Msg = proto.String(echoMassiveRequest)
err = client.Call("EchoService.EchoTwice", &args, &reply)
if err != nil {
t.Fatalf(`EchoService.EchoTwice: %v`, err)
}
if reply.GetMsg() != echoMassiveResponse {
got := reply.GetMsg()
if len(got) > 8 {
got = got[:8] + "..."
}
t.Fatalf(`EchoService.EchoTwice: len = %d, got = %v`,
len(reply.GetMsg()), got,
)
}
}
示例9: VerifyToken
//VerifyToken authenticates the zistcl request to zistd
func VerifyToken(client *rpc.Client, token string) (bool, error) {
var valid bool
if err := client.Call("Communicator.VerifyToken", token, &valid); err != nil {
return false, err
}
return valid, nil
}
示例10: run_argstring
func run_argstring(client *rpc.Client) {
var reply int
arg := &myrpc.Sarg{Na: "aaaa", Id: 10, Va: "bbbbbbb"}
fmt.Printf("mseed: key:%s value:%s\n", arg.Na, arg.Va)
arg.List = make([]int, 5)
for i := 0; i < len(arg.List); i++ {
arg.List[i] = i
}
fmt.Printf("len:%d\n", len(arg.List))
err := client.Call("MSeed.AddString", arg, &reply)
if err != nil {
log.Fatal("arith error:", err)
}
/*
var arg1 string;
err = client.Call("MSeed.GetString",arg.Na, &arg1)
if err != nil {
log.Fatal("arith error:", err)
}
fmt.Printf("mseed: key:%s value:%s\n",arg.Na,arg1)
*/
//sed := new(myrpc.Args)
var sed myrpc.Sarg
err = client.Call("MSeed.GetSeed", "aaaa", &sed)
if err != nil {
log.Fatal("arith error:", err)
}
fmt.Println(sed)
}
示例11: cmdExit
func cmdExit(c *rpc.Client) {
var req ExitRequest
var res ExitReply
if err := c.Call("Server.Exit", &req, &res); err != nil {
panic(err)
}
}
示例12: Request
func Request(c *rpc.Client, req *clientRequest, res string) (string, error) {
err := c.Call(req.Method, req.Params, &res)
if err != nil {
fmt.Printf("ERROR(call): %v\n", err)
}
return res, err
}
示例13: benchmarkClient
func benchmarkClient(client *rpc.Client, b *testing.B) {
// Synchronous calls
args := &Args{7, 8}
procs := runtime.GOMAXPROCS(-1)
N := int32(b.N)
var wg sync.WaitGroup
wg.Add(procs)
b.StartTimer()
for p := 0; p < procs; p++ {
go func() {
reply := new(Reply)
for atomic.AddInt32(&N, -1) >= 0 {
err := client.Call("Arith.Mul", args, reply)
if err != nil {
b.Fatalf("rpc error: Mul: expected no error but got string %q", err.Error())
}
if reply.C != args.A*args.B {
b.Fatalf("rpc error: Mul: expected %d got %d", reply.C, args.A*args.B)
}
}
wg.Done()
}()
}
wg.Wait()
b.StopTimer()
}
示例14: mGocodeCmdComplete
func mGocodeCmdComplete(c *rpc.Client, fn string, src []byte, pos int) (res M, e string) {
args := struct {
Arg0 []byte
Arg1 string
Arg2 int
}{src, fn, pos}
reply := struct {
Arg0 []candidate
Arg1 int
}{}
if err := c.Call("RPC.RPC_auto_complete", &args, &reply); err != nil {
e = "RPC error: " + err.Error()
}
completions := []M{}
for _, d := range reply.Arg0 {
completions = append(completions, M{
"class": d.Class.String(),
"type": d.Type,
"name": d.Name,
})
}
res = M{"completions": completions}
return
}
示例15: cmd_cursor_type_pkg
func cmd_cursor_type_pkg(c *rpc.Client) {
var args, reply int
var err error
args = 0
err = c.Call("RPC.RPC_setid", &args, &reply)
compl_id = reply
compl_win, err = acme.Open(compl_id, nil)
if err != nil {
compl_win, _ = acme.New()
args = compl_win.GetId()
err = c.Call("RPC.RPC_setid", &args, &reply)
}
//for acme
var src []byte
var searchpos int
var fname string
if afile, err = acmeCurrentFile(); err != nil {
fmt.Printf("%v", err)
}
fname, src, searchpos = afile.name, afile.body, afile.offset
//for acme
typ, pkg := client_cursor_type_pkg(c, src, fname, searchpos)
fmt.Printf("%s,,%s\n", typ, pkg)
}