/src/gtk/Server.hx

http://github.com/blackdog66/bdog-gtk · Haxe · 179 lines · 124 code · 36 blank · 19 comment · 21 complexity · becdf82929473653e38774cdfe7a3658 MD5 · raw file

  1. package gtk;
  2. import gtk.Model;
  3. import ui.Widget;
  4. #if neko
  5. import haxe.io.Bytes;
  6. #elseif nodejs
  7. import js.Node;
  8. #end
  9. class Server {
  10. #if neko
  11. static var sock:neko.net.Socket;
  12. #elseif nodejs
  13. static var handlers = new Array<{cmd:String,fn:Dynamic->Void}>();
  14. static var child:ChildProcess;
  15. public static var ready:Void->Void;
  16. #end
  17. public static function
  18. connect(port:Int) {
  19. #if neko
  20. sock = new neko.net.Socket();
  21. sock.connect(new neko.net.Host("localhost"),port);
  22. #elseif nodejs
  23. child = Node.spawn("gtk-server",["-stdin"]);
  24. // stream = Node.net.createConnection(port,'localhost');
  25. //stream.addListener('connect',function(s) {
  26. // stream.setEncoding('ascii');
  27. child.stdout.addListener('data',function(d:String) {
  28. // trace("========================");
  29. // process all incoming ...
  30. // trace(handlers.length+" handlers pending");
  31. d = Std.string(d);
  32. var toProcess = handlers.length;
  33. var lines = d.split("\n");
  34. for (l in lines) {
  35. if (l.length == 0) continue;
  36. //trace("line data:"+l);
  37. var h = handlers.shift();
  38. if (h != null && h.fn != null) {
  39. try {
  40. toProcess--;
  41. if (!StringTools.startsWith(h.cmd,"gtk_server_callback"))
  42. trace("DOING "+h.cmd);
  43. h.fn(l);
  44. } catch (exc:Dynamic) {
  45. trace("handler exception:"+exc);
  46. }
  47. }
  48. }
  49. // if there are any handlers left then start of processing again
  50. if (toProcess > 0) {
  51. // continue the writing ...
  52. //trace('continuing with:'+handlers[0].cmd);
  53. child.stdin.write(handlers[0].cmd);
  54. }
  55. //trace("--------");
  56. });
  57. ready();
  58. //});
  59. #end
  60. }
  61. public static function
  62. send(o:String,?fn:Dynamic->Void) {
  63. //trace("writing "+o);
  64. #if neko
  65. sock.write(o);
  66. var
  67. s = haxe.io.Bytes.alloc(128),
  68. l = sock.input.readBytes(s,0,128),
  69. p = s.readString(0,l);
  70. trace("read "+p);
  71. fn(p.toString();)
  72. #elseif nodejs
  73. if (fn == null)
  74. fn = function(d) { }
  75. handlers.push({cmd:o,fn:fn});
  76. if (handlers.length == 1) {
  77. if (!StringTools.startsWith(o,"gtk_server_callback"))
  78. trace('starting with '+o);
  79. child.stdin.write(o);
  80. }
  81. #end
  82. }
  83. /*
  84. public static inline function
  85. outWidget(s:String):Widget {
  86. return s;
  87. }
  88. */
  89. public static inline function
  90. outString(s:String):String {
  91. return s;
  92. }
  93. public static inline function
  94. outInt(s:String):Int {
  95. return Std.parseInt(s);
  96. }
  97. public static inline function
  98. outBool(s:String):Bool {
  99. return (s == "1");
  100. }
  101. public static function
  102. outFloat(s:String):Float {
  103. return Std.parseFloat(s);
  104. }
  105. public static function
  106. outDynamic(s:String):Dynamic {
  107. if (s == "NULL")
  108. return null;
  109. return s;
  110. }
  111. public static function
  112. inDynamic(d:Dynamic) {
  113. return Std.string(d);
  114. }
  115. public static function
  116. inString(s:String):String {
  117. if (s == null)
  118. return "NULL";
  119. return '"' + s + '"';
  120. }
  121. public static function
  122. inWidget(w:Widget):String {
  123. if (w == null)
  124. return "NULL";
  125. return w.toString();
  126. }
  127. public static function
  128. inBool(b:Bool):String {
  129. return (b) ? "1" : "0";
  130. }
  131. public static function
  132. inFloat(f:Float):Float {
  133. return f;
  134. }
  135. public static function
  136. inInt(i:Int):Int {
  137. return i;
  138. }
  139. public static function
  140. inBytes(b:String):String {
  141. return b.toString();
  142. }
  143. }