<commit_before>//  ConnectionManger.swift
//  MultipeerJamSession


import Foundation
import MultipeerConnectivity

//The name of the app's "service"
let JamSessionServiceType = "JamSession-srvc"

let JamSessionStringEncoding = NSASCIIStringEncoding


protocol JamSessionClientDelegate {
    
    func peerListChanged(peers: [MCPeerID])
    
    func recievedMessage(peer: MCPeerID, message: String)
    
    func recivedInvitationRequest(session: MCSession, peer: MCPeerID, accept: (Void)->(Void), reject: (Void)->(Void))
    
}

class JamSessionClient: NSObject, MCSessionDelegate {
    var delegate: JamSessionClientDelegate?
    let session: MCSession
    var outputStreams: [NSOutputStream] = []
    var peerID: MCPeerID {
        get { return self.session.myPeerID }
    }
    var peerList: [MCPeerID] {
        var currentPeers = self.session.connectedPeers as [MCPeerID]
        currentPeers.insert(self.peerID, atIndex: 0)
        return currentPeers
    }
    
    init(session: MCSession){
        self.session = session
        
        super.init()
                
        session.delegate = self
    }
    
    // Remote peer changed state
    func session(session: MCSession!, peer peerID: MCPeerID!, didChangeState state: MCSessionState) {
        
        if (state == .Connected || state == .NotConnected) {
            if let delegate = self.delegate {
                dispatch_async(dispatch_get_main_queue(), {
                    delegate.peerListChanged(self.peerList)
                })
            }
        }
        
        
    }
    
    // Received data from remote peer
    func session(session: MCSession!, didReceiveData data: NSData!, fromPeer peerID: MCPeerID!) {
        if let delegate = self.delegate {
            dispatch_sync(dispatch_get_main_queue(), {
                
                let message = NSString(data: data, encoding: JamSessionStringEncoding)
                delegate.recievedMessage(peerID, message:message)
                
            })
        }
    }
    
    // Received a byte stream from remote peer
    func session(session: MCSession!, didReceiveStream stream: NSInputStream!, withName streamName: String!, fromPeer peerID: MCPeerID!) {
        
    }
    
    // Start receiving a resource from remote peer
    func session(session: MCSession!, didStartReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, withProgress progress: NSProgress!) {
        
    }
    
    // Finished receiving a resource from remote peer and saved the content in a temporary location - the app is responsible for moving the file to a permanent location within its sandbox
    func session(session: MCSession!, didFinishReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, atURL localURL: NSURL!, withError error: NSError!) {
        
    }
    
    func sendMessage(message: String) {
        self.session.sendData(message.dataUsingEncoding(JamSessionStringEncoding, allowLossyConversion: true),
                            toPeers: self.session.connectedPeers,
                            withMode: .Reliable, error: nil)
        
        self.delegate?.recievedMessage(self.peerID, message:message)
        
    }
    
    func sendNoteOn(note:Int) {
        sendMessage("1:\(note)")
    }
    
    func sendNoteOff(note:Int) {
        sendMessage("0\(note)")
    }
    
}

class JamSessionServer : JamSessionClient, MCNearbyServiceAdvertiserDelegate
{
    let advertiser: MCNearbyServiceAdvertiser
    <commit_msg>fix typo<commit_after>//  ConnectionManger.swift
//  MultipeerJamSession


import Foundation
import MultipeerConnectivity

//The name of the app's "service"
let JamSessionServiceType = "JamSession-srvc"

let JamSessionStringEncoding = NSASCIIStringEncoding


protocol JamSessionClientDelegate {
    
    func peerListChanged(peers: [MCPeerID])
    
    func recievedMessage(peer: MCPeerID, message: String)
    
    func recivedInvitationRequest(session: MCSession, peer: MCPeerID, accept: (Void)->(Void), reject: (Void)->(Void))
    
}

class JamSessionClient: NSObject, MCSessionDelegate {
    var delegate: JamSessionClientDelegate?
    let session: MCSession
    var outputStreams: [NSOutputStream] = []
    var peerID: MCPeerID {
        get { return self.session.myPeerID }
    }
    var peerList: [MCPeerID] {
        var currentPeers = self.session.connectedPeers as [MCPeerID]
        currentPeers.insert(self.peerID, atIndex: 0)
        return currentPeers
    }
    
    init(session: MCSession){
        self.session = session
        
        super.init()
                
        session.delegate = self
    }
    
    // Remote peer changed state
    func session(session: MCSession!, peer peerID: MCPeerID!, didChangeState state: MCSessionState) {
        
        if (state == .Connected || state == .NotConnected) {
            if let delegate = self.delegate {
                dispatch_async(dispatch_get_main_queue(), {
                    delegate.peerListChanged(self.peerList)
                })
            }
        }
        
        
    }
    
    // Received data from remote peer
    func session(session: MCSession!, didReceiveData data: NSData!, fromPeer peerID: MCPeerID!) {
        if let delegate = self.delegate {
            dispatch_sync(dispatch_get_main_queue(), {
                
                let message = NSString(data: data, encoding: JamSessionStringEncoding)
                delegate.recievedMessage(peerID, message:message)
                
            })
        }
    }
    
    // Received a byte stream from remote peer
    func session(session: MCSession!, didReceiveStream stream: NSInputStream!, withName streamName: String!, fromPeer peerID: MCPeerID!) {
        
    }
    
    // Start receiving a resource from remote peer
    func session(session: MCSession!, didStartReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, withProgress progress: NSProgress!) {
        
    }
    
    // Finished receiving a resource from remote peer and saved the content in a temporary location - the app is responsible for moving the file to a permanent location within its sandbox
    func session(session: MCSession!, didFinishReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, atURL localURL: NSURL!, withError error: NSError!) {
        
    }
    
    func sendMessage(message: String) {
        self.session.sendData(message.dataUsingEncoding(JamSessionStringEncoding, allowLossyConversion: true),
                            toPeers: self.session.connectedPeers,
                            withMode: .Reliable, error: nil)
        
        self.delegate?.recievedMessage(self.peerID, message:message)
        
    }
    
    func sendNoteOn(note:Int) {
        sendMessage("1:\(note)")
    }
    
    func sendNoteOff(note:Int) {
        sendMessage("0:\(note)")
    }
    
}

class JamSessionServer : JamSessionClient, MCNearbyServiceAdvertiserDelegate
{
    let advertiser: MCNearbyServiceAdvertiser
    
{'fraction_non_alphanumeric': 0.08266932270916334, 'fraction_numerical': 0.0008537279453614115, 'mean_word_length': 2.493538767395626, 'pattern_counts': {'":': 0, '<': 3, '<?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': '120795', 'n_tokens_mistral': 2025, 'n_tokens_neox': 1923, 'n_words': 477}