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

View File

@@ -2,6 +2,6 @@ package announce
type Handler interface { type Handler interface {
AspectFilter() []string AspectFilter() []string
ReceivedAnnounce(destHash []byte, identity interface{}, appData []byte) error ReceivedAnnounce(destHash []byte, identity interface{}, appData []byte, hops uint8) error
ReceivePathResponses() bool 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() t.mutex.RLock()
handlers := make([]announce.Handler, len(t.announceHandlers)) handlers := make([]announce.Handler, len(t.announceHandlers))
copy(handlers, t.announceHandlers) copy(handlers, t.announceHandlers)
t.mutex.RUnlock() t.mutex.RUnlock()
for _, handler := range handlers { 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) 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 // Notify handlers
t.notifyAnnounceHandlers(destHash, identity, appData) t.notifyAnnounceHandlers(destHash, identity, appData, data[0])
return lastErr return lastErr
} }
@@ -1117,7 +1117,7 @@ func (t *Transport) handleAnnouncePacket(data []byte, iface common.NetworkInterf
// Notify handlers first, regardless of forwarding limits // Notify handlers first, regardless of forwarding limits
debug.Log(debug.DEBUG_INFO, "Notifying announce handlers", "destHash", fmt.Sprintf("%x", destinationHash), "appDataLen", len(appData)) 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") debug.Log(debug.DEBUG_INFO, "Announce handlers notified")
// Don't forward if max hops reached // Don't forward if max hops reached

View File

@@ -335,12 +335,13 @@ func (h *genericAnnounceHandler) ReceivePathResponses() bool {
return false 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() { if !announceHandler.IsUndefined() {
hashStr := hex.EncodeToString(destHash) hashStr := hex.EncodeToString(destHash)
announceHandler.Invoke(js.ValueOf(map[string]interface{}{ announceHandler.Invoke(js.ValueOf(map[string]interface{}{
"hash": hashStr, "hash": hashStr,
"appData": string(appData), "appData": string(appData),
"hops": int(hops),
})) }))
} }
return nil return nil