Skip to content

Commit

Permalink
Suggested implementation for option 2 described in fraunhoferfokus#2
Browse files Browse the repository at this point in the history
  • Loading branch information
mwittig committed Jun 9, 2015
1 parent a7e7819 commit 26443f3
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions lib/peer-upnp.js
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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 || ""));
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -606,7 +613,7 @@ var unsubscribe = function(service){
SID: service.sid
}
},function(rsp){

});
req.end();
service.sid = null;
Expand All @@ -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") {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -942,7 +952,7 @@ var respond = function(st,peer,address){
headers['USN'] = device.UDN+"::"+service.serviceType;
peer.ssdpPeer.reply(headers,address);
}
}
}
}
}
}
Expand Down

0 comments on commit 26443f3

Please sign in to comment.