2026-02-02 12:14:01 +01:00
|
|
|
package node
|
2026-01-28 17:22:29 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2026-02-05 16:17:14 +01:00
|
|
|
"oc-discovery/daemons/node/common"
|
2026-02-24 14:31:37 +01:00
|
|
|
"oc-discovery/daemons/node/stream"
|
2026-03-17 11:57:22 +01:00
|
|
|
"slices"
|
2026-01-28 17:22:29 +01:00
|
|
|
|
2026-02-05 16:17:14 +01:00
|
|
|
oclib "cloud.o-forge.io/core/oc-lib"
|
|
|
|
|
"cloud.o-forge.io/core/oc-lib/config"
|
2026-01-28 17:22:29 +01:00
|
|
|
"cloud.o-forge.io/core/oc-lib/tools"
|
2026-02-05 16:17:14 +01:00
|
|
|
pp "github.com/libp2p/go-libp2p/core/peer"
|
2026-02-24 14:31:37 +01:00
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
2026-01-28 17:22:29 +01:00
|
|
|
)
|
|
|
|
|
|
2026-02-24 14:31:37 +01:00
|
|
|
type configPayload struct {
|
|
|
|
|
PeerID string `json:"source_peer_id"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type executionConsidersPayload struct {
|
|
|
|
|
PeerIDs []string `json:"peer_ids"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-09 13:28:00 +01:00
|
|
|
func ListenNATS(n *Node) {
|
2026-01-28 17:22:29 +01:00
|
|
|
tools.NewNATSCaller().ListenNats(map[tools.NATSMethod]func(tools.NATSResponse){
|
|
|
|
|
tools.PROPALGATION_EVENT: func(resp tools.NATSResponse) {
|
2026-03-03 16:38:24 +01:00
|
|
|
fmt.Println("PROPALGATION")
|
2026-02-24 14:31:37 +01:00
|
|
|
if resp.FromApp == config.GetAppName() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-03-17 11:57:22 +01:00
|
|
|
p, err := oclib.GetMySelf()
|
|
|
|
|
if err != nil || p == nil || p.PeerID != n.PeerID.String() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-01-28 17:22:29 +01:00
|
|
|
var propalgation tools.PropalgationMessage
|
2026-03-17 11:57:22 +01:00
|
|
|
err = json.Unmarshal(resp.Payload, &propalgation)
|
2026-01-28 17:22:29 +01:00
|
|
|
var dt *tools.DataType
|
|
|
|
|
if propalgation.DataType > 0 {
|
|
|
|
|
dtt := tools.DataType(propalgation.DataType)
|
|
|
|
|
dt = &dtt
|
|
|
|
|
}
|
2026-03-12 15:57:41 +01:00
|
|
|
fmt.Println("PROPALGATION ACT", propalgation.DataType, propalgation.Action, propalgation.Action == tools.PB_CREATE, err)
|
2026-01-28 17:22:29 +01:00
|
|
|
if err == nil {
|
|
|
|
|
switch propalgation.Action {
|
2026-03-03 16:38:24 +01:00
|
|
|
case tools.PB_ADMIRALTY_CONFIG, tools.PB_MINIO_CONFIG:
|
2026-02-24 14:31:37 +01:00
|
|
|
var m configPayload
|
|
|
|
|
var proto protocol.ID = stream.ProtocolAdmiraltyConfigResource
|
|
|
|
|
if propalgation.Action == tools.PB_MINIO_CONFIG {
|
|
|
|
|
proto = stream.ProtocolMinioConfigResource
|
|
|
|
|
}
|
2026-03-17 11:57:22 +01:00
|
|
|
if err := json.Unmarshal(propalgation.Payload, &m); err == nil {
|
2026-03-11 19:29:39 +01:00
|
|
|
peers, _ := n.GetPeerRecord(context.Background(), m.PeerID)
|
2026-02-24 14:31:37 +01:00
|
|
|
for _, p := range peers {
|
2026-03-12 08:57:06 +01:00
|
|
|
n.StreamService.PublishCommon(&resp.Datatype, resp.User, resp.Groups,
|
2026-03-17 11:57:22 +01:00
|
|
|
p.PeerID, proto, propalgation.Payload)
|
2026-02-24 14:31:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-03 16:38:24 +01:00
|
|
|
case tools.PB_CREATE, tools.PB_UPDATE, tools.PB_DELETE:
|
2026-03-17 11:57:22 +01:00
|
|
|
if slices.Contains([]tools.DataType{tools.BOOKING, tools.PURCHASE_RESOURCE}, resp.Datatype) {
|
|
|
|
|
m := map[string]interface{}{}
|
|
|
|
|
if err := json.Unmarshal(propalgation.Payload, &m); err == nil {
|
|
|
|
|
if m["peer_id"] != nil {
|
|
|
|
|
n.StreamService.PublishCommon(&resp.Datatype, resp.User, resp.Groups,
|
|
|
|
|
fmt.Sprintf("%v", m["peer_id"]), stream.ProtocolCreateResource, propalgation.Payload)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println(n.StreamService.ToPartnerPublishEvent(
|
|
|
|
|
context.Background(),
|
|
|
|
|
propalgation.Action,
|
|
|
|
|
dt, resp.User, resp.Groups,
|
|
|
|
|
propalgation.Payload,
|
|
|
|
|
))
|
|
|
|
|
}
|
2026-02-24 14:31:37 +01:00
|
|
|
case tools.PB_CONSIDERS:
|
|
|
|
|
switch resp.Datatype {
|
2026-03-03 16:38:24 +01:00
|
|
|
case tools.BOOKING, tools.PURCHASE_RESOURCE, tools.WORKFLOW_EXECUTION:
|
2026-02-24 14:31:37 +01:00
|
|
|
var m executionConsidersPayload
|
2026-03-17 11:57:22 +01:00
|
|
|
if err := json.Unmarshal(propalgation.Payload, &m); err == nil {
|
2026-02-24 14:31:37 +01:00
|
|
|
for _, p := range m.PeerIDs {
|
2026-03-11 19:29:39 +01:00
|
|
|
peers, _ := n.GetPeerRecord(context.Background(), p)
|
2026-02-24 14:31:37 +01:00
|
|
|
for _, pp := range peers {
|
2026-03-12 08:57:06 +01:00
|
|
|
n.StreamService.PublishCommon(&resp.Datatype, resp.User, resp.Groups,
|
2026-03-17 11:57:22 +01:00
|
|
|
pp.PeerID, stream.ProtocolConsidersResource, propalgation.Payload)
|
2026-02-24 14:31:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-26 09:14:34 +01:00
|
|
|
default:
|
|
|
|
|
// minio / admiralty config considers — route back to OriginID.
|
|
|
|
|
var m struct {
|
|
|
|
|
OriginID string `json:"origin_id"`
|
|
|
|
|
}
|
|
|
|
|
if err := json.Unmarshal(propalgation.Payload, &m); err == nil && m.OriginID != "" {
|
2026-03-11 19:29:39 +01:00
|
|
|
peers, _ := n.GetPeerRecord(context.Background(), m.OriginID)
|
2026-02-26 09:14:34 +01:00
|
|
|
for _, p := range peers {
|
2026-03-12 08:57:06 +01:00
|
|
|
n.StreamService.PublishCommon(nil, resp.User, resp.Groups,
|
2026-02-26 09:14:34 +01:00
|
|
|
p.PeerID, stream.ProtocolConsidersResource, propalgation.Payload)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-24 14:31:37 +01:00
|
|
|
}
|
|
|
|
|
case tools.PB_PLANNER:
|
2026-01-28 17:22:29 +01:00
|
|
|
m := map[string]interface{}{}
|
2026-03-17 11:57:22 +01:00
|
|
|
if err := json.Unmarshal(propalgation.Payload, &m); err == nil {
|
2026-02-24 14:31:37 +01:00
|
|
|
b := []byte{}
|
|
|
|
|
if len(m) > 1 {
|
2026-03-17 11:57:22 +01:00
|
|
|
b = propalgation.Payload
|
2026-02-24 14:31:37 +01:00
|
|
|
}
|
|
|
|
|
if m["peer_id"] == nil { // send to every active stream
|
|
|
|
|
n.StreamService.Mu.Lock()
|
|
|
|
|
if n.StreamService.Streams[stream.ProtocolSendPlanner] != nil {
|
2026-03-11 16:28:15 +01:00
|
|
|
for pid := range n.StreamService.Streams[stream.ProtocolSendPlanner] { // send Planner can be long lived - it's a conn
|
2026-03-12 08:57:06 +01:00
|
|
|
n.StreamService.PublishCommon(nil, resp.User, resp.Groups, pid.String(), stream.ProtocolSendPlanner, b)
|
2026-02-24 14:31:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-17 11:57:22 +01:00
|
|
|
n.StreamService.Mu.Unlock()
|
2026-02-24 14:31:37 +01:00
|
|
|
} else {
|
2026-03-12 08:57:06 +01:00
|
|
|
n.StreamService.PublishCommon(nil, resp.User, resp.Groups, fmt.Sprintf("%v", m["peer_id"]), stream.ProtocolSendPlanner, b)
|
2026-02-24 14:31:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case tools.PB_CLOSE_PLANNER:
|
|
|
|
|
m := map[string]interface{}{}
|
|
|
|
|
if err := json.Unmarshal(resp.Payload, &m); err == nil {
|
|
|
|
|
n.StreamService.Mu.Lock()
|
|
|
|
|
if pid, err := pp.Decode(fmt.Sprintf("%v", m["peer_id"])); err == nil {
|
|
|
|
|
if n.StreamService.Streams[stream.ProtocolSendPlanner] != nil && n.StreamService.Streams[stream.ProtocolSendPlanner][pid] != nil {
|
|
|
|
|
n.StreamService.Streams[stream.ProtocolSendPlanner][pid].Stream.Close()
|
|
|
|
|
delete(n.StreamService.Streams[stream.ProtocolSendPlanner], pid)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
n.StreamService.Mu.Unlock()
|
|
|
|
|
}
|
2026-03-12 15:57:41 +01:00
|
|
|
case tools.PB_CLOSE_SEARCH:
|
|
|
|
|
if propalgation.DataType == int(tools.PEER) {
|
|
|
|
|
n.peerSearches.Cancel(resp.User)
|
|
|
|
|
} else {
|
|
|
|
|
n.StreamService.ResourceSearches.Cancel(resp.User)
|
|
|
|
|
}
|
2026-02-24 14:31:37 +01:00
|
|
|
case tools.PB_SEARCH:
|
|
|
|
|
if propalgation.DataType == int(tools.PEER) {
|
|
|
|
|
m := map[string]interface{}{}
|
|
|
|
|
if err := json.Unmarshal(propalgation.Payload, &m); err == nil {
|
2026-03-11 16:28:15 +01:00
|
|
|
needle := fmt.Sprintf("%v", m["search"])
|
|
|
|
|
userKey := resp.User
|
|
|
|
|
go n.SearchPeerRecord(userKey, needle, func(hit common.SearchHit) {
|
|
|
|
|
if b, err := json.Marshal(hit); err == nil {
|
|
|
|
|
tools.NewNATSCaller().SetNATSPub(tools.SEARCH_EVENT, tools.NATSResponse{
|
|
|
|
|
FromApp: "oc-discovery",
|
|
|
|
|
Datatype: tools.DataType(tools.PEER),
|
|
|
|
|
Method: int(tools.SEARCH_EVENT),
|
|
|
|
|
Payload: b,
|
|
|
|
|
})
|
2026-02-24 14:31:37 +01:00
|
|
|
}
|
2026-03-11 16:28:15 +01:00
|
|
|
})
|
2026-02-24 14:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
m := map[string]interface{}{}
|
|
|
|
|
if err := json.Unmarshal(propalgation.Payload, &m); err == nil {
|
|
|
|
|
n.PubSubService.SearchPublishEvent(
|
|
|
|
|
context.Background(),
|
|
|
|
|
dt,
|
|
|
|
|
fmt.Sprintf("%v", m["type"]),
|
2026-03-12 08:57:06 +01:00
|
|
|
resp.User, resp.Groups,
|
2026-02-24 14:31:37 +01:00
|
|
|
fmt.Sprintf("%v", m["search"]),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-28 17:22:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|