From c1c5c1d8c77a63a3b07a63e5cbafd40bb6cbe8e2 Mon Sep 17 00:00:00 2001 From: marcus Date: Thu, 11 Jun 2015 00:42:50 +0200 Subject: [PATCH] Revert "Revert "Suggested implementation for option 2 described in https://github.com/fraunhoferfokus/peer-upnp/issues/2"" This reverts commit cf8c4f9ad0b7ff0a8270936db9e3f97fc53b1c1a. --- lib/peer-upnp.js | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/peer-upnp.js b/lib/peer-upnp.js index 3e7c3e1..bdf654c 100644 --- a/lib/peer-upnp.js +++ b/lib/peer-upnp.js @@ -1,26 +1,26 @@ /******************************************************************************* - * + * * Copyright (c) 2013 Louay Bassbouss, Fraunhofer FOKUS, All rights reserved. - * + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - * + * License along with this library. If not, see . + * * AUTHORS: Louay Bassbouss (louay.bassbouss@fokus.fraunhofer.de) * Martin Lasak (martin.lasak@fokus.fraunhofer.de) * Alexander Futasz (alexander.futasz@fokus.fraunhofer.de) * ******************************************************************************/ - + var os = require('os'); var fs = require('fs'); var ejs = require('ejs'); @@ -67,6 +67,13 @@ var createPeer = function(options){ var Peer = function(options){ var self = this; this.prefix = options.prefix || ""; + this.iconPath = options.iconPath || "/icons"; + if (this.iconPath.indexOf('/') === 0) { + this.iconPathPrefix = this.iconPath; + } + else { + this.iconPathPrefix = (this.prefix + "/device/" + this.iconPath).replace(/\/{2,}/, '/'); + } this.server = options.server || null; this.hostname = options.hostname || getHostname(); this.port = options.port || (this.server && this.server.address() && this.server.address().port); @@ -317,7 +324,7 @@ Device.prototype.removeService = function(serviceType){ var Service = function(device, options){ this.device = device; - this.domain = options.domain || this.device.domain || null; + this.domain = options.domain || this.device.domain || null; this.type = options.type || null; this.version = options.version || "1"; this.serviceId = options.serviceId || ("urn:"+(this.domain || "")+":serviceId:"+(this.type || "")); @@ -423,13 +430,13 @@ RemoteDevice.prototype.bind = function(callback){ self.services = {}; var serviceList = options.serviceList && options.serviceList.service || []; serviceList = serviceList instanceof Array? serviceList: [serviceList]; - + for ( var i in serviceList) { var options = serviceList[i]; options.SCPDURL = URL.resolve(self.descriptionUrl, options.SCPDURL); options.controlURL = URL.resolve(self.descriptionUrl, options.controlURL); options.eventSubURL = URL.resolve(self.descriptionUrl, options.eventSubURL); - + var service = new RemoteService(self,options); self.services[service.serviceType] = service; } @@ -456,7 +463,7 @@ var RemoteService = function(device, options){ this.actions = null; this.variables = null; this.timeoutHandle = null; - + this.on("newListener",function(event, listener){ if (event == "event" && this.listeners("event") == 0) { subscribe(this); @@ -550,7 +557,7 @@ var subscribe = function(service){ method: 'SUBSCRIBE', headers: { HOST: url.host, - CALLBACK: "", + CALLBACK: "", NT: "upnp:event" } },function(rsp){ @@ -611,7 +618,7 @@ var unsubscribe = function(service){ SID: service.sid } },function(rsp){ - + }); req.end(); service.sid = null; @@ -622,7 +629,10 @@ var registerHTTPHandler = function(peer){ var self = this; var url = URL.parse(req.url,true); var method = req.method.toUpperCase(); - if (url.pathname.indexOf(peer.prefix) == 0) { + if (url.pathname.indexOf(peer.iconPathPrefix) == 0) { + peer.emit("iconRequest", url.pathname.substring(url.pathname.indexOf(peer.iconPath)), rsp) + } + else if (url.pathname.indexOf(peer.prefix) == 0) { var path = url.pathname.substr(peer.prefix.length); var handler = httpHandlers[method+" "+path]; if (typeof handler == "function") { @@ -722,7 +732,7 @@ var handlePostControl = function(req,rsp,peer){ serviceType: service.type, actionName: actionName }; - + try { options.outputs = service.implementation[actionName].call(service,inputs) || {}; } catch (e) { @@ -964,7 +974,7 @@ var respond = function(st,peer,address){ headers['USN'] = device.UDN+"::"+service.serviceType; peer.ssdpPeer.reply(headers,address); } - } + } } } }