-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start building
core/sender
package with core/sender/state.go
file
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} | ||
} |