PageRenderTime 45ms CodeModel.GetById 38ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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