func (r *defaultRouter) exists(p peer.NodePeer) bool { r.mutex.Lock() defer r.mutex.Unlock() var node node.Node = p.Node() _, ok := r.peers[node.String()] return ok }
//HandleHello Request func (r *defaultRouter) HandleHello(c peer.NodePeer, msg message.Message) (message.Message, error) { c.Identify(msg.(*message.Hello).From) if r.exists(c) { return &message.Abort{Id: msg.(*message.Hello).Id, From: r.from}, nil } c.State(peer.PeerStatusConnecting) return &message.Welcome{Id: msg.(*message.Hello).Id, From: r.from}, nil }
func (r *defaultRouter) HandleAck(c peer.NodePeer, msg message.Message) (message.Message, error) { err := r.accept(c) if err != nil { log.Println("Unexpected Error accepting Peer on HandleAck ", c.Node(), "msg:", msg) } c.State(peer.PeerStatusConnected) r.eventChan <- &peer.OnPeerConnectedEvent{msg.(*message.Ack).From, peer.PeerStatusConnected, c.Mode()} go r.watcher.Watch(c) return nil, nil }
func (r *defaultRouter) remove(p peer.NodePeer) error { r.mutex.Lock() defer r.mutex.Unlock() node := p.Node() if _, ok := r.peers[node.String()]; !ok { return fmt.Errorf("Peer Not found") } delete(r.peers, node.String()) delete(r.peerIDs, p.Id()) return nil }
func (r *defaultRouter) accept(p peer.NodePeer) error { r.mutex.Lock() defer r.mutex.Unlock() var node node.Node = p.Node() if _, ok := r.peers[node.String()]; ok { return fmt.Errorf("Peer: %s Already registered", node.String()) } r.peers[node.String()] = p r.peerIDs[p.Id()] = true return nil }
func (r *defaultRouter) HandleAbort(c peer.NodePeer, msg message.Message) (message.Message, error) { r.eventChan <- &peer.OnPeerAbortedEvent{c.Node(), peer.PeerStatusAbort} c.State(peer.PeerStatusAbort) c.Exit() return nil, nil }
//HandleWelcome Request func (r *defaultRouter) HandleWelcome(c peer.NodePeer, msg message.Message) (message.Message, error) { err := r.accept(c) if err != nil { r.eventChan <- &peer.OnPeerErroredEvent{c.Node(), peer.PeerStatusError, err} return &message.Error{Id: msg.(*message.Welcome).Id, From: r.from}, err } c.State(peer.PeerStatusConnected) r.eventChan <- &peer.OnPeerConnectedEvent{c.Node(), peer.PeerStatusConnected, c.Mode()} go r.watcher.Watch(c) return &message.Ack{Id: msg.(*message.Welcome).Id, From: r.from}, nil }
func (r *defaultRouter) HandleError(c peer.NodePeer, msg message.Message) (message.Message, error) { log.Println("HandleError ", c.Node(), "msg:", msg) return nil, nil }
func (r *Coordinator) HandleCommand(c peer.NodePeer, msg message.Message) (message.Message, error) { from := c.From() log.Println("HandleCommand, from peer", from.String()) return nil, nil }