mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-24 04:38:04 +00:00
The client state machine has now been fully rewritten, and encapsulates the miniature state machines implemented in previous PRs. I've extracted the state machine from IMAPClientHandler and moved it into ClientStateMachine, hence all the code deletion. The state machine receives a command, response, or continuation request from the IMAPClientHandler and returns an array of Chunk/EventLoopPromise pairs that the client handler should then write to the network. Also we didn't previously queue Append commands like we did for others, however it's now become apparent that all commands types should be queued, so we do that now. A lot of tests have been added to make sure everything works. Note that a lot of sendCommand tests have been deleted as we now precondition on user errors instead of throwing.
193 lines
8.3 KiB
Swift
193 lines
8.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2021 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import NIOIMAPCore
|
|
|
|
extension ClientStateMachine {
|
|
struct Append: Hashable {
|
|
enum State: Hashable {
|
|
// TODO: Find a better name than "started".
|
|
/// We've sent the append command to the server, and will now send data
|
|
/// Can move to `.waitingForAppend...` or `catenating`.
|
|
/// `canFinish` is true iff at least one "standard" append, or one catenate
|
|
/// has completed.
|
|
case started(canFinish: Bool)
|
|
|
|
/// We want to send a literal to the server, but first need the server to confirm it's ready.
|
|
/// When we receive the continuation request, we'll move to `.sendingMessageBytes`.
|
|
case waitingForAppendContinuationRequest
|
|
|
|
/// We're streaming a message to the server.
|
|
/// Once the message has been sent, we return to `.started`.
|
|
case sendingMessageBytes
|
|
|
|
/// We're catenating messages or URLS. No state transformation is required for URLs, as they're
|
|
/// done in one shot, but to catenate a message we need to move to `.waitingForCatenate...`
|
|
case catenating(sentFirstObject: Bool)
|
|
|
|
/// We want to send a literal to the server, but first need the server to confirm it's ready.
|
|
/// Once we've received the continuation, we'll move to `.sendingCatenateBytes`.
|
|
case waitingForCatenateContinuationRequest
|
|
|
|
/// We're streaming a catenate message to the server. Once the
|
|
/// message is sent we return to `.started`.
|
|
case sendingCatenateBytes
|
|
|
|
/// The client has sent the "finish" command, we just need the server to confirm.
|
|
/// From here we move to `finished`.
|
|
case waitingForTaggedResponse
|
|
|
|
/// The server has returned a tagged response to finish the append command. No
|
|
/// valid state transformations at this point.
|
|
case finished
|
|
}
|
|
|
|
var state: State = .started(canFinish: false)
|
|
|
|
var hasCatenatedAtLeastOneObject: Bool {
|
|
switch self.state {
|
|
case .started, .waitingForAppendContinuationRequest, .sendingMessageBytes, .waitingForCatenateContinuationRequest, .sendingCatenateBytes, .waitingForTaggedResponse, .finished:
|
|
return false
|
|
case .catenating(sentFirstObject: let sentFirstObject):
|
|
return sentFirstObject
|
|
}
|
|
}
|
|
|
|
// We don't expect any responses when appending other than the
|
|
// final tagged response.
|
|
mutating func receiveResponse(_ response: Response) throws -> Bool {
|
|
switch self.state {
|
|
case .started, .waitingForAppendContinuationRequest, .sendingMessageBytes, .catenating,
|
|
.waitingForCatenateContinuationRequest, .sendingCatenateBytes, .finished:
|
|
throw UnexpectedContinuationRequest()
|
|
case .waitingForTaggedResponse:
|
|
break
|
|
}
|
|
|
|
switch response {
|
|
case .untagged, .fetch, .fatal, .authenticationChallenge, .idleStarted:
|
|
throw UnexpectedResponse(activePromise: nil)
|
|
case .tagged:
|
|
self.state = .finished
|
|
return true
|
|
}
|
|
}
|
|
|
|
mutating func receiveContinuationRequest(_: ContinuationRequest) throws {
|
|
switch self.state {
|
|
case .started, .sendingMessageBytes, .catenating,
|
|
.sendingCatenateBytes, .finished, .waitingForTaggedResponse:
|
|
throw UnexpectedContinuationRequest()
|
|
case .waitingForAppendContinuationRequest:
|
|
self.state = .sendingMessageBytes
|
|
case .waitingForCatenateContinuationRequest:
|
|
self.state = .sendingCatenateBytes
|
|
}
|
|
}
|
|
|
|
/// - returns: `true` if a continuation is required after this command is sent, otherwise `false`.
|
|
mutating func sendCommand(_ command: CommandStreamPart) -> Bool {
|
|
// we only care about append commands, obviously
|
|
let appendCommand: AppendCommand
|
|
switch command {
|
|
case .idleDone, .tagged, .continuationResponse:
|
|
preconditionFailure("Invalid command for state: \(self.state)")
|
|
case .append(let _appendCommand):
|
|
appendCommand = _appendCommand
|
|
}
|
|
|
|
switch self.state {
|
|
case .waitingForTaggedResponse, .waitingForAppendContinuationRequest, .waitingForCatenateContinuationRequest, .finished:
|
|
preconditionFailure("Invalid state: \(self.state)")
|
|
case .started:
|
|
return self.sendCommand_startedState(appendCommand)
|
|
case .sendingMessageBytes:
|
|
self.sendCommand_sendingMessageBytesState(appendCommand)
|
|
return false
|
|
case .catenating:
|
|
return self.sendCommand_catenatingState(appendCommand)
|
|
case .sendingCatenateBytes:
|
|
self.sendCommand_sendingCatenateBytesState(appendCommand)
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Send
|
|
|
|
extension ClientStateMachine.Append {
|
|
/// `true` if a continuation is required, otherwise `false`
|
|
private mutating func sendCommand_startedState(_ command: AppendCommand) -> Bool {
|
|
switch command {
|
|
case .start, .messageBytes, .endMessage, .catenateURL, .catenateData, .endCatenate:
|
|
preconditionFailure("Invalid command for state: \(self.state)")
|
|
case .beginMessage:
|
|
self.state = .waitingForAppendContinuationRequest
|
|
return true
|
|
case .beginCatenate:
|
|
self.state = .catenating(sentFirstObject: false)
|
|
return false
|
|
case .finish:
|
|
if self.state == .started(canFinish: true) {
|
|
self.state = .waitingForTaggedResponse
|
|
return false
|
|
} else {
|
|
preconditionFailure("Invalid command for state: \(self.state)")
|
|
}
|
|
}
|
|
}
|
|
|
|
/// `true` if a continuation is required, otherwise `false`
|
|
private mutating func sendCommand_sendingMessageBytesState(_ command: AppendCommand) {
|
|
switch command {
|
|
case .start, .beginMessage, .beginCatenate, .catenateURL, .catenateData, .endCatenate, .finish:
|
|
preconditionFailure("Invalid command for state: \(self.state)")
|
|
case .endMessage:
|
|
self.state = .started(canFinish: true)
|
|
case .messageBytes:
|
|
self.state = .sendingMessageBytes // continue sending bytes until we're told to stop
|
|
}
|
|
}
|
|
|
|
/// `true` if a continuation is required, otherwise `false`
|
|
private mutating func sendCommand_catenatingState(_ command: AppendCommand) -> Bool {
|
|
switch command {
|
|
case .start, .beginMessage, .beginCatenate, .messageBytes, .endMessage, .finish, .catenateData(.bytes), .catenateData(.end):
|
|
preconditionFailure("Invalid command for state: \(self.state)")
|
|
case .catenateURL:
|
|
self.state = .catenating(sentFirstObject: true)
|
|
return false
|
|
case .catenateData(.begin):
|
|
self.state = .waitingForCatenateContinuationRequest
|
|
return true
|
|
case .endCatenate:
|
|
self.state = .started(canFinish: true)
|
|
return false
|
|
}
|
|
}
|
|
|
|
/// `true` if a continuation is required, otherwise `false`
|
|
private mutating func sendCommand_sendingCatenateBytesState(_ command: AppendCommand) {
|
|
switch command {
|
|
case .start, .beginMessage, .beginCatenate, .endMessage, .catenateURL, .messageBytes, .finish, .endCatenate, .catenateData(.begin):
|
|
preconditionFailure("Invalid command for state: \(self.state)")
|
|
case .catenateData(.end):
|
|
self.state = .catenating(sentFirstObject: true)
|
|
case .catenateData(.bytes):
|
|
self.state = .sendingCatenateBytes // continue sending bytes until we're told to stop
|
|
}
|
|
}
|
|
}
|