~ubuntu-branches/ubuntu/lucid/haxe/lucid

« back to all changes in this revision

Viewing changes to haxe/std/haxe/remoting/FlashJsConnection.hx

  • Committer: Bazaar Package Importer
  • Author(s): Jens Peter Secher
  • Date: 2009-03-18 23:09:50 UTC
  • mfrom: (3.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090318230950-pgfuxg2ucolps74t
Tags: 1:2.2-2
* Use ocamlfind to locate and use the libraries xml-light and extlib
  which already exist in Debian as separate packages.
  (Closes: #519630)
* Fixed compile error with camlp4 3.11, thanks to Stéphane Glondu.
  (Closes: #519627)
* Use quilt instead of dpatch for patches, and describe how to use
  quilt in Debian.source (thanks to Russ Allbery).
* Added a Vcs-Hg control filed to indicate the location of the public
  repository.
* Bumped Standards-Version to 3.8.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
1
25
package haxe.remoting;
2
26
 
3
 
class FlashJsConnection extends haxe.remoting.AsyncConnection {
4
 
 
5
 
        #if flash
6
 
 
7
 
        public override function __resolve( field : String ) : AsyncConnection {
 
27
class FlashJsConnection #if flash implements AsyncConnection, implements Dynamic<AsyncConnection> #end {
 
28
 
 
29
#if flash
 
30
 
 
31
        var __path : Array<String>;
 
32
        var __data : {
 
33
                id : String,
 
34
                name : String,
 
35
                ctx : Context,
 
36
                error : Dynamic -> Void,
 
37
                queue : haxe.TimerQueue,
 
38
        };
 
39
 
 
40
        function new( data, path ) {
 
41
                __data = data;
 
42
                __path = path;
 
43
        }
 
44
 
 
45
        public function close() {
 
46
                connections.remove(__data.name);
 
47
        }
 
48
 
 
49
        public function resolve( name ) : AsyncConnection {
8
50
                var c = new FlashJsConnection(__data,__path.copy());
9
 
                c.__error = __error;
10
 
                c.__path.push(field);
 
51
                c.__path.push(name);
11
52
                return c;
12
53
        }
13
54
 
14
 
        override public function call( params : Array<Dynamic>, ?onData : Dynamic -> Void ) {
15
 
                var p = __path.copy();
16
 
                var f = p.pop();
17
 
                var path = p.join(".");
 
55
        public function setErrorHandler(h) {
 
56
                __data.error = h;
 
57
        }
 
58
 
 
59
        public function call( params : Array<Dynamic>, ?onResult : Dynamic -> Void ) {
18
60
                var s = new haxe.Serializer();
19
61
                s.serialize(params);
20
 
                var cnx : { private function escapeString(s : String) : String; } = haxe.remoting.Connection;
21
 
                var params = cnx.escapeString(s.toString());
 
62
                var params = escapeString(s.toString());
 
63
                var error = __data.error;
22
64
                var me = this;
23
 
                haxe.Timer.queue(function() {
24
 
                        var s = flash.external.ExternalInterface.call("haxe.remoting.FlashJsConnection.flashCall",me.__data,path,f,params);
25
 
                        var v = null;
 
65
                __data.queue.add(function() {
 
66
                        var data = flash.external.ExternalInterface.call("haxe.remoting.FlashJsConnection.flashCall",me.__data.id,me.__data.name,me.__path.join("."),params);
 
67
                        var v : Dynamic;
26
68
                        try {
27
 
                                if( s == null )
28
 
                                        throw "Failed to call JS method "+path;
29
 
                                v = { r : new haxe.Unserializer(s).unserialize() };
 
69
                                if( data == null )
 
70
                                        throw "Call failure : FlashJsConnection is not compiled in JS";
 
71
                                v = new haxe.Unserializer(data).unserialize();
30
72
                        } catch( e : Dynamic ) {
31
 
                                me.onError(e);
 
73
                                error(e);
 
74
                                return;
32
75
                        }
33
 
                        if( v != null )
34
 
                                onData(v.r);
 
76
                        if( onResult != null )
 
77
                                onResult(v);
35
78
                });
36
79
        }
37
80
 
38
 
        public static function flashConnect( objId : String ) : AsyncConnection {
 
81
        static var connections = new Hash<FlashJsConnection>();
 
82
 
 
83
        static function escapeString( s : String ) {
 
84
                #if flash9
 
85
                return s.split("\\").join("\\\\");
 
86
                #else
 
87
                return s.split("\\").join("\\\\").split("&").join("&amp;");
 
88
                #end
 
89
        }
 
90
 
 
91
        static function doCall( name : String, path : String, params : String ) : String {
 
92
                try {
 
93
                        var cnx = connections.get(name);
 
94
                        if( cnx == null ) throw "Unknown connection : "+name;
 
95
                        if( cnx.__data.ctx == null ) throw "No context shared for the connection "+name;
 
96
                        var params = new haxe.Unserializer(params).unserialize();
 
97
                        var ret = cnx.__data.ctx.call(path.split("."),params);
 
98
                        var s = new haxe.Serializer();
 
99
                        s.serialize(ret);
 
100
                        return escapeString(s.toString());
 
101
                } catch( e : Dynamic ) {
 
102
                        var s = new haxe.Serializer();
 
103
                        s.serializeException(e);
 
104
                        return s.toString();
 
105
                }
 
106
                #if as3
 
107
                return "";
 
108
                #end
 
109
        }
 
110
 
 
111
        public static function connect( name : String, objId : String, ?ctx : Context ) {
39
112
                if( !flash.external.ExternalInterface.available )
40
113
                        throw "External Interface not available";
41
 
                if( flash.external.ExternalInterface.call("haxe.remoting.FlashJsConnection"+".jsRemoting") != "yes" )
42
 
                        throw "haxe.remoting.FlashJsConnection"+" is not available in JavaScript";
43
 
                return new FlashJsConnection(objId,[]);
44
 
        }
45
 
 
46
 
        #else js
47
 
 
48
 
        static function jsRemoting() {
49
 
                return "yes";
50
 
        }
51
 
 
52
 
        static function flashCall( flashObj : String, path : String, f : String, params : String ) : String {
 
114
                #if flash9
 
115
                try flash.external.ExternalInterface.addCallback("flashJsRemotingCall",doCall) catch( e : Dynamic ) {};
 
116
                #else
 
117
                flash.external.ExternalInterface.addCallback("flashJsRemotingCall",null,doCall);
 
118
                #end
 
119
                var cnx = new FlashJsConnection({
 
120
                        id : objId,
 
121
                        name : name,
 
122
                        ctx : ctx,
 
123
                        error : function(e) throw e,
 
124
                        queue : new haxe.TimerQueue(),
 
125
                },[]);
 
126
                connections.set(name,cnx);
 
127
                return cnx;
 
128
        }
 
129
 
 
130
#elseif js
 
131
 
 
132
        static function flashCall( flashObj : String, name : String, path : String, params : String ) : String {
53
133
                try {
54
 
                        var cnx : { private var __data : Dynamic; } = haxe.remoting.Connection.flashConnect(flashObj);
55
 
                        return cnx.__data.remotingCall(path,f,params);
 
134
                        var fobj : Dynamic = untyped window.document[flashObj];
 
135
                        if( fobj == null ) fobj = untyped window.document.getElementById[flashObj];
 
136
                        if( fobj == null ) throw "Could not find flash object '"+flashObj+"'";
 
137
                        var data = null;
 
138
                        try data = fobj.flashJsRemotingCall(name,path,params) catch( e : Dynamic ) {};
 
139
                        if( data == null ) throw "Flash object "+flashObj+" does not have an active FlashJsConnection";
 
140
                        return data;
56
141
                } catch( e : Dynamic ) {
57
142
                        var s = new haxe.Serializer();
58
143
                        s.serializeException(e);
60
145
                }
61
146
        }
62
147
 
63
 
        #end
 
148
#end
64
149
 
65
150
}