-
Notifications
You must be signed in to change notification settings - Fork 0
/
logiccreator.js
146 lines (143 loc) · 4.44 KB
/
logiccreator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
function createLogic(execlib, bufferlib) {
'use strict';
var lib = execlib.lib,
q = lib.q,
userProducer = bufferlib.userProducer;
function Logic(usernamearry, frombuffercb) {
if (!usernamearry) {
throw new lib.Error('USERNAME_ARRAY_NEEDED');
}
this.users = usernamearry.map(userProducer);
this.cb = frombuffercb;
this.results = new Array(this.users.length);
this.current = 0;
this.buffers = [];
this.blocked = false;
}
Logic.prototype.destroy = function () {
this.blocked = null;
if (this.buffers) {
if (this.buffers.length) {
console.error('BUFFERS NOT CONSUMED!');
}
this.buffers = null;
}
this.results = null;
this.cb = null;
if (this.users) {
lib.arryDestroyAll(this.users);
}
this.users = null;
};
Logic.prototype.takeBuffer = function (buffer) {
if (!this.cb) {
return;
}
if (this.blocked) {
this.buffers.push(buffer);
return;
};
var ret = this.process(buffer), shouldstop;
while(ret) {
if (!this.cb) {
console.log('no cb, will get out now');
return;
}
shouldstop = this.cb(ret);
if (shouldstop === 'stop') {
this.reset();
this.current = 0;
return;
}
if (q.isPromise(shouldstop)) {
//console.log('blocking on promise');
this.blocked = true;
shouldstop.then(this.unblock.bind(this));
return;
}
ret = this.process();
}
if (!this.blocked) {
buffer = this.buffers.shift();
if (buffer) {
this.takeBuffer(buffer);
}
}
};
function resetter(user) {
user.init(null);
}
Logic.prototype.unblock = function () {
this.blocked = false;
this.takeBuffer();
};
Logic.prototype.reset = function () {
if (!this.users) {
return;
}
this.users.forEach(resetter);
};
Logic.prototype.process = function (buffer) {
//console.log('process?', buffer);
var currentuser = this.users[this.current],
ret = currentuser.process(buffer);
if ('undefined' !== typeof ret) {
if (currentuser.availableBytes() < 1){
//console.log('resetting at ret', ret, 'current user', this.current, require('util').inspect(currentuser, {depth:null}));
this.reset();
}
this.results[this.current] = ret;
//console.log('results', this.results, 'currently at', this.current);
this.current++;
if (this.current === this.users.length) {
this.current = 0;
this.users[this.current].init(currentuser.buffer, currentuser.cursor);
return this.results;
}
this.users[this.current].init(currentuser.buffer, currentuser.cursor);
return this.process();
}
//console.log('stopping at', currentuser.cursor, 'of', currentuser.buffer ? currentuser.buffer.length : null, 'current user index', this.current);
};
Logic.prototype.currentPosition = function () {
if (!this.users) {return null;}
var currentuser = this.users[this.current];
if (!currentuser) {return null;}
return currentuser.currentPosition();
};
function bytelenSummer(users, result, item, index) {
return result + users[index].neededBytes(item);
}
function bufferWriter(buffer, users, byteoffset, item, index) {
var user = users[index], nb = user.neededBytes(item);
user.toBuffer(item, buffer.slice(byteoffset, byteoffset+nb));
return byteoffset+nb;
}
Logic.prototype.neededBytes = function (dataarray) {
if (dataarray.length !== this.users.length) {
console.log('dataarray: ',dataarray);
throw new lib.Error('DATA_ARRAY_LENGTH_MISMATCH', 'Data array provided has to be '+this.users.length+' elements long');
}
var _users = this.users, ret;
ret = dataarray.reduce(bytelenSummer.bind(null, _users), 0)
_users = null;
return ret;
};
Logic.prototype.toBuffer = function (dataarray, buffer, offset) {
var buflen = this.neededBytes(dataarray), ret, _ret, _users;
if (dataarray.length !== this.users.length) {
throw new lib.Error('DATA_ARRAY_LENGTH_MISMATCH', 'Data array provided has to be '+this.users.length+' elements long');
}
offset = offset || 0;
buffer = buffer || Buffer.alloc(buflen);
ret = buffer.slice(offset, offset+buflen);
_ret = ret;
_users = this.users;
dataarray.reduce(bufferWriter.bind(null, _ret, _users), 0);
_ret = null;
_users = null;
return ret;
};
return Logic;
}
module.exports = createLogic;