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)
test:
cd $(SRC_DIR) && go test ./... ; cd ..
cd $(SRC_DIR) && go test ./...
clean:
rm $(EXE_NAME)

10
src/main.go

@ -30,7 +30,7 @@ import (
)
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)
@ -43,6 +43,7 @@ var (
ADDRESS *string = flag.String("a", "", "Specifies an address to connect to")
DOWNLOADS_DIR *string = flag.String("d", ".", "Downloads folder")
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_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("| -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("| -vp [true|false] to turn off|on verbose output")
fmt.Printf("| -l print license information\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("| creates a node that will send every file in the directory !RECUSRIVELY!\n\n\n")
}
flag.Parse()
@ -118,8 +119,9 @@ func init() {
func main() {
nodeOptions := node.NodeOptions{
IsSending: isSending,
WorkingPort: *PORT,
VerboseOutput: *VERBOSE,
IsSending: isSending,
WorkingPort: *PORT,
ServerSide: &node.ServerSideNodeOptions{
ServingPath: *SEND,
Recursive: *RECUSRIVE,

79
src/node/node.go

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

Loading…
Cancel
Save