<commit_before>/*
 * Copyright 2015 <EMAIL>
 *
 * 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 Foundation
import MultipeerConnectivity

// Delegate method for SessionController
protocol SessionControllerDelegate {
    // Multipeer Connectivity session changed state - connecting, connected and disconnected peers changed
    func sessionDidChangeState()
}

/*!
@class SessionController
@abstract
A SessionController creates the MCSession that peers will be invited/join
into, as well as creating service advertisers and browsers.

MCSessionDelegate calls occur on a private operation queue. If your app
needs to perform an action on a particular run loop or operation queue,
its delegate method should explicitly dispatch or schedule that work
*/
class SessionController: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate {
    
    // MARK: Public properties
    
    var connectedPeers: [MCPeerID] {
        get {
            return session!.connectedPeers
        }
    }
    
    var connectingPeers: [MCPeerID] {
        get {
            return connectingPeersOrderedSet.array as! [MCPeerID]
        }
    }

    var disconnectedPeers: [MCPeerID] {
        get {
            return disconnectedPeersOrderedSet.array as! [MCPeerID]
        }
    }
    
    var displayName: NSString {
        get {
            return session!.myPeerID.displayName
        }
    }

    var delegate: SessionControllerDelegate?
    
    // MARK: Private properties
    
    private var session: MCSession?
    private var serviceAdvertiser: MCNearbyServiceAdvertiser?
    private var serviceBrowser: MCNearbyServiceBrowser?
    
    // Connected peers are stored in the MCSession
    // Manually track connecting and disconnected peers
    private var connectingPeersOrderedSet = NSMutableOrderedSet()
    private var disconnectedPeersOrderedSet = NSMutableOrderedSet()

    private let kMCSessionServiceType = "mcsessionp2p"

    // MARK: Initializer

    override init() {
        super.init()
        
        // Register for notifications
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "startServices", name: UIApplicationWillEnterForegroundNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "stopServices", name: UIApplicationDidEnterBackgroundNotification, object: nil)
        
        startServices()
    }

    // MARK: Deinitialization

    deinit {
        // Unregister for notifications on deinitialization.
        NSNotificationCenter.defaultCenter().removeObserver(self)
        
        // Nil out delegates
        session?.delegate = nil
        serviceAdvertiser?.delegate = nil
        serviceBrowser?.delegate = nil
    }

    // MARK: Multipeer Connectivity session setup / teardown

    private func setupSession() {
        // Create the session that peers will be invited/join into.
        let peerID = MCPeerID(displayName: UIDevice.currentDevice().name)
        session = MCSession(peer: peerID)
        session?.delegate = self
        
        // Create the service advertiser
        serviceAdvertiser = MCNearbyServiceAdvertiser(peer: peerID, discoveryInfo: nil, serviceType: kMCSessionServiceType)
        serviceAdvertiser?.delegate = self
        
        // Create the service browser
        serviceBrowser = MCNearbyServiceBrowser(peer: peerID, serviceType: kMCSessionServiceType)
        serviceBrowser?.delegate = self
    }

    private func teardownSession() {
        session?.disconnect()
        connectingPeersOrderedSet.removeAllObjects()
        disconnectedPeersOrderedSet.removeAllObjects()
    }
    
    // MARK: Services start / stop

    private func startServices() {
        setupSession()
        serviceAdvertiser?.startAdvertisingPeer()
        serviceBrowser?.startBrowsingForPeers()
    }

    private func stopServices() {
        serviceBrowser?.stopBrowsingForPeers()
        serviceAdvertiser?.stopAdvertisingPeer()
        teardownSession()
    }

    // MARK: MCSessionDelegate protocol conformance<commit_msg>Revised access level of startServices and stopServices<commit_after>/*
 * Copyright 2015 <EMAIL>
 *
 * 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 Foundation
import MultipeerConnectivity

// Delegate method for SessionController
protocol SessionControllerDelegate {
    // Multipeer Connectivity session changed state - connecting, connected and disconnected peers changed
    func sessionDidChangeState()
}

/*!
@class SessionController
@abstract
A SessionController creates the MCSession that peers will be invited/join
into, as well as creating service advertisers and browsers.

MCSessionDelegate calls occur on a private operation queue. If your app
needs to perform an action on a particular run loop or operation queue,
its delegate method should explicitly dispatch or schedule that work
*/
class SessionController: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate {
    
    // MARK: Public properties
    
    var connectedPeers: [MCPeerID] {
        get {
            return session!.connectedPeers
        }
    }
    
    var connectingPeers: [MCPeerID] {
        get {
            return connectingPeersOrderedSet.array as! [MCPeerID]
        }
    }

    var disconnectedPeers: [MCPeerID] {
        get {
            return disconnectedPeersOrderedSet.array as! [MCPeerID]
        }
    }
    
    var displayName: NSString {
        get {
            return session!.myPeerID.displayName
        }
    }

    var delegate: SessionControllerDelegate?
    
    // MARK: Private properties
    
    private var session: MCSession?
    private var serviceAdvertiser: MCNearbyServiceAdvertiser?
    private var serviceBrowser: MCNearbyServiceBrowser?
    
    // Connected peers are stored in the MCSession
    // Manually track connecting and disconnected peers
    private var connectingPeersOrderedSet = NSMutableOrderedSet()
    private var disconnectedPeersOrderedSet = NSMutableOrderedSet()

    private let kMCSessionServiceType = "mcsessionp2p"

    // MARK: Initializer

    override init() {
        super.init()
        
        // Register for notifications
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "startServices", name: UIApplicationWillEnterForegroundNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "stopServices", name: UIApplicationDidEnterBackgroundNotification, object: nil)
        
        startServices()
    }

    // MARK: Deinitialization

    deinit {
        // Unregister for notifications on deinitialization.
        NSNotificationCenter.defaultCenter().removeObserver(self)
        
        // Nil out delegates
        session?.delegate = nil
        serviceAdvertiser?.delegate = nil
        serviceBrowser?.delegate = nil
    }

    // MARK: Multipeer Connectivity session setup / teardown

    private func setupSession() {
        // Create the session that peers will be invited/join into.
        let peerID = MCPeerID(displayName: UIDevice.currentDevice().name)
        session = MCSession(peer: peerID)
        session?.delegate = self
        
        // Create the service advertiser
        serviceAdvertiser = MCNearbyServiceAdvertiser(peer: peerID, discoveryInfo: nil, serviceType: kMCSessionServiceType)
        serviceAdvertiser?.delegate = self
        
        // Create the service browser
        serviceBrowser = MCNearbyServiceBrowser(peer: peerID, serviceType: kMCSessionServiceType)
        serviceBrowser?.delegate = self
    }

    private func teardownSession() {
        session?.disconnect()
        connectingPeersOrderedSet.removeAllObjects()
        disconnectedPeersOrderedSet.removeAllObjects()
    }
    
    // MARK: Services start / stop

    func startServices() {
        setupSession()
        serviceAdvertiser?.startAdvertisingPeer()
        serviceBrowser?.startBrowsingForPeers()
    }

    func stopServices() {
        serviceBrowser?.stopBrowsingForPeers()
        serviceAdvertiser?.stopAdvertisingPeer()
        teardownSession()
    }

    // MARK: MCSessionDelegate protocol conformance
{'fraction_non_alphanumeric': 0.06629834254143646, 'fraction_numerical': 0.0019499512512187196, 'mean_word_length': 3.0156589821661592, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7132403', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2289, 'n_words': 721}