<commit_before>//
//  SocketPacketManage.swift
//  viossvc
//
//  Created by yaowang on 2016/11/22.
//  Copyright © 2016年 ywwlcom.yundian. All rights reserved.
//

import UIKit
import Alamofire

class SocketRequestManage: NSObject {
    
    static let shared = SocketRequestManage();
    fileprivate var socketRequests = [UInt64: SocketRequest]()
    fileprivate var _timer: Timer?
    fileprivate var _lastHeardBeatTime:TimeInterval!
    fileprivate var _lastConnectedTime:TimeInterval!
    fileprivate var _reqeustId:UInt32 = 10000
    fileprivate var _socketHelper:SocketHelper?
    fileprivate var _sessionId:UInt64 = 0
    fileprivate var timelineRequest: SocketRequest?
    fileprivate var productsRequest:  SocketRequest?
    fileprivate var kchartRequest: SocketRequest?
    var operate_code = 0
    func start() {
        _lastHeardBeatTime = timeNow()
        _lastConnectedTime = timeNow()
        stop()
        _timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(didActionTimer), userInfo: nil, repeats: true)
#if true
        _socketHelper = APISocketHelper()
#else
        _socketHelper = LocalSocketHelper()
#endif
        
        _socketHelper?.connect()
    }
    
    func  stop() {
        _timer?.invalidate()
        _timer = nil
        objc_sync_enter(self)
        _socketHelper?.disconnect()
        _socketHelper = nil
        objc_sync_exit(self)
    }
    
    var sessionId:UInt64 {
        get {
            objc_sync_enter(self)
            if _sessionId > 2000000000 {
                _sessionId = 10000
            }
            _sessionId += 1
            objc_sync_exit(self)
            return _sessionId;
        }
        
    }

    func notifyResponsePacket(_ packet: SocketDataPacket) {
        
        objc_sync_enter(self)
        var socketReqeust = socketRequests[packet.session_id]
        socketRequests.removeValue(forKey: packet.session_id)
        objc_sync_exit(self)
        let response:SocketJsonResponse = SocketJsonResponse(packet:packet)
        let statusCode:Int = response.statusCode;
        if ( statusCode < 0) {
            socketReqeust?.onError(statusCode)
        } else {
            socketReqeust?.onComplete(response)
        }
    }
    
    
    func checkReqeustTimeout() {
        objc_sync_enter(self)
        for (key,reqeust) in socketRequests {
            if reqeust.isReqeustTimeout() {
                socketRequests.removeValue(forKey: key)
                if NetworkReachabilityManager()?.isReachable == false{
                    reqeust.onError(-11011)
                }
                print(">>>>>>>>>>>>>>>>>>>>>>>>>>\(key)")
                break
            }
        }
        objc_sync_exit(self)
    }
    
    
    fileprivate func sendRequest(_ packet: SocketDataPacket) {
        let block:()->() = {<commit_msg>var-->let<commit_after>//
//  SocketPacketManage.swift
//  viossvc
//
//  Created by yaowang on 2016/11/22.
//  Copyright © 2016年 ywwlcom.yundian. All rights reserved.
//

import UIKit
import Alamofire

class SocketRequestManage: NSObject {
    
    static let shared = SocketRequestManage();
    fileprivate var socketRequests = [UInt64: SocketRequest]()
    fileprivate var _timer: Timer?
    fileprivate var _lastHeardBeatTime:TimeInterval!
    fileprivate var _lastConnectedTime:TimeInterval!
    fileprivate var _reqeustId:UInt32 = 10000
    fileprivate var _socketHelper:SocketHelper?
    fileprivate var _sessionId:UInt64 = 0
    fileprivate var timelineRequest: SocketRequest?
    fileprivate var productsRequest:  SocketRequest?
    fileprivate var kchartRequest: SocketRequest?
    var operate_code = 0
    func start() {
        _lastHeardBeatTime = timeNow()
        _lastConnectedTime = timeNow()
        stop()
        _timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(didActionTimer), userInfo: nil, repeats: true)
#if true
        _socketHelper = APISocketHelper()
#else
        _socketHelper = LocalSocketHelper()
#endif
        
        _socketHelper?.connect()
    }
    
    func  stop() {
        _timer?.invalidate()
        _timer = nil
        objc_sync_enter(self)
        _socketHelper?.disconnect()
        _socketHelper = nil
        objc_sync_exit(self)
    }
    
    var sessionId:UInt64 {
        get {
            objc_sync_enter(self)
            if _sessionId > 2000000000 {
                _sessionId = 10000
            }
            _sessionId += 1
            objc_sync_exit(self)
            return _sessionId;
        }
        
    }

    func notifyResponsePacket(_ packet: SocketDataPacket) {
        
        objc_sync_enter(self)
        let socketReqeust = socketRequests[packet.session_id]
        socketRequests.removeValue(forKey: packet.session_id)
        objc_sync_exit(self)
        let response:SocketJsonResponse = SocketJsonResponse(packet:packet)
        let statusCode:Int = response.statusCode;
        if ( statusCode < 0) {
            socketReqeust?.onError(statusCode)
        } else {
            socketReqeust?.onComplete(response)
        }
    }
    
    
    func checkReqeustTimeout() {
        objc_sync_enter(self)
        for (key,reqeust) in socketRequests {
            if reqeust.isReqeustTimeout() {
                socketRequests.removeValue(forKey: key)
                if NetworkReachabilityManager()?.isReachable == false{
                    reqeust.onError(-11011)
                }
                print(">>>>>>>>>>>>>>>>>>>>>>>>>>\(key)")
                break
            }
        }
        objc_sync_exit(self)
    }
    
    
    fileprivate func sendRequest(_ packet: SocketDataPacket) {
        let block:()->() = {
{'fraction_non_alphanumeric': 0.08964424092990489, 'fraction_numerical': 0.017611835153222965, 'mean_word_length': 2.1922428330522767, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1219338', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1642, 'n_words': 332}