2020-05-29 22:22:00 +02:00
|
|
|
namespace OS {
|
2020-06-18 17:09:00 +02:00
|
|
|
/**
|
|
|
|
* This namespace dedicated to all operations related to system
|
|
|
|
* process management
|
|
|
|
*/
|
2020-05-29 22:22:00 +02:00
|
|
|
export namespace PM {
|
2020-06-18 17:09:00 +02:00
|
|
|
/**
|
|
|
|
* A process is either an instance of an application or a service
|
|
|
|
*/
|
|
|
|
export type ProcessType =
|
|
|
|
| application.BaseApplication
|
|
|
|
| application.BaseService;
|
|
|
|
/**
|
|
|
|
* Alias to all classes that extends [[BaseModel]]
|
|
|
|
*/
|
2020-06-15 18:10:13 +02:00
|
|
|
export type ModelTypeClass = {
|
2020-05-29 22:22:00 +02:00
|
|
|
new <T extends BaseModel>(args: AppArgumentsType[]): T;
|
|
|
|
};
|
2020-06-18 17:09:00 +02:00
|
|
|
/**
|
|
|
|
* Process id allocator, when a new process is created, the value of
|
|
|
|
* this variable is increased
|
|
|
|
*/
|
2020-05-29 22:22:00 +02:00
|
|
|
export var pidalloc: number = 0;
|
2020-06-18 17:09:00 +02:00
|
|
|
/**
|
|
|
|
* All running processes is stored in this variables
|
|
|
|
*/
|
2020-05-29 22:22:00 +02:00
|
|
|
export var processes: GenericObject<BaseModel[]> = {};
|
|
|
|
/**
|
2020-06-18 17:09:00 +02:00
|
|
|
* Create a new process of application or service
|
2020-05-29 22:22:00 +02:00
|
|
|
*
|
|
|
|
* @export
|
2020-06-18 17:09:00 +02:00
|
|
|
* @param {string} app class name string
|
|
|
|
* @param {ProcessTypeClass} cls prototype class
|
|
|
|
* @param {GUI.AppArgumentsType[]} [args] process arguments
|
|
|
|
* @returns {Promise<ProcessType>} a promise on the created process
|
2020-05-29 22:22:00 +02:00
|
|
|
*/
|
|
|
|
export function createProcess(
|
|
|
|
app: string,
|
2020-06-15 18:10:13 +02:00
|
|
|
cls: ModelTypeClass,
|
2020-05-29 22:22:00 +02:00
|
|
|
args?: AppArgumentsType[]
|
|
|
|
): Promise<ProcessType> {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
let metaclass = (cls as any) as typeof BaseModel;
|
|
|
|
const f = function () {
|
|
|
|
//if it is single ton
|
|
|
|
// and a process is existing
|
|
|
|
// just return it
|
|
|
|
let obj: ProcessType;
|
|
|
|
if (
|
|
|
|
metaclass.singleton &&
|
|
|
|
PM.processes[app] &&
|
|
|
|
PM.processes[app].length === 1
|
|
|
|
) {
|
|
|
|
obj = PM.processes[
|
|
|
|
app
|
|
|
|
][0] as application.BaseApplication;
|
|
|
|
obj.show();
|
|
|
|
} else {
|
|
|
|
if (!PM.processes[app]) {
|
|
|
|
PM.processes[app] = [];
|
|
|
|
}
|
|
|
|
obj = new cls(args);
|
|
|
|
obj.birth = new Date().getTime();
|
|
|
|
PM.pidalloc++;
|
|
|
|
obj.pid = PM.pidalloc;
|
2021-11-25 01:13:16 +01:00
|
|
|
obj.subscribe("systemlocalechange", (d) => {
|
|
|
|
obj.updateLocale(d.message as string);
|
|
|
|
return obj.update();
|
|
|
|
});
|
2020-05-29 22:22:00 +02:00
|
|
|
PM.processes[app].push(obj);
|
|
|
|
if (metaclass.type === ModelType.Application) {
|
|
|
|
GUI.dock(
|
|
|
|
obj as application.BaseApplication,
|
|
|
|
metaclass.meta
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
GUI.attachservice(obj as application.BaseService);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
if (metaclass.dependencies) {
|
2020-06-03 23:43:08 +02:00
|
|
|
const libs = metaclass.dependencies;
|
2020-05-29 22:22:00 +02:00
|
|
|
return API.require(libs)
|
|
|
|
.then(() => resolve(f()))
|
|
|
|
.catch((e: Error) => reject(__e(e)));
|
|
|
|
} else {
|
|
|
|
return resolve(f());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-18 17:09:00 +02:00
|
|
|
* Get the reference to a process using its id
|
2020-05-29 22:22:00 +02:00
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {number} pid
|
|
|
|
* @returns {BaseModel}
|
|
|
|
*/
|
|
|
|
export function appByPid(pid: number): BaseModel {
|
|
|
|
let app = undefined;
|
|
|
|
const find = function (l: Array<any>) {
|
2020-06-03 23:43:08 +02:00
|
|
|
for (let a of l) {
|
2020-05-29 22:22:00 +02:00
|
|
|
if (a.pid === pid) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for (let k in PM.processes) {
|
|
|
|
const v = PM.processes[k];
|
|
|
|
app = find(v);
|
|
|
|
if (app) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-18 17:09:00 +02:00
|
|
|
* Kill a process
|
2020-05-29 22:22:00 +02:00
|
|
|
*
|
|
|
|
* @export
|
2020-06-18 17:09:00 +02:00
|
|
|
* @param {OS.GUI.BaseModel} app reference to the process
|
2020-05-29 22:22:00 +02:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
export function kill(app: BaseModel): void {
|
|
|
|
if (!app.name || !PM.processes[app.name]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const i = PM.processes[app.name].indexOf(app);
|
|
|
|
if (i >= 0) {
|
|
|
|
if (application[app.name].type === ModelType.Application) {
|
|
|
|
GUI.undock(app as application.BaseApplication);
|
|
|
|
} else {
|
|
|
|
GUI.detachservice(app as application.BaseService);
|
|
|
|
}
|
|
|
|
announcer.unregister(app);
|
|
|
|
delete PM.processes[app.name][i];
|
|
|
|
PM.processes[app.name].splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-18 17:09:00 +02:00
|
|
|
* Kill all process of an application or service
|
2020-05-29 22:22:00 +02:00
|
|
|
*
|
|
|
|
* @export
|
2020-06-18 17:09:00 +02:00
|
|
|
* @param {string} app process class name
|
|
|
|
* @param {boolean} force force exit all process
|
2020-05-29 22:22:00 +02:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
export function killAll(app: string, force: boolean): void {
|
|
|
|
if (!PM.processes[app]) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-25 01:13:16 +01:00
|
|
|
const arr = PM.processes[app].map( e => e);
|
|
|
|
for(const p of arr)
|
|
|
|
{
|
|
|
|
p.quit(force);
|
|
|
|
}
|
2020-05-29 22:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|