Skip to content

Commit

Permalink
Added NodeChange and SetState and Event functions
Browse files Browse the repository at this point in the history
  • Loading branch information
VoyTechnology committed Apr 21, 2016
1 parent 05ac14f commit e256465
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
53 changes: 51 additions & 2 deletions pfsd/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import (
"github.com/cpssd/paranoid/logger"
)

var Log *logger.ParanoidLogger
var server *Server
var (
Log *logger.ParanoidLogger
server *Server
nodeList map[string]MessageNode
)

func init() {
nodeList = make(map[string]MessageNode)
}

func Send(msg Message) {
server.Send(msg)
Expand All @@ -19,3 +26,45 @@ func NewStdServer(port string) {
func Listen() {
server.Run()
}

// SetState is used to set the local exporter state
func SetState(nodes []MessageNode) {
for i := 0; i < len(nodes); i++ {
nodeList[nodes[i].Uuid] = nodes[i]
}
}

// NodeChange sends a message to the client whenever there is a change in nodes
func NodeChange(node MessageNode) {
msg := Message{
Type: NodeChangeMessage,
Data: MessageData{
Node: node,
},
}

// Check does the message exist to determine the response
// TODO: Add implementation when a node is deleted. This is currently
// not supported with our raft implementation
if _, ok := nodeList[node.Uuid]; !ok {
msg.Data.Action = "add"
} else {
msg.Data.Action = "update"
}

nodeList[node.Uuid] = node
server.Send(msg)
}

// Send an event that happened to the client
func Event(msg Message) {
server.Send(msg)
}

func toNodeArray(m map[string]MessageNode) []MessageNode {
var nodes []MessageNode
for _, n := range m {
nodes = append(nodes, n)
}
return nodes
}
29 changes: 23 additions & 6 deletions pfsd/exporter/message.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
package exporter

type MessageType int
type MessageType string

const (
NodeChangeMessage MessageType = iota
RaftActionMessage
StateMessage MessageType = "state"
NodeChangeMessage = "nodechange"
RaftActionMessage = "event"
)

func (m MessageType) String() string {
switch m {
case StateMessage:
return "state"
case NodeChangeMessage:
return "nodechange"
case RaftActionMessage:
return "event"
default:
return ""
}
}

type Message struct {
Type MessageType `json:"type"`
Data MessageData `json:"data"`
}

type MessageData struct {
// Used for "nodechange"
Node MessageNode `json:"node,omitempty"`
// Used for "status"
Nodes []MessageNode `json:"nodes,omitempty"`
// Used for Event
// Used for "nodechange"
Action string `json:"action,omitempty"`
// Used for "nodechange"
Node MessageNode `json:"node,omitempty"`
// Used for "event"
Event MessageEvent `json:"event,omitempty"`
}

type MessageNode struct {
Uuid string `json:"uuid"`
CommonName string `json:"commonName"`
State string `json:"state"`
Addr string `json:"addr"`
}

type MessageEvent struct {
Expand Down
8 changes: 8 additions & 0 deletions pfsd/exporter/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func (s *Server) Run() {
msgs: make(chan Message),
}
s.client.listen()

// Send the state message
s.Send(Message{
Type: StateMessage,
Data: MessageData{
Nodes: toNodeArray(nodeList),
},
})
}

http.ListenAndServe(":"+s.port, websocket.Handler(onConnected))
Expand Down

0 comments on commit e256465

Please sign in to comment.