1// Copyright 2020 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package bind 16 17import ( 18 "errors" 19 "net/rpc" 20) 21 22//RemoteBinderClient sends client requests to RPC 23type RemoteBinderClient struct { 24 rpcClient *rpc.Client 25 SocketPath string 26} 27 28func NewRemoteBindClient(socketPath string) PathBinder { 29 var r RemoteBinderClient 30 r.SocketPath = socketPath 31 return &r 32} 33 34func (r *RemoteBinderClient) initRpcClient() error { 35 if r.rpcClient != nil { 36 return nil 37 } 38 var err error 39 r.rpcClient, err = rpc.DialHTTP("unix", r.SocketPath) 40 return err 41} 42 43func (r *RemoteBinderClient) BindReadOnly(source string, destination string) error { 44 args := BindReadOnlyArgs{source, destination} 45 var reply BindReadOnlyReply 46 if err := r.initRpcClient(); err != nil { 47 return err 48 } 49 if err := r.rpcClient.Call("Server.BindReadOnly", &args, &reply); err != nil { 50 return err 51 } 52 if reply.Err != "" { 53 return errors.New(reply.Err) 54 } 55 return nil 56} 57 58func (r *RemoteBinderClient) BindReadWrite(source string, destination string) error { 59 args := BindReadWriteArgs{source, destination} 60 var reply BindReadWriteReply 61 if err := r.initRpcClient(); err != nil { 62 return err 63 } 64 if err := r.rpcClient.Call("Server.BindReadWrite", &args, &reply); err != nil { 65 return err 66 } 67 if reply.Err != "" { 68 return errors.New(reply.Err) 69 } 70 return nil 71} 72 73func (r *RemoteBinderClient) Unbind(destination string) error { 74 args := UnbindArgs{destination} 75 var reply UnbindReply 76 if err := r.initRpcClient(); err != nil { 77 return err 78 } 79 if err := r.rpcClient.Call("Server.Unbind", &args, &reply); err != nil { 80 return err 81 } 82 if reply.Err != "" { 83 return errors.New(reply.Err) 84 } 85 return nil 86} 87 88func (r *RemoteBinderClient) List() ([]string, error) { 89 var args ListArgs 90 var reply ListReply 91 if err := r.initRpcClient(); err != nil { 92 return nil, err 93 } 94 if err := r.rpcClient.Call("Server.List", &args, &reply); err != nil { 95 return nil, err 96 } 97 98 if reply.Err != "" { 99 return nil, errors.New(reply.Err) 100 } 101 102 return reply.BindList, nil 103} 104