Browse Source

Added a handling for interrupt signal

main
Unbewohnte 3 years ago
parent
commit
1aa968d93e
  1. 2
      main.go
  2. 29
      receiver/receiver.go
  3. 44
      sender/sender.go

2
main.go

@ -58,6 +58,7 @@ func main() {
// 3) send info about the file -> 4) if accepted - upload file // 3) send info about the file -> 4) if accepted - upload file
sender := sender.NewSender(*PORT, *SHAREDFILE) sender := sender.NewSender(*PORT, *SHAREDFILE)
sender.WaitForConnection() sender.WaitForConnection()
sender.HandleInterrupt()
sender.MainLoop() sender.MainLoop()
} else { } else {
@ -65,6 +66,7 @@ func main() {
// 4) accept or refuse -> 5) download|don`t_download file // 4) accept or refuse -> 5) download|don`t_download file
receiver := receiver.NewReceiver(*DOWNLOADSFOLDER) receiver := receiver.NewReceiver(*DOWNLOADSFOLDER)
receiver.Connect(fmt.Sprintf("%s:%d", *SENDERADDR, *PORT)) receiver.Connect(fmt.Sprintf("%s:%d", *SENDERADDR, *PORT))
receiver.HandleInterrupt()
receiver.MainLoop() receiver.MainLoop()
} }
} }

29
receiver/receiver.go

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net" "net"
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -21,9 +22,9 @@ type Receiver struct {
IncomingPackets chan protocol.Packet IncomingPackets chan protocol.Packet
FileToDownload *file FileToDownload *file
EncryptionKey []byte EncryptionKey []byte
ReadyToReceive bool
Stopped bool
TransferInfo *transferInfo TransferInfo *transferInfo
ReadyToReceive bool // waiting for a new packet
Stopped bool // controlls the mainloop
} }
// Creates a new client with default fields // Creates a new client with default fields
@ -58,19 +59,29 @@ func NewReceiver(downloadsFolder string) *Receiver {
} }
} }
// Closes the connection // When the interrupt signal is sent - exit cleanly
func (r *Receiver) Disconnect() { func (r *Receiver) HandleInterrupt() {
r.Connection.Close() signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
go func() {
<-signalChan
r.Stop()
}()
} }
// Closes the connection, warns the sender and exits the mainloop // Closes the connection, warns the sender and exits the mainloop
func (r *Receiver) Stop() { func (r *Receiver) Stop() {
disconnectionPacket := protocol.Packet{ if r.Connection != nil {
Header: protocol.HeaderDisconnecting, disconnectionPacket := protocol.Packet{
Header: protocol.HeaderDisconnecting,
}
protocol.SendEncryptedPacket(r.Connection, disconnectionPacket, r.EncryptionKey)
r.Connection.Close()
} }
protocol.SendEncryptedPacket(r.Connection, disconnectionPacket, r.EncryptionKey)
r.Stopped = true r.Stopped = true
r.Disconnect()
} }
// Connects to a given address over tcp. Sets a connection to a corresponding field in receiver // Connects to a given address over tcp. Sets a connection to a corresponding field in receiver

44
sender/sender.go

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net" "net"
"os" "os"
"os/signal"
"strconv" "strconv"
"time" "time"
@ -21,9 +22,9 @@ type Sender struct {
IncomingPackets chan protocol.Packet IncomingPackets chan protocol.Packet
EncryptionKey []byte EncryptionKey []byte
TransferInfo *transferInfo TransferInfo *transferInfo
TransferAllowed bool TransferAllowed bool // the receiver had agreed to receive a file
ReceiverIsReady bool ReceiverIsReady bool // receiver is waiting for a new packet
Stopped bool Stopped bool // controlls the mainloop
} }
// Creates a new sender with default|necessary fields // Creates a new sender with default|necessary fields
@ -41,8 +42,10 @@ func NewSender(port int, filepath string) *Sender {
remoteIP, err := GetRemoteIP() remoteIP, err := GetRemoteIP()
if err != nil { if err != nil {
panic(err) // don`t panic if couldn`t get remote IP
remoteIP = ""
} }
localIP, err := GetLocalIP() localIP, err := GetLocalIP()
if err != nil { if err != nil {
panic(err) panic(err)
@ -70,23 +73,32 @@ func NewSender(port int, filepath string) *Sender {
} }
} }
// When the interrupt signal is sent - exit cleanly
func (s *Sender) HandleInterrupt() {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
go func() {
<-signalChan
s.Stop()
}()
}
// Closes the connection, warns about it the receiver and exits the mainloop // Closes the connection, warns about it the receiver and exits the mainloop
func (s *Sender) Stop() { func (s *Sender) Stop() {
disconnectionPacket := protocol.Packet{ if s.Connection != nil {
Header: protocol.HeaderDisconnecting, disconnectionPacket := protocol.Packet{
} Header: protocol.HeaderDisconnecting,
err := protocol.SendEncryptedPacket(s.Connection, disconnectionPacket, s.EncryptionKey) }
if err != nil { err := protocol.SendEncryptedPacket(s.Connection, disconnectionPacket, s.EncryptionKey)
panic(fmt.Sprintf("could not send a disconnection packet: %s", err)) if err != nil {
panic(fmt.Sprintf("could not send a disconnection packet: %s", err))
}
s.Connection.Close()
} }
s.Stopped = true s.Stopped = true
s.Disconnect()
}
// Closes current connection
func (s *Sender) Disconnect() {
s.Connection.Close()
} }
// Accepts one connection // Accepts one connection

Loading…
Cancel
Save