-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailbox.go
163 lines (149 loc) · 3.87 KB
/
mailbox.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
package chat
import (
"context"
"crypto/ecdsa"
"errors"
"github.com/cc14514/go-achat-node/ldb"
"github.com/cc14514/go-alibp2p"
"github.com/tendermint/go-amino"
"io"
"log"
"path"
"sort"
)
// TODO 映射绑定关系即 jid + mailboxid ,接受指令时要验证 jid
type mailbox struct {
myid JID
ctx context.Context
stop chan struct{}
db ldb.Database
p2pservice alibp2p.Libp2pService
}
func newMailbox(ctx context.Context, homedir string, myid JID, p2pservice alibp2p.Libp2pService) *mailbox {
db, err := ldb.NewLDBDatabase(path.Join(homedir, "mailbox"), 0, 0)
if err != nil {
panic(err)
}
return &mailbox{
ctx: ctx,
myid: myid,
stop: make(chan struct{}),
db: db,
p2pservice: p2pservice,
}
}
func (m *mailbox) verifyMsg(msg *Message) error {
// TODO 验证绑定关系
log.Println("TODO 验证绑定关系", msg)
return nil
}
func (m *mailbox) putMsg(msg *Message) error {
id := msg.Envelope.To.Peerid()
tab := ldb.NewTable(m.db, id)
return tab.Put([]byte(msg.Envelope.Id), msg.Bytes())
}
func (m *mailbox) doCleanMsg(cleanMsg *CleanMsg) {
id := cleanMsg.Jid.Peerid()
tab := ldb.NewTable(m.db, id)
for _, mid := range cleanMsg.Ids {
tab.Delete([]byte(mid))
}
}
func (m *mailbox) doQueryMsg(jid JID) *MessageBag {
id := jid.Peerid()
tab := ldb.NewTable(m.db, id)
it := tab.NewIterator()
sl := make([]*Message, 0)
for it.Next() {
if ldb.Kfilter([]byte(id), it.Key()) {
if m, err := new(Message).FromBytes(it.Value()); err == nil {
sl = append(sl, m.(*Message))
}
}
}
ml := MessageList(sl)
sort.Sort(ml)
return &MessageBag{Messages: ml}
}
func (m *mailbox) Stop() error {
close(m.stop)
return nil
}
func (m *mailbox) Start() error {
m.queryService()
m.msgService()
m.cleanService()
m.groupService()
return nil
}
func (m *mailbox) CleanMsg(jid JID, ids []string) error {
data, err := amino.MarshalBinaryLengthPrefixed(&CleanMsg{jid, ids})
if err != nil {
return err
}
_, err = m.p2pservice.RequestWithTimeout(jid.Mailid(), PID_MAILBOX_CLEAN, data, timeout)
return err
}
func (m *mailbox) cleanService() {
m.p2pservice.SetHandler(PID_MAILBOX_CLEAN, func(sessionId string, pubkey *ecdsa.PublicKey, rw io.ReadWriter) error {
cleanMsg := new(CleanMsg)
_, err := amino.UnmarshalBinaryLengthPrefixedReader(rw, cleanMsg, 2*1024*1024)
if err != nil {
log.Println("PID_MAILBOX_CLEAN error", "err", err)
return err
}
m.doCleanMsg(cleanMsg)
return err
})
}
func (m *mailbox) QueryMsg(jid JID) (*MessageBag, error) {
data, err := amino.MarshalBinaryLengthPrefixed(jid)
if err != nil {
return nil, err
}
rtn, err := m.p2pservice.RequestWithTimeout(jid.Mailid(), PID_MAILBOX_QUERY, data, timeout)
if err != nil {
return nil, err
}
var msgs = new(MessageBag)
err = amino.UnmarshalBinaryLengthPrefixed(rtn, msgs)
return msgs, err
}
func (m *mailbox) queryService() {
m.p2pservice.SetHandler(PID_MAILBOX_QUERY, func(sessionId string, pubkey *ecdsa.PublicKey, rw io.ReadWriter) error {
var k JID
_, err := amino.UnmarshalBinaryLengthPrefixedReader(rw, &k, 128)
if err != nil {
log.Println("PID_MAILBOX_QUERY error", "err", err)
rw.Write(new(MessageBag).Bytes())
return err
}
_, err = rw.Write(m.doQueryMsg(k).Bytes())
return err
})
}
func (m *mailbox) msgService() {
m.p2pservice.SetHandler(PID_MAILBOX, func(sessionId string, pubkey *ecdsa.PublicKey, rw io.ReadWriter) error {
msg, err := new(Message).FromReader(rw)
if err != nil {
rw.Write([]byte(err.Error()))
return err
}
message := msg.(*Message)
if err := m.verifyMsg(message); err != nil {
rw.Write([]byte(err.Error()))
return err
}
switch message.Envelope.Type {
case NormalMsg, GroupMsg:
if err := m.putMsg(msg.(*Message)); err != nil {
rw.Write([]byte(err.Error()))
return err
}
default:
return errors.New("not support yet")
}
rw.Write(SUCCESS)
return nil
})
}