Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- Added configuration object

 - Added free link to documentation
 - Updated npm package
  • Loading branch information...
commit 309d8b63ed22b352d8b8cb4eac2dd957726bc3ac 1 parent f5fdc78
@digitalwm authored
Showing with 53 additions and 21 deletions.
  1. +1 −1  README.md
  2. +38 −8 cloudjs.js
  3. +11 −11 example.js
  4. +3 −1 package.json
View
2  README.md
@@ -16,6 +16,6 @@
For this check the example.js file
# Explenations?
- More and an updated documentation and explanations can be found at https://gumroad.com/l/fsA. It is a paid link that costs 1 Euro. By purchasing this you will support the module development.
+ More and an updated documentation and explanations can be found at http://dan.harabagiu.net/cloudjs.
# Enjoy it, and drop me a line if you like it
View
46 cloudjs.js
@@ -11,6 +11,15 @@
- "userEvent" -> array(msg, id)
*/
+/*
+ config = {
+ hasPool : bool,
+ timeout : int,
+ balance : int,
+ heartbeat : int
+ }
+ */
+
var util = require('util'),
events = require('events').EventEmitter,
dgram = require('dgram'),
@@ -245,23 +254,44 @@ function obidGenerator() {
return now + "-" + S4();
}
-function Clouder(port, group, hasPools) {
- //By default is not activated
- if(typeof(hasPools) === 'undefined') {
- hasPools = false;
+function Clouder(port, group, config) {
+ var hasPool = false,
+ timeout = 5000,
+ balance = 10000,
+ heartbeat = 2000;
+
+ if(typeof(config) !== "undefined") {
+ //set config values
+ if(typeof(config.hasPool) === 'boolean') {
+ hasPool = config.hasPool;
+ }
+ if(typeof(config.timeout) === 'number') {
+ timeout = config.timeout;
+ }
+ if(typeof(config.balance) === 'number') {
+ balance = config.balance;
+ }
+ if(typeof(config.heartbeat) === 'number') {
+ heartbeat = config.heartbeat;
+ }
}
+
+ this._hasPool = hasPool;
+ this._timeout = timeout;
+ this._balance = balance;
+ this._heartbeat = heartbeat;
this.id = guidGenerator();
this.port = port;
this.group = group;
this.score = 0;
this.socket = dgram.createSocket("udp4");
this.peers = [];
- if(hasPools === true) {
+ if(this._hasPool === true) {
this.pool = [];
this.timers = [];
this.removeTimers = [];
- setInterval(clearTimers, 5000, this);
- setInterval(balanceSelf, 10000, this);
+ setInterval(clearTimers, this._timeout, this);
+ setInterval(balanceSelf, this._balance, this);
}
return this;
}
@@ -278,7 +308,7 @@ Clouder.prototype.connect = function () {
obj.sendOperational("heartbeat", obj.score);
};
- setInterval(this.sendHeartbeat, 2000, this);
+ setInterval(this.sendHeartbeat, this._heartbeat, this);
//noinspection JSUnresolvedFunction
this.on("heartbeat", function (data) {
var sid, score;
View
22 example.js
@@ -6,11 +6,18 @@
var cloud,
numberOfObjects = 3,
- addEveryXSeconds = 2;
+ i,
+ cloudConfig;
cloud = require("./cloudjs.js");
-var myObject = new cloud.Clouder(11211, "255.255.255.255", true);
+cloudConfig = {
+ hasPool : true,
+ timeout : 3000,
+ balance : 20000
+};
+
+var myObject = new cloud.Clouder(11211, "255.255.255.255", cloudConfig);
myObject.connect();
function guidGenerator() {
@@ -30,13 +37,6 @@ function Ob1(id) {
};
}
-var now = new Date().getTime();
-
-function addObjects() {
- var localNow = new Date().getTime();
- if(localNow - now < numberOfObjects * 1000) {
- myObject.addElementToPool(new Ob1(guidGenerator()), 5000);
- }
+for(i = 0 ; i < numberOfObjects ; i++) {
+ myObject.addElementToPool(new Ob1(guidGenerator()), 5000);
}
-
-setInterval(addObjects, addEveryXSeconds * 1000);
View
4 package.json
@@ -2,7 +2,9 @@
"author" : "Dan Harabagiu <harabagiu.dan@gmail.com> (http://dan.harabagiu.net/)",
"name" : "cloudjs",
"description" : "Making from nodeJS a cloud distributed event system",
- "version" : "0.0.1",
+ "version" : "0.0.2",
+ "homepage" : "http://dan.harabagiu.net/cloudjs",
+ "keywords" : ["udp", "broadcast", "realtime", "cloud", "event"],
"repository" : {
"type" : "git",
"url" : "git@github.com:digitalwm/cloudjs.git"
Please sign in to comment.
Something went wrong with that request. Please try again.