package peer import ( "encoding/json" "fmt" "cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/tools" "github.com/google/uuid" ) // now write a go enum for the state partner with self, blacklist, partner type PeerState int const ( NONE PeerState = iota SELF PARTNER BLACKLIST ) func (m PeerState) String() string { return [...]string{"NONE", "SELF", "PARTNER", "BLACKLIST"}[m] } func (m PeerState) EnumIndex() int { return int(m) } // Peer is a struct that represents a peer type Peer struct { utils.AbstractObject Url string `json:"url,omitempty" bson:"url,omitempty" validate:"required,base64url"` // Url is the URL of the peer (base64url) PublicKey string `json:"public_key,omitempty" bson:"public_key,omitempty"` // PublicKey is the public key of the peer Services map[string]int `json:"services,omitempty" bson:"services,omitempty"` State PeerState `json:"state" bson:"state" default:"0"` FailedExecution []PeerExecution `json:"failed_execution" bson:"failed_execution"` // FailedExecution is the list of failed executions, to be retried } // AddExecution adds an execution to the list of failed executions func (ao *Peer) AddExecution(exec PeerExecution) { found := false for _, v := range ao.FailedExecution { // Check if the execution is already in the list if v.Url == exec.Url && v.Method == exec.Method && fmt.Sprint(v.Body) == fmt.Sprint(exec.Body) { found = true break } } if !found { ao.FailedExecution = append(ao.FailedExecution, exec) } } // RemoveExecution removes an execution from the list of failed executions func (ao *Peer) RemoveExecution(exec PeerExecution) { new := []PeerExecution{} for i, v := range ao.FailedExecution { if !(v.Url == exec.Url && v.Method == exec.Method && fmt.Sprint(v.Body) == fmt.Sprint(exec.Body)) { new = append(new, ao.FailedExecution[i]) } } ao.FailedExecution = new } // IsMySelf checks if the peer is the local peer func (ao *Peer) IsMySelf() (bool, string) { d, code, err := ao.GetAccessor(nil).Search(nil, SELF.String()) if code != 200 || err != nil || len(d) == 0 { return false, "" } id := d[0].GetID() return ao.UUID == id, id } // LaunchPeerExecution launches an execution on a peer func (p *Peer) LaunchPeerExecution(peerID string, dataID string, dt tools.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) { p.UUID = peerID return cache.LaunchPeerExecution(peerID, dataID, dt, method, body, caller) // Launch the execution on the peer through the cache } func (ao *Peer) GetID() string { return ao.UUID } func (r *Peer) GenerateID() { if r.UUID == "" { r.UUID = uuid.New().String() } } func (d *Peer) GetName() string { return d.Name } func (d *Peer) GetAccessor(caller *tools.HTTPCaller) utils.Accessor { data := New() // Create a new instance of the accessor data.Init(tools.PEER, caller) // Initialize the accessor with the PEER model type return data } func (dma *Peer) Deserialize(j map[string]interface{}) utils.DBObject { b, err := json.Marshal(j) if err != nil { return nil } json.Unmarshal(b, dma) return dma } func (dma *Peer) Serialize() map[string]interface{} { var m map[string]interface{} b, err := json.Marshal(dma) if err != nil { return nil } json.Unmarshal(b, &m) return m }