refactor: replace AnnounceHandler interface with Handler and update ReceivedAnnounce method to include hops parameter

This commit is contained in:
2025-12-31 18:42:50 -06:00
parent e6fd7188d2
commit d6152ccd85
4 changed files with 13 additions and 18 deletions

View File

@@ -49,12 +49,6 @@ const (
MAX_RETRIES = 3
)
type AnnounceHandler interface {
AspectFilter() []string
ReceivedAnnounce(destinationHash []byte, announcedIdentity interface{}, appData []byte) error
ReceivePathResponses() bool
}
type Announce struct {
mutex *sync.RWMutex
destinationHash []byte
@@ -67,7 +61,7 @@ type Announce struct {
signature []byte
pathResponse bool
retries int
handlers []AnnounceHandler
handlers []Handler
ratchetID []byte
packet []byte
hash []byte
@@ -97,7 +91,7 @@ func New(dest *identity.Identity, destinationHash []byte, destinationName string
timestamp: time.Now().Unix(),
pathResponse: pathResponse,
retries: 0,
handlers: make([]AnnounceHandler, 0),
handlers: make([]Handler, 0),
}
// Get current ratchet ID if enabled
@@ -156,13 +150,13 @@ func (a *Announce) Propagate(interfaces []common.NetworkInterface) error {
return nil
}
func (a *Announce) RegisterHandler(handler AnnounceHandler) {
func (a *Announce) RegisterHandler(handler Handler) {
a.mutex.Lock()
defer a.mutex.Unlock()
a.handlers = append(a.handlers, handler)
}
func (a *Announce) DeregisterHandler(handler AnnounceHandler) {
func (a *Announce) DeregisterHandler(handler Handler) {
a.mutex.Lock()
defer a.mutex.Unlock()
for i, h := range a.handlers {
@@ -283,7 +277,7 @@ func (a *Announce) HandleAnnounce(data []byte) error {
// Process with handlers
for _, handler := range a.handlers {
if handler.ReceivePathResponses() || !a.pathResponse {
if err := handler.ReceivedAnnounce(destHash, announcedIdentity, appData); err != nil {
if err := handler.ReceivedAnnounce(destHash, announcedIdentity, appData, hopCount); err != nil {
return err
}
}
@@ -480,7 +474,7 @@ func NewAnnounce(identity *identity.Identity, destinationHash []byte, appData []
destinationHash: destHash,
hops: 0,
mutex: &sync.RWMutex{},
handlers: make([]AnnounceHandler, 0),
handlers: make([]Handler, 0),
config: config,
}

View File

@@ -2,6 +2,6 @@ package announce
type Handler interface {
AspectFilter() []string
ReceivedAnnounce(destHash []byte, identity interface{}, appData []byte) error
ReceivedAnnounce(destHash []byte, identity interface{}, appData []byte, hops uint8) error
ReceivePathResponses() bool
}

View File

@@ -492,14 +492,14 @@ func (t *Transport) UnregisterAnnounceHandler(handler announce.Handler) {
}
}
func (t *Transport) notifyAnnounceHandlers(destHash []byte, identity interface{}, appData []byte) {
func (t *Transport) notifyAnnounceHandlers(destHash []byte, identity interface{}, appData []byte, hops uint8) {
t.mutex.RLock()
handlers := make([]announce.Handler, len(t.announceHandlers))
copy(handlers, t.announceHandlers)
t.mutex.RUnlock()
for _, handler := range handlers {
if err := handler.ReceivedAnnounce(destHash, identity, appData); err != nil {
if err := handler.ReceivedAnnounce(destHash, identity, appData, hops); err != nil {
debug.Log(debug.DEBUG_ERROR, "Error in announce handler", "error", err)
}
}
@@ -716,7 +716,7 @@ func (t *Transport) HandleAnnounce(data []byte, sourceIface common.NetworkInterf
}
// Notify handlers
t.notifyAnnounceHandlers(destHash, identity, appData)
t.notifyAnnounceHandlers(destHash, identity, appData, data[0])
return lastErr
}
@@ -1117,7 +1117,7 @@ func (t *Transport) handleAnnouncePacket(data []byte, iface common.NetworkInterf
// Notify handlers first, regardless of forwarding limits
debug.Log(debug.DEBUG_INFO, "Notifying announce handlers", "destHash", fmt.Sprintf("%x", destinationHash), "appDataLen", len(appData))
t.notifyAnnounceHandlers(destinationHash, id, appData)
t.notifyAnnounceHandlers(destinationHash, id, appData, hopCount)
debug.Log(debug.DEBUG_INFO, "Announce handlers notified")
// Don't forward if max hops reached

View File

@@ -335,12 +335,13 @@ func (h *genericAnnounceHandler) ReceivePathResponses() bool {
return false
}
func (h *genericAnnounceHandler) ReceivedAnnounce(destHash []byte, ident interface{}, appData []byte) error {
func (h *genericAnnounceHandler) ReceivedAnnounce(destHash []byte, ident interface{}, appData []byte, hops uint8) error {
if !announceHandler.IsUndefined() {
hashStr := hex.EncodeToString(destHash)
announceHandler.Invoke(js.ValueOf(map[string]interface{}{
"hash": hashStr,
"appData": string(appData),
"hops": int(hops),
}))
}
return nil