From dde3bed148ea90cd505bf6fb67b08719ebc8933a Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 18 May 2023 00:08:21 +0300 Subject: refactor --- internal/config.go | 10 ++-------- internal/run.go | 4 +--- internal/tcpserver.go | 4 ++-- 3 files changed, 5 insertions(+), 13 deletions(-) (limited to 'internal') diff --git a/internal/config.go b/internal/config.go index c3fff26..1ed0bd8 100644 --- a/internal/config.go +++ b/internal/config.go @@ -6,15 +6,9 @@ import ( "os" ) -type participant struct { - Hostname string - Port int -} - type config struct { - Hostname string - Port int - Participants []participant + Address string + Participants []string } func newConfig(configFile string) (config, error) { diff --git a/internal/run.go b/internal/run.go index 4904d33..4165f50 100644 --- a/internal/run.go +++ b/internal/run.go @@ -2,7 +2,6 @@ package internal import ( "context" - "fmt" "log" ) @@ -25,8 +24,7 @@ func Run(ctx context.Context, configFile string) { } }() - address := fmt.Sprintf("%s:%d", config.Hostname, config.Port) - if err := startTcpServer(ctx, address, ch); err != nil { + if err := startTcpServer(ctx, config.Address, ch); err != nil { panic(err) } } diff --git a/internal/tcpserver.go b/internal/tcpserver.go index f264d9e..c65fb0b 100644 --- a/internal/tcpserver.go +++ b/internal/tcpserver.go @@ -28,7 +28,6 @@ func startTcpServer(ctx context.Context, address string, ch chan<- vote) error { log.Printf("Client connected: %s\n", conn.RemoteAddr().String()) - // Handle the connection in a new goroutine go handleConnection(ctx, conn, ch) } } @@ -36,6 +35,7 @@ func startTcpServer(ctx context.Context, address string, ch chan<- vote) error { func handleConnection(ctx context.Context, conn net.Conn, ch chan<- vote) { defer conn.Close() remoteAddr := conn.RemoteAddr().String() + // TODO: Reject remote connection when not in participant list! log.Printf("Client %s connected\n", remoteAddr) reader := bufio.NewReader(conn) @@ -48,7 +48,7 @@ func handleConnection(ctx context.Context, conn net.Conn, ch chan<- vote) { message, err := reader.ReadString('\n') if err != nil { log.Printf("Client %s disconnected: %s\n", remoteAddr, err.Error()) - break + return } log.Printf("Received message from %s: %s", remoteAddr, message) -- cgit v1.2.3