From 95cd8f17b1ffec58c21aee80cc1d8cf08599c443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Wed, 8 Nov 2017 15:31:26 +0200 Subject: [PATCH] Fixing the use of the Client interface & ditching the ClientStack interface --- server/main.go | 2 +- server/server/connection.go | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/server/main.go b/server/main.go index 9c72920..79dfaef 100644 --- a/server/main.go +++ b/server/main.go @@ -25,7 +25,7 @@ func main() { } client := server.FTPClient{} - client.SetStack(new(server.StringStack)) + client.SetStack(server.MakeStringStack(30)) client.SetConnection(conn) go server.HandleConnection(&client) diff --git a/server/server/connection.go b/server/server/connection.go index 0e50948..f43f84c 100644 --- a/server/server/connection.go +++ b/server/server/connection.go @@ -2,21 +2,18 @@ package server import ( "bufio" + "fmt" "io" "log" "net" ) -// ClientStack interface holds the function needed to work with a stack. -type ClientStack interface { - Stack() Stack // Returns the underlying Stack. -} - // Client interface provides the blueprints for the Client that is used by the server. type Client interface { Connection() net.Conn // Connection returns the connection stream. SetConnection(conn net.Conn) // SetConnection sets the connection for the client. Disconnect() // Disconnect closes the Client's connections and clears up resources. + Stack() Stack // Returns the underlying Stack. } // FTPClient represents a FTPClient connection, it holds a root cage and the underlying connection. @@ -52,6 +49,17 @@ func (c *FTPClient) Disconnect() { func HandleConnection(client Client) { defer client.Disconnect() + defer func() { + if r := recover(); r != nil { + log.Println("PANIC: ", r) + + recoveryError, ok := r.(string) + if ok { + io.WriteString(client.Connection(), fmt.Sprintf("PANIC: %s", recoveryError)) + } + } + }() + io.WriteString(client.Connection(), "Hello and welcome to simple ftp\n") log.Println(client.Connection().RemoteAddr(), "has connected.")