Skip to content

Commit

Permalink
start building core/sender package with core/sender/state.go file
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Feb 4, 2022
1 parent b076d02 commit ac1890f
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions core/sender/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package sender

import "fmt"

type TransferState int

const (
Initial TransferState = iota
WaitForFileRequest
SendingData
WaitForFileAck
WaitForCloseMessage
WaitForCloseAck
)

// UIUpdate is a struct that is continously communicated to the UI (if sender has attached a UI)
type UIUpdate struct {
State TransferState
Progress float32
}

// WrongStateError is a custom error for the Transfer sequence
type WrongStateError struct {
expected TransferState
got TransferState
}

// WrongStateError constructor
func NewWrongStateError(expected, got TransferState) *WrongStateError {
return &WrongStateError{
expected: expected,
got: got,
}
}

func (e *WrongStateError) Error() string {
return fmt.Sprintf("wrong message type, expected type: %d(%s), got: %d(%s)", e.expected, e.expected.Name(), e.got, e.got.Name())
}

// Name returns the associated to the state enum.
func (s TransferState) Name() string {
switch s {
case Initial:
return "Initial"

case WaitForFileRequest:
return "WaitForFileRequest"

case SendingData:
return "SendingData"

case WaitForFileAck:
return "WaitForFileAck"

case WaitForCloseMessage:
return "WaitForCloseMessage"

case WaitForCloseAck:
return "WaitForCloseAck"

default:
return ""
}
}

0 comments on commit ac1890f

Please # to comment.