refactor CHORUS
This commit is contained in:
@@ -21,12 +21,12 @@ type PubSub struct {
|
||||
cancel context.CancelFunc
|
||||
|
||||
// Topic subscriptions
|
||||
bzzzTopic *pubsub.Topic
|
||||
chorusTopic *pubsub.Topic
|
||||
hmmmTopic *pubsub.Topic
|
||||
contextTopic *pubsub.Topic
|
||||
|
||||
// Message subscriptions
|
||||
bzzzSub *pubsub.Subscription
|
||||
chorusSub *pubsub.Subscription
|
||||
hmmmSub *pubsub.Subscription
|
||||
contextSub *pubsub.Subscription
|
||||
|
||||
@@ -37,7 +37,7 @@ type PubSub struct {
|
||||
dynamicSubsMux sync.RWMutex
|
||||
|
||||
// Configuration
|
||||
bzzzTopicName string
|
||||
chorusTopicName string
|
||||
hmmmTopicName string
|
||||
contextTopicName string
|
||||
|
||||
@@ -121,14 +121,14 @@ type Message struct {
|
||||
}
|
||||
|
||||
// NewPubSub creates a new PubSub instance for Bzzz coordination and HMMM meta-discussion
|
||||
func NewPubSub(ctx context.Context, h host.Host, bzzzTopic, hmmmTopic string) (*PubSub, error) {
|
||||
return NewPubSubWithLogger(ctx, h, bzzzTopic, hmmmTopic, nil)
|
||||
func NewPubSub(ctx context.Context, h host.Host, chorusTopic, hmmmTopic string) (*PubSub, error) {
|
||||
return NewPubSubWithLogger(ctx, h, chorusTopic, hmmmTopic, nil)
|
||||
}
|
||||
|
||||
// NewPubSubWithLogger creates a new PubSub instance with hypercore logging
|
||||
func NewPubSubWithLogger(ctx context.Context, h host.Host, bzzzTopic, hmmmTopic string, logger HypercoreLogger) (*PubSub, error) {
|
||||
if bzzzTopic == "" {
|
||||
bzzzTopic = "CHORUS/coordination/v1"
|
||||
func NewPubSubWithLogger(ctx context.Context, h host.Host, chorusTopic, hmmmTopic string, logger HypercoreLogger) (*PubSub, error) {
|
||||
if chorusTopic == "" {
|
||||
chorusTopic = "CHORUS/coordination/v1"
|
||||
}
|
||||
if hmmmTopic == "" {
|
||||
hmmmTopic = "hmmm/meta-discussion/v1"
|
||||
@@ -154,7 +154,7 @@ func NewPubSubWithLogger(ctx context.Context, h host.Host, bzzzTopic, hmmmTopic
|
||||
host: h,
|
||||
ctx: pubsubCtx,
|
||||
cancel: cancel,
|
||||
bzzzTopicName: bzzzTopic,
|
||||
chorusTopicName: chorusTopic,
|
||||
hmmmTopicName: hmmmTopic,
|
||||
contextTopicName: contextTopic,
|
||||
dynamicTopics: make(map[string]*pubsub.Topic),
|
||||
@@ -173,7 +173,7 @@ func NewPubSubWithLogger(ctx context.Context, h host.Host, bzzzTopic, hmmmTopic
|
||||
go p.handleHmmmMessages()
|
||||
go p.handleContextFeedbackMessages()
|
||||
|
||||
fmt.Printf("📡 PubSub initialized - Bzzz: %s, HMMM: %s, Context: %s\n", bzzzTopic, hmmmTopic, contextTopic)
|
||||
fmt.Printf("📡 PubSub initialized - Bzzz: %s, HMMM: %s, Context: %s\n", chorusTopic, hmmmTopic, contextTopic)
|
||||
return p, nil
|
||||
}
|
||||
|
||||
@@ -190,17 +190,17 @@ func (p *PubSub) SetContextFeedbackHandler(handler func(msg Message, from peer.I
|
||||
// joinStaticTopics joins the main Bzzz, HMMM, and Context Feedback topics
|
||||
func (p *PubSub) joinStaticTopics() error {
|
||||
// Join Bzzz coordination topic
|
||||
bzzzTopic, err := p.ps.Join(p.bzzzTopicName)
|
||||
chorusTopic, err := p.ps.Join(p.chorusTopicName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to join Bzzz topic: %w", err)
|
||||
}
|
||||
p.bzzzTopic = bzzzTopic
|
||||
p.chorusTopic = chorusTopic
|
||||
|
||||
bzzzSub, err := bzzzTopic.Subscribe()
|
||||
chorusSub, err := chorusTopic.Subscribe()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to subscribe to Bzzz topic: %w", err)
|
||||
}
|
||||
p.bzzzSub = bzzzSub
|
||||
p.chorusSub = chorusSub
|
||||
|
||||
// Join HMMM meta-discussion topic
|
||||
hmmmTopic, err := p.ps.Join(p.hmmmTopicName)
|
||||
@@ -366,8 +366,8 @@ func (p *PubSub) PublishRaw(topicName string, payload []byte) error {
|
||||
|
||||
// Static topics by name
|
||||
switch topicName {
|
||||
case p.bzzzTopicName:
|
||||
return p.bzzzTopic.Publish(p.ctx, payload)
|
||||
case p.chorusTopicName:
|
||||
return p.chorusTopic.Publish(p.ctx, payload)
|
||||
case p.hmmmTopicName:
|
||||
return p.hmmmTopic.Publish(p.ctx, payload)
|
||||
case p.contextTopicName:
|
||||
@@ -391,7 +391,7 @@ func (p *PubSub) PublishBzzzMessage(msgType MessageType, data map[string]interfa
|
||||
return fmt.Errorf("failed to marshal message: %w", err)
|
||||
}
|
||||
|
||||
return p.bzzzTopic.Publish(p.ctx, msgBytes)
|
||||
return p.chorusTopic.Publish(p.ctx, msgBytes)
|
||||
}
|
||||
|
||||
// PublishHmmmMessage publishes a message to the HMMM meta-discussion topic
|
||||
@@ -468,7 +468,7 @@ func (p *PubSub) PublishRoleBasedMessage(msgType MessageType, data map[string]in
|
||||
ProjectUpdate, DeliverableReady:
|
||||
topic = p.hmmmTopic // Use HMMM topic for role-based messages
|
||||
default:
|
||||
topic = p.bzzzTopic // Default to Bzzz topic
|
||||
topic = p.chorusTopic // Default to Bzzz topic
|
||||
}
|
||||
|
||||
return topic.Publish(p.ctx, msgBytes)
|
||||
@@ -521,7 +521,7 @@ type MessageOptions struct {
|
||||
// handleBzzzMessages processes incoming Bzzz coordination messages
|
||||
func (p *PubSub) handleBzzzMessages() {
|
||||
for {
|
||||
msg, err := p.bzzzSub.Next(p.ctx)
|
||||
msg, err := p.chorusSub.Next(p.ctx)
|
||||
if err != nil {
|
||||
if p.ctx.Err() != nil {
|
||||
return // Context cancelled
|
||||
@@ -534,13 +534,13 @@ func (p *PubSub) handleBzzzMessages() {
|
||||
continue
|
||||
}
|
||||
|
||||
var bzzzMsg Message
|
||||
if err := json.Unmarshal(msg.Data, &bzzzMsg); err != nil {
|
||||
var chorusMsg Message
|
||||
if err := json.Unmarshal(msg.Data, &chorusMsg); err != nil {
|
||||
fmt.Printf("❌ Failed to unmarshal Bzzz message: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
p.processBzzzMessage(bzzzMsg, msg.ReceivedFrom)
|
||||
p.processBzzzMessage(chorusMsg, msg.ReceivedFrom)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -768,8 +768,8 @@ func (p *PubSub) processContextFeedbackMessage(msg Message, from peer.ID) {
|
||||
func (p *PubSub) Close() error {
|
||||
p.cancel()
|
||||
|
||||
if p.bzzzSub != nil {
|
||||
p.bzzzSub.Cancel()
|
||||
if p.chorusSub != nil {
|
||||
p.chorusSub.Cancel()
|
||||
}
|
||||
if p.hmmmSub != nil {
|
||||
p.hmmmSub.Cancel()
|
||||
@@ -778,8 +778,8 @@ func (p *PubSub) Close() error {
|
||||
p.contextSub.Cancel()
|
||||
}
|
||||
|
||||
if p.bzzzTopic != nil {
|
||||
p.bzzzTopic.Close()
|
||||
if p.chorusTopic != nil {
|
||||
p.chorusTopic.Close()
|
||||
}
|
||||
if p.hmmmTopic != nil {
|
||||
p.hmmmTopic.Close()
|
||||
|
||||
Reference in New Issue
Block a user