<commit_before>//
//  ViewController.swift
//  Autobahn
//
//  Created by <NAME> on 7/24/15.
//  Copyright (c) 2015 vluxe. All rights reserved.
//

import UIKit
import Starscream

class ViewController: UIViewController {
    
    let host = "localhost:9001"
    var socketArray = [WebSocket]()
    var caseCount = 300 //starting cases
    override func viewDidLoad() {
        super.viewDidLoad()
        getCaseCount()
        //getTestInfo(1)
    }
    
    func removeSocket(_ s: WebSocket) {
        socketArray = socketArray.filter{$0 != s}
    }
    
    func getCaseCount() {
        
        let s = WebSocket(url: URL(string: "ws://\(host)/getCaseCount")!, protocols: [])
        socketArray.append(s)
        s.onText = {[unowned self] (text: String) in
            if let c = Int(text) {
                print("number of cases is: \(c)")
                self.caseCount = c
            }
        }
        s.onDisconnect = {[unowned self] (error: NSError?) in
            self.getTestInfo(1)
            self.removeSocket(s)
        }
        s.connect()
    }
    
    func getTestInfo(_ caseNum: Int) {
        let s = createSocket("getCaseInfo",caseNum)
        socketArray.append(s)
        s.onText = {(text: String) in
//            let data = text.dataUsingEncoding(NSUTF8StringEncoding)
//            do {
//                let resp: AnyObject? = try NSJSONSerialization.JSONObjectWithData(data!,
//                    options: NSJSONReadingOptions())
//                if let dict = resp as? Dictionary<String,String> {
//                    let num = dict["id"]
//                    let summary = dict["description"]
//                    if let n = num, let sum = summary {
//                        print("running case:\(caseNum) id:\(n) summary: \(sum)")
//                    }
//                }
//            } catch {
//                print("error parsing the json")
//            }

        }
        var once = false
        s.onDisconnect = {[unowned self] (error: NSError?) in
            if !once {
                once = true
                self.runTest(caseNum)
            }
            self.removeSocket(s)
        }
        s.connect()
    }
    
    func runTest(_ caseNum: Int) {
        let s = createSocket("runCase",caseNum)
        self.socketArray.append(s)
        s.onText = {(text: String) in
            s.write(string: text)
        }
        s.onData = {(data: Data) in
            s.write(data: data)
        }
        var once = false
        s.onDisconnect = {[unowned self] (error: NSError?) in
            if !once {
                once = true
                print("case:\(caseNum) finished")
                self.verifyTest(caseNum)
                self.removeSocket(s)
            }
        }
        s.connect()
    }
    
    func verifyTest(_ caseNum: Int) {
        let s = createSocket("getCaseStatus",caseNum)
        self.socketArray.append(s)
        s.onText = {(text: String) in
            let data = text.data(using: String.Encoding.utf8)
            do {
                let resp: Any? = try JSONSerialization.jsonObject(with: data!,
                    options: JSONSerialization.ReadingOptions())
                if let dict = resp as? Dictionary<String,String> {
                    if let status = dict["behavior"] {
                        if status == "OK" {
                            print("SUCCESS: \(caseNum)")
                            return
                        }
                    }
                    print("FAILURE: \(caseNum)")
                }
            } catch {
               print("error parsing the json")
            }
        }
        var once = false
        s.onDisconnect = {[unowned self] (error: NSError?) in
            if !once {
                once = true
                let nextCase = caseNum+1
                if nextCase <= self.caseCount {
                    self.getTestInfo(nextCase)
                } else {
                    self.finishReports()
                }
            }
            self.removeSocket(s)
        }
        s.connect()
    }
    
    func finishReports() {
        let s = createSocket("updateReports",0)
        self.socketArray.append(s)
        s.onDisconnect = {[unowned self] (error: NSError?) in
            print("finished all the tests!")
            self.removeSocket(s)
        }
        s.connect()
    }
    
    func createSocket(_ cmd: String, _ caseNum: Int) -> WebSocket {
        return WebSocket(url: URL(string: "ws://\(host)\(buildPath(cmd,caseNum))")!, protocols: [])
    }
    
    func buildPath(_ cmd: String, _ caseNum: Int) -> String {
        return "/\(cmd)?case=\(caseNum)&agent=Starscream"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
<commit_msg>Fix capture memory leaks in Autobahn tester.<commit_after>//
//  ViewController.swift
//  Autobahn
//
//  Created by <NAME> on 7/24/15.
//  Copyright (c) 2015 vluxe. All rights reserved.
//

import UIKit
import Starscream

class ViewController: UIViewController {
    
    let host = "localhost:9001"
    var socketArray = [WebSocket]()
    var caseCount = 300 //starting cases
    override func viewDidLoad() {
        super.viewDidLoad()
        getCaseCount()
        //getTestInfo(1)
    }
    
    func removeSocket(_ s: WebSocket?) {
        socketArray = socketArray.filter{$0 != s}
    }
    
    func getCaseCount() {
        
        let s = WebSocket(url: URL(string: "ws://\(host)/getCaseCount")!, protocols: [])
        socketArray.append(s)
        s.onText = { [weak self]  (text: String) in
            if let c = Int(text) {
                print("number of cases is: \(c)")
                self?.caseCount = c
            }
        }
        s.onDisconnect = { [weak self, weak s]  (error: NSError?) in
            self?.getTestInfo(1)
            self?.removeSocket(s)
        }
        s.connect()
    }
    
    func getTestInfo(_ caseNum: Int) {
        let s = createSocket("getCaseInfo",caseNum)
        socketArray.append(s)
        s.onText = { (text: String) in
//            let data = text.dataUsingEncoding(NSUTF8StringEncoding)
//            do {
//                let resp: AnyObject? = try NSJSONSerialization.JSONObjectWithData(data!,
//                    options: NSJSONReadingOptions())
//                if let dict = resp as? Dictionary<String,String> {
//                    let num = dict["id"]
//                    let summary = dict["description"]
//                    if let n = num, let sum = summary {
//                        print("running case:\(caseNum) id:\(n) summary: \(sum)")
//                    }
//                }
//            } catch {
//                print("error parsing the json")
//            }

        }
        var once = false
        s.onDisconnect = { [weak self, weak s]  (error: NSError?) in
            if !once {
                once = true
                self?.runTest(caseNum)
            }
            self?.removeSocket(s)
        }
        s.connect()
    }
    
    func runTest(_ caseNum: Int) {
        let s = createSocket("runCase",caseNum)
        self.socketArray.append(s)
        s.onText = { [weak s]  (text: String) in
            s?.write(string: text)
        }
        s.onData = { [weak s]  (data: Data) in
            s?.write(data: data)
        }
        var once = false
        s.onDisconnect = {[weak self, weak s] (error: NSError?) in
            if !once {
                once = true
                print("case:\(caseNum) finished")
                self?.verifyTest(caseNum)
                self?.removeSocket(s)
            }
        }
        s.connect()
    }
    
    func verifyTest(_ caseNum: Int) {
        let s = createSocket("getCaseStatus",caseNum)
        self.socketArray.append(s)
        s.onText = { (text: String) in
            let data = text.data(using: String.Encoding.utf8)
            do {
                let resp: Any? = try JSONSerialization.jsonObject(with: data!,
                    options: JSONSerialization.ReadingOptions())
                if let dict = resp as? Dictionary<String,String> {
                    if let status = dict["behavior"] {
                        if status == "OK" {
                            print("SUCCESS: \(caseNum)")
                            return
                        }
                    }
                    print("FAILURE: \(caseNum)")
                }
            } catch {
               print("error parsing the json")
            }
        }
        var once = false
        s.onDisconnect = { [weak self, weak s]  (error: NSError?) in
            if !once {
                once = true
                let nextCase = caseNum+1
                if nextCase <= (self?.caseCount)! {
                    self?.getTestInfo(nextCase)
                } else {
                    self?.finishReports()
                }
            }
            self?.removeSocket(s)
        }
        s.connect()
    }
    
    func finishReports() {
        let s = createSocket("updateReports",0)
        self.socketArray.append(s)
        s.onDisconnect = { [weak self, weak s]  (error: NSError?) in
            print("finished all the tests!")
            self?.removeSocket(s)
        }
        s.connect()
    }
    
    func createSocket(_ cmd: String, _ caseNum: Int) -> WebSocket {
        return WebSocket(url: URL(string: "ws://\(host)\(buildPath(cmd,caseNum))")!, protocols: [])
    }
    
    func buildPath(_ cmd: String, _ caseNum: Int) -> String {
        return "/\(cmd)?case=\(caseNum)&agent=Starscream"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

{'fraction_non_alphanumeric': 0.11787498725400224, 'fraction_numerical': 0.0046905271744672175, 'mean_word_length': 1.377120698012603, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6032065', 'n_tokens_mistral': 2858, 'n_tokens_neox': 2714, 'n_words': 640}