Merge pull request #12 from eepp/improve-protocol

Improve protocol
This commit is contained in:
David Cormier 2013-10-05 14:42:10 -07:00
commit be93fa7042
4 changed files with 225 additions and 182 deletions

View file

@ -7,7 +7,7 @@
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="js/underscore.js"></script>
<script type="text/javascript" src="js/localstorage.js"></script>
<script type="text/javascript" src="js/protocol.js"></script>
<script type="text/javascript" src="js/weechat-protocol.js"></script>
<script type="text/javascript" src="js/websockets.js"></script>
</head>
<body ng-app="weechat" ng-controller="WeechatCtrl">

View file

@ -1,180 +0,0 @@
var Protocol = function() {
var self = this;
var getInfo = function() {
var info = {};
info.key = getString();
info.value = getString();
return info;
};
var getHdata = function() {
var paths;
var count;
var objs = [];
var hpath = getString();
keys = getString().split(',');
paths = hpath.split('/');
count = getInt();
keys = keys.map(function(key) {
return key.split(':');
});
var i;
for (i = 0; i < count; i++) {
var tmp = {};
tmp.pointers = paths.map(function(path) {
return getPointer();
});
keys.forEach(function(key) {
tmp[key[0]] = runType(key[1]);
});
objs.push(tmp);
};
return objs;
};
function getPointer() {
var l = getChar();
var pointer = getSlice(l)
var parsed_data = new Uint8Array(pointer);
return _uiatos(parsed_data);
};
var _uiatos =function(uia) {
var _str = [];
for (var c = 0; c < uia.length; c++) {
_str[c] = String.fromCharCode(uia[c]);
}
return decodeURIComponent(escape(_str.join("")));
};
var getInt = function() {
var parsed_data = new Uint8Array(getSlice(4));
var i = ((parsed_data[0] & 0xff) << 24) | ((parsed_data[1] & 0xff) << 16) | ((parsed_data[2] & 0xff) << 8) | (parsed_data[3] & 0xff);
return i;
};
var getChar = function() {
var parsed_data = new Uint8Array(getSlice(1));
return parsed_data[0];
};
var getString = function() {
var l = getInt();
if (l > 0) {
var s = getSlice(l);
var parsed_data = new Uint8Array(s);
return _uiatos(parsed_data);
}
return "";
};
var getSlice = function(length) {
var slice = self.data.slice(0,length);
self.data = self.data.slice(length);
return slice;
};
var getType = function() {
var t = getSlice(3);
return _uiatos(new Uint8Array(t));
};
var runType = function(type) {
if (type in types) {
return types[type]();
}
0;
};
var getHeader = function() {
return {
length: getInt(),
compression: getChar(),
}
};
var getId = function() {
return getString();
}
var getObject = function() {
var type = getType();
if (type) {
return object = {
type: type,
content: runType(type),
}
}
}
self.parse = function(data) {
self.setData(data);
var header = getHeader();
var id = getId();
var objects = [];
var object = getObject();
while(object) {
objects.push(object);
object = getObject();
}
return {
header: header,
id: id,
objects: objects,
}
}
self.setData = function (data) {
self.data = data;
};
function array() {
var type;
var count;
var values;
type = getType();
count = getInt();
values = [];
var i;
for (i = 0; i < count; i++) {
values.push(runType(type));
};
return values;
}
var types = {
chr: getChar,
"int": getInt,
str: getString,
inf: getInfo,
hda: getHdata,
ptr: getPointer,
lon: getPointer,
tim: getPointer,
buf: getString,
arr: array
};
//TODO: IMPLEMENT THIS STUFF
// chr: this.getChar,
// 'int': getInt,
// hacks
// hacks
// htb: getHashtable,
// inf: Protocol.getInfo,
// inl: getInfolist,
// },
}

View file

@ -324,7 +324,7 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'pluginManager', function($
}]);
weechat.factory('connection', ['$rootScope', '$log', 'handlers', 'colors', function($rootScope, $log, handlers, colors) {
protocol = new Protocol();
protocol = new WeeChatProtocol();
var websocket = null;

223
js/weechat-protocol.js Normal file
View file

@ -0,0 +1,223 @@
var WeeChatProtocol = function() {
this._types = {
'chr': this._getChar,
'int': this._getInt,
'str': this._getString,
'inf': this._getInfo,
'hda': this._getHdata,
'ptr': this._getPointer,
'lon': this._getStrNumber,
'tim': this._getTime,
'buf': this._getString,
'arr': this._getArray,
'htb': this._getHashTable
};
this._typesStr = {
'chr': this._strDirect,
'str': this._strDirect,
'int': this._strToString,
'tim': this._strToString,
'ptr': this._strDirect
};
};
WeeChatProtocol._uia2s = function(uia) {
var str = [];
for (var c = 0; c < uia.length; c++) {
str.push(String.fromCharCode(uia[c]));
}
return decodeURIComponent(escape(str.join('')));
};
WeeChatProtocol.prototype = {
_getType: function() {
var t = this._getSlice(3);
return WeeChatProtocol._uia2s(new Uint8Array(t));
},
_runType: function(type) {
var cb = this._types[type];
var boundCb = cb.bind(this);
return boundCb();
},
_getStrNumber: function() {
var len = this._getByte();
var str = this._getSlice(len);
return WeeChatProtocol._uia2s(new Uint8Array(str));
},
_strDirect: function(obj) {
return obj;
},
_strToString: function(obj) {
return obj.toString();
},
_objToString: function(obj, type) {
var cb = this._typesStr[type];
var boundCb = cb.bind(this);
return boundCb(obj);
},
_getInfo: function() {
var info = {};
info.key = this._getString();
info.value = this._getString();
return info;
},
_getHdata: function() {
var self = this;
var paths;
var count;
var objs = [];
var hpath = this._getString();
keys = this._getString().split(',');
paths = hpath.split('/');
count = this._getInt();
keys = keys.map(function(key) {
return key.split(':');
});
for (var i = 0; i < count; i++) {
var tmp = {};
tmp.pointers = paths.map(function(path) {
return self._getPointer();
});
keys.forEach(function(key) {
tmp[key[0]] = self._runType(key[1]);
});
objs.push(tmp);
};
return objs;
},
_getPointer: function() {
return this._getStrNumber();
},
_getTime: function() {
var str = this._getStrNumber();
return new Date(parseInt(str));
},
_getInt: function() {
var parsedData = new Uint8Array(this._getSlice(4));
return ((parsedData[0] & 0xff) << 24) |
((parsedData[1] & 0xff) << 16) |
((parsedData[2] & 0xff) << 8) |
(parsedData[3] & 0xff);
},
_getByte: function() {
var parsedData = new Uint8Array(this._getSlice(1));
return parsedData[0];
},
_getChar: function() {
return String.fromCharCode(this._getByte());
},
_getString: function() {
var l = this._getInt();
if (l > 0) {
var s = this._getSlice(l);
var parsedData = new Uint8Array(s);
return WeeChatProtocol._uia2s(parsedData);
}
return "";
},
_getHeader: function() {
var len = this._getInt();
var comp = this._getByte();
return {
length: len,
compression: comp,
};
},
_getId: function() {
return this._getString();
},
_getObject: function() {
var self = this;
var type = this._getType();
if (type) {
return {
type: type,
content: self._runType(type),
};
}
},
_getHashTable: function() {
var self = this;
var typeKeys, typeValues, count;
var dict = {};
typeKeys = this._getType();
typeValues = this._getType();
count = this._getInt();
for (var i = 0; i < count; ++i) {
var key = self._runType(typeKeys);
var keyStr = self._objToString(key, typeKeys);
var value = self.runType(typeValues);
dict[keyStr] = value;
}
return dict;
},
_getArray: function() {
var self = this;
var type;
var count;
var values;
type = this._getType();
count = this._getInt();
values = [];
for (var i = 0; i < count; i++) {
values.push(self._runType(type));
};
return values;
},
_getSlice: function(length) {
var slice = this._data.slice(this._dataAt, this._dataAt + length);
this._dataAt += length;
return slice;
},
_setData: function (data) {
this._data = data;
},
parse: function(data) {
var self = this;
this._setData(data);
this._dataAt = 0;
var header = this._getHeader();
var id = this._getId();
var objects = [];
var object = this._getObject();
while (object) {
objects.push(object);
object = self._getObject();
}
return {
header: header,
id: id,
objects: objects,
};
}
};