Code coverage report for node-ssdp/lib/server.js

Statements: 84.62% (44 / 52)      Branches: 58.33% (7 / 12)      Functions: 88.89% (8 / 9)      Lines: 88% (44 / 50)      Ignored: none     

All files » node-ssdp/lib/ » server.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 1321         1 14 14     1                 1 15   15 2 2     13   13   13   13                   1 10   10     10   10                 1 1         1 1   1   1         1 10   10         1 1   1 1                 1 2   2 2   2 2     2             2           2   2   2           1  
var SSDP = require('./')
  , util = require('util')
  , assert = require('assert')
  , c = require('./const')
 
function SsdpServer(opts, sock) {
  this._subclass = 'ssdp-server'
  SSDP.call(this, opts, sock)
}
 
util.inherits(SsdpServer, SSDP)
 
 
/**
 * Binds UDP socket to an interface/port
 * and starts advertising.
 *
 * @param ipAddress
 */
SsdpServer.prototype.start = function () {
  var self = this
 
  if (self._socketBound) {
    self._logger.warn('Server already running.')
    return
  }
 
  self._socketBound = true
 
  this._usns[this._udn] = this._udn
 
  this._logger.trace('Will try to bind to ' + this._unicastHost + ':' + this._ssdpPort)
 
  self._start(this._initAdLoop.bind(this))
}
 
 
/**
 * Binds UDP socket
 *
 * @param ipAddress
 * @private
 */
SsdpServer.prototype._initAdLoop = function () {
  var self = this
 
  self._logger.info('UDP socket bound', {host: this._unicastHost, port: self._ssdpPort})
 
  // Wake up.
  setTimeout(self.advertise.bind(self), 3000)
 
  self._startAdLoop()
}
 
 
 
 
/**
 * Advertise shutdown and close UDP socket.
 */
SsdpServer.prototype.stop = function () {
  Iif (!this.sockets) {
    this._logger.warn('Already stopped.')
    return
  }
 
  this.advertise(false)
  this.advertise(false)
 
  this._stopAdLoop()
 
  this._stop()
}
 
 
 
SsdpServer.prototype._startAdLoop = function () {
  assert.equal(this._adLoopInterval, null, 'Attempting to start a parallel ad loop')
 
  this._adLoopInterval = setInterval(this.advertise.bind(this), this._adInterval)
}
 
 
 
SsdpServer.prototype._stopAdLoop = function () {
  assert.notEqual(this._adLoopInterval, null, 'Attempting to clear a non-existing interval')
 
  clearInterval(this._adLoopInterval)
  this._adLoopInterval = null
}
 
 
 
/**
 *
 * @param alive
 */
SsdpServer.prototype.advertise = function (alive) {
  var self = this
 
  Iif (!this.sockets) return
  Iif (alive === undefined) alive = true
 
  Object.keys(self._usns).forEach(function (usn) {
    var udn = self._usns[usn]
      , nts = alive ? c.SSDP_ALIVE : c.SSDP_BYE // notification sub-type
 
    var heads = {
      'HOST': self._ssdpServerHost,
      'NT': usn, // notification type, in this case same as ST
      'NTS': nts,
      'USN': udn
    }
 
    Iif (alive) {
      heads.LOCATION = self._location
      heads['CACHE-CONTROL'] = 'max-age=1800'
      heads.SERVER = self._ssdpSig // why not include this?
    }
 
    self._logger.trace('Sending an advertisement event')
 
    var message = self._getSSDPHeader(c.NOTIFY, heads)
 
    self._send(new Buffer(message), function (err, bytes) {
      self._logger.trace({'message': message}, 'Outgoing server message')
    })
  })
}
 
module.exports = SsdpServer