<commit_before>//
//  ViewController.swift
//  Peripheral_Example
//
//  Created by <NAME> on 6/23/16.
//  Copyright © 2016 kil-dev. All rights reserved.
//

import Cocoa

protocol PeripheralDelegate: class {
    func logMessage(message: String)
    func central(central: String, didSubscribeToCharacteristic characteristic: String)
    func central(central: String, didUnsubscribeFromCharacteristic characteristic: String)
    func sending(toggle: Bool)
}

class ViewController: NSViewController {

    @IBOutlet var logView: NSTextView!

    private var _peripheral: Peripheral!
    private let _sampleData = "01234567890123456789".dataUsingEncoding(NSASCIIStringEncoding)!
    private let _kRepeatCount: UInt = 640
    private var _timer: NSTimer?
    private var _isSubscribed = false
    private var _isSending = false


    required init?(coder: NSCoder) {
        super.init(coder: coder)
        _peripheral = Peripheral(delegate: self)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override var representedObject: AnyObject? {
        didSet {
            // Update the view, if already loaded.
        }
    }

    func timerFired() {
        _peripheral.sendToSubscribers(_sampleData, repeatCount: _kRepeatCount)
    }

    private func _toggleTimer() {
        if _isSubscribed && _isSending {
            if _timer == nil {
                DLog("start timer.")
                _timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
                                                                target: self,
                                                                selector: #selector(timerFired),
                                                                userInfo: nil,
                                                                repeats: true)
                _timer?.fire()
            }
        } else {
            if let timer = _timer {
                DLog("stop timer.")
                timer.invalidate()
                _timer = nil
            }
        }
    }
}


extension ViewController: PeripheralDelegate {

    func logMessage(message: String) {
        let msg = message + "\n"
        logView.textStorage?.appendAttributedString(NSAttributedString(string: msg))
    }

    func central(central: String, didSubscribeToCharacteristic characteristic: String) {
        logMessage("subscribed central: \(central) for \(characteristic)")
        _isSubscribed = true
    }

    func central(central: String, didUnsubscribeFromCharacteristic characteristic: String) {
        logMessage("unsubscribed central: \(central) for \(characteristic)")
        _isSubscribed = false
        _toggleTimer()
    }

    func sending(toggle: Bool) {
        logMessage("sending toggled: " + (toggle ? "true" : "false"))
        _isSending = toggle<commit_msg>Fix reserve state feature of peripheral.<commit_after>//
//  ViewController.swift
//  Peripheral_Example
//
//  Created by <NAME> on 6/23/16.
//  Copyright © 2016 kil-dev. All rights reserved.
//

import Cocoa

protocol PeripheralDelegate: class {
    func logMessage(message: String)
    func central(central: String, didSubscribeToCharacteristic characteristic: String)
    func central(central: String, didUnsubscribeFromCharacteristic characteristic: String)
    func sending(toggle: Bool)
}

class ViewController: NSViewController {

    @IBOutlet var logView: NSTextView!

    private var _peripheral: Peripheral!
    private let _sampleData = "01234567890123456789".dataUsingEncoding(NSASCIIStringEncoding)!
    private let _kRepeatCount: UInt = 640
    private var _timer: NSTimer?
    private var _isSubscribed = false
    private var _isSending = false


    required init?(coder: NSCoder) {
        super.init(coder: coder)
        _peripheral = Peripheral(delegate: self)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override var representedObject: AnyObject? {
        didSet {
            // Update the view, if already loaded.
        }
    }

    func timerFired() {
        _peripheral.sendToSubscribers(_sampleData, repeatCount: _kRepeatCount)
    }

    private func _toggleTimer() {
        if _isSubscribed && _isSending {
            if _timer == nil {
                DLog("start timer.")
                _timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
                                                                target: self,
                                                                selector: #selector(timerFired),
                                                                userInfo: nil,
                                                                repeats: true)
                _timer?.fire()
            }
        } else {
            if let timer = _timer {
                DLog("stop timer.")
                timer.invalidate()
                _timer = nil
            }
        }
    }
}


extension ViewController: PeripheralDelegate {

    func logMessage(message: String) {
        let msg = message + "\n"
        logView.textStorage?.appendAttributedString(NSAttributedString(string: msg))
    }

    func central(central: String, didSubscribeToCharacteristic characteristic: String) {
        logMessage("subscribed central: \(central) for \(characteristic)")
        _isSubscribed = true
        _toggleTimer()
    }

    func central(central: String, didUnsubscribeFromCharacteristic characteristic: String) {
        logMessage("unsubscribed central: \(central) for \(characteristic)")
        _isSubscribed = false
        _toggleTimer()
    }

    func sending(toggle: Bool) {
        logMessage("sending toggled: " + (toggle ? "true" : "false"))
        _isSending = toggle
{'fraction_non_alphanumeric': 0.08365485794458084, 'fraction_numerical': 0.011925640126271484, 'mean_word_length': 1.927618069815195, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5317277', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1425, 'n_words': 363}