-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheaas-client.js
More file actions
381 lines (314 loc) · 11.1 KB
/
Copy patheaas-client.js
File metadata and controls
381 lines (314 loc) · 11.1 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import {
NetworkSession
} from "./lib/networkSession.js";
import {
ComponentSession,
SnapshotRequestBuilder
} from "./lib/componentSession.js";
import {
ClientError,
sendEsc,
sendCtrlAltDel,
sendAltTab,
_fetch,
} from "./lib/util.js";
import EventTarget from "./third_party/event-target/esm/index.js";
export {
sendEsc,
sendCtrlAltDel,
sendAltTab
};
export {
ClientError,
SnapshotRequestBuilder
};
/**
* Main EaaS Client class
*
*
* @export
* @class Client
* @extends {EventTarget}
* @param {URL} api_entrypoint
* @param {Object} idToken
* @param {Object} kbLayoutPrefs
*/
export class Client extends EventTarget {
constructor(api_entrypoint, idToken = null,
{kbLayoutPrefs, emulatorContainer = document.getElementById("emulator-container")} = {}) {
super();
this.API_URL = api_entrypoint.replace(/([^:])(\/\/+)/g, '$1/').replace(/\/+$/, '');
this.container = undefined;
this.kbLayoutPrefs = kbLayoutPrefs ? kbLayoutPrefs : {
language: {
name: 'us'
},
layout: {
name: 'pc105'
}
};
this.idToken = idToken;
this.deleteOnUnload = true;
this.options = null;
this.sessions = [];
/**
* component session attached to browser canvas
*/
this.activeView = null;
this.defaultView = null;
this.envsComponentsData = [];
this.isConnected = false;
this.xpraConf = {
xpraWidth: 640,
xpraHeight: 480,
xpraDPI: 96,
xpraEncoding: "jpeg"
};
this.emulatorContainer = emulatorContainer;
// ID for registered this.pollState() with setInterval()
this.pollStateIntervalId = null;
// Clean up on window close
window.addEventListener("beforeunload", () => {
if (this.deleteOnUnload)
this.release();
});
}
setXpraConf(width, height, dpi, xpraEncoding) {
this.xpraConf = {
xpraWidth: width,
xpraHeight: height,
xpraDPI: dpi,
xpraEncoding: xpraEncoding
};
}
// ... token && { authorization : `Bearer ${token}`},
// ... obj && {"content-type" : "application/json" }
// ...obj && {body: JSON.stringify(obj) },
async _pollState() {
if (this.network) {
this.network.keepalive();
}
for (const session of this.sessions) {
if (session.getNetwork() && !session.forceKeepalive)
continue;
let result = await session.getEmulatorState();
if (!result)
continue;
let emulatorState = result.state;
if (emulatorState == "INITIALIZING" || emulatorState == "RUNNING" ||
// HACK: "OK" and "READY" are obsolete state names which might still be used by the eaas-server
emulatorState == "OK" || emulatorState == "READY")
session.keepalive();
else if (emulatorState == "STOPPED" || emulatorState == "FAILED") {
if (this.onEmulatorStopped)
this.onEmulatorStopped();
session.keepalive();
this.dispatchEvent(new CustomEvent("error", {
detail: `${emulatorState}`
})); // .addEventListener("error", (e) => {})
} else
this.dispatchEvent(new CustomEvent("error", {
detail: session
}));
}
}
getActiveSession() {
return this.activeView;
}
/*
needs to be a global client function,
we may checkpoint more then a single machine in the future.
*/
async checkpoint(request) {
let session = this.activeView;
this.disconnect();
return session.checkpoint(request);
}
disconnect() {
if (!this.activeView) {
return;
}
let myNode = this.emulatorContainer;
// it's supposed to be faster, than / myNode.innerHTML = ''; /
while (myNode && myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
this.activeView.disconnect();
this.activeView = undefined;
this.container = undefined;
console.log("Viewer disconnected successfully.");
}
async attachNewEnv(sessionId, container, environmentRequest) {
let session = await _fetch(`${this.API_URL}/sessions/${sessionId}`, "GET", null, this.idToken);
session.sessionId = sessionId;
this.load(session);
environmentRequest.setKeyboard(this.kbLayoutPrefs.language.name, this.kbLayoutPrefs.layout.name);
let componentSession = await ComponentSession.createComponent(environmentRequest, this.API_URL, this.idToken);
this.pollStateIntervalId = setInterval(() => {
this._pollState();
}, 1500);
this._connectToNetwork(componentSession, sessionId);
componentSession.forceKeepalive = true;
this.network.sessionComponents.push(componentSession);
this.network.networkConfig.components.push({
componentId: componentSession.componentId,
networkLabel: "Temp Client"
});
this.sessions.push(componentSession);
await this.connect(container, componentSession);
}
async attach(sessionId, container, _componentId) {
let session = await _fetch(`${this.API_URL}/sessions/${sessionId}`, "GET", null, this.idToken);
session.sessionId = sessionId;
this.load(session);
let componentSession;
if (_componentId) {
componentSession = this.getSession(_componentId);
}
this.pollStateIntervalId = setInterval(() => {
this._pollState();
}, 1500);
console.log("attching component:" + componentSession);
await this.connect(container, componentSession);
}
async start(components, options) {
if (options) {
console.log("setting xpra encoding to " + options.getXpraEncoding());
this.xpraConf.xpraEncoding = options.getXpraEncoding();
}
try {
const promisedComponents = components.map(async component => {
component.setKeyboard(this.kbLayoutPrefs.language.name, this.kbLayoutPrefs.layout.name);
let componentSession = await ComponentSession.createComponent(component, this.API_URL, this.idToken);
this.sessions.push(componentSession);
if (component.isInteractive() === true) {
this.defaultView = componentSession;
}
return componentSession;
});
console.log("starting client side keep alive");
this.pollStateIntervalId = setInterval(() => {
this._pollState();
}, 1500);
await Promise.all(promisedComponents);
if (options && options.isNetworkEnabled()) {
console.log("starting network...");
this.network = new NetworkSession(this.API_URL, this.idToken);
await this.network.startNetwork(this.sessions, options);
}
} catch (e) {
this.release(true);
console.log(e);
throw new ClientError("Starting environment session failed!", e);
}
}
load(session) {
const sessionId = session.sessionId;
const sessionComponents = session.components;
const networkInfo = session.network;
for (const sc of sessionComponents) {
if (sc.type !== "machine")
continue;
if (this.sessions.filter((sessionComp) => sessionComp.componentId === sc.componentId).length > 0)
continue;
let session = new ComponentSession(this.API_URL, sc.environmentId, sc.componentId, this.idToken);
this.sessions.push(session);
}
this.network = new NetworkSession(this.API_URL, this.idToken);
this.network.load(sessionId, this.sessions, networkInfo);
}
async _connectToNetwork(component, networkID) {
const result = await _fetch(`${this.API_URL}/networks/${networkID}/addComponentToSwitch`, "POST", {
componentId: component.getId(),
},
this.idToken);
return result;
}
async release(destroyNetworks = false) {
console.log("released: " + destroyNetworks);
this.disconnect();
clearInterval(this.pollStateIntervalId);
if (this.network) {
// we do not release by default network session, as they are detached by default
if (destroyNetworks)
await this.network.release();
return;
}
let url;
for (const session of this.sessions) {
url = await session.stop();
await session.release();
}
this.sessions = [];
return url;
}
getSession(id) {
if (!this.network)
throw new Error("no sessions available");
return this.network.getSession(id);
}
getSessions() {
if (!this.network) {
return [];
}
const sessionInfo = [];
let networkSessions = this.network.getSessions();
for (let session of networkSessions) {
const conf = this.network.getNetworkConfig(session.componentId);
let componentSession = this.getSession(conf.componentId);
console.log(componentSession);
sessionInfo.push({
id: conf.componentId,
title: conf.networkLabel
});
}
return sessionInfo;
}
async connect(container, view) {
if (!view) {
if(this.defaultView)
{
view = this.defaultView;
}
else
{
console.log("no view defined. using first session");
view = this.sessions[0];
}
}
if (this.activeView)
this.disconnect();
if (!view)
throw new Error("no active view possible");
this.activeView = view;
console.log(`Connecting viewer... @ ${container}`);
try {
await this.activeView.connect(container, this.xpraConf);
this.isConnected = true;
} catch (e) {
console.error("Connecting viewer failed!");
console.log(e);
this.activeView = undefined;
}
}
async detach(name, detachTime_minutes) {
if (!this.network)
throw new Error("No network session available");
await this.network.detach(name, detachTime_minutes);
window.onbeforeunload = () => {};
this.disconnect();
}
async stop() {
// let activeSession = this.activeView;
let results = [];
this.disconnect();
for (const session of this.sessions) {
let result = await session.stop();
results.push({
id: session.getId(),
result: result
});
}
return results;
}
}