-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_sync_connection.go
145 lines (113 loc) · 2.89 KB
/
db_sync_connection.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
package gomahawk
import (
"bytes"
"errors"
"log"
msg "github.com/MStoykov/gomahawk/msg"
)
func (d *dBConn) Trigger() error {
return nil
}
// request changes after given id. "" means all
func (d *dBConn) sendFetchOps(id string) error {
return d.WriteMsg(msg.NewFetchOpsMsg(id))
}
type dBConn struct {
*secondaryConnection
offer *msg.DBsyncOffer
dbconn DBConnection
fom msg.FetchOpsMethod
commandProcessor *msg.CommandParser
}
func newDBConn(conn *secondaryConnection) (*dBConn, error) {
d := new(dBConn)
d.secondaryConnection = conn
d.commandProcessor = msg.NewCommandParser()
d.msgHandler = d.handleMsg
return d, nil
}
func openNewDBConn(offer *msg.DBsyncOffer, conn *connection, controlid string) (*dBConn, error) {
log.Println("OpenNewDBConn")
d := new(dBConn)
d.offer = offer
d.connection = conn
d.commandProcessor = msg.NewCommandParser()
message := msg.NewSecondaryOffer(controlid, offer.Key, 50210)
if err := d.WriteMsg(message); err != nil {
log.Println("error while sending offer on dbconnection")
return nil, err
}
d.msgHandler = d.handleMsg
return d, nil
}
func (d *dBConn) FetchOps(fom msg.FetchOpsMethod, id string) error {
d.fom = fom
return d.sendFetchOps(id)
}
func (d *dBConn) handleMsg(message *msg.Msg) error {
if message.IsDBOP() {
if d.fom == nil {
return errors.New("Got DBOP but no FetchOpsMethod")
}
if !message.IsJSON() { // 'ok' ?
if bytes.Equal(message.Payload(), []byte("ok")) {
return d.fom.Close()
} else {
return errors.New("Got DBOP that's JSON but not 'ok' :\n" + message.String())
}
}
command, err := d.commandProcessor.ParseCommand(message)
if err != nil {
if nerr, ok := err.(msg.NotRegisteredError); ok {
log.Println(nerr)
return nil
}
return err
}
err = d.fom.SendCommand(command)
if !message.IsFragment() {
return d.fom.Close()
}
return err
}
op, err := msg.GetOpFromFetchOpsMsg(message)
if err == nil {
if d.dbconn != nil {
return d.dbconn.FetchOps(newDummyFetchOps(d.connection), op)
} else {
return d.WriteMsg(msg.NewMsg([]byte("ok"), msg.DBOP))
}
}
if msg.IsTrigger(message) {
if d.dbconn != nil {
return d.dbconn.Trigger()
}
return nil
}
log.Println("unhandled message received on a DBConnection\n", message)
return nil
}
type DummyFetchOps struct {
lastCommand msg.Command
conn *connection
}
func newDummyFetchOps(conn *connection) msg.FetchOpsMethod {
return &DummyFetchOps{
conn: conn,
}
}
func (d *DummyFetchOps) SendCommand(command msg.Command) (err error) {
if d.lastCommand != nil {
err = d.conn.WriteMsg(msg.WrapCommand(d.lastCommand, false))
}
d.lastCommand = command
return
}
func (d *DummyFetchOps) Close() (err error) {
if d.lastCommand != nil {
err = d.conn.WriteMsg(msg.WrapCommand(d.lastCommand, false))
} else {
err = d.conn.WriteMsg(msg.NewMsg([]byte("ok"), msg.SETUP))
}
return
}