Browse Source

[node] non-recursive directory send !; [Makefile] tests

main
Unbewohnte 3 years ago
parent
commit
d0a8d22d12
  1. 3
      Makefile
  2. 105
      src/node/node.go
  3. 2
      src/protocol/recv.go
  4. 2
      src/protocol/send.go

3
Makefile

@ -13,5 +13,8 @@ race:
install: all
cp $(EXE_NAME) $(INSTALLATION_DIR)
test:
cd $(SRC_DIR) && go test ./... ; cd ..
clean:
rm $(EXE_NAME)

105
src/node/node.go

@ -185,7 +185,7 @@ func (node *Node) waitForConnection() error {
func (node *Node) Start() {
switch node.isSending {
case true:
// SENDER
// SENDER NODE
localIP, err := addr.GetLocal()
if err != nil {
@ -247,7 +247,8 @@ func (node *Node) Start() {
// receive incoming packets and decrypt them if necessary
incomingPacket, ok := <-node.packetPipe
if !ok {
node.state.Stopped = true
fmt.Printf("The connection has been closed unexpectedly\n")
break
}
// if encryption key is set - decrypt packet on the spot
@ -351,23 +352,26 @@ func (node *Node) Start() {
fmt.Printf("%s disconnected\n", node.netInfo.Conn.RemoteAddr())
}
// 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 {
// handle a single file or a directory
switch node.transferInfo.Sending.IsDirectory {
case true:
// sending a file in a directory (possibly more than 1)
if !node.transferInfo.Sending.Recursive {
// Transfer section
} else {
// send bytes of all files one by one
if len(node.transferInfo.Sending.FilesToSend) == 0 {
// if there`s nothing else to send - create and send DONE packet
protocol.SendPacket(node.netInfo.Conn, protocol.Packet{
Header: protocol.HeaderDone,
})
fmt.Printf("Transfer ended successfully\n")
node.state.Stopped = true
}
case false:
// 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
currentFileID := node.transferInfo.Sending.CurrentFileID
err = protocol.SendPiece(node.transferInfo.Sending.FilesToSend[currentFileID], node.netInfo.Conn, node.netInfo.EncryptionKey)
switch err {
case protocol.ErrorSentAll:
@ -392,18 +396,24 @@ func (node *Node) Start() {
protocol.SendPacket(node.netInfo.Conn, endFilePacket)
// as only one file has been requested to send - there`s nothing else to do
// sending DONE packet
protocol.SendPacket(node.netInfo.Conn, protocol.Packet{
// start sending the next file
if uint64(len(node.transferInfo.Sending.FilesToSend)) == (node.transferInfo.Sending.CurrentFileID + 1) {
// all files has been sent
node.state.Stopped = true
// send DONE packet
donePacket := protocol.Packet{
Header: protocol.HeaderDone,
})
}
fmt.Printf("Transfer ended successfully\n")
protocol.SendPacket(node.netInfo.Conn, donePacket)
node.state.Stopped = true
} else {
node.transferInfo.Sending.CurrentFileID++
}
case nil:
break
node.transferInfo.Sending.CanSendBytes = false
default:
node.state.Stopped = true
@ -413,14 +423,11 @@ func (node *Node) Start() {
panic(err)
}
node.transferInfo.Sending.CanSendBytes = false
}
}
}
case false:
// RECEIVER
// RECEIVER NODE
// connect to the sending node
err := node.connect()
@ -446,6 +453,7 @@ func (node *Node) Start() {
// receive incoming packets and decrypt them if necessary
incomingPacket, ok := <-node.packetPipe
if !ok {
fmt.Printf("The connection has been closed unexpectedly\n")
break
}
@ -482,8 +490,20 @@ func (node *Node) Start() {
if strings.EqualFold(answer, "y") || answer == "" {
// yes
// send aceptance packet
// in case it`s a directory - create it now
if dir != nil {
err = os.MkdirAll(filepath.Join(node.transferInfo.Receiving.DownloadsPath, dir.Name), os.ModePerm)
if err != nil {
// well, just download all files in the default downloads folder then
fmt.Printf("[ERROR] could not create a directory\n")
} else {
// also download everything in a newly created directory
node.transferInfo.Receiving.DownloadsPath = filepath.Join(node.transferInfo.Receiving.DownloadsPath, dir.Name)
}
}
// send aceptance packet
acceptancePacket := protocol.Packet{
Header: protocol.HeaderAccept,
}
@ -545,18 +565,20 @@ func (node *Node) Start() {
node.mutex.Unlock()
case protocol.HeaderDirectory:
// directory
dir, err := protocol.DecodeDirectoryPacket(incomingPacket)
if err != nil {
panic(err)
}
// (TODO)
// add a new directory to downloads path
node.transferInfo.Receiving.DownloadsPath = filepath.Join(node.transferInfo.Receiving.DownloadsPath, dir.Name)
err = os.MkdirAll(node.transferInfo.Receiving.DownloadsPath, os.ModePerm)
if err != nil {
panic(err)
}
// directory
// dir, err := protocol.DecodeDirectoryPacket(incomingPacket)
// if err != nil {
// panic(err)
// }
// // add a new directory to downloads path
// node.transferInfo.Receiving.DownloadsPath = filepath.Join(node.transferInfo.Receiving.DownloadsPath, dir.Name)
// err = os.MkdirAll(node.transferInfo.Receiving.DownloadsPath, os.ModePerm)
// if err != nil {
// panic(err)
// }
case protocol.HeaderFileBytes:
// check if this file has been accepted to receive
@ -581,7 +603,7 @@ func (node *Node) Start() {
}
}
// notify the other one that this node is ready
// notify the other node that this one is ready
err = protocol.SendPacket(node.netInfo.Conn, protocol.Packet{
Header: protocol.HeaderReady,
})
@ -617,7 +639,7 @@ func (node *Node) Start() {
panic(err)
}
fmt.Printf("| Checking hashes for file \"%s\"\n", acceptedFile.Name)
fmt.Printf("\n| Checking hashes for file \"%s\"\n", acceptedFile.Name)
if realChecksum != acceptedFile.Checksum {
fmt.Printf("| %s --- %s file is corrupted\n", realChecksum, acceptedFile.Checksum)
break
@ -628,7 +650,12 @@ func (node *Node) Start() {
}
}
// node.state.Stopped = true
err = protocol.SendPacket(node.netInfo.Conn, protocol.Packet{
Header: protocol.HeaderReady,
})
if err != nil {
panic(err)
}
case protocol.HeaderEncryptionKey:
// retrieve the key

2
src/protocol/recv.go

@ -38,7 +38,7 @@ func ReadFromConn(connection net.Conn) ([]byte, error) {
packetBuffer.Write(buff[:read])
}
fmt.Printf("[RECV] read from connection: %s; length: %d\n", packetBuffer.Bytes()[:30], packetBuffer.Len())
// fmt.Printf("[RECV] read from connection: %s; length: %d\n", packetBuffer.Bytes()[:30], packetBuffer.Len())
return packetBuffer.Bytes(), nil
}

2
src/protocol/send.go

@ -20,7 +20,7 @@ func SendPacket(connection net.Conn, packet Packet) error {
return err
}
fmt.Printf("[SEND] packet %+s; len: %d\n", packetBytes[:30], len(packetBytes))
// fmt.Printf("[SEND] packet %+s; len: %d\n", packetBytes[:30], len(packetBytes))
// write the result (ie: (packetsize)(header)~(bodybytes))
connection.Write(packetBytes)

Loading…
Cancel
Save