From 73eb91dce35be530c593d07b3f84782ec98e92b1 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Fri, 29 Oct 2021 11:01:34 +0300 Subject: [PATCH] Bolerplate code for node --- fs/dir_test.go | 5 +++ fs/file.go | 17 ++++++++ fs/file_test.go | 29 +++++++++++-- node/node.go | 49 ++++++++++++++++++++++ node/noder.go | 8 ++-- node/options.go | 23 ++++++++++ testfiles/{testdir2 => testdir3}/testfile4 | 0 7 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 node/options.go rename testfiles/{testdir2 => testdir3}/testfile4 (100%) diff --git a/fs/dir_test.go b/fs/dir_test.go index 155ea1e..d6bc0b5 100644 --- a/fs/dir_test.go +++ b/fs/dir_test.go @@ -23,4 +23,9 @@ func Test_GetDirRecursive(t *testing.T) { if len(dir.Directories) != expectedAmountOfUpperDirectories { t.Fatalf("GetDir error: expected to have %d inner directories; got %d", expectedAmountOfUpperDirectories, len(dir.Directories)) } + + innerDir1 := dir.Directories[0] + if innerDir1 == nil || innerDir1.Name != "testdir" { + t.Fatalf("GetDir error: expected to have the first inner directory to be \"%s\"; got \"%s\"", "testdir", innerDir1.Name) + } } diff --git a/fs/file.go b/fs/file.go index 1e7cd79..e58b2e0 100644 --- a/fs/file.go +++ b/fs/file.go @@ -12,10 +12,15 @@ type File struct { Path string ParentPath string Size uint64 + Handler *os.File } var ErrorNotFile error = fmt.Errorf("not a file") +// Get general information about a file with the +// future ability to open it. +// NOTE that Handler field is nil BY DEFAULT until you +// manually call a (file *File) Open() function to open it ! func GetFile(path string) (*File, error) { absPath, err := filepath.Abs(path) if err != nil { @@ -37,7 +42,19 @@ func GetFile(path string) (*File, error) { Path: absPath, ParentPath: filepath.Dir(absPath), Size: uint64(stats.Size()), + Handler: nil, } return &file, nil } + +// Opens file for read/write operations +func (file *File) Open() error { + handler, err := os.OpenFile(file.Path, os.O_RDWR, os.ModePerm) + if err != nil { + return err + } + file.Handler = handler + + return nil +} diff --git a/fs/file_test.go b/fs/file_test.go index 9dbd7de..40ffc04 100644 --- a/fs/file_test.go +++ b/fs/file_test.go @@ -1,6 +1,9 @@ package fs -import "testing" +import ( + "io" + "testing" +) func Test_GetFile(t *testing.T) { filepath := "../testfiles/testfile.txt" @@ -10,7 +13,27 @@ func Test_GetFile(t *testing.T) { t.Fatalf("GetFile error: %s", err) } - if file.Name != "testfile.txt" { - t.Fatalf("GetFile error: filenames do not match") + expectedFilename := "testfile.txt" + if file.Name != expectedFilename { + t.Fatalf("GetFile error: filenames do not match: expected filename to be %s; got %s", expectedFilename, file.Name) + } +} + +func Test_GetFileOpen(t *testing.T) { + filepath := "../testfiles/testfile.txt" + + file, err := GetFile(filepath) + if err != nil { + t.Fatalf("GetFile error: %s", err) + } + + err = file.Open() + if err != nil { + t.Fatalf("GetFile error: could not open file: %s", err) + } + + _, err = io.ReadAll(file.Handler) + if err != nil { + t.Fatalf("GetFile error: could not read from file: %s", err) } } diff --git a/node/node.go b/node/node.go index 3787b91..2293141 100644 --- a/node/node.go +++ b/node/node.go @@ -1,7 +1,56 @@ package node +import ( + "net" + + "github.com/Unbewohnte/ftu/protocol" +) + +type NodeInnerStates struct { + Connected bool + InTransfer bool + IsWaiting bool + Stopped bool +} + +type Security struct { + EncryptionKey []byte +} // Server and a client in one type ! type Node struct { + conn net.Conn + packetPipe chan []protocol.Packet + State *NodeInnerStates + Security *Security +} + +// Creates a new either a server-side or client-side node +func NewNode(options *NodeOptions) (*Node, error) { + + node := Node{} + return &node, nil +} + +func (node *Node) Connect(addr string, port uint) error { + return nil +} + +func (node *Node) Disconnect() error { + if node.State.Connected { + err := node.conn.Close() + if err != nil { + return err + } + } + return nil +} + +func (node *Node) Send(packet protocol.Packet) error { + return nil +} +func (node *Node) Listen() error { + return nil } + diff --git a/node/noder.go b/node/noder.go index 557d89f..781cb87 100644 --- a/node/noder.go +++ b/node/noder.go @@ -1,9 +1,11 @@ package node -// Implementation for sender and receiver node +import "github.com/Unbewohnte/ftu/protocol" + +// Implementation for sender and receiver nodes. [I`ll Probably remove it later. I don`t see the use-cases rn] type Noder interface { Connect(addr string, port uint) error Disconnect() error - Listen(dataPipe chan []byte) - Send(data []byte) error + Listen(packetPipe chan protocol.Packet) + Send(packet protocol.Packet) error } diff --git a/node/options.go b/node/options.go new file mode 100644 index 0000000..d7cda55 --- /dev/null +++ b/node/options.go @@ -0,0 +1,23 @@ +package node + +import ( + "github.com/Unbewohnte/ftu/fs" + "github.com/Unbewohnte/ftu/protocol" +) + +type ServerSideNodeOptions struct { + ServingDirectory *fs.Directory // Can be set to nil + ServingFile *fs.File // Can be set to nil +} + +type ClientSideNodeOptions struct { + DownloadsFolder *fs.Directory // Must be set during the Node creation, even if it will be changed afterwards +} + +// Options to configure the node +type NodeOptions struct { + WorkingPort uint + PacketPipe chan protocol.Packet + ServerSide *ServerSideNodeOptions + ClientSide *ClientSideNodeOptions +} diff --git a/testfiles/testdir2/testfile4 b/testfiles/testdir3/testfile4 similarity index 100% rename from testfiles/testdir2/testfile4 rename to testfiles/testdir3/testfile4