Permalink
Browse files

take ports on the command line

  • Loading branch information...
benvanik committed Nov 6, 2011
1 parent bd65e2d commit 4308be0b06ce83c4806f44f1b3f57afd5c2b5d6d
Showing with 87 additions and 43 deletions.
  1. +26 −2 README.md
  2. +6 −0 {lib → }/api.js
  3. +5 −25 {lib → }/devicehandler.js
  4. +0 −13 lib/server.js
  5. +11 −3 package.json
  6. +39 −0 server.js
View
@@ -7,12 +7,36 @@ on the local network via
by applications on the local machine or network to discover AirPlay devices and
control the playback of those devices with one API.
-### Quickstart
+## Quickstart
npm install trampoline
+ npm start trampoline
+## Installation
-### Installation
+With [npm](http://npmjs.org):
+
+ npm install trampoline
+
+From source:
+
+ cd ~
+ git clone https://benvanik@github.com/benvanik/trampoline.git
+ npm link trampoline/
+
+## Configuration
+
+When using `npm start`, use `npm config` to change the launch options:
+
+ npm config set trampoline:api_port 8090
+ npm config set trampoline:http_port 8091
+ npm start trampoline
+
+If launching directly via `trampoline`:
+
+ trampoline --api_port=8090 --http_port=8091
+
+## API
### Service REST API
View
@@ -10,6 +10,12 @@ var API = function(port) {
this.port = port || 8090;
this.browser = airplay.createBrowser();
+ this.browser.on('deviceOnline', function(device) {
+ util.puts('device online: ' + device.id + ' / ' + device.getName());
+ });
+ this.browser.on('deviceOffline', function(device) {
+ util.puts('device offline: ' + device.id + ' / ' + device.getName());
+ });
this.server = http.createServer(function (req, res) {
var requestBody = '';
@@ -1,5 +1,3 @@
-var util = require('util');
-
var DeviceHandler = function(device) {
this.device = device;
};
@@ -10,9 +8,7 @@ DeviceHandler.prototype.default = function(req, callback) {
};
DeviceHandler.prototype.status = function(req, callback) {
- util.puts('getting status...');
this.device.status(function(res) {
- util.puts('status retreived');
callback(res);
});
};
@@ -23,67 +19,51 @@ DeviceHandler.prototype.authorize = function(req, callback) {
};
DeviceHandler.prototype.play = function(req, callback) {
- util.puts('playing...');
this.device.play(req.content, req.start, function(res) {
- util.puts('played');
callback(res);
});
};
DeviceHandler.prototype.stop = function(req, callback) {
- util.puts('stopping...');
this.device.stop(function(res) {
- util.puts('stopped');
callback(res);
});
};
DeviceHandler.prototype.scrub = function(req, callback) {
- util.puts('scrubbing...');
this.device.scrub(req.position, function(res) {
- util.puts('scrubbed');
callback(res);
});
};
DeviceHandler.prototype.reverse = function(req, callback) {
- util.puts('reversing...');
this.device.reverse(function(res) {
- util.puts('reversed');
callback(res);
- })
+ });
};
DeviceHandler.prototype.rate = function(req, callback) {
- util.puts('setting rate...');
this.device.rate(req.value, function(res) {
- util.puts('rate set');
callback(res);
- })
+ });
};
DeviceHandler.prototype.pause = function(req, callback) {
- util.puts('pausing...');
this.device.rate(0, function(res) {
- util.puts('paused');
callback(res);
- })
+ });
};
DeviceHandler.prototype.resume = function(req, callback) {
- util.puts('resuming...');
this.device.rate(1, function(res) {
- util.puts('resumed');
callback(res);
- })
+ });
};
DeviceHandler.prototype.volume = function(req, callback) {
- util.puts('setting volume...');
this.device.volume(req.value, function(res) {
- util.puts('volume set');
callback(res);
- })
+ });
};
DeviceHandler.prototype.photo = function(req, callback) {
View
@@ -1,13 +0,0 @@
-#!/usr/bin/env node
-
-var API = require('./trampoline/api').API;
-
-var apiPort = 8090;
-var httpPort = 8091;
-
-// Setup API
-var api = new API(apiPort);
-api.start();
-
-// Setup local HTTP server
-// TODO: local HTTP server
View
@@ -17,16 +17,24 @@
"server"
],
"directories": {
- "lib": "./lib/trampoline"
},
- "main": "./lib/server",
+ "main": "./server",
+ "bin": {
+ "trampoline" : "./server.js"
+ },
"dependencies": {
+ "tav": ">=0.1.0",
"airplay": ">=0.0.1"
},
"scripts": {
+ "start": "node server.js"
},
"engines": {
"node": ">= 0.5.x"
},
- "devDependencies": {}
+ "devDependencies": {},
+ "config": {
+ "api_port": "8090",
+ "http_port": "8091"
+ }
}
View
@@ -0,0 +1,39 @@
+#!/usr/bin/env node
+
+var util = require('util');
+
+var opts = require('tav').set({
+ api_port: {
+ note: 'API binding port',
+ value: 8090
+ },
+ http_port: {
+ note: 'HTTP server binding port',
+ value: 8091
+ }
+});
+
+var API = require('./api').API;
+
+var api_port =
+ parseInt(process.env.npm_package_config_api_port) ||
+ opts['api_port'] ||
+ 8090;
+var http_port =
+ parseInt(process.env.npm_package_config_http_port) ||
+ opts['http_port'] ||
+ 8091;
+
+util.puts('starting trampoline...');
+util.puts(' api_port: ' + api_port);
+util.puts(' http_port: ' + http_port);
+
+// Setup API
+var api = new API(api_port);
+api.start();
+
+// Setup local HTTP server
+// TODO: local HTTP server
+
+util.puts('trampoline online and ready');
+util.puts('');

0 comments on commit 4308be0

Please sign in to comment.