mock_link.go 1.66 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
package mocknet

import (
Karthik Bala's avatar
Karthik Bala committed
4
	//	"fmt"
Steven Allen's avatar
Steven Allen committed
5
	"io"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6
	"sync"
Karthik Bala's avatar
Karthik Bala committed
7
	"time"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8

Jeromy's avatar
Jeromy committed
9
	inet "github.com/libp2p/go-libp2p-net"
Jeromy's avatar
Jeromy committed
10
	peer "github.com/libp2p/go-libp2p-peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11
12
13
14
15
)

// link implements mocknet.Link
// and, for simplicity, inet.Conn
type link struct {
Karthik Bala's avatar
Karthik Bala committed
16
17
18
19
	mock        *mocknet
	nets        []*peernet
	opts        LinkOptions
	ratelimiter *ratelimiter
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20
21
22
23
24
25
	// this could have addresses on both sides.

	sync.RWMutex
}

func newLink(mn *mocknet, opts LinkOptions) *link {
Karthik Bala's avatar
Karthik Bala committed
26
27
28
29
	l := &link{mock: mn,
		opts:        opts,
		ratelimiter: NewRatelimiter(opts.Bandwidth)}
	return l
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
31
32
33
34
35
}

func (l *link) newConnPair(dialer *peernet) (*conn, *conn) {
	l.RLock()
	defer l.RUnlock()

36
37
	c1 := newConn(l.nets[0], l.nets[1], l, inet.DirOutbound)
	c2 := newConn(l.nets[1], l.nets[0], l, inet.DirInbound)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38
39
40
41
42
43
44
45
46
47
	c1.rconn = c2
	c2.rconn = c1

	if dialer == c1.net {
		return c1, c2
	}
	return c2, c1
}

func (l *link) newStreamPair() (*stream, *stream) {
Steven Allen's avatar
Steven Allen committed
48
49
	ra, wb := io.Pipe()
	rb, wa := io.Pipe()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50

51
52
	sa := NewStream(wa, ra, inet.DirOutbound)
	sb := NewStream(wb, rb, inet.DirInbound)
Steven Allen's avatar
Steven Allen committed
53
	return sa, sb
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
}

func (l *link) Networks() []inet.Network {
	l.RLock()
	defer l.RUnlock()

	cp := make([]inet.Network, len(l.nets))
	for i, n := range l.nets {
		cp[i] = n
	}
	return cp
}

func (l *link) Peers() []peer.ID {
	l.RLock()
	defer l.RUnlock()

	cp := make([]peer.ID, len(l.nets))
	for i, n := range l.nets {
		cp[i] = n.peer
	}
	return cp
}

func (l *link) SetOptions(o LinkOptions) {
	l.opts = o
Karthik Bala's avatar
Karthik Bala committed
80
	l.ratelimiter.UpdateBandwidth(l.opts.Bandwidth)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81
82
83
84
85
}

func (l *link) Options() LinkOptions {
	return l.opts
}
Karthik Bala's avatar
Karthik Bala committed
86
87
88
89
90
91
92
93

func (l *link) GetLatency() time.Duration {
	return l.opts.Latency
}

func (l *link) RateLimit(dataSize int) time.Duration {
	return l.ratelimiter.Limit(dataSize)
}