package rpc
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type Client struct {
|
|
tokenLock sync.Mutex
|
|
token string
|
|
|
|
codec Codec
|
|
transport ClientTransport
|
|
}
|
|
|
|
func NewClient(codec Codec, transport ClientTransport) *Client {
|
|
return &Client{
|
|
codec: codec,
|
|
transport: transport,
|
|
}
|
|
}
|
|
|
|
func (cl *Client) SetToken(t string) {
|
|
cl.tokenLock.Lock()
|
|
cl.token = t
|
|
cl.tokenLock.Unlock()
|
|
}
|
|
|
|
func (cl *Client) GetToken() string {
|
|
cl.tokenLock.Lock()
|
|
t := cl.token
|
|
cl.tokenLock.Unlock()
|
|
return t
|
|
}
|
|
|
|
func (cl *Client) Call(fn string, args, resp interface{}) error {
|
|
rawReq, err := EncodeReq(cl.codec, cl.token, fn, args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rawResp, err := cl.transport.Request(rawReq)
|
|
if err != nil {
|
|
return err // Not testable.
|
|
}
|
|
return DecodeResp(cl.codec, rawResp, resp)
|
|
}
|