Skip to content

Commit

Permalink
Rename dmsg.Server's ListenAndServe -> Serve(l net.Listener)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jun 2, 2019
1 parent d127cfb commit 9fc80d0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
8 changes: 7 additions & 1 deletion cmd/messaging-server/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"
"log/syslog"
"net"
"net/http"
"os"

Expand Down Expand Up @@ -71,9 +72,14 @@ var rootCmd = &cobra.Command{
}
}()

l, err := net.Listen("tcp", conf.LocalAddress)
if err != nil {
log.Fatal(err)
}

// Start
srv := dmsg.NewServer(conf.PubKey, conf.SecKey, conf.PublicAddress, client.NewHTTP(conf.Discovery))
log.Fatal(srv.ListenAndServe(conf.LocalAddress))
log.Fatal(srv.Serve(l))
},
}

Expand Down
15 changes: 8 additions & 7 deletions pkg/dmsg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,23 +269,24 @@ func (s *Server) Close() (err error) {
}

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

lis, err := net.Listen("tcp", addr)
/*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, lis.Addr())
lis = noise.WrapListener(lis, s.pk, s.sk, false, noise.HandshakeXK)
s.lis = lis
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
for {
rawConn, err := lis.Accept()
rawConn, err := l.Accept()
if err != nil {
if err == io.ErrUnexpectedEOF {
continue
Expand Down
16 changes: 13 additions & 3 deletions pkg/dmsg/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dmsg
import (
"context"
"fmt"
"net"
"sync"
"testing"
"time"
Expand All @@ -22,8 +23,11 @@ func TestNewServer(t *testing.T) {
sPK, sSK := cipher.GenerateKeyPair()
dc := client.NewMock()

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

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

time.Sleep(time.Second)

Expand All @@ -34,8 +38,11 @@ func TestServer_ListenAndServe(t *testing.T) {
sPK, sSK := cipher.GenerateKeyPair()
dc := client.NewMock()

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

s := NewServer(sPK, sSK, "", dc)
go s.ListenAndServe("")
go s.Serve(l)

}

Expand All @@ -54,8 +61,11 @@ func TestNewClient(t *testing.T) {

dc := client.NewMock()

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

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

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

0 comments on commit 9fc80d0

Please sign in to comment.