Provide Links() method in Tunnel
This commit is contained in:
		| @@ -911,6 +911,19 @@ func (t *tun) Listen(channel string) (Listener, error) { | |||||||
| 	return tl, nil | 	return tl, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (t *tun) Links() []Link { | ||||||
|  | 	t.RLock() | ||||||
|  | 	defer t.RUnlock() | ||||||
|  |  | ||||||
|  | 	var links []Link | ||||||
|  |  | ||||||
|  | 	for _, link := range t.links { | ||||||
|  | 		links = append(links, link) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return links | ||||||
|  | } | ||||||
|  |  | ||||||
| func (t *tun) String() string { | func (t *tun) String() string { | ||||||
| 	return "mucp" | 	return "mucp" | ||||||
| } | } | ||||||
|   | |||||||
| @@ -81,14 +81,24 @@ func (l *link) run() { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func (l *link) Close() { | func (l *link) Id() string { | ||||||
|  | 	l.RLock() | ||||||
|  | 	defer l.RUnlock() | ||||||
|  |  | ||||||
|  | 	return l.id | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (l *link) Close() error { | ||||||
| 	l.Lock() | 	l.Lock() | ||||||
| 	defer l.Unlock() | 	defer l.Unlock() | ||||||
|  |  | ||||||
| 	select { | 	select { | ||||||
| 	case <-l.closed: | 	case <-l.closed: | ||||||
| 		return | 		return nil | ||||||
| 	default: | 	default: | ||||||
| 		close(l.closed) | 		close(l.closed) | ||||||
|  | 		return l.Socket.Close() | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
| } | } | ||||||
|   | |||||||
| @@ -31,10 +31,20 @@ type Tunnel interface { | |||||||
| 	Dial(channel string, opts ...DialOption) (Session, error) | 	Dial(channel string, opts ...DialOption) (Session, error) | ||||||
| 	// Accept connections on a channel | 	// Accept connections on a channel | ||||||
| 	Listen(channel string) (Listener, error) | 	Listen(channel string) (Listener, error) | ||||||
|  | 	// All the links the tunnel is connected to | ||||||
|  | 	Links() []Link | ||||||
| 	// Name of the tunnel implementation | 	// Name of the tunnel implementation | ||||||
| 	String() string | 	String() string | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // Link represents internal links to the tunnel | ||||||
|  | type Link interface { | ||||||
|  | 	// The id of the link | ||||||
|  | 	Id() string | ||||||
|  | 	// honours transport socket | ||||||
|  | 	transport.Socket | ||||||
|  | } | ||||||
|  |  | ||||||
| // The listener provides similar constructs to the transport.Listener | // The listener provides similar constructs to the transport.Listener | ||||||
| type Listener interface { | type Listener interface { | ||||||
| 	Accept() (Session, error) | 	Accept() (Session, error) | ||||||
|   | |||||||
| @@ -187,30 +187,15 @@ func testBrokenTunAccept(t *testing.T, tun Tunnel, wait chan bool, wg *sync.Wait | |||||||
| 	if err := c.Recv(m); err != nil { | 	if err := c.Recv(m); err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| 	tun.Close() |  | ||||||
|  |  | ||||||
| 	// re-start tunnel | 	// close all the links | ||||||
| 	err = tun.Connect() | 	for _, link := range tun.Links() { | ||||||
| 	if err != nil { | 		link.Close() | ||||||
| 		t.Fatal(err) |  | ||||||
| 	} |  | ||||||
| 	defer tun.Close() |  | ||||||
|  |  | ||||||
| 	// listen on some virtual address |  | ||||||
| 	tl, err = tun.Listen("test-tunnel") |  | ||||||
| 	if err != nil { |  | ||||||
| 		t.Fatal(err) |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// receiver ready; notify sender | 	// receiver ready; notify sender | ||||||
| 	wait <- true | 	wait <- true | ||||||
|  |  | ||||||
| 	// accept a connection |  | ||||||
| 	c, err = tl.Accept() |  | ||||||
| 	if err != nil { |  | ||||||
| 		t.Fatal(err) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	// accept the message | 	// accept the message | ||||||
| 	m = new(transport.Message) | 	m = new(transport.Message) | ||||||
| 	if err := c.Recv(m); err != nil { | 	if err := c.Recv(m); err != nil { | ||||||
| @@ -279,6 +264,7 @@ func TestReconnectTunnel(t *testing.T) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
|  | 	defer tunB.Close() | ||||||
|  |  | ||||||
| 	// we manually override the tunnel.ReconnectTime value here | 	// we manually override the tunnel.ReconnectTime value here | ||||||
| 	// this is so that we make the reconnects faster than the default 5s | 	// this is so that we make the reconnects faster than the default 5s | ||||||
| @@ -289,6 +275,7 @@ func TestReconnectTunnel(t *testing.T) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
|  | 	defer tunA.Close() | ||||||
|  |  | ||||||
| 	wait := make(chan bool) | 	wait := make(chan bool) | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user