50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
|
|
package indexer
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"oc-discovery/daemons/node/common"
|
||
|
|
|
||
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
||
|
|
"github.com/libp2p/go-libp2p/core/host"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Index Record is the model for the specialized registry of node connected to Indexer
|
||
|
|
type IndexerService struct {
|
||
|
|
*common.LongLivedStreamRecordedService[PeerRecord]
|
||
|
|
PS *pubsub.PubSub
|
||
|
|
isStrictIndexer bool
|
||
|
|
}
|
||
|
|
|
||
|
|
// if a pubsub is given... indexer is also an active oc-node. If not... your a strict indexer
|
||
|
|
func NewIndexerService(h host.Host, ps *pubsub.PubSub, maxNode int) *IndexerService {
|
||
|
|
var err error
|
||
|
|
ix := &IndexerService{
|
||
|
|
LongLivedStreamRecordedService: common.NewStreamRecordedService[PeerRecord](h, maxNode, false),
|
||
|
|
isStrictIndexer: ps == nil,
|
||
|
|
}
|
||
|
|
if ps == nil { // generate your fresh gossip for the flow of killed node... EVERYBODY should know !
|
||
|
|
ps, err = pubsub.NewGossipSub(context.Background(), ix.Host)
|
||
|
|
if err != nil {
|
||
|
|
panic(err) // can't run your indexer without a propalgation pubsub, of state of node.
|
||
|
|
}
|
||
|
|
}
|
||
|
|
ix.PS = ps
|
||
|
|
// later TODO : all indexer laucnh a private replica of them self. DEV OPS
|
||
|
|
if ix.isStrictIndexer {
|
||
|
|
common.ConnectToIndexers(h, 0, 5, ix.Host.ID()) // TODO : make var to change how many indexers are allowed.
|
||
|
|
ix.SubscribeToNodeActivity(ix.PS) // now we subscribe to a long run topic named node-activity, to relay message.
|
||
|
|
ix.SubscribeToSearch(ix.PS)
|
||
|
|
}
|
||
|
|
ix.initNodeHandler() // then listen up on every protocol expected
|
||
|
|
return ix
|
||
|
|
}
|
||
|
|
|
||
|
|
func (ix *IndexerService) Close() {
|
||
|
|
for _, s := range ix.StreamRecords {
|
||
|
|
for _, ss := range s {
|
||
|
|
ss.Stream.Close()
|
||
|
|
ss.HeartbeatStream.Stream.Close()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|