Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored CMakeServerClient.sendRequest() to improve consistency #949

Merged
merged 3 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 44 additions & 24 deletions src/drivers/cms-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,43 +468,63 @@ export class CMakeServerClient {
log.warning(localize('cant.yet.handle.message', 'Can\'t yet handle the {0} messages', some.type));
}

sendRequest(t: 'handshake', p: HandshakeParams): Promise<HandshakeContent>;
sendRequest(t: 'globalSettings', p?: GlobalSettingsParams): Promise<GlobalSettingsContent>;
sendRequest(t: 'setGlobalSettings', p: SetGlobalSettingsParams): Promise<SetGlobalSettingsContent>;
sendRequest(t: 'configure', p: ConfigureParams): Promise<ConfigureContent>;
sendRequest(t: 'compute', p?: ComputeParams): Promise<ComputeContent>;
sendRequest(t: 'codemodel', p?: CodeModelParams): Promise<CodeModelContent>;
sendRequest(t: 'cmakeInputs', p?: CMakeInputsParams): Promise<CMakeInputsContent>;
sendRequest(T: 'cache', p?: CacheParams): Promise<CacheContent>;
sendRequest(type: string, params: any = {}): Promise<any> {
const cp = {type, ...params};
const cookie = cp.cookie = Math.random().toString();
const pr = new Promise((resolve, reject) => { this._promisesResolvers.set(cookie, {resolve, reject}); });
const msg = JSON.stringify(cp);
private _sendRequest(type: 'handshake', params: HandshakeParams): Promise<HandshakeContent>;
private _sendRequest(type: 'globalSettings', params?: GlobalSettingsParams): Promise<GlobalSettingsContent>;
private _sendRequest(type: 'setGlobalSettings', params: SetGlobalSettingsParams): Promise<SetGlobalSettingsContent>;
private _sendRequest(type: 'configure', params: ConfigureParams): Promise<ConfigureContent>;
private _sendRequest(type: 'compute', params?: ComputeParams): Promise<ComputeContent>;
private _sendRequest(type: 'codemodel', params?: CodeModelParams): Promise<CodeModelContent>;
private _sendRequest(type: 'cmakeInputs', params?: CMakeInputsParams): Promise<CMakeInputsContent>;
private _sendRequest(type: 'cache', params?: CacheParams): Promise<CacheContent>;
private _sendRequest(type: string, params: any = {}): Promise<any> {
const cookiedMessage = {type, ...params};
const cookie = cookiedMessage.cookie = Math.random().toString();
const promise = new Promise((resolve, reject) => { this._promisesResolvers.set(cookie, {resolve, reject}); });
const jsonMessage = JSON.stringify(cookiedMessage);
if (ENABLE_CMSERVER_PROTO_DEBUG) {
log.debug(localize('sending.message.to.cmake-server', 'Sending message to cmake-server: {0}', msg));
log.debug(localize('sending.message.to.cmake-server', 'Sending message to cmake-server: {0}', jsonMessage));
}
this._pipe.write('\n[== "CMake Server" ==[\n');
this._pipe.write(msg);
this._pipe.write(jsonMessage);
this._pipe.write('\n]== "CMake Server" ==]\n');
return pr;
return promise;
}

/**
* CMake server requests:
*/

setGlobalSettings(params: SetGlobalSettingsParams): Promise<SetGlobalSettingsContent> {
return this.sendRequest('setGlobalSettings', params);
return this._sendRequest('setGlobalSettings', params);
}

getCMakeCacheContent(): Promise<CacheContent> { return this.sendRequest('cache'); }
handshake(params: HandshakeParams): Promise<HandshakeContent> {
return this._sendRequest('handshake', params);
}

getGlobalSettings(): Promise<GlobalSettingsContent> { return this.sendRequest('globalSettings'); }
getCMakeCacheContent(): Promise<CacheContent> {
return this._sendRequest('cache');
}

configure(params: ConfigureParams): Promise<ConfigureContent> { return this.sendRequest('configure', params); }
getGlobalSettings(): Promise<GlobalSettingsContent> {
return this._sendRequest('globalSettings');
}

compute(params?: ComputeParams): Promise<ComputeParams> { return this.sendRequest('compute', params); }
configure(params: ConfigureParams): Promise<ConfigureContent> {
return this._sendRequest('configure', params);
}

codemodel(params?: CodeModelParams): Promise<CodeModelContent> { return this.sendRequest('codemodel', params); }
compute(params?: ComputeParams): Promise<ComputeParams> {
return this._sendRequest('compute', params);
}

codemodel(params?: CodeModelParams): Promise<CodeModelContent> {
return this._sendRequest('codemodel', params);
}

cmakeInputs(params?: CMakeInputsParams): Promise<CMakeInputsContent> { return this.sendRequest('cmakeInputs', params); }
cmakeInputs(params?: CMakeInputsParams): Promise<CMakeInputsContent> {
return this._sendRequest('cmakeInputs', params);
}

protected _shutdown = false;
public async shutdown() {
Expand Down Expand Up @@ -639,7 +659,7 @@ export class CMakeServerClient {
log.info(configureMessage + extraMessage);
}

await client.sendRequest('handshake', hsparams);
await client.handshake(hsparams);
resolved = true;
resolve(client);
} catch (e) {
Expand Down
8 changes: 4 additions & 4 deletions src/drivers/cms-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ export class CMakeServerClientDriver extends codemodel.CodeModelDriver {
}

async _refreshPostConfigure(): Promise<void> {
const cl = await this.getClient();
const cmake_inputs = await cl.cmakeInputs(); // <-- 1. This line generates the error
const client = await this.getClient();
const cmake_inputs = await client.cmakeInputs(); // <-- 1. This line generates the error
// Scan all the CMake inputs and capture their mtime so we can check for
// out-of-dateness later
this._cmakeInputFileSet = await InputFileSet.create(cmake_inputs);
const clcache = await cl.getCMakeCacheContent();
const clcache = await client.getCMakeCacheContent();
this._cacheEntries = clcache.cache.reduce((acc, el) => {
const entry_map: {[key: string]: api.CacheEntryType|undefined} = {
BOOL: api.CacheEntryType.Bool,
Expand All @@ -158,7 +158,7 @@ export class CMakeServerClientDriver extends codemodel.CodeModelDriver {
new cache.Entry(el.key, el.value, type, el.properties.HELPSTRING, el.properties.ADVANCED === '1'));
return acc;
}, new Map<string, cache.Entry>());
this.codeModel = await cl.sendRequest('codemodel');
this.codeModel = await client.codemodel();
this._codeModelChanged.fire(this.codeModel);
}

Expand Down