46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package pubsub
|
|
|
|
import (
|
|
"context"
|
|
"oc-discovery/daemons/node/common"
|
|
"oc-discovery/daemons/node/stream"
|
|
"strings"
|
|
"sync"
|
|
|
|
"cloud.o-forge.io/core/oc-lib/tools"
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
)
|
|
|
|
type PubSubService struct {
|
|
Node common.DiscoveryPeer
|
|
Host host.Host
|
|
PS *pubsub.PubSub
|
|
StreamService *stream.StreamService
|
|
Subscription []string
|
|
mutex sync.RWMutex
|
|
}
|
|
|
|
func InitPubSub(ctx context.Context, h host.Host, ps *pubsub.PubSub, node common.DiscoveryPeer, streamService *stream.StreamService) (*PubSubService, error) {
|
|
service := &PubSubService{
|
|
Node: node,
|
|
Host: h,
|
|
StreamService: streamService,
|
|
PS: ps,
|
|
}
|
|
|
|
service.initSubscribeEvents(ctx)
|
|
return service, nil
|
|
}
|
|
|
|
func (ps *PubSubService) getTopicName(topicName string) tools.PubSubAction {
|
|
ns := strings.Split(topicName, ".")
|
|
if len(ns) > 0 {
|
|
return tools.GetActionString(ns[0])
|
|
}
|
|
return tools.NONE
|
|
}
|
|
|
|
func (ix *PubSubService) Close() {
|
|
}
|