Advertisement
Serafim

Untitled

Feb 28th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. package raid;
  2.  
  3. import raid.cluster.Connection;
  4.  
  5. #if neko
  6.     import neko.vm.Thread;
  7. #elseif cpp
  8.     import cpp.vm.Thread;
  9. #elseif java
  10.     import java.lang.Thread;
  11. #end
  12.  
  13.  
  14. class Cluster
  15. {
  16.     inline private static var CMD_STOP: String = 'stop';
  17.  
  18.     private var count: Int;
  19.     private var input: Dynamic;
  20.     public var exec:   String;
  21.     public var port:   Int;
  22.  
  23.     public function new(exec: String, count: Int, port: Int)
  24.     {
  25.         this.exec  = exec;
  26.         this.count = count;
  27.         this.port  = port;
  28.     }
  29.  
  30.     public function run(process: String)
  31.     {
  32.         for(i in 0...this.count) {
  33.             Thread.create(function(){
  34.                 new Connection(this, process);
  35.             });
  36.         }
  37.  
  38.         Sys.println('${count} threads started.');
  39.         Sys.println('Write "${CMD_STOP}" to stop server: ');
  40.  
  41.         while (input != CMD_STOP) {
  42.             Sys.print('> ');
  43.             input = Sys.stdin().readLine();
  44.             if (input != CMD_STOP) {
  45.                 Sys.println('Unresolved command "${input}"');
  46.             }
  47.         }
  48.  
  49.         Sys.println("done!");
  50.         Connection.closeAll();
  51.     }
  52. }
  53.  
  54.  
  55.  
  56.  
  57. /*************************/
  58.  
  59. package raid.cluster;
  60.  
  61. import sys.io.Process;
  62. import raid.Cluster;
  63.  
  64. class Connection
  65. {
  66.     private static var number: Int = 0;
  67.     private static var connections: Array<Connection> = [];
  68.  
  69.     private var _pid: Int;
  70.     private var _process: Process;
  71.  
  72.     public function new(parent: Cluster, process: String)
  73.     {
  74.         connections.push(this);
  75.         this._process = new Process(
  76.         parent.exec,
  77.         [process, "-p", parent.port + Std.string(number++)]
  78.         );
  79.  
  80.         this._pid = this._process.getPid();
  81.     }
  82.  
  83.     public function memory()
  84.     {
  85.  
  86.     }
  87.  
  88.     public function id()
  89.     {
  90.         return this._pid;
  91.     }
  92.  
  93.     public function process()
  94.     {
  95.         return this._process;
  96.     }
  97.  
  98.     public function close()
  99.     {
  100.         this._process.kill();
  101.     }
  102.  
  103.     public static function all()
  104.     {
  105.         return connections;
  106.     }
  107.  
  108.     public static function closeAll()
  109.     {
  110.         for (instance in connections) {
  111.             instance.close();
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement