telebit.js/machine.js

188 lines
6.1 KiB
JavaScript
Raw Normal View History

2016-09-22 00:42:57 +00:00
'use strict';
var machine = {};
machine._version = 1;
machine.state = 0;
machine.states = { 0: 'version', 1: 'headerLength', 2: 'header', 3: 'data'/*, 4: 'error'*/ };
machine.states_length = Object.keys(machine.states).length;
machine.chunkIndex = 0;
machine.fns = {};
function debug(chunk, i, len) {
i = i || 0;
len = len || chunk.length - i;
console.log(chunk.slice(i, len)[0]);
console.log(chunk);
console.log('state:', machine.states[machine.state]);
console.log('statei:', machine.state);
console.log('index:', machine.chunkIndex);
}
machine.fns.version = function (chunk) {
2016-09-22 17:06:34 +00:00
console.log('');
console.log('[version]');
2016-09-22 00:42:57 +00:00
if ((255 - machine._version) !== chunk[machine.chunkIndex]) {
console.error("not v" + machine._version + " (or data is corrupt)");
// no idea how to fix this yet
}
machine.chunkIndex += 1;
2016-09-22 17:06:34 +00:00
return true;
2016-09-22 00:42:57 +00:00
};
machine.headerLen = 0;
machine.fns.headerLength = function (chunk) {
2016-09-22 17:06:34 +00:00
console.log('');
console.log('[headerLength]');
2016-09-22 00:42:57 +00:00
machine.headerLen = chunk[machine.chunkIndex];
machine.chunkIndex += 1;
2016-09-22 17:06:34 +00:00
return true;
2016-09-22 00:42:57 +00:00
};
machine.buf = null;
machine.bufIndex = 0;
//var buf = Buffer.alloc(4096);
machine.fns.header = function (chunk) {
2016-09-22 17:06:34 +00:00
console.log('');
console.log('[header]');
2016-09-22 00:42:57 +00:00
var curSize = machine.bufIndex + (chunk.length - machine.chunkIndex);
var partLen = 0;
var str = '';
2016-09-22 17:06:34 +00:00
var part;
2016-09-22 00:42:57 +00:00
if (curSize < machine.headerLen) {
// I still don't have the whole header,
// so just create a large enough buffer,
// write these bits, and wait for the next chunk.
if (!machine.buf) {
machine.buf = Buffer.alloc(machine.headerLen);
2016-09-22 17:06:34 +00:00
console.log('[1a] machine.headerLen:', machine.headerLen);
2016-09-22 00:42:57 +00:00
}
2016-09-22 17:06:34 +00:00
// WHERE I STOPPED LAST NIGHT:
// partLen should be no more than the available size
partLen = Math.min(machine.headerLen - machine.bufIndex, chunk.length - machine.chunkIndex);
console.log('[1a] chunkIndex:', machine.chunkIndex);
console.log('[1a] chunk.length:', chunk.length);
console.log('[1a] partLen:', partLen);
console.log('[1a] bufIndex:', machine.bufIndex, typeof machine.bufIndex);
part = chunk.slice(machine.chunkIndex, machine.chunkIndex + partLen);
console.log('[1a] part:', part, typeof part);
//machine.buf.write(part.toString('binary'), machine.bufIndex, 'binary');
//part.copy(machine.buf, machine.bufIndex, machine.bufIndex, machine.bufIndex + part.length);
chunk.copy(machine.buf, machine.bufIndex, machine.chunkIndex, machine.chunkIndex + partLen);
2016-09-22 00:42:57 +00:00
machine.chunkIndex += partLen; // this MUST be chunk.length
machine.bufIndex += partLen;
2016-09-22 17:06:34 +00:00
console.log('[1a] chunkIndex:', machine.chunkIndex);
console.log('[1a] bufIndex:', machine.bufIndex);
return false;
2016-09-22 00:42:57 +00:00
}
else {
// it's now ready to discover the whole header
if (machine.buf) {
str += machine.buf.slice(0, machine.bufIndex).toString();
}
2016-09-22 17:06:34 +00:00
console.log('machine.headerLen:', machine.headerLen);
console.log('machine.chunkIndex:', machine.chunkIndex);
2016-09-22 00:42:57 +00:00
partLen = machine.headerLen - str.length;
2016-09-22 17:06:34 +00:00
console.log('[1b] partLen:', partLen);
part = chunk.slice(machine.chunkIndex, machine.chunkIndex + partLen);
str += part.toString();
2016-09-22 00:42:57 +00:00
machine.chunkIndex += partLen;
2016-09-22 17:06:34 +00:00
console.log('machine.chunkIndex:', machine.chunkIndex);
2016-09-22 00:42:57 +00:00
// TODO maybe just use maxlen of buffer
machine.buf = null; // back to null
machine.bufIndex = 0; // back to 0
machine._headers = str.split(/,/g);
2016-09-22 17:06:34 +00:00
//console.log(chunk.toString());
console.log('str:', str);
console.log('headers:', machine._headers);
2016-09-22 00:42:57 +00:00
machine.family = machine._headers[0];
machine.address = machine._headers[1];
machine.port = machine._headers[2];
2016-09-22 17:06:34 +00:00
machine.bodyLen = parseInt(machine._headers[3], 10) || -1;
return true;
2016-09-22 00:42:57 +00:00
}
};
machine.fns.data = function (chunk) {
2016-09-22 17:06:34 +00:00
console.log('');
console.log('[data]');
2016-09-22 00:42:57 +00:00
var curSize = machine.bufIndex + (chunk.length - machine.chunkIndex);
2016-09-22 17:06:34 +00:00
console.log('curSize:', curSize);
console.log('bodyLen:', machine.bodyLen, typeof machine.bodyLen);
2016-09-22 00:42:57 +00:00
var partLen = 0;
var buf;
2016-09-22 17:06:34 +00:00
partLen = Math.min(machine.bodyLen - machine.bufIndex, chunk.length - machine.chunkIndex);
2016-09-22 00:42:57 +00:00
if (curSize < machine.bodyLen) {
2016-09-22 17:06:34 +00:00
console.log('curSize < bodyLen');
2016-09-22 00:42:57 +00:00
// I still don't have the whole header,
// so just create a large enough buffer,
// write these bits, and wait for the next chunk.
if (!machine.buf) {
machine.buf = Buffer.alloc(machine.bodyLen);
}
2016-09-22 17:06:34 +00:00
console.log('bufIndex:', machine.bufIndex);
console.log('chunkIndex:', machine.chunkIndex);
console.log('chunk.length:', chunk.length);
console.log('partLen:', partLen);
//machine.buf.write(chunk.slice(machine.chunkIndex, machine.chunkIndex + partLen), machine.bufIndex);
chunk.copy(machine.buf, machine.bufIndex, machine.chunkIndex, machine.chunkIndex + partLen);
machine.chunkIndex += partLen; // this MUST be chunk.length
2016-09-22 00:42:57 +00:00
machine.bufIndex += partLen;
2016-09-22 17:06:34 +00:00
return false;
2016-09-22 00:42:57 +00:00
}
2016-09-22 17:06:34 +00:00
// it's now ready to discover the whole body
2016-09-22 00:42:57 +00:00
if (!machine.buf) {
2016-09-22 17:06:34 +00:00
buf = chunk.slice(machine.chunkIndex, machine.chunkIndex + partLen);
machine.chunkIndex += partLen;
2016-09-22 00:42:57 +00:00
}
else {
2016-09-22 17:06:34 +00:00
//machine.buf.write(chunk.slice(machine.chunkIndex, machine.chunkIndex + partLen), machine.bufIndex);
chunk.copy(machine.buf, machine.bufIndex, machine.chunkIndex, machine.chunkIndex + partLen);
2016-09-22 00:42:57 +00:00
machine.chunkIndex += partLen;
2016-09-22 17:06:34 +00:00
buf = machine.buf;
2016-09-22 00:42:57 +00:00
}
machine.onMessage({
family: machine.family
, address: machine.address
, port: machine.port
, data: buf
});
// TODO maybe just use maxlen of buffer
machine.buf = null; // back to null
machine.bufIndex = 0; // back to 0
2016-09-22 17:06:34 +00:00
return true;
2016-09-22 00:42:57 +00:00
};
machine.fns.addChunk = function (chunk) {
2016-09-22 17:06:34 +00:00
console.log('');
console.log('[addChunk]');
2016-09-22 00:42:57 +00:00
machine.chunkIndex = 0;
while (machine.chunkIndex < chunk.length) {
2016-09-22 17:06:34 +00:00
console.log('chunkIndex:', machine.chunkIndex, 'state:', machine.state);
if (true === machine.fns[machine.states[machine.state]](chunk)) {
machine.state += 1;
machine.state %= machine.states_length;
}
2016-09-22 00:42:57 +00:00
}
};
module.exports = machine;
2016-09-22 17:06:34 +00:00
process.on('uncaughtException', function (err) {
console.error(err);
debug(Buffer.from([0]));
2016-09-22 00:42:57 +00:00
});