ServerHandlerStateMachine.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright 2022, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if compiler(>=5.6)
  17. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  18. internal struct ServerHandlerStateMachine {
  19. private var state: Self.State
  20. init(userInfoRef: Ref<UserInfo>, context: CallHandlerContext) {
  21. self.state = .idle(.init(userInfoRef: userInfoRef, context: context))
  22. }
  23. mutating func handleMetadata() -> HandleMetadataAction {
  24. switch self.state {
  25. case var .idle(idle):
  26. let nextStateAndOutput = idle.handleMetadata()
  27. self.state = nextStateAndOutput.nextState.state
  28. return nextStateAndOutput.output
  29. case var .handling(handling):
  30. let nextStateAndOutput = handling.handleMetadata()
  31. self.state = nextStateAndOutput.nextState.state
  32. return nextStateAndOutput.output
  33. case var .draining(draining):
  34. let nextStateAndOutput = draining.handleMetadata()
  35. self.state = nextStateAndOutput.nextState.state
  36. return nextStateAndOutput.output
  37. case var .finished(finished):
  38. let nextStateAndOutput = finished.handleMetadata()
  39. self.state = nextStateAndOutput.nextState.state
  40. return nextStateAndOutput.output
  41. }
  42. }
  43. mutating func handleMessage() -> HandleMessageAction {
  44. switch self.state {
  45. case var .idle(idle):
  46. let nextStateAndOutput = idle.handleMessage()
  47. self.state = nextStateAndOutput.nextState.state
  48. return nextStateAndOutput.output
  49. case var .handling(handling):
  50. let nextStateAndOutput = handling.handleMessage()
  51. self.state = nextStateAndOutput.nextState.state
  52. return nextStateAndOutput.output
  53. case var .draining(draining):
  54. let nextStateAndOutput = draining.handleMessage()
  55. self.state = nextStateAndOutput.nextState.state
  56. return nextStateAndOutput.output
  57. case var .finished(finished):
  58. let nextStateAndOutput = finished.handleMessage()
  59. self.state = nextStateAndOutput.nextState.state
  60. return nextStateAndOutput.output
  61. }
  62. }
  63. mutating func handleEnd() -> HandleEndAction {
  64. switch self.state {
  65. case var .idle(idle):
  66. let nextStateAndOutput = idle.handleEnd()
  67. self.state = nextStateAndOutput.nextState.state
  68. return nextStateAndOutput.output
  69. case var .handling(handling):
  70. let nextStateAndOutput = handling.handleEnd()
  71. self.state = nextStateAndOutput.nextState.state
  72. return nextStateAndOutput.output
  73. case var .draining(draining):
  74. let nextStateAndOutput = draining.handleEnd()
  75. self.state = nextStateAndOutput.nextState.state
  76. return nextStateAndOutput.output
  77. case var .finished(finished):
  78. let nextStateAndOutput = finished.handleEnd()
  79. self.state = nextStateAndOutput.nextState.state
  80. return nextStateAndOutput.output
  81. }
  82. }
  83. mutating func sendMessage() -> SendMessageAction {
  84. switch self.state {
  85. case var .handling(handling):
  86. let nextStateAndOutput = handling.sendMessage()
  87. self.state = nextStateAndOutput.nextState.state
  88. return nextStateAndOutput.output
  89. case var .draining(draining):
  90. let nextStateAndOutput = draining.sendMessage()
  91. self.state = nextStateAndOutput.nextState.state
  92. return nextStateAndOutput.output
  93. case var .finished(finished):
  94. let nextStateAndOutput = finished.sendMessage()
  95. self.state = nextStateAndOutput.nextState.state
  96. return nextStateAndOutput.output
  97. case .idle:
  98. preconditionFailure()
  99. }
  100. }
  101. mutating func sendStatus() -> SendStatusAction {
  102. switch self.state {
  103. case var .handling(handling):
  104. let nextStateAndOutput = handling.sendStatus()
  105. self.state = nextStateAndOutput.nextState.state
  106. return nextStateAndOutput.output
  107. case var .draining(draining):
  108. let nextStateAndOutput = draining.sendStatus()
  109. self.state = nextStateAndOutput.nextState.state
  110. return nextStateAndOutput.output
  111. case var .finished(finished):
  112. let nextStateAndOutput = finished.sendStatus()
  113. self.state = nextStateAndOutput.nextState.state
  114. return nextStateAndOutput.output
  115. case .idle:
  116. preconditionFailure()
  117. }
  118. }
  119. mutating func cancel() -> CancelAction {
  120. switch self.state {
  121. case var .idle(idle):
  122. let nextStateAndOutput = idle.cancel()
  123. self.state = nextStateAndOutput.nextState.state
  124. return nextStateAndOutput.output
  125. case var .handling(handling):
  126. let nextStateAndOutput = handling.cancel()
  127. self.state = nextStateAndOutput.nextState.state
  128. return nextStateAndOutput.output
  129. case var .draining(draining):
  130. let nextStateAndOutput = draining.cancel()
  131. self.state = nextStateAndOutput.nextState.state
  132. return nextStateAndOutput.output
  133. case var .finished(finished):
  134. let nextStateAndOutput = finished.cancel()
  135. self.state = nextStateAndOutput.nextState.state
  136. return nextStateAndOutput.output
  137. }
  138. }
  139. mutating func handlerInvoked(context: GRPCAsyncServerCallContext) {
  140. switch self.state {
  141. case var .idle(idle):
  142. let nextStateAndOutput = idle.handlerInvoked(context: context)
  143. self.state = nextStateAndOutput.nextState.state
  144. return nextStateAndOutput.output
  145. case .handling:
  146. preconditionFailure()
  147. case .draining:
  148. preconditionFailure()
  149. case .finished:
  150. preconditionFailure()
  151. }
  152. }
  153. }
  154. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  155. extension ServerHandlerStateMachine {
  156. /// The possible states the state machine may be in.
  157. fileprivate enum State {
  158. case idle(ServerHandlerStateMachine.Idle)
  159. case handling(ServerHandlerStateMachine.Handling)
  160. case draining(ServerHandlerStateMachine.Draining)
  161. case finished(ServerHandlerStateMachine.Finished)
  162. }
  163. }
  164. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  165. extension ServerHandlerStateMachine {
  166. /// The next state to transition to and any output which may be produced as a
  167. /// result of a substate handling an action.
  168. internal struct NextStateAndOutput<NextState, Output> {
  169. internal var nextState: NextState
  170. internal var output: Output
  171. internal init(nextState: NextState, output: Output) {
  172. self.nextState = nextState
  173. self.output = output
  174. }
  175. }
  176. }
  177. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  178. extension ServerHandlerStateMachine.NextStateAndOutput where Output == Void {
  179. internal init(nextState: NextState) {
  180. self.nextState = nextState
  181. self.output = ()
  182. }
  183. }
  184. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  185. extension ServerHandlerStateMachine.Idle {
  186. /// States which can be reached directly from 'Idle'.
  187. internal struct NextState {
  188. fileprivate let state: ServerHandlerStateMachine.State
  189. private init(_ state: ServerHandlerStateMachine.State) {
  190. self.state = state
  191. }
  192. internal static func idle(_ state: ServerHandlerStateMachine.Idle) -> Self {
  193. return Self(.idle(state))
  194. }
  195. internal static func handling(
  196. from: ServerHandlerStateMachine.Idle,
  197. context: GRPCAsyncServerCallContext
  198. ) -> Self {
  199. return Self(.handling(.init(from: from, context: context)))
  200. }
  201. internal static func finished(from: ServerHandlerStateMachine.Idle) -> Self {
  202. return Self(.finished(.init(from: from)))
  203. }
  204. }
  205. }
  206. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  207. extension ServerHandlerStateMachine.Handling {
  208. /// States which can be reached directly from 'Handling'.
  209. internal struct NextState {
  210. fileprivate let state: ServerHandlerStateMachine.State
  211. private init(_ state: ServerHandlerStateMachine.State) {
  212. self.state = state
  213. }
  214. internal static func handling(_ state: ServerHandlerStateMachine.Handling) -> Self {
  215. return Self(.handling(state))
  216. }
  217. internal static func draining(from: ServerHandlerStateMachine.Handling) -> Self {
  218. return Self(.draining(.init(from: from)))
  219. }
  220. internal static func finished(from: ServerHandlerStateMachine.Handling) -> Self {
  221. return Self(.finished(.init(from: from)))
  222. }
  223. }
  224. }
  225. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  226. extension ServerHandlerStateMachine.Draining {
  227. /// States which can be reached directly from 'Draining'.
  228. internal struct NextState {
  229. fileprivate let state: ServerHandlerStateMachine.State
  230. private init(_ state: ServerHandlerStateMachine.State) {
  231. self.state = state
  232. }
  233. internal static func draining(_ state: ServerHandlerStateMachine.Draining) -> Self {
  234. return Self(.draining(state))
  235. }
  236. internal static func finished(from: ServerHandlerStateMachine.Draining) -> Self {
  237. return Self(.finished(.init(from: from)))
  238. }
  239. }
  240. }
  241. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  242. extension ServerHandlerStateMachine.Finished {
  243. /// States which can be reached directly from 'Finished'.
  244. internal struct NextState {
  245. fileprivate let state: ServerHandlerStateMachine.State
  246. private init(_ state: ServerHandlerStateMachine.State) {
  247. self.state = state
  248. }
  249. internal static func finished(_ state: ServerHandlerStateMachine.Finished) -> Self {
  250. return Self(.finished(state))
  251. }
  252. }
  253. }
  254. #endif // compiler(>=5.6)