Refactoring to use Client and Stack

This commit is contained in:
Denis-Cosmin Nutiu 2017-11-08 22:18:23 +02:00
parent 95cd8f17b1
commit 8158edf88c
4 changed files with 24 additions and 14 deletions

View file

@ -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 <filename> - 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
}

View file

@ -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.

View file

@ -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

View file

@ -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}
}