Permalink
Browse files

added events to the cleint.

  • Loading branch information...
1 parent 5abdefb commit ba330adf1f6cff9d47d89ed1f1c41a611173864f Timothy committed Jan 14, 2013
Showing with 141 additions and 32 deletions.
  1. +36 −0 README.md
  2. +80 −24 lib/transmission.js
  3. +1 −1 package.json
  4. +24 −7 test/testquery.js
View
@@ -38,6 +38,34 @@ br.status =
SEED : 6 # Seeding
ISOLATED : 7 # Torrent can't find peers
```
+##Events
+
+### Event: added ###
+
+`function (hash, id, name) { }`
+
+Emits when a new torrent is added
+### Event: removed ###
+
+`function (id) { }`
+
+Emits when a torrent is removed
+### Event: stopped ###
+
+`function (id) { }`
+
+Emits when a torrent is stopped
+### Event: start-now ###
+
+`function (id) { }`
+
+Emits when a torrent is forced to start
+### Event: active ###
+
+`function (torrents) { }`
+
+Emits when the active method is called.
+
## Methods
@@ -59,6 +87,14 @@ Remove also local data when `del` is `true`.
br.remove [1, 7], true, (err, arg) ->
```
+### br.active(callback)
+
+List of active torrents. Callback is not needed and will fire the `active` event.
+
+```coffee
+br.active (err, arg) ->
+```
+
### br.get([ids], callback)
Get torrents info that optional `ids`.
View
@@ -24,7 +24,7 @@ var Transmission = module.exports = function(options) {
// So will act like an event emitter
util.inherits(Transmission, events.EventEmitter);
-function onResult(callback) {
+function onResult(error, callback) {
callback = callback ||
function() {
}
@@ -36,37 +36,50 @@ function onResult(callback) {
if (result.result === 'success') {
callback(null, result.arguments)
} else {
- callback(new Error(result.result))
+ error(new Error(result.result))
}
}
}
Transmission.prototype.add = function(path, callback) {
+ var self = this
this.callServer({
arguments : {
filename : path
},
method : this.methods.torrents.add,
tag : uuid()
- }, onResult(callback))
+ }, onResult(function(err) {
+ self.emit('error', err)
+ callback ? callback(err) : null
+ }, function(err, result) {
+ self.emit('added', result['torrent-added'].hashString, result['torrent-added'].id, result['torrent-added'].name)
+ callback(err, result['torrent-added'])
+ }))
}
Transmission.prototype.remove = function(ids, del, callback) {
+ var self = this;
this.callServer({
arguments : {
ids : ids,
"delete-local-data" : !!del
},
method : this.methods.torrents.remove,
tag : uuid()
- }, onResult(callback))
+ }, onResult(function(err) {
+ self.emit('error', err)
+ callback ? callback(err) : null
+ }, function(err, result) {
+ self.emit('removed', ids)
+ callback ? callback(err) : null
+ }))
}
Transmission.prototype.get = function(ids, callback) {
var options = {
arguments : {
fields : this.methods.torrents.fields,
ids : ids
},
- ids : ids,
method : this.methods.torrents.get,
tag : uuid()
}
@@ -77,50 +90,97 @@ Transmission.prototype.get = function(ids, callback) {
}
var self = this;
- self.callServer(options, onResult(callback))
+ self.callServer(options, onResult(function(err) {
+ self.emit('error', err)
+ callback(err)
+ }, callback))
}
Transmission.prototype.stop = function(ids, callback) {
var self = this;
self.callServer({
arguments : {
ids : Array.isArray(ids) ? ids : [ids]
},
- ids : '',
method : this.methods.torrents.stop,
tag : uuid()
- }, onResult(callback))
+ }, onResult(function(err) {
+ self.emit('error', err)
+ callback ? callback(err) : null
+ }, function(err, result) {
+ self.emit('stopped', ids)
+ callback ? callback(err) : null
+ }))
}
Transmission.prototype.start = function(ids, callback) {
var self = this;
self.callServer({
arguments : {
ids : ids
},
- ids : '',
method : this.methods.torrents.start,
tag : uuid()
- }, onResult(callback))
+ }, onResult(function(err) {
+ self.emit('error', err)
+ callback ? callback(err) : null
+ }, function(err, result) {
+ self.emit('started', ids)
+ callback ? callback(err) : null
+ }))
}
Transmission.prototype.startNow = function(ids, callback) {
var self = this;
self.callServer({
arguments : {
ids : ids
},
- ids : '',
method : this.methods.torrents.startNow,
tag : uuid()
- }, onResult(callback))
+ }, onResult(function(err) {
+ self.emit('error', err)
+ callback ? callback(err) : null
+ }, function(err, result) {
+ self.emit('start-now', ids)
+ callback ? callback(err) : null
+ }))
}
-Transmission.prototype.update = function(callback) {
+Transmission.prototype.all = function(callback) {
var self = this;
self.callServer({
arguments : {
fields : this.methods.torrents.fields
},
method : this.methods.torrents.get,
tag : uuid()
- }, onResult(callback))
+ }, onResult(function(err) {
+ self.emit('error', err)
+ callback ? callback(err) : null
+ }, callback))
+}
+
+Transmission.prototype.active = function(callback) {
+ var self = this;
+ var options = {
+ arguments : {
+ fields : this.methods.torrents.fields,
+ ids : 'recently-active'
+ },
+ method : this.methods.torrents.get,
+ tag : uuid()
+ }
+ this.callServer(options, onResult(function(err) {
+ self.emit('error', err)
+ callback ? callback(err) : null
+ }, function(err, result) {
+
+ self.emit('active', result.torrents)
+ if (result.removed) {
+ for (var i = 0; i < result.removed.length; i++) {
+ self.emit('removed', result.removed[i])
+ };
+ }
+
+ callback ? callback(err, result) : null
+ }))
}
Transmission.prototype.callServer = function(query, callBack) {
var self = this;
@@ -173,17 +233,13 @@ Transmission.prototype.callServer = function(query, callBack) {
http.request(options, onResponse).end(JSON.stringify(query), 'utf8');
};
-Transmission.prototype.status = {
- STOPPED : 0,
- CHECK_WAIT : 1,
- CHECK : 2,
- DOWNLOAD_WAIT : 3,
- DOWNLOAD : 4,
- SEED_WAIT : 5,
- SEED : 6,
- ISOLATED : 7
-};
+
Transmission.prototype.statusArray = ['STOPPED', 'CHECK_WAIT', 'CHECK', 'DOWNLOAD_WAIT', 'DOWNLOAD', 'SEED_WAIT', 'SEED', 'ISOLATED']
+Transmission.prototype.status = {}
+Transmission.prototype.statusArray.forEach(function(status, i) {
+ Transmission.prototype.status[status] = i
+})
+
Transmission.prototype.methods = {
torrents : {
stop : 'torrent-stop',
View
@@ -2,7 +2,7 @@
"author": "Tim <price.timmy@gmail.com",
"name": "transmission",
"description": "API client for transmissionbt",
- "version": "0.1.2",
+ "version": "0.2.0",
"repository": {
"type": "git",
"url": "git://github.com/FLYBYME/node-transmission.git"
View
@@ -5,30 +5,47 @@ var bt = new (require('../lib/transmission.js'))({
//password : 'password1'
})
+bt.on('added', function(hash, id, name) {
+ console.log('torrent added', hash, id, name)
+})
+bt.on('removed', function(id) {
+ console.log('torrent removed id:', id)
+})
+bt.on('stopped', function(id) {
+ console.log('torrent stopped id:', id)
+})
+bt.on('start-now', function(id) {
+ console.log('torrent start now id:', id)
+})
+bt.on('active', function(torrents) {
+ console.log('active torrent count:', torrents.length)
+})
+
bt.add('http://cdimage.debian.org/debian-cd/6.0.6/i386/bt-cd/debian-6.0.6-i386-netinst.iso.torrent', function(err, result) {
if (err) {
throw err
}
- console.log(result)
- bt.stop('5db3a7a15a1391795a74b48c74df5d515a12c6f7', function(err) {
+ var id = result.id
+ //console.log(result)
+ bt.stop(id, function(err) {
if (err) {
throw err
}
- bt.start('5db3a7a15a1391795a74b48c74df5d515a12c6f7', function(err) {
+ bt.start(id, function(err) {
if (err) {
throw err
}
- bt.get('5db3a7a15a1391795a74b48c74df5d515a12c6f7', function(err, result) {
+ bt.get(id, function(err, result) {
if (err) {
throw err
}
- console.log(result)
+ //console.log(result)
- bt.remove('5db3a7a15a1391795a74b48c74df5d515a12c6f7', true, function(err) {
+ bt.remove(id, true, function(err) {
if (err) {
throw err
}
-
+ bt.active()
})
})
})

0 comments on commit ba330ad

Please sign in to comment.