Code coverage report for lib/server.js

Statements: 96.36% (53 / 55)      Branches: 91.67% (11 / 12)      Functions: 90% (9 / 10)      Lines: 98.11% (52 / 53)      Ignored: none     

All files » 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 132 133 134 135 136 137 1381       1 13     1                 1 14   14 2 2     12   12   12   12                   1 12   12       12   12 1       12 12   12                 1 4 2 2     2 2   2   2         1 12   12         1 2   2 2                 1 17   17 17   17 22   22             22 6 6 6     22   22   22           1  
var SSDP = require('./')
  , util = require('util')
  , assert = require('assert')
 
function SsdpServer(opts, sock) {
  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._ssdpPort, this._unicastHost, this._initAdLoop.bind(this))
}
 
 
/**
 * Binds UDP socket
 *
 * @param ipAddress
 * @private
 */
SsdpServer.prototype._initAdLoop = function () {
  var self = this
 
  self._logger.info('UDP socket bound to ' + this._unicastHost + ':' + self._ssdpPort)
 
  // FIXME: what's the point in advertising we're dead
  // if the cache is going to be busted anyway?
  self.advertise(false)
 
  setTimeout(function () {
    self.advertise(false)
  }, 1000)
 
  // Wake up.
  setTimeout(self.advertise.bind(self), 2000)
  setTimeout(self.advertise.bind(self), 3000)
 
  self._startAdLoop()
}
 
 
 
 
/**
 * Advertise shutdown and close UDP socket.
 */
SsdpServer.prototype.stop = function () {
  if (!this.sock) {
    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.sock) return
  if (alive === undefined) alive = true
 
  Object.keys(self._usns).forEach(function (usn) {
    var udn = self._usns[usn]
 
    var heads = {
      'HOST': self._ssdpServerHost,
      'NT': usn, // notification type, in this case same as ST
      'NTS': (alive ? 'ssdp:alive' : 'ssdp:byebye'), // notification sub-type
      'USN': udn
    }
 
    if (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 = new Buffer(self._getSSDPHeader('NOTIFY', heads))
 
    self._send(message, function (err, bytes) {
      self._logger.trace({'message': message.toString()}, 'Outgoing server message')
    })
  })
}
 
module.exports = SsdpServer