-
Notifications
You must be signed in to change notification settings - Fork 6
/
old.js
89 lines (71 loc) · 1.97 KB
/
old.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
const logLines = logData.toString()
.split('\n')
.map(l => l.replace('\n', '')
.replace('\r', '')
.trim())
.filter(l => l.length && l !== LOG_HEADER)
.map(l => l.split(' '));
console.log('Got', logLines.length, 'lines of log');
console.log(logLines[1]);
const LINES_TYPES = {};
let strLog = '';
logLines.forEach(line => {
let sign = sha1(line.join(':'));
if (!LINES_TYPES[sign]) {
LINES_TYPES[sign] = line;
}
let msg = messageToString(parseMessage(line));
console.log(msg);
strLog += msg + '\n';
});
fs.writeFileSync(OUTPUT_PATH_LOG, strLog);
console.log('Different lines log values types:', Object.keys(LINES_TYPES).length);
function parseMessage(line) {
const message = {
length: null,
src: null,
dst: null,
cmd: null,
arg: null,
payload: null
}
// check length
if (line.length < 7) {
console.log(line);
throw 'Line error, invalid length';
}
// check header
if (line[0] !== D.headerBytes[0] || line[1] !== D.headerBytes[1]) {
console.log(line);
throw 'Line error, invalid header';
}
// fill message
message.length = line[2];
message.src = line[3];
message.dst = line[4];
message.cmd = line[5];
message.arg = line[6];
// todo payload
return message;
}
function messageToString(message) {
let str = '';
// who -> whom?
str += `[${D.addresses[message.src]} --> `;
str += `${D.addresses[message.dst]}] `;
// command
str += `using cmd ${D.commands[message.cmd] || '-----'} (${message.cmd}) `;
// argument
str += `and arg ${(message.arg < 10 ? ' ' : '') + message.arg} `;
return str;
}
function hashCode(str) {
var hash = 0, i, chr;
if (str.length === 0) return hash;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};