|
|
|
@ -53,36 +53,15 @@ func (node *Node) StreamHandler(s p2p.Stream) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ReceiveGroupMessage use libp2p pubsub mechanism to receive broadcast messages
|
|
|
|
|
func (node *Node) ReceiveGroupMessage() { |
|
|
|
|
func (node *Node) ReceiveGroupMessage(receiver p2p.GroupReceiver) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
for { |
|
|
|
|
if node.groupReceiver == nil { |
|
|
|
|
if receiver == nil { |
|
|
|
|
time.Sleep(100 * time.Millisecond) |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
msg, sender, err := node.groupReceiver.Receive(ctx) |
|
|
|
|
msg, sender, err := receiver.Receive(ctx) |
|
|
|
|
if sender != node.host.GetID() { |
|
|
|
|
// utils.GetLogInstance().Info("[PUBSUB]", "received group msg", len(msg), "sender", sender)
|
|
|
|
|
if err == nil { |
|
|
|
|
// skip the first 5 bytes, 1 byte is p2p type, 4 bytes are message size
|
|
|
|
|
node.messageHandler(msg[5:], string(sender)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ReceiveClientGroupMessage use libp2p pubsub mechanism to receive broadcast messages for client
|
|
|
|
|
func (node *Node) ReceiveClientGroupMessage() { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
for { |
|
|
|
|
if node.clientReceiver == nil { |
|
|
|
|
// check less frequent on client messages
|
|
|
|
|
time.Sleep(1000 * time.Millisecond) |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
msg, sender, err := node.clientReceiver.Receive(ctx) |
|
|
|
|
if sender != node.host.GetID() { |
|
|
|
|
utils.GetLogInstance().Info("[CLIENT]", "received group msg", len(msg), "sender", sender) |
|
|
|
|
if err == nil { |
|
|
|
|
// skip the first 5 bytes, 1 byte is p2p type, 4 bytes are message size
|
|
|
|
|
node.messageHandler(msg[5:], string(sender)) |
|
|
|
|