-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwist.js
executable file
·121 lines (93 loc) · 2.9 KB
/
twist.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
var steps = {}
var messageProcessor = {};
module.exports.step = function (steptext, callback) {
steps[steptext] = callback;
}
require('./step_implementation')
var protoBuf = require("protobufjs");
var builder = protoBuf.loadProtoFile("../messages.proto");
var s = require('net').Socket();
s.connect(8888, 'localhost');
var ByteBuffer = require("bytebuffer");
var message = builder.build("main.Message");
var ExecutionStarting = 0;
var ExecuteStep = 1;
var ExecuteStepResponse = 2;
var ExecutionEnding = 3;
var StepValidateRequest = 4;
var StepValidateResponse = 5;
messageProcessor[ExecutionStarting] = function (request) {
return request;
};
messageProcessor[StepValidateRequest] = function (request) {
var stepImpl = steps[request.stepValidateRequest.stepText];
var response = null;
if (stepImpl) {
response = new message({messageId: request.messageId, messageType: StepValidateResponse, stepValidateResponse: {
isValid: true
}});
}
else {
response = new message({messageId: request.messageId, messageType: StepValidateResponse, stepValidateResponse: {
isValid: false
}});
}
return response;
};
messageProcessor[ExecuteStep] = function (request) {
var stepImpl = steps[request.executeStepRequest.stepText];
var response = null;
if (stepImpl) {
try {
var args = request.executeStepRequest.args;
if (args.length == 0)
stepImpl();
else
stepImpl.apply(this, args);
response = new message({messageId: request.messageId, messageType: ExecuteStepResponse, executeStepResponse: {
passed: true
}});
}
catch (e) {
response = new message({messageId: request.messageId, messageType: ExecuteStepResponse, executeStepResponse: {
passed: false, recoverableError: false, errorMessage: e.message
}});
if (e.stack) {
response.executeStepResponse.stackTrace = e.stack;
}
}
}
else {
console.log("step not implemented");
}
return response;
}
messageProcessor[ExecutionEnding] = function (request) {
s.end();
};
function writeResponse(response) {
var encoded = response.encode().toBuffer();
// finding the message length
var messageLengthEncoded = new ByteBuffer();
messageLengthEncoded.writeVarint64(encoded.length);
var messageLengthEncodedBuffer = messageLengthEncoded.toBuffer();
var bufferToWrite = new Buffer(messageLengthEncodedBuffer.length + encoded.length);
messageLengthEncodedBuffer.copy(bufferToWrite);
encoded.copy(bufferToWrite, messageLengthEncodedBuffer.length);
s.write(bufferToWrite);
}
s.on('data', function(d){
var bb = ByteBuffer.wrap(d)
var messageLength = bb.readVarint64(0);
// Take the remaining part as the actual message
var data = d.slice(messageLength.length, messageLength.value.low + 1);
var request = message.decode(data);
if (request.messageType == ExecutionEnding) {
writeResponse(request);
s.end();
}
else {
var response = messageProcessor[request.messageType](request);
writeResponse(response);
}
});