<commit_before>dgram = require 'dgram'
url = require 'url'
event = require('events').EventEmitter
device = require './device'

event = new event

event.on 'ssdpMsg', (msg) ->
    console.log msg

# Initializes a UPnP Device
start = (config, callback) ->
    
    # unique ID's used by Control Points
    bootId = new Date().getTime()
    configId = Math.floor(Math.random() * 16777216)

    # generate SSDP (HTTPU/HTTPMU) header suiting the message type
    makeMessage = (type, nt, nts) ->
        makeServerString = ->
            [
                'OS/1.0 '
                'UPnP/1.0 '
                config['app']['name']
                '/'
                config['app']['version']
            ].join('')

        makeDescriptionUrl = ->
            url.format(
                protocol: 'http:'
                hostname: config['network']['http']['address']
                port: config['network']['http']['port']
                pathname: '/device/description'
            )

        # possible headers and values
        headers =
            'HOST': config['network']['ssdp']['address'] + ':' + config['network']['ssdp']['port']
            'CACHE-CONTROL': 'max-age=' + config['network']['ssdp']['timeout']
            'LOCATION': makeDescriptionUrl config
            'NT': nt
            'NTS': 'ssdp:' + nts
            'SERVER': makeServerString config
            'USN': config['device']['uuid']
            'BOOTID.UPNP.ORG': bootId
            'CONFIGID.UPNP.ORG': configId

        # append Notification Type to USN, except for
        # requests with only uuid as NT
        unless nt is config['device']['uuid']
            headers['USN'] += '::' + nt

        switch type
            when 'NOTIFY'
                useHeaders = [ 'HOST', 'CACHE-CONTROL', 'LOCATION',
                               'NT', 'NTS', 'SERVER', 'USN',
                               'BOOTID.UPNP.ORG', 'CONFIGID.UPNP.ORG' ]
            else
                useHeaders = []

        message = [ type + ' * HTTP/1.1' ]
        for header in useHeaders
            message.push header + ': ' + headers[header]

        # add carriage returns and new lines as required by HTTP spec
        message.push '\r\n'
        message.join '\r\n'


    ssdpSend = (socket, message, callback) ->
        socket.send message, 0, message.length, config['network']['ssdp']['port'], config['network']['ssdp']['address'], (err) ->
            event.emit 'ssdpMsg', "SSDP message sent on port #{socket.address().port}"
            callback err

    makeAdvDelay = ->
        # recommended delay between advertisements is a random interval of less than half of timeout
        max = (config['network']['ssdp']['timeout'] / 2) * 1000
        min = 100
        Math.floor(Math.random() * (max - min))

    advertise = (callback) ->
        socket = dgram.createSocket 'udp4'
        socket.bind()
        socket.setBroadcast 1
        socket.setMulticastTTL 2
        # First send three notification messages as per UPnP specification<commit_msg>These were for Device Architecture 1.1, stick to 1.0 for now<commit_after>dgram = require 'dgram'
url = require 'url'
event = require('events').EventEmitter
device = require './device'

event = new event

event.on 'ssdpMsg', (msg) ->
    console.log msg

# Initializes a UPnP Device
start = (config, callback) ->
    
    # generate SSDP (HTTPU/HTTPMU) header suiting the message type
    makeMessage = (type, nt, nts) ->
        makeServerString = ->
            [
                'OS/1.0 '
                'UPnP/1.0 '
                config['app']['name']
                '/'
                config['app']['version']
            ].join('')

        makeDescriptionUrl = ->
            url.format(
                protocol: 'http:'
                hostname: config['network']['http']['address']
                port: config['network']['http']['port']
                pathname: '/device/description'
            )

        # possible headers and values
        headers =
            'HOST': config['network']['ssdp']['address'] + ':' + config['network']['ssdp']['port']
            'CACHE-CONTROL': 'max-age=' + config['network']['ssdp']['timeout']
            'LOCATION': makeDescriptionUrl config
            'NT': nt
            'NTS': 'ssdp:' + nts
            'SERVER': makeServerString config
            'USN': config['device']['uuid']

        # append Notification Type to USN, except for
        # requests with only uuid as NT
        unless nt is config['device']['uuid']
            headers['USN'] += '::' + nt

        switch type
            when 'NOTIFY'
                useHeaders = [ 'HOST', 'CACHE-CONTROL', 'LOCATION',
                               'NT', 'NTS', 'SERVER', 'USN' ]
            else
                useHeaders = []

        message = [ type + ' * HTTP/1.1' ]
        for header in useHeaders
            message.push header + ': ' + headers[header]

        # add carriage returns and new lines as required by HTTP spec
        message.push '\r\n'
        message.join '\r\n'


    ssdpSend = (socket, message, callback) ->
        socket.send message, 0, message.length, config['network']['ssdp']['port'], config['network']['ssdp']['address'], (err) ->
            event.emit 'ssdpMsg', "SSDP message sent on port #{socket.address().port}"
            callback err

    makeAdvDelay = ->
        # recommended delay between advertisements is a random interval of less than half of timeout
        max = (config['network']['ssdp']['timeout'] / 2) * 1000
        min = 100
        Math.floor(Math.random() * (max - min))

    advertise = (callback) ->
        socket = dgram.createSocket 'udp4'
        socket.bind()
        socket.setBroadcast 1
        socket.setMulticastTTL 2
        # First send three notification messages as per UPnP specification
{'fraction_non_alphanumeric': 0.14066804407713498, 'fraction_numerical': 0.008264462809917356, 'mean_word_length': 1.884309831181728, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '3047519', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1556, 'n_words': 427}