12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- var spawn = require("child_process").spawn;
-
- function lineReader(stream, cb) {
- stream.on("data", function ondata(d) {
- d.toString().split("\n").forEach(str => {
- str = str.trim();
- if (str === "") return;
-
- cb(str);
- });
- });
- }
-
- module.exports = class CPrime {
- constructor(maxnum) {
- this.maxqueue = 30;
-
- this.maxnum = maxnum;
- this.queue = [];
- this.child = spawn("./cprime", [ maxnum ]);
- this.ready = false;
-
- lineReader(this.child.stderr, str => {
- console.log("stderr: "+str);
- if (str === "done" && !this.ready) {
- this.ready = true;
- }
- });
-
- lineReader(this.child.stdout, str => {
- if (this.queue.length === 0)
- return;
-
- var cb = this.queue.shift();
- cb(str);
- });
- }
-
- factor(num, cb) {
- num = num.replace(/\n/g, "");
- if (num.length > 1000)
- return cb("Invalid input.");
-
- if (!this.ready)
- return cb("Not ready.");
-
- if (this.queue.length >= this.maxqueue) {
- console.log("Warning: over "+this.maxqueue+" concurrent requests");
- return cb("Overloaded.");
- }
-
- this.queue.push(cb);
- this.child.stdin.write(num+"\n");
- }
- }
|