ServerInterceptorStateMachine.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. internal struct ServerInterceptorStateMachine {
  18. private var state: Self.State
  19. init() {
  20. self.state = .intercepting(.init())
  21. }
  22. mutating func interceptRequestMetadata() -> InterceptAction {
  23. switch self.state {
  24. case var .intercepting(intercepting):
  25. let nextStateAndOutput = intercepting.interceptRequestMetadata()
  26. self.state = nextStateAndOutput.nextState.state
  27. return nextStateAndOutput.output
  28. case var .finished(finished):
  29. let nextStateAndOutput = finished.interceptRequestMetadata()
  30. self.state = nextStateAndOutput.nextState.state
  31. return nextStateAndOutput.output
  32. }
  33. }
  34. mutating func interceptRequestMessage() -> InterceptAction {
  35. switch self.state {
  36. case var .intercepting(intercepting):
  37. let nextStateAndOutput = intercepting.interceptRequestMessage()
  38. self.state = nextStateAndOutput.nextState.state
  39. return nextStateAndOutput.output
  40. case var .finished(finished):
  41. let nextStateAndOutput = finished.interceptRequestMessage()
  42. self.state = nextStateAndOutput.nextState.state
  43. return nextStateAndOutput.output
  44. }
  45. }
  46. mutating func interceptRequestEnd() -> InterceptAction {
  47. switch self.state {
  48. case var .intercepting(intercepting):
  49. let nextStateAndOutput = intercepting.interceptRequestEnd()
  50. self.state = nextStateAndOutput.nextState.state
  51. return nextStateAndOutput.output
  52. case var .finished(finished):
  53. let nextStateAndOutput = finished.interceptRequestEnd()
  54. self.state = nextStateAndOutput.nextState.state
  55. return nextStateAndOutput.output
  56. }
  57. }
  58. mutating func interceptedRequestMetadata() -> InterceptedAction {
  59. switch self.state {
  60. case var .intercepting(intercepting):
  61. let nextStateAndOutput = intercepting.interceptedRequestMetadata()
  62. self.state = nextStateAndOutput.nextState.state
  63. return nextStateAndOutput.output
  64. case var .finished(finished):
  65. let nextStateAndOutput = finished.interceptedRequestMetadata()
  66. self.state = nextStateAndOutput.nextState.state
  67. return nextStateAndOutput.output
  68. }
  69. }
  70. mutating func interceptedRequestMessage() -> InterceptedAction {
  71. switch self.state {
  72. case var .intercepting(intercepting):
  73. let nextStateAndOutput = intercepting.interceptedRequestMessage()
  74. self.state = nextStateAndOutput.nextState.state
  75. return nextStateAndOutput.output
  76. case var .finished(finished):
  77. let nextStateAndOutput = finished.interceptedRequestMessage()
  78. self.state = nextStateAndOutput.nextState.state
  79. return nextStateAndOutput.output
  80. }
  81. }
  82. mutating func interceptedRequestEnd() -> InterceptedAction {
  83. switch self.state {
  84. case var .intercepting(intercepting):
  85. let nextStateAndOutput = intercepting.interceptedRequestEnd()
  86. self.state = nextStateAndOutput.nextState.state
  87. return nextStateAndOutput.output
  88. case var .finished(finished):
  89. let nextStateAndOutput = finished.interceptedRequestEnd()
  90. self.state = nextStateAndOutput.nextState.state
  91. return nextStateAndOutput.output
  92. }
  93. }
  94. mutating func interceptResponseMetadata() -> InterceptAction {
  95. switch self.state {
  96. case var .intercepting(intercepting):
  97. let nextStateAndOutput = intercepting.interceptResponseMetadata()
  98. self.state = nextStateAndOutput.nextState.state
  99. return nextStateAndOutput.output
  100. case var .finished(finished):
  101. let nextStateAndOutput = finished.interceptResponseMetadata()
  102. self.state = nextStateAndOutput.nextState.state
  103. return nextStateAndOutput.output
  104. }
  105. }
  106. mutating func interceptResponseMessage() -> InterceptAction {
  107. switch self.state {
  108. case var .intercepting(intercepting):
  109. let nextStateAndOutput = intercepting.interceptResponseMessage()
  110. self.state = nextStateAndOutput.nextState.state
  111. return nextStateAndOutput.output
  112. case var .finished(finished):
  113. let nextStateAndOutput = finished.interceptResponseMessage()
  114. self.state = nextStateAndOutput.nextState.state
  115. return nextStateAndOutput.output
  116. }
  117. }
  118. mutating func interceptResponseStatus() -> InterceptAction {
  119. switch self.state {
  120. case var .intercepting(intercepting):
  121. let nextStateAndOutput = intercepting.interceptResponseStatus()
  122. self.state = nextStateAndOutput.nextState.state
  123. return nextStateAndOutput.output
  124. case var .finished(finished):
  125. let nextStateAndOutput = finished.interceptResponseStatus()
  126. self.state = nextStateAndOutput.nextState.state
  127. return nextStateAndOutput.output
  128. }
  129. }
  130. mutating func interceptedResponseMetadata() -> InterceptedAction {
  131. switch self.state {
  132. case var .intercepting(intercepting):
  133. let nextStateAndOutput = intercepting.interceptedResponseMetadata()
  134. self.state = nextStateAndOutput.nextState.state
  135. return nextStateAndOutput.output
  136. case var .finished(finished):
  137. let nextStateAndOutput = finished.interceptedResponseMetadata()
  138. self.state = nextStateAndOutput.nextState.state
  139. return nextStateAndOutput.output
  140. }
  141. }
  142. mutating func interceptedResponseMessage() -> InterceptedAction {
  143. switch self.state {
  144. case var .intercepting(intercepting):
  145. let nextStateAndOutput = intercepting.interceptedResponseMessage()
  146. self.state = nextStateAndOutput.nextState.state
  147. return nextStateAndOutput.output
  148. case var .finished(finished):
  149. let nextStateAndOutput = finished.interceptedResponseMessage()
  150. self.state = nextStateAndOutput.nextState.state
  151. return nextStateAndOutput.output
  152. }
  153. }
  154. mutating func interceptedResponseStatus() -> InterceptedAction {
  155. switch self.state {
  156. case var .intercepting(intercepting):
  157. let nextStateAndOutput = intercepting.interceptedResponseStatus()
  158. self.state = nextStateAndOutput.nextState.state
  159. return nextStateAndOutput.output
  160. case var .finished(finished):
  161. let nextStateAndOutput = finished.interceptedResponseStatus()
  162. self.state = nextStateAndOutput.nextState.state
  163. return nextStateAndOutput.output
  164. }
  165. }
  166. mutating func cancel() -> CancelAction {
  167. switch self.state {
  168. case var .intercepting(intercepting):
  169. let nextStateAndOutput = intercepting.cancel()
  170. self.state = nextStateAndOutput.nextState.state
  171. return nextStateAndOutput.output
  172. case var .finished(finished):
  173. let nextStateAndOutput = finished.cancel()
  174. self.state = nextStateAndOutput.nextState.state
  175. return nextStateAndOutput.output
  176. }
  177. }
  178. }
  179. extension ServerInterceptorStateMachine {
  180. /// The possible states the state machine may be in.
  181. fileprivate enum State {
  182. case intercepting(ServerInterceptorStateMachine.Intercepting)
  183. case finished(ServerInterceptorStateMachine.Finished)
  184. }
  185. }
  186. extension ServerInterceptorStateMachine {
  187. /// The next state to transition to and any output which may be produced as a
  188. /// result of a substate handling an action.
  189. internal struct NextStateAndOutput<NextState, Output> {
  190. internal var nextState: NextState
  191. internal var output: Output
  192. internal init(nextState: NextState, output: Output) {
  193. self.nextState = nextState
  194. self.output = output
  195. }
  196. }
  197. }
  198. extension ServerInterceptorStateMachine.NextStateAndOutput where Output == Void {
  199. internal init(nextState: NextState) {
  200. self.nextState = nextState
  201. self.output = ()
  202. }
  203. }
  204. extension ServerInterceptorStateMachine.Intercepting {
  205. /// States which can be reached directly from 'Intercepting'.
  206. internal struct NextState {
  207. fileprivate let state: ServerInterceptorStateMachine.State
  208. private init(_ state: ServerInterceptorStateMachine.State) {
  209. self.state = state
  210. }
  211. internal static func intercepting(_ state: ServerInterceptorStateMachine.Intercepting) -> Self {
  212. return Self(.intercepting(state))
  213. }
  214. internal static func finished(from: ServerInterceptorStateMachine.Intercepting) -> Self {
  215. return Self(.finished(.init(from: from)))
  216. }
  217. }
  218. }
  219. extension ServerInterceptorStateMachine.Finished {
  220. /// States which can be reached directly from 'Finished'.
  221. internal struct NextState {
  222. fileprivate let state: ServerInterceptorStateMachine.State
  223. private init(_ state: ServerInterceptorStateMachine.State) {
  224. self.state = state
  225. }
  226. internal static func finished(_ state: ServerInterceptorStateMachine.Finished) -> Self {
  227. return Self(.finished(state))
  228. }
  229. }
  230. }
  231. #endif // compiler(>=5.6)