From 8158edf88c66af9d798f300da7c045c57921c1aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Wed, 8 Nov 2017 22:18:23 +0200 Subject: [PATCH] Refactoring to use Client and Stack --- server/server/commands.go | 27 ++++++++++++++++++--------- server/server/connection.go | 2 +- server/server/errors.go | 1 + server/server/parser.go | 8 ++++---- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/server/server/commands.go b/server/server/commands.go index 003cc15..74a10d0 100644 --- a/server/server/commands.go +++ b/server/server/commands.go @@ -4,17 +4,21 @@ import ( "bytes" "io/ioutil" "log" - "net" "os" "strconv" "strings" ) // GetFile sends the file to the client and returns true if it succeeds and false otherwise. -func GetFile(c net.Conn, path string) (int, error) { +func GetFile(c Client, path string) (int, error) { fileName := sanitizeFilePath(path) - file, err := os.Open(BasePath + fileName) + stack, ok := c.Stack().(*StringStack) + if !ok { + return 0, ErrStackCast + } + + file, err := os.Open(MakePathFromStringStack(stack) + fileName) if err != nil { log.Println(err) return 0, err @@ -26,7 +30,7 @@ func GetFile(c net.Conn, path string) (int, error) { return 0, err } - n, err := c.Write(data) + n, err := c.Connection().Write(data) if err != nil { log.Println(err) return 0, err @@ -61,8 +65,13 @@ func sanitizeFilePath(path string) string { } // ListFiles list the files from path and sends them to the connection -func ListFiles(c net.Conn) error { - files, err := ioutil.ReadDir(BasePath) +func ListFiles(c Client) error { + stack, ok := c.Stack().(*StringStack) + if !ok { + return ErrStackCast + } + + files, err := ioutil.ReadDir(MakePathFromStringStack(stack)) if err != nil { return err } @@ -73,7 +82,7 @@ func ListFiles(c net.Conn) error { strconv.FormatInt(f.Size(), 10) + " " + f.ModTime().String() + " " + string(f.Name()) + " " + "\n") } - _, err = c.Write(buffer.Bytes()) + _, err = c.Connection().Write(buffer.Bytes()) if err != nil { return err } @@ -81,7 +90,7 @@ func ListFiles(c net.Conn) error { return nil } -func ShowHelp(c net.Conn) error { +func ShowHelp(c Client) error { var helpText = ` The available commands are: get - Download the requested filename. @@ -89,7 +98,7 @@ ls - List the files in the current directory. clear - Clear the screen. exit - Close the connection with the server. ` - _, err := c.Write([]byte(helpText)) + _, err := c.Connection().Write([]byte(helpText)) return err } diff --git a/server/server/connection.go b/server/server/connection.go index f43f84c..1c36664 100644 --- a/server/server/connection.go +++ b/server/server/connection.go @@ -13,7 +13,7 @@ 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. + Stack() Stack // Returns the underlying String Stack. } // FTPClient represents a FTPClient connection, it holds a root cage and the underlying connection. diff --git a/server/server/errors.go b/server/server/errors.go index dd8443b..74b7473 100644 --- a/server/server/errors.go +++ b/server/server/errors.go @@ -36,6 +36,7 @@ var ( StackInvalidTypeError = StackError{"InvalidTypeError", errors.New("invalid item type for the Stack")} StackOverflowError = StackError{"StackOverflowError", errors.New("stack capacity exceeded")} StackUnderflowError = StackError{"StackUnderflowError", errors.New("stack is empty")} + ErrStackCast = StackError{"StackCastError", errors.New("stack can't be casted to selected type")} ) // PathErrors diff --git a/server/server/parser.go b/server/server/parser.go index 6cee415..8420eec 100644 --- a/server/server/parser.go +++ b/server/server/parser.go @@ -32,7 +32,7 @@ func ProcessInput(c Client, text string) error { } // Get the file - _, err = GetFile(c.Connection(), commands[1]) + _, err = GetFile(c, commands[1]) if err != nil { return &InputError{thisCommand, err} } @@ -42,7 +42,7 @@ func ProcessInput(c Client, text string) error { return &InputError{thisCommand, err} } - err = ListFiles(c.Connection()) + err = ListFiles(c) if err != nil { return &InputError{thisCommand, err} } @@ -64,7 +64,7 @@ func ProcessInput(c Client, text string) error { return &InputError{thisCommand, err} } - err = ShowHelp(c.Connection()) + err = ShowHelp(c) if err != nil { return &InputError{thisCommand, err} } @@ -74,7 +74,7 @@ func ProcessInput(c Client, text string) error { return &InputError{thisCommand, err} } - c.Connection().Close() + c.Disconnect() default: return &InputError{thisCommand, InputInvalidCommand} }