-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
75 lines (65 loc) · 1.56 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package distkvs
import (
"errors"
"example.org/cpsc416/a6/kvslib"
"github.com/DistributedClocks/tracing"
)
const ChCapacity = 10
type ClientConfig struct {
ClientID string
FrontEndAddr string
TracerServerAddr string
TracerSecret []byte
}
type Client struct {
NotifyChannel kvslib.NotifyChannel
id string
frontEndAddr string
kvs *kvslib.KVS
tracer *tracing.Tracer
initialized bool
tracerConfig tracing.TracerConfig
}
func NewClient(config ClientConfig, kvs *kvslib.KVS) *Client {
tracerConfig := tracing.TracerConfig{
ServerAddress: config.TracerServerAddr,
TracerIdentity: config.ClientID,
Secret: config.TracerSecret,
}
return &Client{
id: config.ClientID,
frontEndAddr: config.FrontEndAddr,
kvs: kvs,
tracerConfig: tracerConfig,
initialized: false,
}
}
func (c *Client) Initialize() error {
if c.initialized {
return errors.New("client has been initialized")
}
c.tracer = tracing.NewTracer(c.tracerConfig)
ch, err := c.kvs.Initialize(c.tracer, c.id, c.frontEndAddr, ChCapacity)
if err != nil {
return err
}
c.NotifyChannel = ch
c.initialized = true
return nil
}
func (c *Client) Get(clientId string, key string) (uint32, error) {
return c.kvs.Get(c.tracer, clientId, key)
}
func (c *Client) Put(clientId string, key string, value string) (uint32, error) {
return c.kvs.Put(c.tracer, clientId, key, value)
}
func (c *Client) Close() error {
if err := c.kvs.Close(); err != nil {
return err
}
if err := c.tracer.Close(); err != nil {
return err
}
c.initialized = false
return nil
}