Browse Source

[node] print transfer info. The output is not flooded anymore on receiver

main
Unbewohnte 3 years ago
parent
commit
09e3c7b960
  1. 82
      src/node/node.go

82
src/node/node.go

@ -46,7 +46,7 @@ type nodeInnerstates struct {
} }
// netInfowork specific settings // netInfowork specific settings
type netInfoInfo struct { type netInfo struct {
ConnAddr string // address to connect to. Does not include port ConnAddr string // address to connect to. Does not include port
Conn net.Conn // the core TCP connection of the node. Self-explanatory Conn net.Conn // the core TCP connection of the node. Self-explanatory
Port uint // a port to connect to/listen on Port uint // a port to connect to/listen on
@ -55,12 +55,11 @@ type netInfoInfo struct {
// Sending-side node information // Sending-side node information
type sending struct { type sending struct {
ServingPath string // path to the thing that will be sent ServingPath string // path to the thing that will be sent
IsDirectory bool // is ServingPath a directory IsDirectory bool // is ServingPath a directory
Recursive bool // recursively send directory Recursive bool // recursively send directory
CanSendBytes bool // is the other node ready to receive another piece CanSendBytes bool // is the other node ready to receive another piece
FilesToSend []*fsys.File FilesToSend []*fsys.File
// CurrentFileIndex uint64 // an index of a file with a specific ID that is currently being transported
CurrentFileID uint64 // an id of a file that is currently being transported CurrentFileID uint64 // an id of a file that is currently being transported
} }
@ -81,7 +80,7 @@ type Node struct {
mutex *sync.Mutex mutex *sync.Mutex
packetPipe chan *protocol.Packet // a way to receive incoming packets from another goroutine packetPipe chan *protocol.Packet // a way to receive incoming packets from another goroutine
isSending bool // sending or a receiving node isSending bool // sending or a receiving node
netInfo *netInfoInfo netInfo *netInfo
state *nodeInnerstates state *nodeInnerstates
transferInfo *transferInfo transferInfo *transferInfo
} }
@ -122,7 +121,7 @@ func NewNode(options *NodeOptions) (*Node, error) {
mutex: &sync.Mutex{}, mutex: &sync.Mutex{},
packetPipe: make(chan *protocol.Packet, 100), packetPipe: make(chan *protocol.Packet, 100),
isSending: options.IsSending, isSending: options.IsSending,
netInfo: &netInfoInfo{ netInfo: &netInfo{
Port: options.WorkingPort, Port: options.WorkingPort,
ConnAddr: options.ClientSide.ConnectionAddr, ConnAddr: options.ClientSide.ConnectionAddr,
EncryptionKey: nil, EncryptionKey: nil,
@ -153,14 +152,14 @@ func (node *Node) connect() error {
node.netInfo.Port = 7270 node.netInfo.Port = 7270
} }
fmt.Printf("Connecting to %s:%d...\n", node.netInfo.ConnAddr, node.netInfo.Port) fmt.Printf("\nConnecting to %s:%d...", node.netInfo.ConnAddr, node.netInfo.Port)
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", node.netInfo.ConnAddr, node.netInfo.Port), time.Second*5) conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", node.netInfo.ConnAddr, node.netInfo.Port), time.Second*5)
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("Connected\n") fmt.Printf("\nConnected")
node.netInfo.Conn = conn node.netInfo.Conn = conn
@ -202,13 +201,33 @@ func (node *Node) waitForConnection() error {
return err return err
} }
fmt.Printf("New connection from %s\n", connection.RemoteAddr().String()) fmt.Printf("\nNew connection from %s", connection.RemoteAddr().String())
node.netInfo.Conn = connection node.netInfo.Conn = connection
return nil return nil
} }
// Prints information about the transfer after defined delay
func (node *Node) printTransferInfo(delay time.Duration) error {
time.Sleep(delay)
switch node.isSending {
case true:
if !node.state.AllowedToTransfer {
break
}
fmt.Printf("\r| files(s) left to send: %4d", len(node.transferInfo.Sending.FilesToSend))
case false:
if len(node.transferInfo.Receiving.AcceptedFiles) <= 0 {
break
}
fmt.Printf("\r| file(s) left to receive: %4d", len(node.transferInfo.Receiving.AcceptedFiles))
}
return nil
}
// Starts the node in either sending or receiving state and performs the transfer // Starts the node in either sending or receiving state and performs the transfer
func (node *Node) Start() { func (node *Node) Start() {
switch node.isSending { switch node.isSending {
@ -245,7 +264,7 @@ func (node *Node) Start() {
sizeLevel = "GiB" sizeLevel = "GiB"
} }
fmt.Printf("Sending \"%s\" (%.3f %s) locally on %s:%d\n", dirToSend.Name, size, sizeLevel, localIP, node.netInfo.Port) fmt.Printf("\nSending \"%s\" (%.3f %s) locally on %s:%d", dirToSend.Name, size, sizeLevel, localIP, node.netInfo.Port)
} else { } else {
size := float32(fileToSend.Size) / 1024 / 1024 size := float32(fileToSend.Size) / 1024 / 1024
sizeLevel := "MiB" sizeLevel := "MiB"
@ -254,7 +273,7 @@ func (node *Node) Start() {
size = size / 1024 size = size / 1024
sizeLevel = "GiB" sizeLevel = "GiB"
} }
fmt.Printf("Sending \"%s\" (%.3f %s) locally on %s:%d\n", fileToSend.Name, size, sizeLevel, localIP, node.netInfo.Port) fmt.Printf("\nSending \"%s\" (%.3f %s) locally on %s:%d and remotely", fileToSend.Name, size, sizeLevel, localIP, node.netInfo.Port)
} }
@ -267,7 +286,7 @@ func (node *Node) Start() {
// generate and send encryption key // generate and send encryption key
encrKey := encryption.Generate32AESkey() encrKey := encryption.Generate32AESkey()
node.netInfo.EncryptionKey = encrKey node.netInfo.EncryptionKey = encrKey
fmt.Printf("Generated encryption key: %s\n", encrKey) fmt.Printf("\nGenerated encryption key: %s\n", encrKey)
err = protocol.SendEncryptionKey(node.netInfo.Conn, encrKey) err = protocol.SendEncryptionKey(node.netInfo.Conn, encrKey)
if err != nil { if err != nil {
@ -290,7 +309,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("The connection has been closed unexpectedly\n") fmt.Printf("\nThe connection has been closed unexpectedly")
os.Exit(-1.) os.Exit(-1.)
} }
@ -313,7 +332,7 @@ func (node *Node) Start() {
// the receiving node has accepted the transfer // the receiving node has accepted the transfer
node.state.AllowedToTransfer = true node.state.AllowedToTransfer = true
fmt.Printf("Transfer allowed. Sending...\n") fmt.Printf("\nTransfer allowed. Sending...")
// notify it about all the files that are going to be sent // notify it about all the files that are going to be sent
switch node.transferInfo.Sending.IsDirectory { switch node.transferInfo.Sending.IsDirectory {
@ -393,11 +412,11 @@ func (node *Node) Start() {
case protocol.HeaderReject: case protocol.HeaderReject:
node.state.Stopped = true node.state.Stopped = true
fmt.Printf("Transfer rejected. Disconnecting...\n") fmt.Printf("\nTransfer rejected. Disconnecting...")
case protocol.HeaderDisconnecting: case protocol.HeaderDisconnecting:
node.state.Stopped = true node.state.Stopped = true
fmt.Printf("%s disconnected\n", node.netInfo.Conn.RemoteAddr()) fmt.Printf("\n%s disconnected", node.netInfo.Conn.RemoteAddr())
case protocol.HeaderAlreadyHave: case protocol.HeaderAlreadyHave:
// the other node already has a file with such ID. // the other node already has a file with such ID.
@ -425,7 +444,7 @@ func (node *Node) Start() {
Header: protocol.HeaderDone, Header: protocol.HeaderDone,
}) })
fmt.Printf("Transfer ended successfully\n") fmt.Printf("\nTransfer ended successfully")
node.state.Stopped = true node.state.Stopped = true
@ -482,11 +501,12 @@ func (node *Node) Start() {
default: default:
node.state.Stopped = true node.state.Stopped = true
fmt.Printf("An error occured while sending a piece of \"%s\": %s\n", node.transferInfo.Sending.FilesToSend[currentFileIndex].Name, err) fmt.Printf("\nAn error occured while sending a piece of \"%s\": %s", node.transferInfo.Sending.FilesToSend[currentFileIndex].Name, err)
panic(err) panic(err)
} }
} }
go node.printTransferInfo(time.Second)
} }
case false: case false:
@ -495,7 +515,7 @@ func (node *Node) Start() {
// connect to the sending node // connect to the sending node
err := node.connect() err := node.connect()
if err != nil { if err != nil {
fmt.Printf("Could not connect to %s:%d\n", node.netInfo.ConnAddr, node.netInfo.Port) fmt.Printf("\nCould not connect to %s:%d", node.netInfo.ConnAddr, node.netInfo.Port)
os.Exit(-1) os.Exit(-1)
} }
@ -516,7 +536,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("The connection has been closed unexpectedly\n") fmt.Printf("\nThe connection has been closed unexpectedly")
os.Exit(-1) os.Exit(-1)
} }
@ -572,7 +592,7 @@ func (node *Node) Start() {
err = os.MkdirAll(filepath.Join(node.transferInfo.Receiving.DownloadsPath, dir.Name), os.ModePerm) err = os.MkdirAll(filepath.Join(node.transferInfo.Receiving.DownloadsPath, dir.Name), os.ModePerm)
if err != nil { if err != nil {
// well, just download all files in the default downloads folder then // well, just download all files in the default downloads folder then
fmt.Printf("[ERROR] could not create a directory\n") fmt.Printf("\n[ERROR] could not create a directory")
} else { } else {
// also download everything in a newly created directory // also download everything in a newly created directory
node.transferInfo.Receiving.DownloadsPath = filepath.Join(node.transferInfo.Receiving.DownloadsPath, dir.Name) node.transferInfo.Receiving.DownloadsPath = filepath.Join(node.transferInfo.Receiving.DownloadsPath, dir.Name)
@ -648,8 +668,6 @@ func (node *Node) Start() {
// it`s the exact same file. No need to receive it again // it`s the exact same file. No need to receive it again
// notify the other node // notify the other node
fmt.Printf("| Already have \"%s\". Skipping...\n\n", file.Name)
alreadyHavePacketBodyBuffer := new(bytes.Buffer) alreadyHavePacketBodyBuffer := new(bytes.Buffer)
binary.Write(alreadyHavePacketBodyBuffer, binary.BigEndian, file.ID) binary.Write(alreadyHavePacketBodyBuffer, binary.BigEndian, file.ID)
@ -765,13 +783,11 @@ func (node *Node) Start() {
panic(err) panic(err)
} }
fmt.Printf("\n| Checking hashes for file \"%s\"\n", acceptedFile.Name)
if realChecksum != acceptedFile.Checksum { if realChecksum != acceptedFile.Checksum {
fmt.Printf("| %s --- %s file is corrupted\n", realChecksum, acceptedFile.Checksum) fmt.Printf("\n| %s is corrupted", acceptedFile.Name)
acceptedFile.Close() acceptedFile.Close()
break break
} else { } else {
fmt.Printf("| %s --- %s\n", realChecksum, acceptedFile.Checksum)
acceptedFile.Close() acceptedFile.Close()
break break
} }
@ -797,7 +813,7 @@ func (node *Node) Start() {
node.netInfo.EncryptionKey = encrKey node.netInfo.EncryptionKey = encrKey
fmt.Printf("Got an encryption key: %s\n", encrKey) fmt.Printf("\nGot an encryption key: %s", encrKey)
case protocol.HeaderDone: case protocol.HeaderDone:
node.mutex.Lock() node.mutex.Lock()
@ -809,9 +825,9 @@ func (node *Node) Start() {
node.state.Stopped = true node.state.Stopped = true
node.mutex.Unlock() node.mutex.Unlock()
fmt.Printf("%s disconnected\n", node.netInfo.Conn.RemoteAddr()) fmt.Printf("\n%s disconnected", node.netInfo.Conn.RemoteAddr())
} }
go node.printTransferInfo(time.Second)
} }
} }
} }

Loading…
Cancel
Save