<commit_before>/*
 * Copyright 2016, gRPC Authors All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import AppKit
import gRPC

class EchoViewController: NSViewController, NSTextFieldDelegate {
  @IBOutlet var messageField: NSTextField!
  @IBOutlet var sentOutputField: NSTextField!
  @IBOutlet var receivedOutputField: NSTextField!
  @IBOutlet var addressField: NSTextField!
  @IBOutlet var TLSButton: NSButton!
  @IBOutlet var callSelectButton: NSSegmentedControl!
  @IBOutlet var closeButton: NSButton!

  private var service: Echo_EchoService?

  private var expandCall: Echo_EchoExpandCall?
  private var collectCall: Echo_EchoCollectCall?
  private var updateCall: Echo_EchoUpdateCall?

  private var nowStreaming = false

  required init?(coder: NSCoder) {
    super.init(coder: coder)
  }

  private var enabled = false

  @IBAction func messageReturnPressed(sender _: NSTextField) {
    if enabled {
      do {
        try callServer(address: addressField.stringValue,
                       host: "example.com")
      } catch (let error) {
        print(error)
      }
    }
  }

  @IBAction func addressReturnPressed(sender _: NSTextField) {
    if nowStreaming {
      do {
        try sendClose()
      } catch (let error) {
        print(error)
      }
    }
    // invalidate the service
    service = nil
  }

  @IBAction func buttonValueChanged(sender _: NSSegmentedControl) {
    if nowStreaming {
      do {
        try sendClose()
      } catch (let error) {
        print(error)
      }
    }
  }

  @IBAction func closeButtonPressed(sender _: NSButton) {
    if nowStreaming {
      do {
        try sendClose()
      } catch (let error) {
        print(error)
      }
    }
  }

  override func viewDidLoad() {
    closeButton.isEnabled = false
    // prevent the UI from trying to send messages until gRPC is initialized
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
      self.enabled = true
    }
  }

  func displayMessageSent(_ message: String) {
    DispatchQueue.main.async {
      self.sentOutputField.stringValue = message
    }
  }

  func displayMessageReceived(_ message: String) {
    DispatchQueue.main.async {
      self.receivedOutputField.stringValue = message
    }
  }

  func prepareService(address: String, host: String) {
    if service != nil {
      return
    }
    if TLSButton.intValue == 0 {
      service = Echo_EchoService(address: address, secure: false)
    } else {
      let certificateURL = Bundle.main.url(forResource: "ssl",
                                           withExtension: "crt")!
      let certificates = try! String(contentsOf: certificateURL)
      service = Echo_EchoService(address: address, certificates: certificates, host: host)
    }
    if let service = service {
      service.host = "example.com" // sample override
      service.metadata = Metadata([
        "x-goog-api-key": "YOUR_API_KEY",
        "x-ios-bundle-identifier": Bundle.main.bundleIdentifier!
      ])
    }
  }

  func callServer(address: String, host: String) throws {
    prepareService(address: address, host: host)<commit_msg>Update Echo Xcode sample to use latest API.<commit_after>/*
 * Copyright 2016, gRPC Authors All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import AppKit
import gRPC

class EchoViewController: NSViewController, NSTextFieldDelegate {
  @IBOutlet var messageField: NSTextField!
  @IBOutlet var sentOutputField: NSTextField!
  @IBOutlet var receivedOutputField: NSTextField!
  @IBOutlet var addressField: NSTextField!
  @IBOutlet var TLSButton: NSButton!
  @IBOutlet var callSelectButton: NSSegmentedControl!
  @IBOutlet var closeButton: NSButton!

  private var service: Echo_EchoServiceClient?

  private var expandCall: Echo_EchoExpandCall?
  private var collectCall: Echo_EchoCollectCall?
  private var updateCall: Echo_EchoUpdateCall?

  private var nowStreaming = false

  required init?(coder: NSCoder) {
    super.init(coder: coder)
  }

  private var enabled = false

  @IBAction func messageReturnPressed(sender _: NSTextField) {
    if enabled {
      do {
        try callServer(address: addressField.stringValue,
                       host: "example.com")
      } catch (let error) {
        print(error)
      }
    }
  }

  @IBAction func addressReturnPressed(sender _: NSTextField) {
    if nowStreaming {
      do {
        try sendClose()
      } catch (let error) {
        print(error)
      }
    }
    // invalidate the service
    service = nil
  }

  @IBAction func buttonValueChanged(sender _: NSSegmentedControl) {
    if nowStreaming {
      do {
        try sendClose()
      } catch (let error) {
        print(error)
      }
    }
  }

  @IBAction func closeButtonPressed(sender _: NSButton) {
    if nowStreaming {
      do {
        try sendClose()
      } catch (let error) {
        print(error)
      }
    }
  }

  override func viewDidLoad() {
    closeButton.isEnabled = false
    // prevent the UI from trying to send messages until gRPC is initialized
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
      self.enabled = true
    }
  }

  func displayMessageSent(_ message: String) {
    DispatchQueue.main.async {
      self.sentOutputField.stringValue = message
    }
  }

  func displayMessageReceived(_ message: String) {
    DispatchQueue.main.async {
      self.receivedOutputField.stringValue = message
    }
  }

  func prepareService(address: String, host: String) {
    if service != nil {
      return
    }
    if TLSButton.intValue == 0 {
      service = Echo_EchoServiceClient(address: address, secure: false)
    } else {
      let certificateURL = Bundle.main.url(forResource: "ssl",
                                           withExtension: "crt")!
      let certificates = try! String(contentsOf: certificateURL)
      service = Echo_EchoServiceClient(address: address, certificates: certificates, host: host)
    }
    if let service = service {
      service.host = "example.com" // sample override
      service.metadata = Metadata([
        "x-goog-api-key": "YOUR_API_KEY",
        "x-ios-bundle-identifier": Bundle.main.bundleIdentifier!
      ])
    }
  }

  func callServer(address: String, host: String) throws {
    prepareService(address: address, host: host)
{'fraction_non_alphanumeric': 0.08591394902713072, 'fraction_numerical': 0.0030145245272677444, 'mean_word_length': 2.8476541908276225, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2644547', 'n_tokens_mistral': 2169, 'n_tokens_neox': 2074, 'n_words': 620}