Skip to content

Commit

Permalink
move member to its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
gdiazlo committed Nov 22, 2018
1 parent f897f64 commit fdb714c
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
91 changes: 91 additions & 0 deletions gossip/member/member.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, n.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package member

import (
"net"

"github.com/hashicorp/memberlist"
)

type Type int

func (t Type) String() string {
switch t {
case Auditor:
return "auditor"
case Monitor:
return "monitor"
case Publisher:
return "publisher"
case Server:
return "server"
default:
return "unknown"
}
}

func ParseType(value string) Type {
switch value {
case "auditor":
return Auditor
case "monitor":
return Monitor
case "publisher":
return Publisher
default:
return Server
}
}

const (
Auditor Type = iota
Monitor
Publisher
Server
MaxType
)

// Member is a single member of the gossip cluster.
type Peer struct {
Name string
Addr net.IP
Port uint16
Meta *Meta
Status Status
}

func (p Peer) Node() *memberlist.Node {
return &memberlist.Node{
Name: p.Name,
Addr: p.Addr,
Port: p.Port,
}
}

func NewPeer(name, addr string, port uint16, role Type) *Peer {
meta := &Meta{
Role: role,
}

return &Peer{
Name: name,
Addr: net.IP(addr),
Port: port,
Meta: meta,
}
}

func ParsePeer(node *memberlist.Node) *Peer {
return nil
}
44 changes: 44 additions & 0 deletions gossip/member/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, n.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package member

import (
"bytes"

"github.com/bbva/qed/log"
"github.com/hashicorp/go-msgpack/codec"
)

type Meta struct {
Role Type
}

func (a *Meta) Encode() ([]byte, error) {
var buf bytes.Buffer
encoder := codec.NewEncoder(&buf, &codec.MsgpackHandle{})
if err := encoder.Encode(a); err != nil {
log.Errorf("Failed to encode agent metadata: %v", err)
return nil, err
}
return buf.Bytes(), nil
}

func (a *Meta) Decode(buf []byte) error {
reader := bytes.NewReader(buf)
decoder := codec.NewDecoder(reader, &codec.MsgpackHandle{})
if err := decoder.Decode(a); err != nil {
log.Errorf("Failed to decode agent metadata: %v", err)
return err
}
return nil
}
39 changes: 39 additions & 0 deletions gossip/member/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, n.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package member

// Status is the state of the Agent instance.
type Status int32

const (
Alive Status = iota
Leaving
Left
Shutdown
Failed
)

func (s Status) String() string {
switch s {
case Alive:
return "alive"
case Leaving:
return "leaving"
case Left:
return "left"
case Shutdown:
return "shutdown"
default:
return "failed"
}
}

0 comments on commit fdb714c

Please sign in to comment.