<commit_before>//
//  WebSocketTransport.swift
//  WebSocketTransport
//
//  Created by <NAME> on 28/08/2014.
//  Copyright (c) 2014 CocoaFlow. All rights reserved.
//

import Transport
import MessageTransfer
import JSONLib

public struct WebSocketTransport: Transport {
    
    private let webSocketServer = BLWebSocketsServer.sharedInstance()
    public var messageReceiver: MessageReceiver?
    
    // TODO: Make port option in protocol
    public init() {
    public init(var _ messageReceiver: MessageReceiverWorkaround) {
        self.webSocketServer.setHandleRequestBlock { (data) -> NSData! in
            let jsonString = NSString(data: data, encoding: UInt())

            if let json = JSON.parse(jsonString).value {
                let channel = json["protocol"].string
                let topic = json["command"].string
                let jsonPayload: JSON = json["payload"]
                let payload = jsonPayload.object
                
                switch (channel, topic, payload) {
                case let (.Some(channel), .Some(topic), .Some(payload)):
                    println(channel)
                    println(topic)
                    println(jsonPayload)
                default:
                    break
                }
            }
            
            // FIXME - transport has no knowledge of the runtime so can't call runtime.receive()
            
            // TODO: Determine if returning nil is valid - "Attempt to write empty data on the websocket"
            return nil
        }
        
        self.webSocketServer.startListeningOnPort(3569, withProtocolName: nil) { (error) in
            if (error != nil) {
                println(error)
            }
        }
    }
    
    public func send(channel: String, _ topic: String, _ payload: JSON) {
        // TODO
    }
}<commit_msg>Update implementation to cause spec to pass.<commit_after>//
//  WebSocketTransport.swift
//  WebSocketTransport
//
//  Created by <NAME> on 28/08/2014.
//  Copyright (c) 2014 CocoaFlow. All rights reserved.
//

import Transport
import MessageTransfer
import JSONLib

public struct WebSocketTransport: Transport {
    
    private let webSocketServer = BLWebSocketsServer.sharedInstance()
    public var messageReceiver: MessageReceiver?
    
    // TODO: Make port option in protocol
    public init(var _ messageReceiver: MessageReceiverWorkaround) {
        self.messageReceiver = messageReceiver
        messageReceiver.messageSender = self
        
        self.webSocketServer.setHandleRequestBlock { (data) -> NSData! in
            
            if let receiver = self.messageReceiver {
                let jsonString = NSString(data: data, encoding: UInt())

                if let json = JSON.parse(jsonString).value {
                    let channel = json["protocol"].string
                    let topic = json["command"].string
                    let jsonPayload: JSON = json["payload"]
                    let payload = jsonPayload.object
                    
                    switch (channel, topic, payload) {
                    case let (.Some(channel), .Some(topic), .Some(payload)):
                        receiver.receive(channel, topic, jsonPayload)
                    default:
                        break
                    }
                }
            }
            
            // TODO: Determine if returning nil is valid - "Attempt to write empty data on the websocket"
            return nil
        }
        
        self.webSocketServer.startListeningOnPort(3569, withProtocolName: nil) { (error) in
            if (error != nil) {
                println(error)
            }
        }
    }
    
    public func send(channel: String, _ topic: String, _ payload: JSON) {
        // TODO
    }
}
{'fraction_non_alphanumeric': 0.08401585204755614, 'fraction_numerical': 0.00845442536327609, 'mean_word_length': 1.6643209007741027, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '358778', 'n_tokens_mistral': 965, 'n_tokens_neox': 891, 'n_words': 266}