-
Notifications
You must be signed in to change notification settings - Fork 0
/
crcls.go
209 lines (178 loc) · 4.04 KB
/
crcls.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"context"
"crcls-converse/account"
"crcls-converse/channel"
"crcls-converse/config"
"crcls-converse/datastore"
"crcls-converse/inout"
"crcls-converse/logger"
"crcls-converse/network"
"encoding/json"
"fmt"
"strconv"
"time"
)
var crcls *CRCLS
type CRCLS struct {
Account *account.Account
ChMgr *channel.ChannelManager
Config *config.Config
DS *datastore.Datastore
Net *network.Network
IO *inout.IO
}
func LoadCRCLS(ctx context.Context, conf *config.Config, io *inout.IO) error {
a, err := account.Load(conf)
if err != nil {
return err
}
crcls = &CRCLS{
Account: a,
Config: conf,
IO: io,
}
if err := crcls.initServices(ctx); err != nil {
return err
}
return nil
}
func CreateCRCLS(ctx context.Context, conf *config.Config, io *inout.IO) error {
a, err := account.New(conf)
if err != nil {
return err
}
bal, err := a.Wallet.Balance()
if err != nil {
return err
}
msg := inout.NewAccount{
Type: "account-create",
Address: a.Wallet.Address,
SeedPhrase: a.Wallet.SeedPhrase,
Balance: bal,
}
data, err := json.Marshal(&msg)
if err != nil {
return err
}
io.Write(data)
crcls = &CRCLS{
Account: a,
Config: conf,
IO: io,
}
if err := crcls.initServices(ctx); err != nil {
return err
}
return nil
}
func (crcls *CRCLS) initServices(ctx context.Context) error {
netw, err := network.New(ctx, crcls.Config, crcls.Account.Wallet.PrivKey)
if err != nil {
return err
}
ds := datastore.NewDatastore(ctx, crcls.Config, netw.Host, netw.PubSub, netw.DHT)
ds.Authenticate(crcls.Account.Wallet.PrivKey)
chMgr := channel.NewManager(ctx, crcls.Account, netw, crcls.IO, ds)
crcls.Net = netw
crcls.DS = ds
crcls.ChMgr = chMgr
return nil
}
func (crcls *CRCLS) Run(ctx context.Context, cmd *inout.InputCommand) error {
ensureInit()
log := logger.GetLogger()
switch cmd.Type {
case inout.LIST:
subcmd, err := cmd.NextSubcommand()
if err != nil {
return err
}
switch subcmd {
case inout.CHANNELS:
data, err := json.Marshal(&inout.ListChannelsMessage{
Type: "list-channels",
Subject: subcmd,
Channels: crcls.ChMgr.ListChannels(),
})
if err != nil {
log.Fatal(err)
}
crcls.IO.Write(data)
case inout.PEERS:
peers, err := json.Marshal(&inout.ListPeersMessage{
Type: "list-peers",
Subject: subcmd,
Peers: crcls.Net.Peers,
})
if err != nil {
log.Fatal(err)
}
crcls.IO.Write(peers)
case inout.MESSAGES:
if crcls.ChMgr.Active == nil {
return fmt.Errorf("No active channel.")
} else {
sscmd, err := cmd.NextSubcommand()
if err != nil {
return err
} else {
days, err := strconv.ParseInt(string(sscmd), 10, 64)
if err != nil {
log.Fatal(err)
}
dur := time.Hour * time.Duration(24*days)
result, err := crcls.ChMgr.Active.GetRecentMessages(dur)
if err != nil {
return err
}
messages, err := json.Marshal(&inout.ListMessagesMessage{Type: "list-messages", Channel: crcls.ChMgr.Active.ID, Messages: result})
if err != nil {
log.Fatal(err)
}
crcls.IO.Write(messages)
}
}
}
case inout.JOIN:
chid, err := cmd.NextSubcommand()
if err != nil {
return err
} else {
crcls.ChMgr.Join(string(chid))
}
case inout.REPLY:
if crcls.ChMgr.Active == nil {
inout.EmitError(fmt.Errorf("No active channel."))
} else {
if err := crcls.ChMgr.Active.Publish(string(cmd.Data)); err != nil {
inout.EmitError(err)
}
}
case inout.MEMBER:
ensureInit()
subcmd, err := cmd.NextSubcommand()
if err != nil {
return err
}
switch subcmd {
case inout.CREATE:
member := &account.Member{}
if err := json.Unmarshal(cmd.Data, member); err != nil {
return err
}
if err := account.NewMember(ctx, crcls.Account, member, crcls.DS); err != nil {
inout.EmitError(err)
// continue
}
evt := inout.MemberChangeMessage{Type: "member-create"}
data, err := json.Marshal(evt)
if err != nil {
log.Fatal(err)
}
crcls.IO.Write(data)
}
}
return nil
}