/
client.go
84 lines (69 loc) · 1.57 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
76
77
78
79
80
81
82
83
84
package pool
import (
"github.com/sasha-s/go-deadlock"
"github.com/HyperspaceApp/Hyperspace/persist"
"github.com/HyperspaceApp/Hyperspace/types"
)
//
// A ClientRecord represents the persistent data portion of the Client record
//
type ClientRecord struct {
clientID int64
name string
wallet types.UnlockHash
}
//
// A Client represents a user and may have one or more workers associated with it. It is primarily used for
// accounting and statistics.
//
type Client struct {
cr ClientRecord
mu deadlock.RWMutex
pool *Pool
log *persist.Logger
}
// newClient creates a new Client record
func newClient(p *Pool, name string) (*Client, error) {
c := &Client{
cr: ClientRecord{
name: name,
},
pool: p,
log: p.clientLog,
}
c.cr.wallet.LoadString(name)
if p.Client(name) != nil {
return p.Client(name), nil
}
return c, nil
}
// Name returns the client's name, which is usually the wallet address
func (c *Client) Name() string {
c.mu.RLock()
defer c.mu.RUnlock()
return c.cr.name
}
// SetName sets the client's name
func (c *Client) SetName(n string) {
c.mu.Lock()
defer c.mu.Unlock()
c.cr.name = n
}
// Wallet returns the unlockhash associated with the client
func (c *Client) Wallet() *types.UnlockHash {
c.mu.RLock()
defer c.mu.RUnlock()
return &c.cr.wallet
}
// SetWallet sets the unlockhash associated with the client
func (c *Client) SetWallet(w types.UnlockHash) {
c.mu.Lock()
defer c.mu.Unlock()
c.cr.wallet = w
}
// Pool returns the client's pool
func (c *Client) Pool() *Pool {
c.mu.RLock()
defer c.mu.RUnlock()
return c.pool
}