/bin/std/haxe/remoting/ExternalConnection.hx

http://github.com/Yoomee/clippy · Haxe · 133 lines · 91 code · 15 blank · 27 comment · 11 complexity · 7b0036a922bbdb88763056e1c88a1e6a MD5 · raw file

  1. /*
  2. * Copyright (c) 2005-2008, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package haxe.remoting;
  26. /**
  27. Synchronous communications between Flash and Javascript.
  28. **/
  29. class ExternalConnection implements Connection, implements Dynamic<Connection> {
  30. var __data : { name : String, ctx : Context, #if js flash : String #end };
  31. var __path : Array<String>;
  32. function new( data, path ) {
  33. __data = data;
  34. __path = path;
  35. }
  36. public function resolve(field) : Connection {
  37. var e = new ExternalConnection(__data,__path.copy());
  38. e.__path.push(field);
  39. return e;
  40. }
  41. public function close() {
  42. connections.remove(__data.name);
  43. }
  44. #if flash9
  45. static function escapeString( s : String ) {
  46. return s.split("\\").join("\\\\");
  47. }
  48. #elseif flash
  49. static function escapeString( s : String ) {
  50. return s.split("\\").join("\\\\").split("&").join("&amp;");
  51. }
  52. #else
  53. static inline function escapeString(s) {
  54. return s;
  55. }
  56. #end
  57. public function call( params : Array<Dynamic> ) : Dynamic {
  58. var s = new haxe.Serializer();
  59. s.serialize(params);
  60. var params = escapeString(s.toString());
  61. var data = null;
  62. #if flash
  63. data = flash.external.ExternalInterface.call("haxe.remoting.ExternalConnection.doCall",__data.name,__path.join("."),params);
  64. #elseif js
  65. var fobj : Dynamic = untyped window.document[__data.flash];
  66. if( fobj == null ) fobj = untyped window.document.getElementById[__data.flash];
  67. if( fobj == null ) throw "Could not find flash object '"+__data.flash+"'";
  68. try data = fobj.externalRemotingCall(__data.name,__path.join("."),params) catch( e : Dynamic ) {};
  69. #end
  70. if( data == null )
  71. throw "Call failure : ExternalConnection is not " + #if flash "compiled in JS" #else "initialized in Flash" #end;
  72. return new haxe.Unserializer(data).unserialize();
  73. }
  74. static var connections = new Hash<ExternalConnection>();
  75. static function doCall( name : String, path : String, params : String ) : String {
  76. try {
  77. var cnx = connections.get(name);
  78. if( cnx == null ) throw "Unknown connection : "+name;
  79. if( cnx.__data.ctx == null ) throw "No context shared for the connection "+name;
  80. var params = new haxe.Unserializer(params).unserialize();
  81. var ret = cnx.__data.ctx.call(path.split("."),params);
  82. var s = new haxe.Serializer();
  83. s.serialize(ret);
  84. #if flash
  85. return escapeString(s.toString());
  86. #else
  87. return s.toString()+"#";
  88. #end
  89. } catch( e : Dynamic ) {
  90. var s = new haxe.Serializer();
  91. s.serializeException(e);
  92. return s.toString();
  93. }
  94. #if as3
  95. return "";
  96. #end
  97. }
  98. #if flash
  99. public static function jsConnect( name : String, ?ctx : Context ) {
  100. if( !flash.external.ExternalInterface.available )
  101. throw "External Interface not available";
  102. #if flash9
  103. try flash.external.ExternalInterface.addCallback("externalRemotingCall",doCall) catch( e : Dynamic ) {};
  104. #else
  105. flash.external.ExternalInterface.addCallback("externalRemotingCall",null,doCall);
  106. #end
  107. var cnx = new ExternalConnection({ name : name, ctx : ctx },[]);
  108. connections.set(name,cnx);
  109. return cnx;
  110. }
  111. #elseif js
  112. public static function flashConnect( name : String, flashObjectID : String, ?ctx : Context ) {
  113. var cnx = new ExternalConnection({ ctx : ctx, name : name, flash : flashObjectID },[]);
  114. connections.set(name,cnx);
  115. return cnx;
  116. }
  117. #end
  118. }