-
Notifications
You must be signed in to change notification settings - Fork 24
/
cache.go
58 lines (49 loc) · 1.23 KB
/
cache.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
// Copyright (c) 2016 Company 0, LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"encoding/hex"
"fmt"
"path"
"github.com/companyzero/zkc/rpc"
"github.com/companyzero/zkc/session"
"github.com/companyzero/zkc/zkidentity"
)
// think about establishing whitelist or just blind deliver
func (z *ZKS) handleCache(writer chan *RPCWrapper, kx *session.KX,
msg rpc.Message, cache rpc.Cache) error {
// sanity
if msg.Command != rpc.TaggedCmdCache {
return fmt.Errorf("invalid cache command")
}
// deliver message
var (
from [zkidentity.IdentitySize]byte
ok bool
)
from, ok = kx.TheirIdentity().([32]byte)
if !ok {
return fmt.Errorf("invalid identity type")
}
filename, err := z.account.Deliver(cache.To, from, cache.Payload)
if err != nil {
return fmt.Errorf("delivery failed: %v", err)
}
// ack
writer <- &RPCWrapper{
Message: rpc.Message{
Command: rpc.TaggedCmdAcknowledge,
Tag: msg.Tag,
},
Payload: rpc.Empty{},
}
// dont eval if not in debug mode
if z.settings.Debug {
z.Dbg(idApp, "handleCache: %v -> %v: %v",
hex.EncodeToString(cache.To[:]),
hex.EncodeToString(from[:]),
path.Base(filename))
}
return nil
}