-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from ConsenSys/dev
Dev
- Loading branch information
Showing
110 changed files
with
4,512 additions
and
560 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package network | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"sync" | ||
|
||
"github.com/ConsenSys/handel" | ||
) | ||
|
||
// CounterEncoding is a wrapper around an Encoding that can report how many | ||
// bytes sent and received and implements the monitor.Counter interface | ||
type CounterEncoding struct { | ||
*sync.RWMutex | ||
Encoding | ||
sent int // bytes sent | ||
rcvd int // bytes received | ||
} | ||
|
||
// NewCounterEncoding returns an Encoding that implements the monitor.Counter | ||
// interface | ||
func NewCounterEncoding(e Encoding) *CounterEncoding { | ||
return &CounterEncoding{Encoding: e, RWMutex: new(sync.RWMutex)} | ||
} | ||
|
||
// Encode implements the Encoding interface | ||
func (c *CounterEncoding) Encode(p *handel.Packet, w io.Writer) error { | ||
var b bytes.Buffer | ||
combined := io.MultiWriter(w, &b) | ||
if err := c.Encoding.Encode(p, combined); err != nil { | ||
return err | ||
} | ||
|
||
c.Lock() | ||
c.sent += b.Len() | ||
c.Unlock() | ||
return nil | ||
} | ||
|
||
// Decode implements the Encoding interface | ||
func (c *CounterEncoding) Decode(r io.Reader) (*handel.Packet, error) { | ||
var b bytes.Buffer | ||
var tee = io.TeeReader(r, &b) | ||
p, err := c.Encoding.Decode(tee) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c.Lock() | ||
c.rcvd += b.Len() | ||
c.Unlock() | ||
return p, nil | ||
} | ||
|
||
// Values implements the monitor.Counter interface | ||
func (c *CounterEncoding) Values() map[string]float64 { | ||
c.RLock() | ||
defer c.RUnlock() | ||
return map[string]float64{ | ||
"sentBytes": float64(c.sent), | ||
"rcvdBytes": float64(c.rcvd), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package network | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/ConsenSys/handel" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCounterEncoding(t *testing.T) { | ||
var medium bytes.Buffer | ||
|
||
actual := NewGOBEncoding() | ||
counter := NewCounterEncoding(actual) | ||
|
||
require.True(t, counter.Values()["sentBytes"] == 0.0) | ||
require.True(t, counter.Values()["rcvdBytes"] == 0.0) | ||
|
||
toSend := &handel.Packet{ | ||
Origin: 156, | ||
Level: 8, | ||
MultiSig: []byte("History repeats itself, first as tragedy, second as farce."), | ||
} | ||
|
||
require.NoError(t, counter.Encode(toSend, &medium)) | ||
require.True(t, counter.Values()["sentBytes"] > 0.0) | ||
|
||
read, err := counter.Decode(&medium) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, toSend, read) | ||
require.True(t, counter.Values()["rcvdBytes"] > 0.0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.