Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package raid;
- import raid.cluster.Connection;
- #if neko
- import neko.vm.Thread;
- #elseif cpp
- import cpp.vm.Thread;
- #elseif java
- import java.lang.Thread;
- #end
- class Cluster
- {
- inline private static var CMD_STOP: String = 'stop';
- private var count: Int;
- private var input: Dynamic;
- public var exec: String;
- public var port: Int;
- public function new(exec: String, count: Int, port: Int)
- {
- this.exec = exec;
- this.count = count;
- this.port = port;
- }
- public function run(process: String)
- {
- for(i in 0...this.count) {
- Thread.create(function(){
- new Connection(this, process);
- });
- }
- Sys.println('${count} threads started.');
- Sys.println('Write "${CMD_STOP}" to stop server: ');
- while (input != CMD_STOP) {
- Sys.print('> ');
- input = Sys.stdin().readLine();
- if (input != CMD_STOP) {
- Sys.println('Unresolved command "${input}"');
- }
- }
- Sys.println("done!");
- Connection.closeAll();
- }
- }
- /*************************/
- package raid.cluster;
- import sys.io.Process;
- import raid.Cluster;
- class Connection
- {
- private static var number: Int = 0;
- private static var connections: Array<Connection> = [];
- private var _pid: Int;
- private var _process: Process;
- public function new(parent: Cluster, process: String)
- {
- connections.push(this);
- this._process = new Process(
- parent.exec,
- [process, "-p", parent.port + Std.string(number++)]
- );
- this._pid = this._process.getPid();
- }
- public function memory()
- {
- }
- public function id()
- {
- return this._pid;
- }
- public function process()
- {
- return this._process;
- }
- public function close()
- {
- this._process.kill();
- }
- public static function all()
- {
- return connections;
- }
- public static function closeAll()
- {
- for (instance in connections) {
- instance.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement