Peer Discovery
This commit is contained in:
37
infrastructure/nats.go
Normal file
37
infrastructure/nats.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package infrastructure
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
oclib "cloud.o-forge.io/core/oc-lib"
|
||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
func ListenNATS() {
|
||||
tools.NewNATSCaller().ListenNats(map[tools.NATSMethod]func(tools.NATSResponse){
|
||||
tools.CREATE_PEER: func(resp tools.NATSResponse) {
|
||||
p := &peer.Peer{}
|
||||
err := json.Unmarshal(resp.Payload, p)
|
||||
if err == nil {
|
||||
search := p.PeerID
|
||||
if p.Relation == peer.SELF {
|
||||
search = fmt.Sprintf("%v", peer.SELF.EnumIndex())
|
||||
}
|
||||
access := oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.PEER), nil)
|
||||
if data := access.Search(nil, search, false); len(data.Data) > 0 {
|
||||
pp := data.Data[0].(*peer.Peer)
|
||||
access.UpdateOne(pp.Serialize(pp), pp.GetID())
|
||||
} else {
|
||||
access.StoreOne(p.Serialize(p))
|
||||
}
|
||||
}
|
||||
},
|
||||
tools.REMOVE_PEER: func(tools.NATSResponse) {
|
||||
p := &peer.Peer{}
|
||||
access := oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.PEER), nil)
|
||||
access.DeleteOne(p.GetID())
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user