Permalink
Browse files

Added batched feedback feature.

  • Loading branch information...
1 parent 7b02d30 commit 3f87814e5fdb4309de7f0a64159df5047302da1d Randall Tombaugh committed Oct 16, 2012
Showing with 18 additions and 4 deletions.
  1. +18 −4 lib/feedback.js
View
@@ -29,7 +29,8 @@ if(process.env.DEBUG) {
* @config {String} [passphrase] The passphrase for the connection key, if required
* @config {String} [address="feedback.push.apple.com"] The feedback server to connect to.
* @config {Number} [port=2195] Feedback server port
- * @config {Function} [feedback] A callback which accepts 2 parameters (timestamp, {@link Device}). See: {@link <a href="https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3">Communicating with APS</a>.
+ * @config {Function} [feedback] A callback which accepts 2 parameters (timestamp, {@link Device}) or an array of (timestamp, {@link Device}) object tuples, depending on the value of batchFeedback option. See: {@link <a href="https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3">Communicating with APS</a>.
+ * @config {Boolean} [batchFeedback] If true, the feedback callback will only be called once per connection with an array of timestamp and device token tuples.
* @config {Function} [errorCallback] Callback which will capture connection errors
* @config {Number} [interval=3600] Interval to automatically connect to the Feedback service.
*/
@@ -48,6 +49,7 @@ function Feedback(options) {
port: 2196, /* feedback port */
rejectUnauthorized: true, /* Set this to false incase using a local proxy, reject otherwise */
feedback: false, /* enable feedback service, set to callback */
+ batchFeedback: false, /* If the feedback callback should only be called once per connection. */
errorCallback: false, /* error handler to catch connection exceptions */
interval: 3600, /* interval in seconds to connect to feedback service */
};
@@ -96,7 +98,7 @@ Feedback.prototype.initialize = function () {
this.pfxData = this.options.pfxData;
}
else {
- fx.readFile(this.options.pfx, function (err, data) {
+ fs.readFile(this.options.pfx, function (err, data) {
if (err) {
this.deferredInitialize.reject(err);
return;
@@ -175,6 +177,7 @@ Feedback.prototype.connect = function () {
}.bind(this));
this.readBuffer = new Buffer(0);
+ this.feedbackData = [];
this.socket.on('data', this.receive.bind(this));
this.socket.on("error", this.destroyConnection.bind(this));
this.socket.once('close', this.resetConnection.bind(this));
@@ -210,9 +213,13 @@ Feedback.prototype.receive = function (data) {
this.readBuffer.copy(token, 0, 6, 6 + tokenLength);
debug("Parsed device token: %s, timestamp: %d", token.toString("hex"), time);
- if (typeof this.options.feedback == 'function') {
+ var device = new Device(token);
+ if (!this.options.batchFeedback &&
+ typeof this.options.feedback == 'function') {
debug("Calling feedback method for device");
- this.options.feedback(time, new Device(token));
+ this.options.feedback(time, device);
+ } else {
+ this.feedbackData.push({ time: time, device: device });
}
this.readBuffer = this.readBuffer.slice(6 + tokenLength);
}
@@ -236,6 +243,13 @@ Feedback.prototype.resetConnection = function () {
if (this.socket) {
this.socket.removeAllListeners();
}
+
+ if (this.options.batchFeedback &&
+ typeof this.options.feedback == 'function') {
+ debug("Calling feedback method for a batch of devices");
+ this.options.feedback(this.feedbackData);
+ this.feedbackData = [];
+ }
if(!this.deferredConnection.promise.isResolved()) {
debug("Connection error occurred before TLS Handshake");

0 comments on commit 3f87814

Please sign in to comment.