-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtransport.js
310 lines (267 loc) · 9.29 KB
/
transport.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import context from './context.js'
//
// transport, write to and read from temp DB table
//
let pvcAdminRealm;
let sessionId, messageId, messageSize;
let payloadOutId, payloadInId;
let requestTextAry, requestText, responseText, responseOffset;
//let agentReceive;
// transport.openOutputStream
async function openOutputStream(sessionId_in) {
sessionId = sessionId_in; // new session
messageId = -1;
payloadOutId = 0;
payloadInId = 0;
requestText = "";
requestTextAry = [];
messageSize = 0;
pvcAdminRealm = context.pvcAdminRealm;
pvcAdminRealm.write(() => {
console.log("truncating TABLE pvc__payload_out");
pvcAdminRealm.delete(pvcAdminRealm.objects("pvc__payload_out"));
console.log("truncating TABLE pvc__payload_in");
pvcAdminRealm.delete(pvcAdminRealm.objects("pvc__payload_in"));
});
}
// transport.closeOutputStream
async function closeOutputStream() {//agentReceive_in) {
//agentReceive = agentReceive_in;
if (requestTextAry.length > 0) {
pvcAdminRealm = context.pvcAdminRealm;
pvcAdminRealm.write(() => {
console.log("save requestText to DB");
pvcAdminRealm.create("pvc__payload_out", {
ID: payloadOutId,
PAYLOAD: requestTextAry.join("")
}, 'modified');
});
requestTextAry = [];
messageSize = 0;
}
// reset IDs before httpSend
payloadOutId = 0;
payloadInId = 0;
messageId = -1;
await httpSend();
}
// transport.openInputStream
async function openInputStream() {
payloadInId = 0;
responseText = "";
}
// transport.closeInputStream
async function closeInputStream() {
}
// writeCommand
async function writeCommand(cmd) {
//console.log("writing " + cmd.name);
let cmdJsonLength, strCmdJsonLength, cmdJson, cmdValueJson;
if (cmd.value) {
cmdValueJson = JSON.stringify(cmd.value);
if (cmdValueJson) {
cmd.valueLength = cmdValueJson.length;
}
}
let tempValue = cmd.value;
cmd.value = null;
cmdJson = JSON.stringify(cmd);
cmd.value = tempValue;
cmdJsonLength = cmdJson.length;
if (cmdJsonLength > 0 && cmdJsonLength < 10) {
strCmdJsonLength = "0" + cmdJsonLength;
} else if (cmdJsonLength < 100) {
strCmdJsonLength = "" + cmdJsonLength;
} else {
throw Error("cmdJsonLength not within 1 to 99: " +
cmdJsonLength);
}
let lengthToWrite = strCmdJsonLength.length + cmdJson.length;
if (cmd.valueLength > 0) {
lengthToWrite += cmdValueJson.length;
}
if (lengthToWrite > context.settings.maxMessageSize) {
throw Error("message size limit reached with a single command");
}
if (cmd.name != "MORE" &&
(messageSize + lengthToWrite) > context.settings.maxMessageSize) {
// message size limit reached, send MORE to server
let clientMore = {};
clientMore.name = "MORE";
await writeCommand(clientMore);
pvcAdminRealm = context.pvcAdminRealm;
pvcAdminRealm.write(() => {
console.log("save requestText to DB");
pvcAdminRealm.create("pvc__payload_out", {
ID: payloadOutId,
PAYLOAD: requestTextAry.join("")
}, 'modified');
});
requestTextAry = [];
messageSize = 0;
payloadOutId = Number(payloadOutId) + 1;
// write the original sync command
await writeCommand(cmd);
} else {
messageSize += lengthToWrite;
//console.log("Writing:" + strCmdJsonLength + " " + cmdJson);
requestTextAry.push(strCmdJsonLength);
requestTextAry.push(cmdJson);
if (cmd.valueLength > 0) {
//console.log("cmdValueJson.length:" + cmdValueJson.length);
requestTextAry.push(cmdValueJson);
}
}
}
// readCommand
async function readCommand() {
//console.log("begin readCommand");
let cmd, cmdJsonLength = 0;
let cmdJson, cmdValueJson, charArray;
// retrieve responseText from DB
if (!responseText || responseOffset >= responseText.length) {
console.log("retrieve responseText from DB, payloadInId=" + payloadInId);
pvcAdminRealm = context.pvcAdminRealm;
responseText = pvcAdminRealm.objectForPrimaryKey("pvc__payload_in", payloadInId);
if (responseText) {
responseOffset = 0;
payloadInId = Number(payloadInId) + 1;
console.log("Retrieved responseText");
responseText = responseText['PAYLOAD'];
} else {
console.warn("Retrieved responseText was empty");
}
}
if (!responseText) {
throw Error("No responseText to read");
}
charArray = responseText.substr(responseOffset, 2);
responseOffset += 2;
cmdJsonLength = parseInt(new String(charArray));
//console.log("cmdJsonLength: " + cmdJsonLength);
charArray = responseText.substr(responseOffset, cmdJsonLength);
responseOffset += cmdJsonLength;
cmdJson = charArray;
//console.log("cmdJson: " + cmdJson);
try {
cmd = JSON.parse(cmdJson);
} catch (e1) {
console.warn("Failed to parse cmdJson: " + cmdJson);
throw e1;
}
//console.log("Received: " + cmd.name);
if (cmd.valueLength > 0) {
charArray = responseText.substr(responseOffset, cmd.valueLength);
responseOffset += cmd.valueLength;
cmdValueJson = charArray;
//console.log("cmdValueJson.length: " + cmdValueJson.length);
try {
cmd.value = JSON.parse(cmdValueJson);
} catch (e1) {
console.log("Failed to parse cmdValueJson: " + cmdValueJson);
throw e1;
}
}
// received MORE from server
if (cmd.name == "MORE") {
console.log("received MORE");
cmd = await readCommand();
}
//console.log("end readCommand. cmd.name=" + cmd.name);
return cmd;
}
// send client commands that were cached in temp DB table
// in one or more http requests; server responses are saved
// temp DB table
var serverMore = false;
async function httpSend() {
messageId += 1;
requestText = "";
responseText = "";
responseOffset = 0;
// retrieve requestText from DB
console.log("retrieve requestText from DB");
pvcAdminRealm = context.pvcAdminRealm;
requestText = pvcAdminRealm.objectForPrimaryKey("pvc__payload_out", payloadOutId);
if (requestText) {
requestText = requestText['PAYLOAD'];
}
payloadOutId = Number(payloadOutId) + 1;
if (!requestText && serverMore) {
requestText = context.settings.morePayload;
console.log("Will send client morePayload");
}
if (!requestText) {
throw Error("No more request to send");
}
context.onSyncStateChange("SENDING");
let headers = {
"Content-Type": "application/octet-stream",
"transport-serialization": "Json",
"session-type": "SYNC",
"max-message-size": "" + context.settings.maxMessageSize,
"If-Modified-Since": "Sat, 1 Jan 2005 00:00:00 GMT",
};
if (sessionId) {
headers["session-id"] = sessionId;
headers["message-id"] = "" + messageId;
} else {
throw new Error("Invalid sessionId: " + sessionId);
}
console.log("Calling fetch for message #" + messageId
+ ". headers=" + JSON.stringify(headers));
//+ ". body=" + requestText);
await fetch(context.settings.syncServerUrl, {
method: "POST",
headers: headers,
body: requestText
}).then((response) => {
context.onSyncStateChange("RECEIVING");
//console.log("response=" + JSON.stringify(response));
if (!response.ok) {
throw Error("Got non-OK response: " + response.status);
}
let responseHeaders = response.headers;
sessionId = responseHeaders.get("session-id");
messageId = Number(responseHeaders.get("message-id"));
console.log("Response sessionId: " + sessionId);
console.log("Response messageId: " + messageId);
return response.text();
}).then((responseText) => {
console.log("Recieved responseText, length: " + responseText.length);
//if (responseText.length < 2000) {
//console.log("responseText: " + responseText);
//}
// save responseText to DB
pvcAdminRealm = context.pvcAdminRealm;
pvcAdminRealm.write(() => {
console.log("save responseText to DB");
pvcAdminRealm.create("pvc__payload_in", {
ID: payloadInId,
PAYLOAD: responseText
}, 'modified');
});
payloadInId = Number(payloadInId) + 1;
// http transport done?
if (responseText.length >= context.settings.morePayload.length &&
context.settings.morePayload ==
responseText.substr(responseText.length - context.settings.morePayload.length,
context.settings.morePayload.length)) {
console.log("Server has more to send");
serverMore = true;
return httpSend();
} else {
console.log("http transport done");
serverMore = false;
//agentReceive();
}
})
}
export default {
openOutputStream,
closeOutputStream,
openInputStream,
closeInputStream,
writeCommand,
readCommand
}