mirror of
https://github.com/luxfi/mpc.git
synced 2026-09-24 00:26:24 +00:00
Implement direct messaging
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package messaging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
type DirectMessaging interface {
|
||||
Listen(target string, handler func(data []byte)) error
|
||||
Send(target string, data []byte) error
|
||||
}
|
||||
|
||||
type natsDirectMessaging struct {
|
||||
natsConn *nats.Conn
|
||||
ch chan []byte
|
||||
}
|
||||
|
||||
func newNatsDirectMessaging(natsConn *nats.Conn) DirectMessaging {
|
||||
return &natsDirectMessaging{
|
||||
natsConn: natsConn,
|
||||
ch: make(chan []byte),
|
||||
}
|
||||
}
|
||||
|
||||
func (d *natsDirectMessaging) Send(id string, message []byte) error {
|
||||
msg, err := d.natsConn.Request(id, message, time.Second)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("msg = %+v\n", msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *natsDirectMessaging) Listen(id string, handler func(data []byte)) error {
|
||||
_, err := d.natsConn.Subscribe(id, func(m *nats.Msg) {
|
||||
d.ch <- m.Data
|
||||
handler(m.Data)
|
||||
m.Respond([]byte("OK"))
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package infra
|
||||
package messaging
|
||||
|
||||
import (
|
||||
"github.com/nats-io/nats.go"
|
||||
@@ -0,0 +1,27 @@
|
||||
package node
|
||||
|
||||
import "github.com/cryptoniumX/mpcium/pkg/messaging"
|
||||
|
||||
type ID string
|
||||
|
||||
type Party struct {
|
||||
id string
|
||||
peerIDs []string
|
||||
|
||||
pubSub messaging.PubSub
|
||||
direct messaging.DirectMessaging
|
||||
}
|
||||
|
||||
func NewParty(
|
||||
id string,
|
||||
peerIDs []string,
|
||||
pubSub messaging.PubSub,
|
||||
direct messaging.DirectMessaging,
|
||||
) *Party {
|
||||
return &Party{
|
||||
id: id,
|
||||
peerIDs: peerIDs,
|
||||
pubSub: pubSub,
|
||||
direct: direct,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user