Rearrange datasync methods (#116)

This commit is contained in:
Adam Babik 2019-11-11 20:51:51 +01:00 committed by GitHub
parent 775d17008e
commit 44e7fe793b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 14 deletions

View File

@ -13,7 +13,7 @@ import (
type DataSync struct {
*datasyncnode.Node
// DataSyncNodeTransport is the implemntation of the datasync transport interface
// DataSyncNodeTransport is the implementation of the datasync transport interface.
*DataSyncNodeTransport
logger *zap.Logger
sendingEnabled bool
@ -23,14 +23,6 @@ func New(node *datasyncnode.Node, transport *DataSyncNodeTransport, sendingEnabl
return &DataSync{Node: node, DataSyncNodeTransport: transport, sendingEnabled: sendingEnabled, logger: logger}
}
func (d *DataSync) Add(publicKey *ecdsa.PublicKey, datasyncMessage datasyncproto.Payload) {
packet := datasynctransport.Packet{
Sender: datasyncpeer.PublicKeyToPeerID(*publicKey),
Payload: datasyncMessage,
}
d.DataSyncNodeTransport.AddPacket(packet)
}
func (d *DataSync) Handle(sender *ecdsa.PublicKey, payload []byte) [][]byte {
var payloads [][]byte
logger := d.logger.With(zap.String("site", "Handle"))
@ -48,18 +40,26 @@ func (d *DataSync) Handle(sender *ecdsa.PublicKey, payload []byte) [][]byte {
payloads = append(payloads, message.Body)
}
if d.sendingEnabled {
d.Add(sender, datasyncMessage)
d.add(sender, datasyncMessage)
}
}
return payloads
}
func (d *DataSync) Stop() {
d.Node.Stop()
}
func (d *DataSync) add(publicKey *ecdsa.PublicKey, datasyncMessage datasyncproto.Payload) {
packet := datasynctransport.Packet{
Sender: datasyncpeer.PublicKeyToPeerID(*publicKey),
Payload: datasyncMessage,
}
d.DataSyncNodeTransport.AddPacket(packet)
}
func unwrap(payload []byte) (datasyncPayload datasyncproto.Payload, err error) {
err = proto.Unmarshal(payload, &datasyncPayload)
return
}
func (d *DataSync) Stop() {
d.Node.Stop()
}