Browse Source

Verbose output

main
Unbewohnte 3 years ago
parent
commit
3a40920af5
  1. 2
      Makefile
  2. 10
      src/main.go
  3. 79
      src/node/node.go
  4. 9
      src/node/options.go

2
Makefile

@ -40,7 +40,7 @@ install: all
cp $(EXE_NAME) $(INSTALLATION_DIR) cp $(EXE_NAME) $(INSTALLATION_DIR)
test: test:
cd $(SRC_DIR) && go test ./... ; cd .. cd $(SRC_DIR) && go test ./...
clean: clean:
rm $(EXE_NAME) rm $(EXE_NAME)

10
src/main.go

@ -30,7 +30,7 @@ import (
) )
var ( var (
VERSION string = "v2.1.1" VERSION string = "v2.1.2"
versionInformation string = fmt.Sprintf("ftu %s\n\nCopyright (C) 2021 Kasyanov Nikolay Alexeevich (Unbewohnte (https://unbewohnte.xyz/))\nThis program comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome to redistribute it under certain conditions; type \"ftu -l\" for details.\n", VERSION) versionInformation string = fmt.Sprintf("ftu %s\n\nCopyright (C) 2021 Kasyanov Nikolay Alexeevich (Unbewohnte (https://unbewohnte.xyz/))\nThis program comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome to redistribute it under certain conditions; type \"ftu -l\" for details.\n", VERSION)
@ -43,6 +43,7 @@ var (
ADDRESS *string = flag.String("a", "", "Specifies an address to connect to") ADDRESS *string = flag.String("a", "", "Specifies an address to connect to")
DOWNLOADS_DIR *string = flag.String("d", ".", "Downloads folder") DOWNLOADS_DIR *string = flag.String("d", ".", "Downloads folder")
SEND *string = flag.String("s", "", "Specify a file|directory to send") SEND *string = flag.String("s", "", "Specify a file|directory to send")
VERBOSE *bool = flag.Bool("vp", false, "Turn on/off verbose output")
PRINT_VERSION *bool = flag.Bool("v", false, "Print version information") PRINT_VERSION *bool = flag.Bool("v", false, "Print version information")
PRINT_LICENSE *bool = flag.Bool("l", false, "Print license information") PRINT_LICENSE *bool = flag.Bool("l", false, "Print license information")
@ -59,6 +60,7 @@ func init() {
fmt.Printf("| -a [ip_address|domain_name] address to connect to (cannot be used with -s)\n") fmt.Printf("| -a [ip_address|domain_name] address to connect to (cannot be used with -s)\n")
fmt.Printf("| -d [path_to_directory] where the files will be downloaded to (cannot be used with -s)\n") fmt.Printf("| -d [path_to_directory] where the files will be downloaded to (cannot be used with -s)\n")
fmt.Printf("| -s [path_to_file|directory] to send it (cannot be used with -a)\n") fmt.Printf("| -s [path_to_file|directory] to send it (cannot be used with -a)\n")
fmt.Printf("| -vp [true|false] to turn off|on verbose output")
fmt.Printf("| -l print license information\n") fmt.Printf("| -l print license information\n")
fmt.Printf("| -v print version information\n\n\n") fmt.Printf("| -v print version information\n\n\n")
@ -81,7 +83,6 @@ func init() {
fmt.Printf("| ftu -r -s /home/user/homework/\n") fmt.Printf("| ftu -r -s /home/user/homework/\n")
fmt.Printf("| creates a node that will send every file in the directory !RECUSRIVELY!\n\n\n") fmt.Printf("| creates a node that will send every file in the directory !RECUSRIVELY!\n\n\n")
} }
flag.Parse() flag.Parse()
@ -118,8 +119,9 @@ func init() {
func main() { func main() {
nodeOptions := node.NodeOptions{ nodeOptions := node.NodeOptions{
IsSending: isSending, VerboseOutput: *VERBOSE,
WorkingPort: *PORT, IsSending: isSending,
WorkingPort: *PORT,
ServerSide: &node.ServerSideNodeOptions{ ServerSide: &node.ServerSideNodeOptions{
ServingPath: *SEND, ServingPath: *SEND,
Recursive: *RECUSRIVE, Recursive: *RECUSRIVE,

79
src/node/node.go

@ -77,12 +77,13 @@ type transferInfo struct {
// Sender and receiver in one type ! // Sender and receiver in one type !
type Node struct { type Node struct {
mutex *sync.Mutex verboseOutput bool
packetPipe chan *protocol.Packet // a way to receive incoming packets from another goroutine mutex *sync.Mutex
isSending bool // sending or a receiving node packetPipe chan *protocol.Packet // a way to receive incoming packets from another goroutine
netInfo *netInfo isSending bool // sending or a receiving node
state *nodeInnerstates netInfo *netInfo
transferInfo *transferInfo state *nodeInnerstates
transferInfo *transferInfo
} }
// Creates a new either a sending or receiving node with specified options // Creates a new either a sending or receiving node with specified options
@ -118,9 +119,10 @@ func NewNode(options *NodeOptions) (*Node, error) {
} }
node := Node{ node := Node{
mutex: &sync.Mutex{}, verboseOutput: options.VerboseOutput,
packetPipe: make(chan *protocol.Packet, 100), mutex: &sync.Mutex{},
isSending: options.IsSending, packetPipe: make(chan *protocol.Packet, 100),
isSending: options.IsSending,
netInfo: &netInfo{ netInfo: &netInfo{
Port: options.WorkingPort, Port: options.WorkingPort,
ConnAddr: options.ClientSide.ConnectionAddr, ConnAddr: options.ClientSide.ConnectionAddr,
@ -302,6 +304,7 @@ func (node *Node) Start() {
// mainloop // mainloop
for { for {
if node.state.Stopped { if node.state.Stopped {
fmt.Printf("\n")
node.disconnect() node.disconnect()
break break
} }
@ -309,7 +312,7 @@ func (node *Node) Start() {
// receive incoming packets and decrypt them if necessary // receive incoming packets and decrypt them if necessary
incomingPacket, ok := <-node.packetPipe incomingPacket, ok := <-node.packetPipe
if !ok { if !ok {
fmt.Printf("\nThe connection has been closed unexpectedly") fmt.Printf("\nThe connection has been closed unexpectedly\n")
os.Exit(-1.) os.Exit(-1.)
} }
@ -373,6 +376,10 @@ func (node *Node) Start() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
if node.verboseOutput {
fmt.Printf("\n[File] Sent filepacket for \"%s\"", file.Name)
}
} }
filesInfoDonePacket := protocol.Packet{ filesInfoDonePacket := protocol.Packet{
@ -380,6 +387,10 @@ func (node *Node) Start() {
} }
protocol.SendPacket(node.netInfo.Conn, filesInfoDonePacket) protocol.SendPacket(node.netInfo.Conn, filesInfoDonePacket)
if node.verboseOutput {
fmt.Printf("\n[File] Done sending filepackets")
}
case false: case false:
// send a filepacket of a single file // send a filepacket of a single file
fileToSend.ID = 0 fileToSend.ID = 0
@ -407,11 +418,18 @@ func (node *Node) Start() {
panic(err) panic(err)
} }
if node.verboseOutput {
fmt.Printf("\n[File] Sent filepacket for \"%s\"", fileToSend.Name)
}
if node.verboseOutput {
fmt.Printf("\n[File] Done sending filepackets")
}
} }
case protocol.HeaderReject: case protocol.HeaderReject:
node.state.Stopped = true node.state.Stopped = true
fmt.Printf("\nTransfer rejected. Disconnecting...") fmt.Printf("\nTransfer rejected. Disconnecting...")
case protocol.HeaderDisconnecting: case protocol.HeaderDisconnecting:
@ -431,11 +449,19 @@ func (node *Node) Start() {
node.transferInfo.Sending.FilesToSend = append(node.transferInfo.Sending.FilesToSend[:index], node.transferInfo.Sending.FilesToSend[index+1:]...) node.transferInfo.Sending.FilesToSend = append(node.transferInfo.Sending.FilesToSend[:index], node.transferInfo.Sending.FilesToSend[index+1:]...)
node.transferInfo.Sending.CurrentFileID++ node.transferInfo.Sending.CurrentFileID++
if node.verboseOutput {
fmt.Printf("\n[File] receiver already has \"%s\"", fileToSend.Name)
}
} }
} }
} }
if !node.verboseOutput {
go node.printTransferInfo(time.Second)
}
// Transfer section // Transfer section
if len(node.transferInfo.Sending.FilesToSend) == 0 { if len(node.transferInfo.Sending.FilesToSend) == 0 {
@ -445,7 +471,6 @@ func (node *Node) Start() {
}) })
fmt.Printf("\nTransfer ended successfully") fmt.Printf("\nTransfer ended successfully")
node.state.Stopped = true node.state.Stopped = true
continue continue
@ -454,7 +479,6 @@ func (node *Node) Start() {
// if allowed to transfer and the other node is ready to receive packets - send one piece // if allowed to transfer and the other node is ready to receive packets - send one piece
// and wait for it to be ready again // and wait for it to be ready again
if node.state.AllowedToTransfer && node.transferInfo.Sending.CanSendBytes { if node.state.AllowedToTransfer && node.transferInfo.Sending.CanSendBytes {
// sending a piece of a single file // sending a piece of a single file
// determine an index of a file with current ID // determine an index of a file with current ID
@ -470,6 +494,11 @@ func (node *Node) Start() {
switch err { switch err {
case protocol.ErrorSentAll: case protocol.ErrorSentAll:
// the file has been sent fully // the file has been sent fully
if node.verboseOutput {
fmt.Printf("\n[File] fully sent \"%s\" -- %d bytes", node.transferInfo.Sending.FilesToSend[currentFileIndex].Name, node.transferInfo.Sending.FilesToSend[currentFileIndex].Size)
}
fileIDBuff := new(bytes.Buffer) fileIDBuff := new(bytes.Buffer)
err = binary.Write(fileIDBuff, binary.BigEndian, node.transferInfo.Sending.FilesToSend[currentFileIndex].ID) err = binary.Write(fileIDBuff, binary.BigEndian, node.transferInfo.Sending.FilesToSend[currentFileIndex].ID)
if err != nil { if err != nil {
@ -505,8 +534,6 @@ func (node *Node) Start() {
panic(err) panic(err)
} }
} }
go node.printTransferInfo(time.Second)
} }
case false: case false:
@ -529,6 +556,7 @@ func (node *Node) Start() {
node.mutex.Unlock() node.mutex.Unlock()
if stopped { if stopped {
fmt.Printf("\n")
node.disconnect() node.disconnect()
break break
} }
@ -536,7 +564,7 @@ func (node *Node) Start() {
// receive incoming packets and decrypt them if necessary // receive incoming packets and decrypt them if necessary
incomingPacket, ok := <-node.packetPipe incomingPacket, ok := <-node.packetPipe
if !ok { if !ok {
fmt.Printf("\nThe connection has been closed unexpectedly") fmt.Printf("\nThe connection has been closed unexpectedly\n")
os.Exit(-1) os.Exit(-1)
} }
@ -636,6 +664,10 @@ func (node *Node) Start() {
panic(err) panic(err)
} }
if node.verboseOutput {
fmt.Printf("\n[File] Received info on \"%s\" - %d bytes", file.Name, file.Size)
}
if strings.TrimSpace(file.RelativeParentPath) == "" { if strings.TrimSpace(file.RelativeParentPath) == "" {
// does not have a parent dir // does not have a parent dir
file.Path = filepath.Join(node.transferInfo.Receiving.DownloadsPath, file.Name) file.Path = filepath.Join(node.transferInfo.Receiving.DownloadsPath, file.Name)
@ -686,6 +718,10 @@ func (node *Node) Start() {
protocol.SendPacket(node.netInfo.Conn, alreadyHavePacket) protocol.SendPacket(node.netInfo.Conn, alreadyHavePacket)
if node.verboseOutput {
fmt.Printf("\n[File] already have \"%s\"", file.Name)
}
} else { } else {
// not the same file. Remove it and await new bytes // not the same file. Remove it and await new bytes
os.Remove(file.Path) os.Remove(file.Path)
@ -769,6 +805,10 @@ func (node *Node) Start() {
if acceptedFile.ID == fileID { if acceptedFile.ID == fileID {
// accepted // accepted
if node.verboseOutput {
fmt.Printf("\n[File] fully received \"%s\" -- %d bytes", acceptedFile.Name, acceptedFile.Size)
}
err = acceptedFile.Open() err = acceptedFile.Open()
if err != nil { if err != nil {
panic(err) panic(err)
@ -784,7 +824,7 @@ func (node *Node) Start() {
} }
if realChecksum != acceptedFile.Checksum { if realChecksum != acceptedFile.Checksum {
fmt.Printf("\n| %s is corrupted", acceptedFile.Name) fmt.Printf("\n| \"%s\" is corrupted", acceptedFile.Name)
acceptedFile.Close() acceptedFile.Close()
break break
} else { } else {
@ -827,7 +867,10 @@ func (node *Node) Start() {
fmt.Printf("\n%s disconnected", node.netInfo.Conn.RemoteAddr()) fmt.Printf("\n%s disconnected", node.netInfo.Conn.RemoteAddr())
} }
go node.printTransferInfo(time.Second)
if !node.verboseOutput {
go node.printTransferInfo(time.Second)
}
} }
} }
} }

9
src/node/options.go

@ -32,8 +32,9 @@ type ClientSideNodeOptions struct {
// Options to configure the node // Options to configure the node
type NodeOptions struct { type NodeOptions struct {
IsSending bool IsSending bool
WorkingPort uint WorkingPort uint
ServerSide *ServerSideNodeOptions VerboseOutput bool
ClientSide *ClientSideNodeOptions ServerSide *ServerSideNodeOptions
ClientSide *ClientSideNodeOptions
} }

Loading…
Cancel
Save