forked from fraunhoferfokus/peer-upnp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suggested implementation for option 2 described in fraunhoferfokus#2
- Loading branch information
Showing
1 changed file
with
26 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
* | ||
* License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* AUTHORS: Louay Bassbouss ([email protected]) | ||
* Martin Lasak ([email protected]) | ||
* Alexander Futasz ([email protected]) | ||
* | ||
******************************************************************************/ | ||
|
||
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); | ||
|
@@ -312,7 +319,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 || "")); | ||
|
@@ -418,13 +425,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; | ||
} | ||
|
@@ -451,7 +458,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); | ||
|
@@ -545,7 +552,7 @@ var subscribe = function(service){ | |
method: 'SUBSCRIBE', | ||
headers: { | ||
HOST: url.host, | ||
CALLBACK: "<http://"+peer.hostname+":"+port+peer.prefix+"/events?usn="+service.USN+">", | ||
CALLBACK: "<http://"+peer.hostname+":"+port+peer.prefix+"/events?usn="+service.USN+">", | ||
NT: "upnp:event" | ||
} | ||
},function(rsp){ | ||
|
@@ -606,7 +613,7 @@ var unsubscribe = function(service){ | |
SID: service.sid | ||
} | ||
},function(rsp){ | ||
|
||
}); | ||
req.end(); | ||
service.sid = null; | ||
|
@@ -617,7 +624,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") { | ||
|
@@ -717,7 +727,7 @@ var handlePostControl = function(req,rsp,peer){ | |
serviceType: service.type, | ||
actionName: actionName | ||
}; | ||
|
||
try { | ||
options.outputs = service.implementation[actionName].call(service,inputs) || {}; | ||
} catch (e) { | ||
|
@@ -942,7 +952,7 @@ var respond = function(st,peer,address){ | |
headers['USN'] = device.UDN+"::"+service.serviceType; | ||
peer.ssdpPeer.reply(headers,address); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|