Permalink
Browse files

starting content system

  • Loading branch information...
benvanik committed Nov 6, 2011
1 parent f68eaa7 commit ff8326ea004c7eb0fd1884ffe54c258ef42ba8b6
Showing with 74 additions and 5 deletions.
  1. +1 −1 README.md
  2. +17 −4 api.js
  3. +18 −0 content.js
  4. +37 −0 contentcache.js
  5. +1 −0 package.json
View
@@ -54,7 +54,7 @@ Setup a new content serving request:
target: {
mimeType: string, // 'video/mp4'
resolution: number, // 480, 720, 1080, undefined for original
- quality: number // [0-1]
+ quality: number // [0-1], undefined for don't care
}
}
--> {
View
21 api.js
@@ -2,13 +2,16 @@ var airplay = require('airplay');
var http = require('http');
var util = require('util');
+var ContentCache = require('./contentcache').ContentCache;
var DeviceHandler = require('./devicehandler').DeviceHandler;
var API = function(port) {
var self = this;
this.port = port || 8090;
+ this.contentCache = new ContentCache();
+
this.browser = airplay.createBrowser();
this.browser.on('deviceOnline', function(device) {
util.puts('device online: ' + device.id + ' / ' + device.getName());
@@ -65,7 +68,7 @@ API.prototype.dispatchDeviceListRequest = function(req, requestBody, res) {
};
API.prototype.dispatchDeviceRequest = function(req, requestBody, res) {
- var requestObject = requestBody.length ? JSON.parse(requestBody) : {};
+ var request = requestBody.length ? JSON.parse(requestBody) : {};
var deviceMatch = req.url.match(/\/device\/([a-z0-9]+)\/([a-z0-9]+)?/);
if (deviceMatch) {
@@ -80,11 +83,11 @@ API.prototype.dispatchDeviceRequest = function(req, requestBody, res) {
var actionName = deviceMatch[2] || 'default';
var action = device.handler[actionName];
if (action) {
- action.call(device.handler, requestObject, function(responseObject) {
+ action.call(device.handler, request, function(response) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
- res.end(JSON.stringify(responseObject));
+ res.end(JSON.stringify(response));
});
} else {
// Invalid action
@@ -102,8 +105,18 @@ API.prototype.dispatchDeviceRequest = function(req, requestBody, res) {
};
API.prototype.dispatchContentSetupRequest = function(req, requestBody, res) {
- var requestObject = requestBody.length ? JSON.parse(requestBody) : {};
+ var request = requestBody.length ? JSON.parse(requestBody) : {};
+
+ var source = request.source;
+ var target = request.target;
+ var content = this.contentCache.findOrCreate(source, target);
+ res.writeHead(200, {
+ 'Content-Type': 'text/plain'
+ });
+ res.end(JSON.stringify({
+ id: content.id
+ }));
};
API.prototype.dispatchContentRequest = function(req, requestBody, res) {
View
@@ -0,0 +1,18 @@
+var Content = function(id, source, target) {
+ this.id = id;
+ this.source = source;
+ this.target = target;
+};
+exports.Content = Content;
+
+Content.prototype.matches = function(source, target) {
+ var sourceMatches =
+ source.content == this.source.content;
+ var targetMatches =
+ target.mimeType == this.target.mimeType &&
+ (target.resolution === undefined ||
+ target.resolution == this.target.resolution) &&
+ (target.quality === undefined ||
+ target.quality == this.target.quality);
+ return sourceMatches && targetMatches;
+};
View
@@ -0,0 +1,37 @@
+var uuid = require('node-uuid');
+
+var Content = require('./content').Content;
+
+var ContentCache = function() {
+ this.content_ = {};
+};
+exports.ContentCache = ContentCache;
+
+ContentCache.prototype.create = function(source, target) {
+ var contentId = uuid();
+ var content = new Content(contentId, source, target);
+ this.content_[content.id] = content;
+ return content;
+};
+
+ContentCache.prototype.find = function(source, target) {
+ for (var contentId in this.content_) {
+ var content = this.content_[contentId];
+ if (content.matches(source, target)) {
+ return content;
+ }
+ }
+ return null;
+};
+
+ContentCache.prototype.findOrCreate = function(source, target) {
+ var content = this.find(source, target);
+ if (!content) {
+ content = this.create(source, target);
+ }
+ return content;
+};
+
+ContentCache.prototype.get = function(contentId) {
+ return this.content_[contentId];
+};
View
@@ -24,6 +24,7 @@
},
"dependencies": {
"tav": ">=0.1.0",
+ "node-uuid": ">=1.2.0",
"airplay": ">=0.0.1"
},
"scripts": {

0 comments on commit ff8326e

Please sign in to comment.