PageRenderTime 17ms CodeModel.GetById 9ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 25package haxe.remoting;
 26
 27/**
 28	Synchronous communications between Flash and Javascript.
 29**/
 30class ExternalConnection implements Connection, implements Dynamic<Connection> {
 31
 32	var __data : { name : String, ctx : Context, #if js flash : String #end };
 33	var __path : Array<String>;
 34
 35	function new( data, path ) {
 36		__data = data;
 37		__path = path;
 38	}
 39
 40	public function resolve(field) : Connection {
 41		var e = new ExternalConnection(__data,__path.copy());
 42		e.__path.push(field);
 43		return e;
 44	}
 45
 46	public function close() {
 47		connections.remove(__data.name);
 48	}
 49
 50	#if flash9
 51	static function escapeString( s : String ) {
 52		return s.split("\\").join("\\\\");
 53	}
 54	#elseif flash
 55	static function escapeString( s : String ) {
 56		return s.split("\\").join("\\\\").split("&").join("&amp;");
 57	}
 58	#else
 59	static inline function escapeString(s) {
 60		return s;
 61	}
 62	#end
 63
 64	public function call( params : Array<Dynamic> ) : Dynamic {
 65		var s = new haxe.Serializer();
 66		s.serialize(params);
 67		var params = escapeString(s.toString());
 68		var data = null;
 69		#if flash
 70			data = flash.external.ExternalInterface.call("haxe.remoting.ExternalConnection.doCall",__data.name,__path.join("."),params);
 71		#elseif js
 72			var fobj : Dynamic = untyped window.document[__data.flash];
 73			if( fobj == null ) fobj = untyped window.document.getElementById[__data.flash];
 74			if( fobj == null ) throw "Could not find flash object '"+__data.flash+"'";
 75			try	data = fobj.externalRemotingCall(__data.name,__path.join("."),params) catch( e : Dynamic ) {};
 76		#end
 77		if( data == null )
 78			throw "Call failure : ExternalConnection is not " + #if flash "compiled in JS" #else "initialized in Flash" #end;
 79		return new haxe.Unserializer(data).unserialize();
 80	}
 81
 82	static var connections = new Hash<ExternalConnection>();
 83
 84	static function doCall( name : String, path : String, params : String ) : String {
 85		try {
 86			var cnx = connections.get(name);
 87			if( cnx == null ) throw "Unknown connection : "+name;
 88			if( cnx.__data.ctx == null ) throw "No context shared for the connection "+name;
 89			var params = new haxe.Unserializer(params).unserialize();
 90			var ret = cnx.__data.ctx.call(path.split("."),params);
 91			var s = new haxe.Serializer();
 92			s.serialize(ret);
 93			#if flash
 94			return escapeString(s.toString());
 95			#else
 96			return s.toString()+"#";
 97			#end
 98		} catch( e : Dynamic ) {
 99			var s = new haxe.Serializer();
100			s.serializeException(e);
101			return s.toString();
102		}
103		#if as3
104		return "";
105		#end
106	}
107
108	#if flash
109
110	public static function jsConnect( name : String, ?ctx : Context ) {
111		if( !flash.external.ExternalInterface.available )
112			throw "External Interface not available";
113		#if flash9
114		try flash.external.ExternalInterface.addCallback("externalRemotingCall",doCall) catch( e : Dynamic ) {};
115		#else
116		flash.external.ExternalInterface.addCallback("externalRemotingCall",null,doCall);
117		#end
118		var cnx = new ExternalConnection({ name : name, ctx : ctx },[]);
119		connections.set(name,cnx);
120		return cnx;
121	}
122
123	#elseif js
124
125	public static function flashConnect( name : String, flashObjectID : String, ?ctx : Context ) {
126		var cnx = new ExternalConnection({ ctx : ctx, name : name, flash : flashObjectID },[]);
127		connections.set(name,cnx);
128		return cnx;
129	}
130
131	#end
132
133}