Skip to content

Commit

Permalink
Move net.Listener arg to NewServer, remove addr arg
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jun 2, 2019
1 parent 9fc80d0 commit dadbdcb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
10 changes: 7 additions & 3 deletions cmd/messaging-server/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,16 @@ var rootCmd = &cobra.Command{

l, err := net.Listen("tcp", conf.LocalAddress)
if err != nil {
log.Fatal(err)
logger.Fatalf("Error listening on %s: %v", conf.LocalAddress, err)
}

// Start
srv := dmsg.NewServer(conf.PubKey, conf.SecKey, conf.PublicAddress, client.NewHTTP(conf.Discovery))
log.Fatal(srv.Serve(l))
srv, err := dmsg.NewServer(conf.PubKey, conf.SecKey, l, client.NewHTTP(conf.Discovery))
if err != nil {
logger.Fatalf("Error creating DMSG server instance: %v", err)
}

log.Fatal(srv.Serve())
},
}

Expand Down
37 changes: 18 additions & 19 deletions pkg/dmsg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/skycoin/skywire/pkg/messaging-discovery/client"
)

var ErrListenerAlreadyWrappedToNoise = errors.New("listener is already wrapped to *noise.Listener")

// NextConn provides information on the next connection.
type NextConn struct {
l *ServerConn
Expand Down Expand Up @@ -201,10 +203,9 @@ func (c *ServerConn) handleRequest(ctx context.Context, getLink getConnFunc, id
type Server struct {
log *logging.Logger

pk cipher.PubKey
sk cipher.SecKey
addr string
dc client.APIClient
pk cipher.PubKey
sk cipher.SecKey
dc client.APIClient

lis net.Listener
conns map[cipher.PubKey]*ServerConn
Expand All @@ -214,15 +215,19 @@ type Server struct {
}

// NewServer creates a new dms_server.
func NewServer(pk cipher.PubKey, sk cipher.SecKey, addr string, dc client.APIClient) *Server {
func NewServer(pk cipher.PubKey, sk cipher.SecKey, l net.Listener, dc client.APIClient) (*Server, error) {
if _, ok := l.(*noise.Listener); ok {
return nil, ErrListenerAlreadyWrappedToNoise
}

return &Server{
log: logging.MustGetLogger("dms_server"),
pk: pk,
sk: sk,
addr: addr,
lis: noise.WrapListener(l, pk, sk, false, noise.HandshakeXK),
dc: dc,
conns: make(map[cipher.PubKey]*ServerConn),
}
}, nil
}

// SetLogger set's the logger.
Expand Down Expand Up @@ -269,24 +274,18 @@ func (s *Server) Close() (err error) {
}

// ListenAndServe serves the dms_server.
//func (s *Server) ListenAndServe(addr string) error {
func (s *Server) Serve(l net.Listener) error {
func (s *Server) Serve() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

/*lis, err := net.Listen("tcp", addr)
if err != nil {
return err
}*/
if err := s.updateDiscEntry(ctx); err != nil {
return fmt.Errorf("updating server's discovery entry failed with: %s", err)
}

s.log.Infof("serving: pk(%s) addr(%s)", s.pk, l.Addr())
l = noise.WrapListener(l, s.pk, s.sk, false, noise.HandshakeXK)
s.lis = l
s.log.Infof("serving: pk(%s) addr(%s)", s.pk, s.lis.Addr())

for {
rawConn, err := l.Accept()
rawConn, err := s.lis.Accept()
if err != nil {
if err == io.ErrUnexpectedEOF {
continue
Expand All @@ -311,12 +310,12 @@ func (s *Server) updateDiscEntry(ctx context.Context) error {
s.log.Info("updating server discovery entry...")
entry, err := s.dc.Entry(ctx, s.pk)
if err != nil {
entry = client.NewServerEntry(s.pk, 0, s.addr, 10)
entry = client.NewServerEntry(s.pk, 0, s.lis.Addr().String(), 10)
if err := entry.Sign(s.sk); err != nil {
return err
}
return s.dc.SetEntry(ctx, entry)
}
entry.Server.Address = s.addr
entry.Server.Address = s.lis.Addr().String()
return s.dc.UpdateEntry(ctx, s.sk, entry)
}
19 changes: 12 additions & 7 deletions pkg/dmsg/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ func TestNewServer(t *testing.T) {
l, err := net.Listen("tcp", "")
require.NoError(t, err)

s := NewServer(sPK, sSK, "", dc)
go s.Serve(l) //nolint:errcheck
s, err := NewServer(sPK, sSK, l, dc)
require.NoError(t, err)

go s.Serve() //nolint:errcheck

time.Sleep(time.Second)

Expand All @@ -41,9 +43,10 @@ func TestServer_ListenAndServe(t *testing.T) {
l, err := net.Listen("tcp", "")
require.NoError(t, err)

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

go s.Serve()
}

// Given two client instances (a & b) and a server instance (s),
Expand All @@ -61,11 +64,13 @@ func TestNewClient(t *testing.T) {

dc := client.NewMock()

l, err := net.Listen("tcp", "")
l, err := net.Listen("tcp", sAddr)
require.NoError(t, err)

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

s := NewServer(sPK, sSK, sAddr, dc)
go s.Serve(l) //nolint:errcheck
go s.Serve() //nolint:errcheck

a := NewClient(aPK, aSK, dc)
a.SetLogger(logging.MustGetLogger("A"))
Expand Down

0 comments on commit dadbdcb

Please sign in to comment.