Document.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 client : Client!
  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.client != 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.client = gRPC.Client(address:address)
  141. let 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.client.createCall(host: host, method: method, timeout: 30)
  151. let metadata = Metadata([["x": "xylophone"],
  152. ["y": "yu"],
  153. ["z": "zither"]])
  154. _ = call.performNonStreamingCall(messageData: messageData!,
  155. metadata: metadata)
  156. {(status, statusDetails, messageData, initialMetadata, trailingMetadata) in
  157. if let initialMetadata = initialMetadata {
  158. for j in 0..<initialMetadata.count() {
  159. self.log("\(i): Received initial metadata -> " + initialMetadata.key(index:j)
  160. + " : " + initialMetadata.value(index:j))
  161. }
  162. }
  163. self.log("\(i): Received status: \(status) \(statusDetails)")
  164. if status != 0 {
  165. self.setIsRunning(false)
  166. }
  167. if let messageData = messageData {
  168. let messageString = String(data: messageData as Data, encoding: .utf8)
  169. self.log("\(i): Received message: " + messageString!)
  170. }
  171. if let trailingMetadata = trailingMetadata {
  172. for j in 0..<trailingMetadata.count() {
  173. self.log("\(i): Received trailing metadata -> " + trailingMetadata.key(index:j)
  174. + " : " + trailingMetadata.value(index:j))
  175. }
  176. }
  177. self.log("------------------------------")
  178. }
  179. sleep(1)
  180. }
  181. self.log("Client Stopped")
  182. self.updateInterfaceAfterStopping()
  183. }
  184. }
  185. func runServer(address:String) {
  186. self.log("Server Starting")
  187. self.log("GRPC version " + gRPC.version())
  188. self.setIsRunning(true)
  189. self.server = gRPC.Server(address:address)
  190. var requestCount = 0
  191. self.server.run() {(requestHandler) in
  192. requestCount += 1
  193. self.log("\(requestCount): Received request " + requestHandler.host()
  194. + " " + requestHandler.method()
  195. + " from " + requestHandler.caller())
  196. let initialMetadata = requestHandler.requestMetadata
  197. for i in 0..<initialMetadata.count() {
  198. self.log("\(requestCount): Received initial metadata -> " + initialMetadata.key(index:i)
  199. + ":" + initialMetadata.value(index:i))
  200. }
  201. let initialMetadataToSend = Metadata([["a": "Apple"],
  202. ["b": "Banana"],
  203. ["c": "Cherry"]])
  204. requestHandler.receiveMessage(initialMetadata:initialMetadataToSend)
  205. {(messageData) in
  206. let messageString = String(data: messageData!, encoding: .utf8)
  207. self.log("\(requestCount): Received message: " + messageString!)
  208. }
  209. if requestHandler.method() == "/quit" {
  210. self.stop()
  211. }
  212. let replyMessage = "hello, client!"
  213. let trailingMetadataToSend = Metadata([["0": "zero"],
  214. ["1": "one"],
  215. ["2": "two"]])
  216. requestHandler.sendResponse(message:replyMessage.data(using: .utf8)!,
  217. trailingMetadata:trailingMetadataToSend)
  218. self.log("------------------------------")
  219. }
  220. self.server.onCompletion() {
  221. self.log("Server Stopped")
  222. self.updateInterfaceAfterStopping()
  223. }
  224. }
  225. }