git.schokokeks.org
Repositories
Help
Report an Issue
fs-words.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
2f5e2be
Branches
Tags
develop-client_server
master
typescript
fs-words.git
server
lib
plankton
server
logic-impl.js
[mod] server:lib:plankton
Christian Fraß
commited
2f5e2be
at 2021-03-12 22:01:27
logic-impl.js
Blame
History
Raw
/* This file is part of »bacterio-plankton:server«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:server« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:server« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:server«. If not, see <http://www.gnu.org/licenses/>. */ var lib_server; (function (lib_server) { /** * @author fenris */ function make(port, handle, verbosity = 1) { return { "port": port, "handle": handle, "verbosity": verbosity, "serverobj": undefined, }; } lib_server.make = make; /** * @author fenris */ function log(subject, level, message) { if (subject.verbosity >= level) { console.log("[server]", message); } else { // do nothing } } /** * @author fenris */ function start(subject) { const net = require("net"); return (new Promise((resolve, reject) => { subject.serverobj = net.createServer((socket) => { log(subject, 2, "client connected"); socket.on("readable", () => { let chunk; while (!((chunk = socket.read()) === null)) { const input = chunk.toString(); log(subject, 3, "reading: " + input); subject.handle(input) .then((output) => { log(subject, 3, "writing: " + output); socket.write(output); socket.end(); }); } }); socket.on("end", () => { log(subject, 2, "client disconnected"); }); }); subject.serverobj.on("error", (error) => { throw error; }); subject.serverobj.listen(subject.port, () => { log(subject, 1, "listening on port " + subject.port.toFixed(0)); resolve(undefined); }); })); } lib_server.start = start; /** * @author fenris */ function kill(subject) { subject.serverobj.close(); } lib_server.kill = kill; })(lib_server || (lib_server = {})); /* This file is part of »bacterio-plankton:server«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:server« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:server« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:server«. If not, see <common://www.gnu.org/licenses/>. */ var lib_server; (function (lib_server) { /** * @author fenris */ class class_server { /** * @author fenris */ constructor(port, handle, verbosity = undefined) { this.subject = lib_server.make(port, handle, verbosity); } /** * @author fenris */ start() { return lib_server.start(this.subject); } /** * @author fenris */ kill() { return lib_server.kill(this.subject); } } lib_server.class_server = class_server; })(lib_server || (lib_server = {}));