-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhasq_client.go
More file actions
81 lines (72 loc) · 1.4 KB
/
hasq_client.go
File metadata and controls
81 lines (72 loc) · 1.4 KB
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
package hashq_mod
import (
"container/list"
"os"
"strings"
)
type Client struct {
Tokens map[string]*Token
}
func NewClient() Client {
return Client{Tokens: make(map[string]*Token)}
}
func (c *Client) LoadTokens() bool {
files, err := os.ReadDir(".")
if err != nil {
return false
}
for _, f := range files {
name := f.Name()
index := strings.Index(name, ".tok")
if index <= -1 {
continue
}
w := name[0:index]
token := LoadToken(w)
if token == nil {
continue
}
c.Tokens[w] = token
}
return true
}
func (c *Client) StoreTokens() bool {
for _, v := range c.Tokens {
StoreToken(v)
}
return true
}
func (c *Client) NewToken(data string) string {
hash := Hash(data)
token := c.Tokens[hash]
if token != nil {
return token.Digest
}
newToken := NewToken(data)
c.Tokens[hash] = &newToken
return hash
}
func (c *Client) RegisterToken(hash string, key1 string, key2 string, gen string, owner string) {
c.Tokens[hash] = &Token{
List: list.New(),
Digest: hash,
Key1: key1,
Key2: key2,
LastGen: gen,
}
}
func (c *Client) AddHash(hash string) *CanonicalHash {
token := c.Tokens[hash]
if token == nil {
return nil
}
ch := token.Next()
return &ch
}
func (c *Client) RegisterHash(sequence int32, token string, key string, gen string, owner string) *CanonicalHash {
t := c.Tokens[token]
if t == nil {
return nil
}
return t.Add(sequence, key, gen, owner)
}