[tor-commits] [snowflake/master] webRTCConn has better seam with BytesLogger interface

serene at torproject.org serene at torproject.org
Tue May 24 19:56:48 UTC 2016


commit b2c9fcac5e9d560e4e8165ef2f1cf8f2cf67f967
Author: Serene Han <keroserene+git at gmail.com>
Date:   Tue May 24 12:56:09 2016 -0700

    webRTCConn has better seam with BytesLogger interface
---
 client/client_test.go |  6 +-----
 client/snowflake.go   | 14 +++++++++++++-
 client/util.go        | 23 +++++++++++++++++++----
 client/webrtc.go      | 16 ++++++----------
 4 files changed, 39 insertions(+), 20 deletions(-)

diff --git a/client/client_test.go b/client/client_test.go
index 93e0422..1ccf206 100644
--- a/client/client_test.go
+++ b/client/client_test.go
@@ -109,11 +109,7 @@ func TestSnowflakeClient(t *testing.T) {
 		})
 
 		Convey("WebRTC Connection", func() {
-			c := new(webRTCConn)
-			c.BytesInfo = &BytesInfo{
-				inboundChan: make(chan int), outboundChan: make(chan int),
-				inbound: 0, outbound: 0, inEvents: 0, outEvents: 0,
-			}
+			c := NewWebRTCConnection(nil, nil)
 			So(c.buffer.Bytes(), ShouldEqual, nil)
 
 			Convey("Can construct a WebRTCConn", func() {
diff --git a/client/snowflake.go b/client/snowflake.go
index 61864ca..464420c 100644
--- a/client/snowflake.go
+++ b/client/snowflake.go
@@ -66,6 +66,7 @@ type SnowflakeChannel interface {
 // updated once multiplexed transport on a single circuit is available.
 type Peers struct {
 	Tongue
+	BytesLogger
 
 	snowflakeChan chan *webRTCConn
 	current       *webRTCConn
@@ -88,6 +89,7 @@ func (p *Peers) FindSnowflake() (*webRTCConn, error) {
 		return nil, errors.New(s)
 	}
 	connection, err := p.Catch()
+	connection.BytesLogger = p.BytesLogger
 	if err != nil {
 		return nil, err
 	}
@@ -96,7 +98,11 @@ func (p *Peers) FindSnowflake() (*webRTCConn, error) {
 
 // TODO: Needs fixing.
 func (p *Peers) Count() int {
-	return len(p.snowflakeChan)
+	count := 0
+	if p.current != nil {
+		count = 1
+	}
+	return count + len(p.snowflakeChan)
 }
 
 // Close all remote peers.
@@ -257,6 +263,12 @@ func main() {
 	remotes := NewPeers(SnowflakeCapacity)
 	broker := NewBrokerChannel(brokerURL, frontDomain, CreateBrokerTransport())
 
+	remotes.BytesLogger = &BytesSyncLogger{
+		inboundChan: make(chan int, 5), outboundChan: make(chan int, 5),
+		inbound: 0, outbound: 0, inEvents: 0, outEvents: 0,
+	}
+	go remotes.BytesLogger.Log()
+
 	remotes.Tongue = WebRTCDialer{broker}
 	go ConnectLoop(remotes)
 
diff --git a/client/util.go b/client/util.go
index 6a2b6de..73f0f50 100644
--- a/client/util.go
+++ b/client/util.go
@@ -29,7 +29,22 @@ func (i *IceServerList) Set(s string) error {
 	return nil
 }
 
-type BytesInfo struct {
+type BytesLogger interface {
+	Log()
+	AddOutbound(int)
+	AddInbound(int)
+}
+
+// Default BytesLogger does nothing.
+type BytesNullLogger struct{}
+
+func (b BytesNullLogger) Log()                   {}
+func (b BytesNullLogger) AddOutbound(amount int) {}
+func (b BytesNullLogger) AddInbound(amount int)  {}
+
+// BytesSyncLogger uses channels to safely log from multiple sources with output
+// occuring at reasonable intervals.
+type BytesSyncLogger struct {
 	outboundChan chan int
 	inboundChan  chan int
 	outbound     int
@@ -39,7 +54,7 @@ type BytesInfo struct {
 	isLogging    bool
 }
 
-func (b *BytesInfo) Log() {
+func (b *BytesSyncLogger) Log() {
 	b.isLogging = true
 	var amount int
 	output := func() {
@@ -76,14 +91,14 @@ func (b *BytesInfo) Log() {
 	}
 }
 
-func (b *BytesInfo) AddOutbound(amount int) {
+func (b *BytesSyncLogger) AddOutbound(amount int) {
 	if !b.isLogging {
 		return
 	}
 	b.outboundChan <- amount
 }
 
-func (b *BytesInfo) AddInbound(amount int) {
+func (b *BytesSyncLogger) AddInbound(amount int) {
 	if !b.isLogging {
 		return
 	}
diff --git a/client/webrtc.go b/client/webrtc.go
index e01cbf7..a47ac19 100644
--- a/client/webrtc.go
+++ b/client/webrtc.go
@@ -29,7 +29,8 @@ type webRTCConn struct {
 
 	index  int
 	closed bool
-	*BytesInfo
+
+	BytesLogger
 }
 
 func (c *webRTCConn) Read(b []byte) (int, error) {
@@ -38,7 +39,7 @@ func (c *webRTCConn) Read(b []byte) (int, error) {
 
 // Writes bytes out to the snowflake proxy.
 func (c *webRTCConn) Write(b []byte) (int, error) {
-	c.BytesInfo.AddOutbound(len(b))
+	c.BytesLogger.AddOutbound(len(b))
 	if nil == c.snowflake {
 		log.Printf("Buffered %d bytes --> WebRTC", len(b))
 		c.buffer.Write(b)
@@ -99,13 +100,8 @@ func NewWebRTCConnection(config *webrtc.Configuration,
 	connection.errorChannel = make(chan error, 1)
 	connection.reset = make(chan struct{}, 1)
 
-	// TODO: Separate out.
-	// Log every few seconds.
-	connection.BytesInfo = &BytesInfo{
-		inboundChan: make(chan int, 5), outboundChan: make(chan int, 5),
-		inbound: 0, outbound: 0, inEvents: 0, outEvents: 0,
-	}
-	go connection.BytesInfo.Log()
+	// Override with something that's not NullLogger to have real logging.
+	connection.BytesLogger = &BytesNullLogger{}
 
 	// Pipes remain the same even when DataChannel gets switched.
 	connection.recvPipe, connection.writePipe = io.Pipe()
@@ -224,7 +220,7 @@ func (c *webRTCConn) establishDataChannel() error {
 		if len(msg) <= 0 {
 			log.Println("0 length message---")
 		}
-		c.BytesInfo.AddInbound(len(msg))
+		c.BytesLogger.AddInbound(len(msg))
 		n, err := c.writePipe.Write(msg)
 		if err != nil {
 			// TODO: Maybe shouldn't actually close.



More information about the tor-commits mailing list