-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstrategy.go
94 lines (84 loc) · 2.29 KB
/
strategy.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
85
86
87
88
89
90
91
92
93
94
package handshake
import "encoding/json"
type strategy struct {
Rendezvous storage
Storage storage
Cipher cipher
}
// strategyPeerConfig is a a struct that encapsulates the shared strategy settings for handshake
type strategyPeerConfig struct {
Rendezvous peerStorage `json:"rendezvous"`
Storage peerStorage `json:"storage"`
Cipher peerCipher `json:"cipher"`
}
// strategyConfig is a struct that encapsulates internal chat strategy settings
type strategyConfig struct {
Rendezvous storageConfig
Storage storageConfig
Cipher cipherConfig
}
// Share returns the strategyPeerConfig for the strategy
func (s strategy) Share() (config strategyPeerConfig, err error) {
if config.Rendezvous, err = s.Rendezvous.share(); err != nil {
return
}
if config.Storage, err = s.Storage.share(); err != nil {
return
}
if config.Cipher, err = s.Cipher.share(); err != nil {
return
}
return
}
// Share returns the strategyPeerConfig for the strategy
func (s strategy) Export() (config strategyConfig, err error) {
if config.Rendezvous, err = s.Rendezvous.export(); err != nil {
return
}
if config.Storage, err = s.Storage.export(); err != nil {
return
}
if config.Cipher, err = s.Cipher.export(); err != nil {
return
}
return
}
func strategyFromPeerConfig(config strategyPeerConfig) (s strategy, err error) {
if s.Rendezvous, err = newStorageFromPeer(config.Rendezvous); err != nil {
return
}
if s.Storage, err = newStorageFromPeer(config.Storage); err != nil {
return
}
if s.Cipher, err = newCipherFromPeer(config.Cipher); err != nil {
return
}
return
}
func strategyFromConfig(config strategyConfig) (s strategy, err error) {
if s.Rendezvous, err = newStorageFromConfig(config.Rendezvous); err != nil {
return
}
if s.Storage, err = newStorageFromConfig(config.Storage); err != nil {
return
}
if s.Cipher, err = newCipherFromConfig(config.Cipher); err != nil {
return
}
return
}
// ConfigJSONBytes marshall's the strategyPeerConfig as a json file
func (s strategy) ShareJSONBytes() ([]byte, error) {
config, err := s.Share()
if err != nil {
return []byte{}, err
}
return json.Marshal(config)
}
func newDefaultStrategy() strategy {
return strategy{
Rendezvous: newDefaultRendezvous(),
Storage: newDefaultMessageStorage(),
Cipher: newDefaultCipher(),
}
}