Commit 94001fa9 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent de551085
...@@ -21,10 +21,8 @@ import ( ...@@ -21,10 +21,8 @@ import (
"sync" "sync"
"unsafe" "unsafe"
"fmt" //"fmt"
) )
// XXX temp
var _ = fmt.Println
// NodeLink is a node-node link in NEO // NodeLink is a node-node link in NEO
// //
...@@ -54,8 +52,8 @@ type NodeLink struct { ...@@ -54,8 +52,8 @@ type NodeLink struct {
connTab map[uint32]*Conn // connId -> Conn associated with connId connTab map[uint32]*Conn // connId -> Conn associated with connId
nextConnId uint32 // next connId to use for Conn initiated by us nextConnId uint32 // next connId to use for Conn initiated by us
serveWg sync.WaitGroup serveWg sync.WaitGroup // for serve{Send,Recv}
//handleWg sync.WaitGroup // for spawned handlers XXX do we need this + .Wait() ?
handleNewConn func(conn *Conn) // handler for new connections handleNewConn func(conn *Conn) // handler for new connections
txreq chan txReq // tx requests from Conns go via here txreq chan txReq // tx requests from Conns go via here
...@@ -280,8 +278,6 @@ func (nl *NodeLink) serveRecv() { ...@@ -280,8 +278,6 @@ func (nl *NodeLink) serveRecv() {
if handleNewConn != nil { if handleNewConn != nil {
conn = nl.newConn(connId) conn = nl.newConn(connId)
} }
// FIXME update connTab with born conn
} }
nl.connMu.Unlock() nl.connMu.Unlock()
...@@ -422,7 +418,7 @@ func (c *Conn) close() { ...@@ -422,7 +418,7 @@ func (c *Conn) close() {
// Close connection // Close connection
// Any blocked Send() or Recv() will be unblocked and return error // Any blocked Send() or Recv() will be unblocked and return error
// XXX Send() - if started - will first complete (not to break framing) XXX <- in background // XXX Send() - if started - will first complete (not to break framing) XXX <- in background
func (c *Conn) Close() error { // XXX do we need error here? func (c *Conn) Close() error {
// adjust nodeLink.connTab // adjust nodeLink.connTab
c.nodeLink.connMu.Lock() c.nodeLink.connMu.Lock()
delete(c.nodeLink.connTab, c.connId) delete(c.nodeLink.connTab, c.connId)
......
...@@ -260,10 +260,11 @@ func TestNodeLink(t *testing.T) { ...@@ -260,10 +260,11 @@ func TestNodeLink(t *testing.T) {
xwait(wg) xwait(wg)
xclose(c11) xclose(c11)
xclose(c12) xclose(c12)
xclose(nl2) // for completeness xclose(nl2)
// Conn accept + exchange // Conn accept + exchange
nl1, nl2 = nodeLinkPipe() nl1, nl2 = nodeLinkPipe()
hdone := make(chan struct{})
nl2.HandleNewConn(func(c *Conn) { nl2.HandleNewConn(func(c *Conn) {
// TODO raised err -> errch // TODO raised err -> errch
pkt := xrecv(c) pkt := xrecv(c)
...@@ -271,56 +272,74 @@ func TestNodeLink(t *testing.T) { ...@@ -271,56 +272,74 @@ func TestNodeLink(t *testing.T) {
// change pkt a bit and send it back // change pkt a bit and send it back
xsend(c, mkpkt(34, []byte("pong"))) xsend(c, mkpkt(34, []byte("pong")))
// one more time
pkt = xrecv(c)
xverifyPkt(pkt, c.connId, 35, []byte("ping2"))
xsend(c, mkpkt(36, []byte("pong2")))
xclose(c) xclose(c)
close(hdone)
}) })
c1 := nl1.NewConn() c = nl1.NewConn()
pkt = mkpkt(33, []byte("ping")) xsend(c, mkpkt(33, []byte("ping")))
xsend(c1, pkt) pkt = xrecv(c)
pkt2 := xrecv(c1) xverifyPkt(pkt, c.connId, 34, []byte("pong"))
xverifyPkt(pkt2, c1.connId, 34, []byte("pong")) xsend(c, mkpkt(35, []byte("ping2")))
pkt = xrecv(c)
xverifyPkt(pkt, c.connId, 36, []byte("pong2"))
<-hdone
xclose(c)
xclose(nl1)
xclose(nl2)
// test 2 channels with replies comming in reversed time order // test 2 channels with replies comming in reversed time order
nl1, nl2 = nodeLinkPipe() nl1, nl2 = nodeLinkPipe()
order := map[uint16]struct { // "order" in which to process requests replyOrder := map[uint16]struct { // "order" in which to process requests
start chan int // processing starts when start chan is ready start chan struct{} // processing starts when start chan is ready
next uint16 // after processing this switch to next next uint16 // after processing this switch to next
}{ }{
2: {make(chan int), 1}, 2: {make(chan struct{}), 1},
1: {make(chan int), 0}, 1: {make(chan struct{}), 0},
} }
go func() { close(replyOrder[2].start)
order[2].start <- 0
}()
c1 = nl1.NewConn() // XXX temp?
c2 := nl1.NewConn()
nl2.HandleNewConn(func(c *Conn) { nl2.HandleNewConn(func(c *Conn) {
pkt := xrecv(c) pkt := xrecv(c)
n := ntoh16(pkt.Header().MsgCode) n := ntoh16(pkt.Header().MsgCode)
x := order[n] x := replyOrder[n]
// wait before it is our turn & echo pkt back // wait before it is our turn & echo pkt back
<-x.start <-x.start
xsend(c, pkt) xsend(c, pkt)
xclose(c)
// tell next it can start // tell next it can start
if x.next != 0 { if x.next != 0 {
order[x.next].start <- 0 close(replyOrder[x.next].start)
} }
}) })
c1 := nl1.NewConn()
c2 := nl1.NewConn()
println("111")
xsend(c1, mkpkt(1, []byte(""))) xsend(c1, mkpkt(1, []byte("")))
println("222")
xsend(c2, mkpkt(2, []byte(""))) xsend(c2, mkpkt(2, []byte("")))
println("333")
wg = WorkGroup() // replies must be coming in reverse order
echoWait := func(c *Conn, msgCode uint16) func() { xechoWait := func(c *Conn, msgCode uint16) {
return func() { pkt := xrecv(c)
pkt := xrecv(c) xverifyPkt(pkt, c.connId, msgCode, []byte(""))
xverifyPkt(pkt, c.connId, msgCode, []byte(""))
}
} }
wg.Gox(echoWait(c1, 1)) println("aaa")
wg.Gox(echoWait(c2, 2)) xechoWait(c2, 2)
xwait(wg) println("bbb")
xechoWait(c1, 1)
println("ccc")
xclose(c1) xclose(c1)
xclose(c2) xclose(c2)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment