Discovery Nano the light version.
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"oc-discovery/daemons/node/common"
|
||||
"oc-discovery/daemons/node/indexer"
|
||||
"oc-discovery/daemons/node/stream"
|
||||
"slices"
|
||||
|
||||
@@ -29,6 +29,11 @@ func ListenNATS(n *Node) {
|
||||
tools.PEER_BEHAVIOR_EVENT: func(resp tools.NATSResponse) { //nolint:typecheck
|
||||
handlePeerBehaviorEvent(n, resp)
|
||||
},
|
||||
// PEER_OBSERVE_EVENT is sent by oc-peer to start or stop observations
|
||||
// for a list of peer IDs, or to trigger a close-all.
|
||||
tools.PEER_OBSERVE_EVENT: func(resp tools.NATSResponse) {
|
||||
n.StreamService.HandleObserveNATSCommand(resp)
|
||||
},
|
||||
tools.PROPALGATION_EVENT: func(resp tools.NATSResponse) {
|
||||
if resp.FromApp == config.GetAppName() {
|
||||
return
|
||||
@@ -134,6 +139,21 @@ func ListenNATS(n *Node) {
|
||||
}
|
||||
n.StreamService.Mu.Unlock()
|
||||
}
|
||||
case tools.PB_OBSERVE:
|
||||
print("PROPALGATE OBSERVE")
|
||||
handleObserveEvent(n, propalgation)
|
||||
case tools.PB_OBSERVE_CLOSE:
|
||||
print("PROPALGATE CLOSE")
|
||||
handleObserveCloseEvent(n, propalgation)
|
||||
case tools.PB_PROPAGATE:
|
||||
// Another oc-discovery forwarded a heartbeat batch.
|
||||
// Re-emit on PEER_OBSERVE_RESPONSE_EVENT so the local oc-peer sees it.
|
||||
tools.NewNATSCaller().SetNATSPub(tools.PEER_OBSERVE_RESPONSE_EVENT, tools.NATSResponse{
|
||||
FromApp: resp.FromApp,
|
||||
Datatype: tools.PEER,
|
||||
Method: int(tools.PEER_OBSERVE_RESPONSE_EVENT),
|
||||
Payload: propalgation.Payload,
|
||||
})
|
||||
case tools.PB_CLOSE_SEARCH:
|
||||
if propalgation.DataType == int(tools.PEER) {
|
||||
n.peerSearches.Cancel(resp.User)
|
||||
@@ -141,16 +161,18 @@ func ListenNATS(n *Node) {
|
||||
n.StreamService.ResourceSearches.Cancel(resp.User)
|
||||
}
|
||||
case tools.PB_SEARCH:
|
||||
fmt.Println("PROPALGATE PEER")
|
||||
if propalgation.DataType == int(tools.PEER) {
|
||||
m := map[string]interface{}{}
|
||||
if err := json.Unmarshal(propalgation.Payload, &m); err == nil {
|
||||
needle := fmt.Sprintf("%v", m["search"])
|
||||
userKey := resp.User
|
||||
go n.SearchPeerRecord(userKey, needle, func(hit common.SearchHit) {
|
||||
go n.SearchPeerRecord(userKey, needle, func(hit indexer.PeerRecord) {
|
||||
if b, err := json.Marshal(hit); err == nil {
|
||||
tools.NewNATSCaller().SetNATSPub(tools.SEARCH_EVENT, tools.NATSResponse{
|
||||
FromApp: "oc-discovery",
|
||||
Datatype: tools.DataType(tools.PEER),
|
||||
User: userKey,
|
||||
Method: int(tools.SEARCH_EVENT),
|
||||
Payload: b,
|
||||
})
|
||||
@@ -240,3 +262,37 @@ func handlePeerBehaviorEvent(n *Node, resp tools.NATSResponse) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// handleObserveEvent processes a PB_OBSERVE PropalgationMessage from another
|
||||
// oc-discovery node, starting observation for the listed peers.
|
||||
func handleObserveEvent(n *Node, p tools.PropalgationMessage) {
|
||||
var cmd stream.ObserveCommand
|
||||
if err := json.Unmarshal(p.Payload, &cmd); err != nil {
|
||||
fmt.Println("handleObserveEvent: unmarshal error:", err)
|
||||
return
|
||||
}
|
||||
for _, sp := range cmd.Peers {
|
||||
if err := n.StreamService.OpenObserveStream(sp); err != nil {
|
||||
fmt.Println("handleObserveEvent: OpenObserveStream failed for", sp.PeerID, ":", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleObserveCloseEvent processes a PB_OBSERVE_CLOSE PropalgationMessage from
|
||||
// another oc-discovery node, stopping observation for the listed peer IDs.
|
||||
func handleObserveCloseEvent(n *Node, p tools.PropalgationMessage) {
|
||||
var cmd stream.ObserveCommand
|
||||
if err := json.Unmarshal(p.Payload, &cmd); err != nil {
|
||||
fmt.Println("handleObserveCloseEvent: unmarshal error:", err)
|
||||
return
|
||||
}
|
||||
if cmd.CloseAll {
|
||||
n.StreamService.CloseAllObserves()
|
||||
return
|
||||
}
|
||||
for _, peerID := range cmd.PeerIDs {
|
||||
if err := n.StreamService.CloseObserveStream(peerID); err != nil {
|
||||
fmt.Println("handleObserveCloseEvent: CloseObserveStream failed for", peerID, ":", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user