Document.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. import Cocoa
  34. import gRPC
  35. // https://gist.github.com/rickw/cc198001f5f3aa59ae9f
  36. extension NSTextView {
  37. func appendText(line: String) {
  38. if let textStorage = self.textStorage {
  39. textStorage.append(
  40. NSAttributedString(string:line+"\n",
  41. attributes:[NSFontAttributeName:NSFont.systemFont(ofSize:12.0)]))
  42. }
  43. if let contents = self.string {
  44. self.scrollRangeToVisible(
  45. NSRange(location:contents.lengthOfBytes(using:String.Encoding.utf8),length: 0))
  46. }
  47. }
  48. }
  49. // http://stackoverflow.com/a/28976644/35844
  50. func sync(lock: AnyObject, closure: () -> Void) {
  51. objc_sync_enter(lock)
  52. closure()
  53. objc_sync_exit(lock)
  54. }
  55. class Document: NSDocument {
  56. @IBOutlet weak var hostField: NSTextField!
  57. @IBOutlet weak var portField: NSTextField!
  58. @IBOutlet weak var connectionSelector: NSSegmentedControl!
  59. @IBOutlet weak var startButton: NSButton!
  60. @IBOutlet var textView: NSTextView!
  61. // http://stackoverflow.com/questions/24062437/cannot-form-weak-reference-to-instance-of-class-nstextview
  62. var channel : Channel!
  63. var server : Server!
  64. var running: Bool // all accesses to this should be synchronized
  65. override init() {
  66. running = false
  67. super.init()
  68. }
  69. override func close() {
  70. self.textView = nil // prevents logging to the textView
  71. stop()
  72. super.close()
  73. }
  74. override var windowNibName: String? {
  75. return "Document"
  76. }
  77. func log(_ line:String) {
  78. DispatchQueue.main.async {
  79. if let view = self.textView {
  80. view.appendText(line:line)
  81. }
  82. }
  83. }
  84. @IBAction func startButtonPressed(sender: NSButton){
  85. if sender.title == "Start" {
  86. updateInterfaceBeforeStarting()
  87. let address = hostField.stringValue + ":" + portField.stringValue
  88. if (connectionSelector.selectedSegment == 0) {
  89. runClient(address:address)
  90. } else {
  91. runServer(address:address)
  92. }
  93. } else {
  94. stop()
  95. }
  96. }
  97. func updateInterfaceBeforeStarting() {
  98. startButton.title = "Stop"
  99. hostField.isEnabled = false
  100. portField.isEnabled = false
  101. connectionSelector.isEnabled = false
  102. if let textStorage = self.textView.textStorage {
  103. textStorage.setAttributedString(NSAttributedString(string:"", attributes: [:]))
  104. }
  105. }
  106. func updateInterfaceAfterStopping() {
  107. DispatchQueue.main.async {
  108. if self.startButton != nil {
  109. self.startButton.title = "Start"
  110. self.hostField.isEnabled = true
  111. self.portField.isEnabled = true
  112. self.connectionSelector.isEnabled = true
  113. }
  114. }
  115. }
  116. func setIsRunning(_ value:Bool) {
  117. sync(lock:self) {
  118. self.running = value
  119. }
  120. }
  121. func isRunning() -> Bool {
  122. var result:Bool = false
  123. sync(lock:self) {
  124. result = self.running
  125. }
  126. return result
  127. }
  128. func stop() {
  129. if (self.channel != nil) {
  130. setIsRunning(false) // stops client
  131. }
  132. if (self.server != nil) {
  133. self.server.stop() // stops server
  134. }
  135. }
  136. func runClient(address:String) {
  137. DispatchQueue.global().async {
  138. self.log("Client Starting")
  139. self.log("GRPC version " + gRPC.version())
  140. self.channel = gRPC.Channel(address:address)
  141. self.channel.host = "foo.test.google.fr"
  142. let messageData = "hello, server!".data(using: .utf8)
  143. let steps = 10
  144. self.setIsRunning(true)
  145. for i in 1...steps {
  146. if !self.isRunning() {
  147. break
  148. }
  149. let method = (i < steps) ? "/hello" : "/quit"
  150. let call = self.channel.makeCall(method)
  151. let metadata = Metadata([["x": "xylophone"],
  152. ["y": "yu"],
  153. ["z": "zither"]])
  154. do {
  155. try call.perform(message: messageData!,
  156. metadata: metadata)
  157. {(callResult) in
  158. if let initialMetadata = callResult.initialMetadata {
  159. for j in 0..<initialMetadata.count() {
  160. self.log("\(i): Received initial metadata -> " + initialMetadata.key(index:j)
  161. + " : " + initialMetadata.value(index:j))
  162. }
  163. }
  164. self.log("\(i): Received status: \(callResult.statusCode) \(callResult.statusMessage)")
  165. if callResult.statusCode != 0 {
  166. self.setIsRunning(false)
  167. }
  168. if let messageData = callResult.resultData {
  169. let messageString = String(data: messageData as Data, encoding: .utf8)
  170. self.log("\(i): Received message: " + messageString!)
  171. }
  172. if let trailingMetadata = callResult.trailingMetadata {
  173. for j in 0..<trailingMetadata.count() {
  174. self.log("\(i): Received trailing metadata -> " + trailingMetadata.key(index:j)
  175. + " : " + trailingMetadata.value(index:j))
  176. }
  177. }
  178. }
  179. } catch (let callError) {
  180. Swift.print("call error \(callError)")
  181. }
  182. self.log("------------------------------")
  183. sleep(1)
  184. }
  185. self.log("Client Stopped")
  186. self.updateInterfaceAfterStopping()
  187. }
  188. }
  189. func runServer(address:String) {
  190. self.log("Server Starting")
  191. self.log("GRPC version " + gRPC.version())
  192. self.setIsRunning(true)
  193. self.server = gRPC.Server(address:address)
  194. var requestCount = 0
  195. self.server.run() {(requestHandler) in
  196. do {
  197. requestCount += 1
  198. self.log("\(requestCount): Received request " + requestHandler.host
  199. + " " + requestHandler.method
  200. + " from " + requestHandler.caller)
  201. let initialMetadata = requestHandler.requestMetadata
  202. for i in 0..<initialMetadata.count() {
  203. self.log("\(requestCount): Received initial metadata -> " + initialMetadata.key(index:i)
  204. + ":" + initialMetadata.value(index:i))
  205. }
  206. let initialMetadataToSend = Metadata([["a": "Apple"],
  207. ["b": "Banana"],
  208. ["c": "Cherry"]])
  209. try requestHandler.receiveMessage(initialMetadata:initialMetadataToSend)
  210. {(messageData) in
  211. let messageString = String(data: messageData!, encoding: .utf8)
  212. self.log("\(requestCount): Received message: " + messageString!)
  213. }
  214. if requestHandler.method == "/quit" {
  215. self.stop()
  216. }
  217. let replyMessage = "hello, client!"
  218. let trailingMetadataToSend = Metadata([["0": "zero"],
  219. ["1": "one"],
  220. ["2": "two"]])
  221. try requestHandler.sendResponse(message:replyMessage.data(using: .utf8)!,
  222. statusCode:0,
  223. statusMessage:"OK",
  224. trailingMetadata:trailingMetadataToSend)
  225. self.log("------------------------------")
  226. } catch (let callError) {
  227. Swift.print("call error \(callError)")
  228. }
  229. }
  230. self.server.onCompletion() {
  231. self.log("Server Stopped")
  232. self.updateInterfaceAfterStopping()
  233. }
  234. }
  235. }