Document.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 running: Bool // all accesses to this should be synchronized
  63. override init() {
  64. running = false
  65. super.init()
  66. }
  67. override func close() {
  68. self.textView = nil // prevents logging to the textView
  69. stop()
  70. super.close()
  71. }
  72. override var windowNibName: String? {
  73. return "Document"
  74. }
  75. override func data(ofType typeName: String) throws -> Data {
  76. // Insert code here to write your document to data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning nil.
  77. // You can also choose to override fileWrapperOfType:error:, writeToURL:ofType:error:, or writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
  78. throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
  79. }
  80. override func read(from data: Data, ofType typeName: String) throws {
  81. // Insert code here to read your document from the given data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning false.
  82. // You can also choose to override readFromFileWrapper:ofType:error: or readFromURL:ofType:error: instead.
  83. // If you override either of these, you should also override -isEntireFileLoaded to return false if the contents are lazily loaded.
  84. throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
  85. }
  86. func log(_ line:String) {
  87. DispatchQueue.main.async {
  88. if let view = self.textView {
  89. view.appendText(line:line)
  90. }
  91. }
  92. }
  93. @IBAction func startButtonPressed(sender: NSButton){
  94. if sender.title == "Start" {
  95. updateInterfaceBeforeStarting()
  96. let address = hostField.stringValue + ":" + portField.stringValue
  97. if (connectionSelector.selectedSegment == 0) {
  98. startClient(address:address)
  99. } else {
  100. startServer(address:address)
  101. }
  102. } else {
  103. stop()
  104. }
  105. }
  106. func updateInterfaceBeforeStarting() {
  107. startButton.title = "Stop"
  108. hostField.isEnabled = false
  109. portField.isEnabled = false
  110. connectionSelector.isEnabled = false
  111. if let textStorage = self.textView.textStorage {
  112. textStorage.setAttributedString(NSAttributedString(string:"", attributes: [:]))
  113. }
  114. }
  115. func updateInterfaceAfterStopping() {
  116. DispatchQueue.main.async {
  117. if self.startButton != nil {
  118. self.startButton.title = "Start"
  119. self.hostField.isEnabled = true
  120. self.portField.isEnabled = true
  121. self.connectionSelector.isEnabled = true
  122. }
  123. }
  124. }
  125. func setIsRunning(_ value:Bool) {
  126. sync(lock:self) {
  127. self.running = value
  128. }
  129. }
  130. func isRunning() -> Bool {
  131. var result:Bool = false
  132. sync(lock:self) {
  133. result = self.running
  134. }
  135. return result
  136. }
  137. func stop() {
  138. setIsRunning(false)
  139. }
  140. func startServer(address:String) {
  141. DispatchQueue.global().async {
  142. self.log("Server Starting")
  143. self.log("GRPC version " + gRPC.version())
  144. self.setIsRunning(true)
  145. let server = gRPC.Server(address:address)
  146. server.start()
  147. var requestCount = 0
  148. while(self.isRunning()) {
  149. let (callError, completionType, requestHandler) = server.getNextRequest(timeout:1.0)
  150. if (callError != GRPC_CALL_OK) {
  151. self.log("\(requestCount): Call error \(callError)")
  152. self.log("------------------------------")
  153. } else if (completionType == GRPC_OP_COMPLETE) {
  154. if let requestHandler = requestHandler {
  155. requestCount += 1
  156. self.log("\(requestCount): Received request " + requestHandler.host() + " " + requestHandler.method() + " from " + requestHandler.caller())
  157. let initialMetadata = requestHandler.requestMetadata
  158. for i in 0..<initialMetadata.count() {
  159. self.log("\(requestCount): Received initial metadata -> " + initialMetadata.key(index:i) + ":" + initialMetadata.value(index:i))
  160. }
  161. let initialMetadataToSend = Metadata()
  162. initialMetadataToSend.add(key:"a", value:"Apple")
  163. initialMetadataToSend.add(key:"b", value:"Banana")
  164. initialMetadataToSend.add(key:"c", value:"Cherry")
  165. let (_, _, message) = requestHandler.receiveMessage(initialMetadata:initialMetadataToSend)
  166. if let message = message {
  167. self.log("\(requestCount): Received message: " + message.string())
  168. }
  169. if requestHandler.method() == "/quit" {
  170. self.stop()
  171. }
  172. let replyMessage = "thank you very much!"
  173. let trailingMetadataToSend = Metadata()
  174. trailingMetadataToSend.add(key:"0", value:"zero")
  175. trailingMetadataToSend.add(key:"1", value:"one")
  176. trailingMetadataToSend.add(key:"2", value:"two")
  177. let (_, _) = requestHandler.sendResponse(message:ByteBuffer(string:replyMessage),
  178. trailingMetadata:trailingMetadataToSend)
  179. self.log("------------------------------")
  180. }
  181. } else if (completionType == GRPC_QUEUE_TIMEOUT) {
  182. // everything is fine
  183. } else if (completionType == GRPC_QUEUE_SHUTDOWN) {
  184. self.stop()
  185. }
  186. }
  187. self.log("Server Stopped")
  188. self.updateInterfaceAfterStopping()
  189. }
  190. }
  191. func startClient(address:String) {
  192. DispatchQueue.global().async {
  193. self.log("Client Starting")
  194. self.log("GRPC version " + gRPC.version())
  195. self.setIsRunning(true)
  196. let host = "foo.test.google.fr"
  197. let message = gRPC.ByteBuffer(string:"hello gRPC server!")
  198. let c = gRPC.Client(address:address)
  199. let steps = 10
  200. for i in 1...steps {
  201. if !self.isRunning() {
  202. break
  203. }
  204. let method = (i < steps) ? "/hello" : "/quit"
  205. let metadata = Metadata(pairs:[MetadataPair(key:"x", value:"xylophone"),
  206. MetadataPair(key:"y", value:"yu"),
  207. MetadataPair(key:"z", value:"zither")])
  208. let response = c.performRequest(host:host,
  209. method:method,
  210. message:message,
  211. metadata:metadata)
  212. if let initialMetadata = response.initialMetadata {
  213. for j in 0..<initialMetadata.count() {
  214. self.log("\(i): Received initial metadata -> " + initialMetadata.key(index:j) + " : " + initialMetadata.value(index:j))
  215. }
  216. }
  217. self.log("\(i): Received status: \(response.status) " + response.statusDetails)
  218. if let message = response.message {
  219. self.log("\(i): Received message: " + message.string())
  220. }
  221. if let trailingMetadata = response.trailingMetadata {
  222. for j in 0..<trailingMetadata.count() {
  223. self.log("\(i): Received trailing metadata -> " + trailingMetadata.key(index:j) + " : " + trailingMetadata.value(index:j))
  224. }
  225. }
  226. self.log("------------------------------")
  227. if (response.status != 0) {
  228. break
  229. }
  230. sleep(1)
  231. }
  232. self.log("Client Stopped")
  233. self.updateInterfaceAfterStopping()
  234. }
  235. }
  236. }