Skip to content

Commit

Permalink
now if checksum fails client will be close, and now client handle con…
Browse files Browse the repository at this point in the history
…firmation invalid packet from server
  • Loading branch information
Fabiokleis committed Apr 6, 2024
1 parent 86dd4ca commit ca7ef17
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
16 changes: 13 additions & 3 deletions pkg/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,26 @@ func (c *Client) verifyConfirmation(confirm *msg.Confirmation) {
case msg.Result_VALID_CHECKSUM:
if err := c.validateCheckSum(confirm.Token); err != nil {
fmt.Println(err)
//if err := c.File.DeleteFile(); err != nil {
// fmt.Println(err)
//}
if err := c.File.DeleteFile(); err != nil {
fmt.Println(err)
}
return
}

c.Transfering = false // stop reading packets
fmt.Println("sha256 checksum", c.File.CheckSum)
fmt.Println("file transfering succeed")

break
case msg.Result_OK, msg.Result_INVALID_PACKET_FORMAT:
token := c.File.LastReceivedToken()
if token != nil {
c.sendConfirmationPacket(
msg.Result_OK,
f.TokenHash(token.Index),
)
fmt.Println("re-sending confirmation token")
}
default:
break
}
Expand Down Expand Up @@ -136,6 +145,7 @@ func (c *Client) KeepCheckingServer(miss bool, missed int) {
break
case packet := <-c.PacketChannel:

//fmt.Println("packet: ", packet)
timer.Reset(time.Duration(RESEND_MAX_TIME) * time.Second)

if miss {
Expand Down
11 changes: 11 additions & 0 deletions pkg/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ func (tF *TokenizableFile) FindNotReceivedToken() *Token {
return nil
}

func (tF *TokenizableFile) LastReceivedToken() *Token {

for i := len(tF.Tokens) - 1; i >= 0; i-- {
if tF.Tokens[i].Received {
return tF.Tokens[i]
}
}

return nil
}

func (tF *TokenizableFile) PushToken(idx int, received bool) {
tF.Tokens = append(tF.Tokens, &Token{Index: idx, Received: received})
}
7 changes: 4 additions & 3 deletions pkg/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (w *Worker) Execute() {
if err := pb.Unmarshal(buffer[1:], &confirm); err != nil {
slog.Error("failed confirmation to decode buffer as protobuf message", "address", w.Addr.String(), "error", err)
w.sendConfirmationPacket(msg.Result_INVALID_PACKET_FORMAT, "")
return // exit worker
break
}
// reset timer to wait next confirmation messages
timer.Reset(time.Duration(TIMEOUT) * time.Second)
Expand All @@ -197,10 +197,11 @@ func (w *Worker) Execute() {
case msg.Result_OK:
w.Waiting = false // stop waiting confirmation packet
if err := w.confirmPacket(confirm.Token); err != nil {
slog.Error("cannot confirm packet, failed to find token hash", "address", w.Addr.String(), "token", confirm.Token, "error", err)
//slog.Error("cannot confirm packet, failed to find token hash", "address", w.Addr.String(), "token", confirm.Token, "error", err)
w.sendConfirmationPacket(msg.Result_INVALID_TOKEN, confirm.Token)
break
}
slog.Info("received packet", "address", w.Addr.String(), "token", confirm.Token)
//slog.Info("received packet", "address", w.Addr.String(), "token", confirm.Token)
break
case msg.Result_PACKET_MISS:
w.resendLastPacket(confirm.Token)
Expand Down

0 comments on commit ca7ef17

Please sign in to comment.