예제 #1
0
func NewDialer(from n.Node, destination n.Node) *Peer {
	var conn net.Conn
	var err error
	log.Println("init dialer to:", destination.String())

	for {
		conn, err = net.Dial("tcp", string(destination.String()))
		if err != nil {
			log.Println("dial error:", err)
			time.Sleep(time.Second)
		} else {
			break
		}
	}

	return &Peer{
		from:         from,
		Link:         NewJSONSocketLink(conn),
		to:           destination,
		dataChan:     make(chan message.Message, 10),
		messageChan:  make(chan message.Message, 10),
		sendChan:     make(chan message.Message, 0),
		exitChan:     make(chan bool, 2),
		doneChan:     make(chan bool, 1),
		rstWatchChan: make(chan bool, 1),
		mode:         "client",
		state:        PeerStatusStarted,
	}
}
예제 #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 InitDialClient(from n.Node, destination n.Node) (*Peer, message.ID) {
	//Blocking call, wait until connection success
	p := NewDialer(from, destination)
	go p.Run()
	log.Println("Connected Dial Client from Node ", from.String(), "destination: ", destination.String())

	//Say Hello and wait response
	id, err := p.SayHello()
	if err != nil {
		log.Println("Error getting Hello Id ", err)
	}

	return p, id
}
예제 #4
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
}
예제 #5
0
func TestForwardingChannel(t *testing.T) {
	from := node.Node{Host: "localhost", Port: 9000}
	n := node.Node{Host: "localhost", Port: 9011}
	members := make(map[string]node.Node, 2)
	members[n.String()] = n
	members[from.String()] = from // as fake local node

	o = StartCoordinator(from, members)
	go o.Run()
	time.Sleep(time.Second)
	o.Exit()
}