//
//  Broadcaster.swift
//  HeadNavigationBroadcaster
//
//  Created by Sergii Kryvoblotskyi on 5/20/21.
//

import Foundation
import DTBonjour

final class Broadcaster: NSObject {
    
    private let server: DTBonjourServer
    
    override init() {
        self.server = DTBonjourServer(bonjourType:  "_mt_broadcasting_protocol._tcp.")
        super.init()
        self.server.delegate = self
    }
    
    func start() {
        self.server.start()
    }
    
    func broadcast(gesture: HeadGesture) {
        let encoder = JSONEncoder()
        do {
            let data = try encoder.encode(gesture)
            self.server.broadcast(data)
        } catch {
            print(error)
        }
    }
}

extension Broadcaster: DTBonjourServerDelegate {
    
    func bonjourServer(_ server: DTBonjourServer!, didReceive object: Any!, on connection: DTBonjourDataConnection!) {
        print("did receive")
    }
    
    func bonjourServer(_ server: DTBonjourServer!, didAccept connection: DTBonjourDataConnection!) {
        
        print("connect")
    }
}
