-
Notifications
You must be signed in to change notification settings - Fork 77
/
communication.ts
167 lines (139 loc) · 5.21 KB
/
communication.ts
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
import * as http from 'http';
import * as vscode from 'vscode';
import * as request from 'request';
import { getConfig } from './utils';
import { attachPythonDebuggerToBlender } from './python_debugging';
const RESPONSIVE_LIMIT_MS = 1000;
/* Manage connected Blender instances
********************************************** */
export type AddonPathMapping = { src: string, load: string };
export class BlenderInstance {
blenderPort: number;
debugpyPort: number;
justMyCode: boolean;
path: string;
scriptsFolder: string;
addonPathMappings: AddonPathMapping[];
connectionErrors: Error[];
constructor(blenderPort: number, debugpyPort: number, justMyCode: boolean, path: string,
scriptsFolder: string, addonPathMappings: AddonPathMapping[]) {
this.blenderPort = blenderPort;
this.debugpyPort = debugpyPort;
this.justMyCode = justMyCode;
this.path = path;
this.scriptsFolder = scriptsFolder;
this.addonPathMappings = addonPathMappings;
this.connectionErrors = [];
}
post(data: object): void {
request.post(this.address, { json: data });
}
async ping(): Promise<void> {
return new Promise<void>((resolve, reject) => {
let req = request.get(this.address, { json: { type: 'ping' } });
req.on('end', () => resolve());
req.on('error', err => { this.connectionErrors.push(err); reject(err); });
});
}
async isResponsive(timeout: number = RESPONSIVE_LIMIT_MS) {
return new Promise<boolean>(resolve => {
this.ping().then(() => resolve(true)).catch();
setTimeout(() => resolve(false), timeout);
});
}
attachDebugger() {
attachPythonDebuggerToBlender(this.debugpyPort, this.path, this.justMyCode, this.scriptsFolder, this.addonPathMappings);
}
get address() {
return `http://localhost:${this.blenderPort}`;
}
}
export class BlenderInstances {
private instances: BlenderInstance[];
constructor() {
this.instances = [];
}
register(instance: BlenderInstance) {
this.instances.push(instance);
}
async getResponsive(timeout: number = RESPONSIVE_LIMIT_MS): Promise<BlenderInstance[]> {
if (this.instances.length === 0) return [];
return new Promise<BlenderInstance[]>(resolve => {
let responsiveInstances: BlenderInstance[] = [];
let pingAmount = this.instances.length;
function addInstance(instance: BlenderInstance) {
responsiveInstances.push(instance);
if (responsiveInstances.length === pingAmount) {
resolve(responsiveInstances.slice());
}
}
for (let instance of this.instances) {
instance.ping().then(() => addInstance(instance)).catch(() => { });
}
setTimeout(() => resolve(responsiveInstances.slice()), timeout);
});
}
sendToResponsive(data: object, timeout: number = RESPONSIVE_LIMIT_MS) {
for (const instance of this.instances) {
instance.isResponsive(timeout).then(responsive => {
if (responsive) instance.post(data);
}).catch();
}
}
sendToAll(data: object) {
for (const instance of this.instances) {
instance.post(data);
}
}
}
/* Own server
********************************************** */
export function startServer() {
server = http.createServer(SERVER_handleRequest);
server.listen();
}
export function stopServer() {
server.close();
}
export function getServerPort(): number {
return server.address().port;
}
function SERVER_handleRequest(request: any, response: any) {
if (request.method === 'POST') {
let body = '';
request.on('data', (chunk: any) => body += chunk.toString());
request.on('end', () => {
let req = JSON.parse(body);
switch (req.type) {
case 'setup': {
let config = getConfig();
let justMyCode: boolean = <boolean>config.get('addon.justMyCode')
let instance = new BlenderInstance(req.blenderPort, req.debugpyPort, justMyCode, req.blenderPath, req.scriptsFolder, req.addonPathMappings);
instance.attachDebugger();
RunningBlenders.register(instance);
response.end('OK');
break;
}
case 'enableFailure': {
vscode.window.showWarningMessage('Enabling the addon failed. See console.');
response.end('OK');
break;
}
case 'disableFailure': {
vscode.window.showWarningMessage('Disabling the addon failed. See console.');
response.end('OK');
break;
}
case 'addonUpdated': {
response.end('OK');
break;
}
default: {
throw new Error('unknown type');
}
}
});
}
}
var server: any = undefined;
export const RunningBlenders = new BlenderInstances();