Example #1
0
func sendMessages(t *natty.Traversal, serverId waddell.PeerId, traversalId uint32) {
	for {
		msgOut, done := t.NextMsgOut()
		if done {
			return
		}
		log.Printf("Sending %s", msgOut)
		out <- waddell.Message(serverId, idToBytes(traversalId), []byte(msgOut))
	}
}
Example #2
0
func (p *peer) answer(wm *waddell.MessageIn) {
	p.traversalsMutex.Lock()
	defer p.traversalsMutex.Unlock()
	msg := message(wm.Body)
	traversalId := msg.getTraversalId()
	t := p.traversals[traversalId]
	if t == nil {
		log.Printf("Answering traversal: %d", traversalId)
		// Set up a new Natty traversal
		t = natty.Answer(TIMEOUT)
		go func() {
			// Send
			for {
				msgOut, done := t.NextMsgOut()
				if done {
					return
				}
				log.Printf("Sending %s", msgOut)
				out <- waddell.Message(p.id, idToBytes(traversalId), []byte(msgOut))
			}
		}()

		go func() {
			// Receive
			defer func() {
				p.traversalsMutex.Lock()
				defer p.traversalsMutex.Unlock()
				delete(p.traversals, traversalId)
				t.Close()
			}()

			ft, err := t.FiveTuple()
			if err != nil {
				log.Printf("Unable to answer traversal %d: %s", traversalId, err)
				return
			}

			log.Printf("Got five tuple: %s", ft)
			go readUDP(p.id, traversalId, ft)
		}()
		p.traversals[traversalId] = t
	}
	log.Printf("Received for traversal %d: %s", traversalId, msg.getData())
	t.MsgIn(string(msg.getData()))
}
Example #3
0
func notifyClientOfServerReady(peerId waddell.PeerId, traversalId uint32) {
	out <- waddell.Message(peerId, idToBytes(traversalId), []byte(READY))
}
Example #4
0
// TestWaddell starts up two local Traversals that communicate with each other
// using a local waddell server.  Once connected, one peer sends a UDP packet to
// the other to make sure that the connection works.
//
// Run test with -v flag to get debug output from natty.
func TestWaddell(t *testing.T) {
	doTest(t, func(offer *Traversal, answer *Traversal) {
		// Start a waddell server
		server := &waddell.Server{}
		laddr := "localhost:0"
		tlog.Debugf("Starting waddell at %s", laddr)
		listener, err := net.Listen("tcp", laddr)
		if err != nil {
			t.Fatalf("Unable to listen at %s: %s", laddr, err)
		}
		waddr := listener.Addr().String()

		go func() {
			err = server.Serve(listener)
			if err != nil {
				t.Fatalf("Unable to start waddell at %s: %s", waddr, err)
			}
		}()

		offerClient := makeWaddellClient(t, waddr)
		answerClient := makeWaddellClient(t, waddr)

		// Send from offer -> answer
		go func() {
			out := offerClient.Out(TestTopic)
			for {
				msg, done := offer.NextMsgOut()
				if done {
					return
				}
				tlog.Debugf("offer -> answer: %s", msg)
				out <- waddell.Message(answerClient.CurrentId(), []byte(msg))
			}
		}()

		// Receive to offer
		go func() {
			in := offerClient.In(TestTopic)
			for msg := range in {
				offer.MsgIn(string(msg.Body))
			}
		}()

		// Send from answer -> offer
		go func() {
			out := answerClient.Out(TestTopic)
			for {
				msg, done := answer.NextMsgOut()
				if done {
					return
				}
				tlog.Debugf("answer -> offer: %s", msg)
				out <- waddell.Message(offerClient.CurrentId(), []byte(msg))
			}
		}()

		// Receive to answer
		go func() {
			in := answerClient.In(TestTopic)
			for msg := range in {
				answer.MsgIn(string(msg.Body))
			}
		}()

	})
}