Пример #1
0
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
}
Пример #2
0
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
}
Пример #3
0
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
}
Пример #4
0
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
}
Пример #5
0
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
}
Пример #6
0
//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

}
Пример #7
0
func (r *defaultRouter) HandleError(c peer.NodePeer, msg message.Message) (message.Message, error) {
	log.Println("HandleError ", c.Node(), "msg:", msg)

	return nil, nil
}