         fromPeer peerID: MCPeerID, withProgress progress: NSProgress) {
             
     }
+}
+
+extension MCSessionState {
+    func stringValue() -> String {
+        switch(self) {
+        case .NotConnected: return "NotConnected"
+        case .Connecting: return "Connecting"
+        case .Connected: return "Connected"
+        }
+    }
 }
        stopPeerSearch()
    }
    
    public func startPeerSearch() {
        isSearching = true
        if let advertiser = serviceAdvertiser {
            advertiser.startAdvertisingPeer()
        } else {
            serviceAdvertiser = MCNearbyServiceAdvertiser(peer: myPeerId,
                discoveryInfo: nil, serviceType: serviceType)
            serviceBrowser = MCNearbyServiceBrowser(peer: myPeerId, serviceType: serviceType)
            
            serviceAdvertiser?.delegate = self
            serviceBrowser?.delegate = self
            
            serviceAdvertiser?.startAdvertisingPeer()
            serviceBrowser?.startBrowsingForPeers()
        }
    }
    
    public func cancelPeerSearch() {
        stopPeerSearch()
        session.disconnect()
    }
    
    public func finishPeerSearch() -> MCSession? {
        stopPeerSearch()
        return session
    }
    
    public func stopPeerSearch() {
        if let advertiser = serviceAdvertiser {
            advertiser.stopAdvertisingPeer()
        }
        if let browser = serviceBrowser {
            browser.stopBrowsingForPeers()
        }
        isSearching = false
    }
    
    public func connectToPeer(peer: String) {
        let peerId = MCPeerID(displayName: peer)
        serviceBrowser?.invitePeer(peerId, toSession: session, withContext: nil, timeout: 30)
    }
}

extension SplooshSearch: MCNearbyServiceAdvertiserDelegate {
    // Invitation is received from peer
    public func advertiser(advertiser: MCNearbyServiceAdvertiser,
        didReceiveInvitationFromPeer peerID: MCPeerID,
        withContext context: NSData?, invitationHandler: (Bool, MCSession) -> Void) {
            
            delegate?.invitationReceived(peerID.displayName) {
                (approved) in
                invitationHandler(approved, self.session)
            }
    }
}

import UIKit

public protocol SplooshSearchDelegate: class {
    func foundHostsChanged(foundHosts: [MCPeerID])
    
    func invitationToConnectReceived(peer: MCPeerID, handleInvitation: (Bool) -> Void)
    
    func connectionsChanged(peers: [MCPeerID])
    
    func connectedToPeer(peer: MCPeerID)
    
    func handleDataPacket(data: NSData, peerID: MCPeerID)
}

public class SplooshSearch: NSObject {
    var autoAcceptGuests = true
    
    static let defaultTimeout: NSTimeInterval = 30
    private var serviceAdvertiser: MCNearbyServiceAdvertiser?
    private var serviceBrowser: MCNearbyServiceBrowser?
    private var foundHosts = [MCPeerID]()
    
    public weak var delegate: SplooshSearchDelegate?
    
