036040a3f0f3e13dc562ff1224c66e79dca92c98
[moodle.git] / webservice / amf / testclient / AMFConnector.as
1 package {
2         
3         import flash.events.Event;
4         import flash.net.NetConnection;
5         import flash.net.Responder;
6         
7         import nl.demonsters.debugger.MonsterDebugger;
9         /**
10          * Wrapper class for the NetConnection/Responder instances
11          * 
12          * This program is free software. It comes without any warranty, to
13          * the extent permitted by applicable law. You can redistribute it
14          * and/or modify it under the terms of the Do What The Fuck You Want
15          * To Public License, Version 2, as published by Sam Hocevar. See
16          * http://sam.zoy.org/wtfpl/COPYING for more details.
17          * 
18          * @author Jordi Boggiano <j.boggiano@seld.be>
19          */                     
20         public class AMFConnector extends NetConnection {
21                 private var responder:Responder;
22                 public var data:Object;
23                 public var error:Boolean = false;
24         
25                 public function AMFConnector(url:String) {
26                         responder = new Responder(onSuccess, onError);
27                         connect(url);
28                 }
29                 
30                 /**
31                  * executes a command on the remote server, passing all the given arguments along
32                  */
33                 public function exec(command:String, args:Array = null):void
34                 {
35                         if (args == null) args = [];
36                         args.unshift(responder);
37                         args.unshift(command);
38                         (call as Function).apply(this, args);
39                 } 
40                 
41                 /**
42                  * handles success 
43                  */ 
44                 protected function onSuccess(result:Object):void {
45                         MonsterDebugger.trace(this, {'result':result});
46                         data = result;
47                         dispatchEvent(new Event(Event.COMPLETE));
48                         data = null;
49                 }
51                 /**
52                  * handles errors 
53                  */ 
54                 protected function onError(result:Object):void {
55                         data = result;
56                         MonsterDebugger.trace(this, {'result':result});
57                         error = true;
58                         dispatchEvent(new Event(Event.COMPLETE));
59                         error = false;
60                         data = null;
61                 }
62         }
63 }