Skip to content

Commit

Permalink
Fixed some dmsg.Server logic as well as made Travis happy.
Browse files Browse the repository at this point in the history
  • Loading branch information
林志宇 committed Jun 19, 2019
1 parent 190ac58 commit 35b4f9e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cmd/messaging-server/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ var rootCmd = &cobra.Command{
}

// Start
srv, err := dmsg.NewServer(conf.PubKey, conf.SecKey, l, client.NewHTTP(conf.Discovery))
srv, err := dmsg.NewServer(conf.PubKey, conf.SecKey, conf.PublicAddress, l, client.NewHTTP(conf.Discovery))
if err != nil {
logger.Fatalf("Error creating DMSG server instance: %v", err)
}
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ require (
github.com/spf13/pflag v1.0.3 // indirect
github.com/stretchr/testify v1.3.0
go.etcd.io/bbolt v1.3.2
golang.org/x/crypto v0.0.0-20190418165655-df01cb2cc480
golang.org/x/net v0.0.0-20190419010253-1f3472d942ba
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b
golang.org/x/sys v0.0.0-20190618155005-516e3c20635f // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20190618233249-04b924abaa25 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,32 @@ go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190418165655-df01cb2cc480 h1:O5YqonU5IWby+w98jVUG9h7zlCWCcH4RHyPVReBmhzk=
golang.org/x/crypto v0.0.0-20190418165655-df01cb2cc480/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443 h1:IcSOAf4PyMp3U3XbIEj1/xJ2BjNN2jWv7JoyOsMxXUU=
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190419010253-1f3472d942ba h1:h0zCzEL5UW1mERvwTN6AXcc75PpLkY6OcReia6Dq1BM=
golang.org/x/net v0.0.0-20190419010253-1f3472d942ba/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b h1:lkjdUzSyJ5P1+eal9fxXX9Xg2BTfswsonKUse48C0uE=
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e h1:nFYrTHrdrAOpShe27kaFHjsqYSEQ0KWqdWLu3xuZJts=
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190618155005-516e3c20635f h1:dHNZYIYdq2QuU6w73vZ/DzesPbVlZVYZTtTZmrnsbQ8=
golang.org/x/sys v0.0.0-20190618155005-516e3c20635f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190618233249-04b924abaa25 h1:waeaOEG3M5B3aL+j+hqXDXpBOqBEYHVNApmp5na1jwQ=
golang.org/x/tools v0.0.0-20190618233249-04b924abaa25/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
Expand Down
5 changes: 2 additions & 3 deletions pkg/dmsg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ func NewClientConn(log *logging.Logger, conn net.Conn, local, remote cipher.PubK
return cc
}

func (c *ClientConn) PK() cipher.PubKey {
return c.remoteSrv
}
// RemotePK returns the remote Server's PK that the ClientConn is connected to.
func (c *ClientConn) RemotePK() cipher.PubKey { return c.remoteSrv }

func (c *ClientConn) getNextInitID(ctx context.Context) (uint16, error) {
for {
Expand Down
9 changes: 6 additions & 3 deletions pkg/dmsg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/skycoin/skywire/pkg/messaging-discovery/client"
)

// ErrListenerAlreadyWrappedToNoise occurs when the provided net.Listener is already wrapped with noise.Listener
var ErrListenerAlreadyWrappedToNoise = errors.New("listener is already wrapped to *noise.Listener")

// NextConn provides information on the next connection.
Expand Down Expand Up @@ -222,8 +223,10 @@ type Server struct {
}

// NewServer creates a new dms_server.
func NewServer(pk cipher.PubKey, sk cipher.SecKey, l net.Listener, dc client.APIClient) (*Server, error) {
addr := l.Addr().String()
func NewServer(pk cipher.PubKey, sk cipher.SecKey, addr string, l net.Listener, dc client.APIClient) (*Server, error) {
if addr == "" {
addr = l.Addr().String()
}

if _, ok := l.(*noise.Listener); ok {
return nil, ErrListenerAlreadyWrappedToNoise
Expand Down Expand Up @@ -295,7 +298,7 @@ func (s *Server) Close() (err error) {
return nil
}

// ListenAndServe serves the dms_server.
// Serve serves the dmsg_server.
func (s *Server) Serve() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
45 changes: 25 additions & 20 deletions pkg/dmsg/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"testing"
"time"

"github.com/skycoin/skywire/internal/noise"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/nettest"

"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/skywire/internal/noise"
"github.com/skycoin/skywire/pkg/cipher"
"github.com/skycoin/skywire/pkg/messaging-discovery/client"
"github.com/skycoin/skywire/pkg/transport"
Expand All @@ -39,14 +40,14 @@ func TestServerConn_AddNext(t *testing.T) {
fullNextConns[i] = &NextConn{}
}

timeoutCtx, _ := context.WithTimeout(context.Background(), 10*time.Millisecond)
timeoutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()

cases := []struct {
name string
conn *ServerConn
ctx context.Context
nextConnID uint16
want want
name string
conn *ServerConn
ctx context.Context
want want
}{
{
name: "ok",
Expand Down Expand Up @@ -148,12 +149,16 @@ func TestNewServer(t *testing.T) {
l, err := net.Listen("tcp", "")
require.NoError(t, err)

// must fail on already wrapped listener
wrappedL := noise.WrapListener(l, sPK, sSK, false, noise.HandshakeXK)
s, err := NewServer(sPK, sSK, wrappedL, dc)
require.Equal(t, ErrListenerAlreadyWrappedToNoise, err)
// When calling 'NewServer', if the provided net.Listener is already a noise.Listener,
// An error should be returned.
t.Run("fail_on_wrapped_listener", func(t *testing.T) {
wrappedL := noise.WrapListener(l, sPK, sSK, false, noise.HandshakeXK)
s, err := NewServer(sPK, sSK, "", wrappedL, dc)
assert.Equal(t, ErrListenerAlreadyWrappedToNoise, err)
assert.Nil(t, s)
})

s, err = NewServer(sPK, sSK, l, dc)
s, err := NewServer(sPK, sSK, "", l, dc)
require.NoError(t, err)

go s.Serve() //nolint:errcheck
Expand All @@ -172,7 +177,7 @@ func TestServer_Serve(t *testing.T) {
l, err := nettest.NewLocalListener("tcp")
require.NoError(t, err)

s, err := NewServer(sPK, sSK, l, dc)
s, err := NewServer(sPK, sSK, "", l, dc)
require.NoError(t, err)

go s.Serve() //nolint:errcheck
Expand Down Expand Up @@ -223,12 +228,12 @@ func TestServer_Serve(t *testing.T) {
// must have a ClientConn
aClientConn, ok := a.getConn(sPK)
require.Equal(t, true, ok)
require.Equal(t, sPK, aClientConn.PK())
require.Equal(t, sPK, aClientConn.RemotePK())

// must have a ClientConn
bClientConn, ok := b.getConn(sPK)
require.Equal(t, true, ok)
require.Equal(t, sPK, bClientConn.PK())
require.Equal(t, sPK, bClientConn.RemotePK())

// check whether nextConn's contents are as must be
bClientConn.mx.RLock()
Expand Down Expand Up @@ -427,7 +432,7 @@ func TestServer_Serve(t *testing.T) {
// get and check initiator's ClientConn
initiatorClientConn, ok := initiator.getConn(sPK)
require.Equal(t, true, ok)
require.Equal(t, sPK, initiatorClientConn.PK())
require.Equal(t, sPK, initiatorClientConn.RemotePK())

remote := remotes[pickedRemotes[i]]

Expand All @@ -439,7 +444,7 @@ func TestServer_Serve(t *testing.T) {
// get and check remote's ClientConn
remoteClientConn, ok := remote.getConn(sPK)
require.Equal(t, true, ok)
require.Equal(t, sPK, remoteClientConn.PK())
require.Equal(t, sPK, remoteClientConn.RemotePK())

// get initiator's nextConn
initiatorClientConn.mx.RLock()
Expand Down Expand Up @@ -522,7 +527,7 @@ func TestServer_Serve(t *testing.T) {
for i, remote := range remotes {
require.NoError(t, testWithTimeout(10*time.Second, func() error {
if remote.connCount() != 0 {
return errors.New(fmt.Sprintf("remotes[%v].conns is not empty", i))
return fmt.Errorf("remotes[%v].conns is not empty", i)
}

return nil
Expand All @@ -532,7 +537,7 @@ func TestServer_Serve(t *testing.T) {
for i, initiator := range initiators {
require.NoError(t, testWithTimeout(10*time.Second, func() error {
if initiator.connCount() != 0 {
return errors.New(fmt.Sprintf("initiators[%v].conns is not empty", i))
return fmt.Errorf("initiators[%v].conns is not empty", i)
}

return nil
Expand Down Expand Up @@ -669,7 +674,7 @@ func TestNewClient(t *testing.T) {

log.Println(l.Addr().String())

s, err := NewServer(sPK, sSK, l, dc)
s, err := NewServer(sPK, sSK, "", l, dc)
require.NoError(t, err)

go s.Serve() //nolint:errcheck
Expand Down

0 comments on commit 35b4f9e

Please sign in to comment.