Skip to content

Commit

Permalink
Merge pull request #200 from tomvantilburg/master
Browse files Browse the repository at this point in the history
merge from Tom
  • Loading branch information
tomvantilburg committed Aug 11, 2015
2 parents 2c115b3 + 5a01fd3 commit 08ce9e0
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 18 deletions.
72 changes: 68 additions & 4 deletions dist/cow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2301,6 +2301,63 @@ Cow.websocket.prototype._onClose = function(event){

_.extend(Cow.websocket.prototype, Events);
}.call(this));
/*TT:
Added this from https://gist.github.com/revolunet/843889
to enable LZW encoding
*/
// LZW-compress a string
function lzw_encode(s) {
var dict = {};
var data = (s + "").split("");
var out = [];
var currChar;
var phrase = data[0];
var code = 256;
for (var i=1; i<data.length; i++) {
currChar=data[i];
if (dict[phrase + currChar] != null) {
phrase += currChar;
}
else {
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
dict[phrase + currChar] = code;
code++;
phrase=currChar;
}
}
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
for (var i=0; i<out.length; i++) {
out[i] = String.fromCharCode(out[i]);
}
return out.join("");
}

// Decompress an LZW-encoded string
function lzw_decode(s) {
var dict = {};
var data = (s + "").split("");
var currChar = data[0];
var oldPhrase = currChar;
var out = [currChar];
var code = 256;
var phrase;
for (var i=1; i<data.length; i++) {
var currCode = data[i].charCodeAt(0);
if (currCode < 256) {
phrase = data[i];
}
else {
phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
}
out.push(phrase);
currChar = phrase.charAt(0);
dict[code] = oldPhrase + currChar;
code++;
oldPhrase = phrase;
}
return out.join("");
}

(function(){

var root = this;
Expand Down Expand Up @@ -2373,15 +2430,17 @@ Cow.messenger.prototype.sendData = function(data, action, target){
message.sender = this._core.peerid();
message.target = target;
message.action = action;
message.payload = data;
//message.payload = data;
//TT: newly added lzw compression in 2.2.0. This breaks COW versions!
message.payload = lzw_encode(JSON.stringify(data));
var stringified;
var endcoded;
try {
stringified = JSON.stringify(message);
}
catch (e){
console.error(e, message);
}
//console.info('Sending ' + JSON.stringify(message));
this.ws.send(stringified);
this._numsends++;
this._amountsend = +stringified.length;
Expand All @@ -2393,7 +2452,12 @@ Cow.messenger.prototype._onMessage = function(message){
var sender = data.sender;
var PEERID = core.peerid();
var action = data.action;
var payload = data.payload;
if (typeof(data.payload) == 'object'){
var payload = data.payload;
}
else {
var payload = JSON.parse(lzw_decode(data.payload));
}
var target = data.target;
if (sender != PEERID){
//console.info('Receiving '+JSON.stringify(data));
Expand Down Expand Up @@ -2859,7 +2923,7 @@ Cow.core = function(config){
if (typeof(config) == 'undefined' ) {
config = {};
}
this._version = '2.1.0';
this._version = '2.2.0-beta1';
this._herdname = config.herdname || 'cow';
this._userid = null;
this._socketserverid = null;
Expand Down
7 changes: 4 additions & 3 deletions dist/cow.min.js

Large diffs are not rendered by default.

72 changes: 68 additions & 4 deletions dist/cow.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,63 @@ Cow.websocket.prototype._onError = function(e){
};
_.extend(Cow.websocket.prototype, Events);
}.call(this));
/*TT:
Added this from https://gist.github.com/revolunet/843889
to enable LZW encoding
*/
// LZW-compress a string
function lzw_encode(s) {
var dict = {};
var data = (s + "").split("");
var out = [];
var currChar;
var phrase = data[0];
var code = 256;
for (var i=1; i<data.length; i++) {
currChar=data[i];
if (dict[phrase + currChar] != null) {
phrase += currChar;
}
else {
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
dict[phrase + currChar] = code;
code++;
phrase=currChar;
}
}
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
for (var i=0; i<out.length; i++) {
out[i] = String.fromCharCode(out[i]);
}
return out.join("");
}

// Decompress an LZW-encoded string
function lzw_decode(s) {
var dict = {};
var data = (s + "").split("");
var currChar = data[0];
var oldPhrase = currChar;
var out = [currChar];
var code = 256;
var phrase;
for (var i=1; i<data.length; i++) {
var currCode = data[i].charCodeAt(0);
if (currCode < 256) {
phrase = data[i];
}
else {
phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
}
out.push(phrase);
currChar = phrase.charAt(0);
dict[code] = oldPhrase + currChar;
code++;
oldPhrase = phrase;
}
return out.join("");
}

(function(){

var root = this;
Expand Down Expand Up @@ -2298,15 +2355,17 @@ Cow.messenger.prototype.sendData = function(data, action, target){
message.sender = this._core.peerid();
message.target = target;
message.action = action;
message.payload = data;
//message.payload = data;
//TT: newly added lzw compression in 2.2.0. This breaks COW versions!
message.payload = lzw_encode(JSON.stringify(data));
var stringified;
var endcoded;
try {
stringified = JSON.stringify(message);
}
catch (e){
console.error(e, message);
}
//console.info('Sending ' + JSON.stringify(message));
this.ws.send(stringified);
this._numsends++;
this._amountsend = +stringified.length;
Expand All @@ -2318,7 +2377,12 @@ Cow.messenger.prototype._onMessage = function(message){
var sender = data.sender;
var PEERID = core.peerid();
var action = data.action;
var payload = data.payload;
if (typeof(data.payload) == 'object'){
var payload = data.payload;
}
else {
var payload = JSON.parse(lzw_decode(data.payload));
}
var target = data.target;
if (sender != PEERID){
//console.info('Receiving '+JSON.stringify(data));
Expand Down Expand Up @@ -2784,7 +2848,7 @@ Cow.core = function(config){
if (typeof(config) == 'undefined' ) {
config = {};
}
this._version = '2.1.0';
this._version = '2.2.0-beta1';
this._herdname = config.herdname || 'cow';
this._userid = null;
this._socketserverid = null;
Expand Down
6 changes: 3 additions & 3 deletions dist/cow.node.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/cow2.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Cow.core = function(config){
if (typeof(config) == 'undefined' ) {
config = {};
}
this._version = '2.1.0';
this._version = '2.2.0-beta1';
this._herdname = config.herdname || 'cow';
this._userid = null;
this._socketserverid = null;
Expand Down
70 changes: 67 additions & 3 deletions src/cow2.messenger.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,60 @@
/*TT:
Added this from https://gist.github.com/revolunet/843889
to enable LZW encoding
*/
// LZW-compress a string
function lzw_encode(s) {
var dict = {};
var data = (s + "").split("");
var out = [];
var currChar;
var phrase = data[0];
var code = 256;
for (var i=1; i<data.length; i++) {
currChar=data[i];
if (dict[phrase + currChar] != null) {
phrase += currChar;
}
else {
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
dict[phrase + currChar] = code;
code++;
phrase=currChar;
}
}
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
for (var i=0; i<out.length; i++) {
out[i] = String.fromCharCode(out[i]);
}
return out.join("");
}

// Decompress an LZW-encoded string
function lzw_decode(s) {
var dict = {};
var data = (s + "").split("");
var currChar = data[0];
var oldPhrase = currChar;
var out = [currChar];
var code = 256;
var phrase;
for (var i=1; i<data.length; i++) {
var currCode = data[i].charCodeAt(0);
if (currCode < 256) {
phrase = data[i];
}
else {
phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
}
out.push(phrase);
currChar = phrase.charAt(0);
dict[code] = oldPhrase + currChar;
code++;
oldPhrase = phrase;
}
return out.join("");
}

(function(){

var root = this;
Expand Down Expand Up @@ -70,15 +127,17 @@ Cow.messenger.prototype.sendData = function(data, action, target){
message.sender = this._core.peerid();
message.target = target;
message.action = action;
message.payload = data;
//message.payload = data;
//TT: newly added lzw compression in 2.2.0. This breaks COW versions!
message.payload = lzw_encode(JSON.stringify(data));
var stringified;
var endcoded;
try {
stringified = JSON.stringify(message);
}
catch (e){
console.error(e, message);
}
//console.info('Sending ' + JSON.stringify(message));
this.ws.send(stringified);
this._numsends++;
this._amountsend = +stringified.length;
Expand All @@ -90,7 +149,12 @@ Cow.messenger.prototype._onMessage = function(message){
var sender = data.sender;
var PEERID = core.peerid();
var action = data.action;
var payload = data.payload;
if (typeof(data.payload) == 'object'){
var payload = data.payload;
}
else {
var payload = JSON.parse(lzw_decode(data.payload));
}
var target = data.target;
if (sender != PEERID){
//console.info('Receiving '+JSON.stringify(data));
Expand Down

0 comments on commit 08ce9e0

Please sign in to comment.