Browse Source

Bolerplate code for node

main
Unbewohnte 3 years ago
parent
commit
73eb91dce3
  1. 5
      fs/dir_test.go
  2. 17
      fs/file.go
  3. 29
      fs/file_test.go
  4. 49
      node/node.go
  5. 8
      node/noder.go
  6. 23
      node/options.go
  7. 0
      testfiles/testdir3/testfile4

5
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)
}
}

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

29
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)
}
}

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

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

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

0
testfiles/testdir2/testfile4 → testfiles/testdir3/testfile4

Loading…
Cancel
Save