da4c4e882f228236d8dab625ea88798381b3022e
[potlatch2.git] / net / systemeD / halcyon / connection / AMFConnection.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.events.Event;
4     import flash.net.URLLoader;
5     import flash.net.URLRequest;
6
7         import flash.system.Security;
8         import flash.net.*;
9
10         public class AMFConnection extends Connection {
11
12                 public var readConnection:NetConnection;
13                 public var writeConnection:NetConnection;
14                 private var mapLoader:URLLoader;
15
16                 private var eventTarget:AMFCounter;
17                 private var bboxrequests:Array=new Array();
18
19                 // ------------------------------------------------------------
20                 // Constructor for new AMFConnection
21
22                 public function AMFConnection() {
23
24                         if (Connection.policyURL!='')
25                 Security.loadPolicyFile(Connection.policyURL);
26
27                         readConnection=new NetConnection();
28                         readConnection.objectEncoding = flash.net.ObjectEncoding.AMF0;
29                         readConnection.connect(Connection.apiBaseURL+"amf/read");
30                         
31                         writeConnection=new NetConnection();
32                         writeConnection.objectEncoding = flash.net.ObjectEncoding.AMF0;
33                         writeConnection.connect(Connection.apiBaseURL+"amf/write");
34                         
35                 }
36
37                 override public function getEnvironment(responder:Responder):void {
38                         readConnection.call("getpresets",responder,null,"en");
39                 }
40                 
41                 override public function loadBbox(left:Number,right:Number,
42                                                                 top:Number,bottom:Number):void {
43                         readConnection.call("whichways",new Responder(gotBbox, error),left,bottom,right,top);
44                 }
45                 
46                 override public function sendEvent(e:*):void {
47                         eventTarget.addEvent(e);
48                 }
49
50         private function gotBbox(r:Object):void {
51                         var code:uint=r.shift();
52             if (code) {
53                 error(new Array(r.shift()));
54                 return;
55             }
56
57                         var message:String    =r.shift();
58                         var waylist:Array     =r[0];
59                         var pointlist:Array   =r[1];
60                         var relationlist:Array=r[2];
61                         var id:Number, version:uint;
62                         var requests:AMFCounter=new AMFCounter(this);
63                         eventTarget=requests;
64
65                         // Load relations
66
67                         for each (var a:Array in relationlist) {
68                                 id=Number(a[0]);
69                 version=uint(a[1]);
70
71                 var relation:Relation = getRelation(id);
72                 if ( relation == null || !relation.loaded  ) {
73                     loadRelation(id);
74                                         requests.addRelationRequest(id);
75                 }
76                         }
77
78                         // Load ways
79
80                         for each (var w:Array in waylist) {
81                                 id=Number(w[0]);
82                 version=uint(w[1]);
83
84                 var way:Way = getWay(id);
85                 if ( way == null || !way.loaded ) {
86                     loadWay(id);
87                                         requests.addWayRequest(id);
88                 }
89                         }
90
91                         // Create POIs
92
93                         for each (var p:Array in pointlist) {
94                                 id = Number(p[0]);
95                 version = uint(p[4]);
96
97                 var node:Node = getNode(id);
98                 if ( node == null || !node.loaded ) {
99                     var lat:Number = Number(p[2]);
100                     var lon:Number = Number(p[1]);
101                     var tags:Object = p[3];
102                     node = new Node(id, version, tags, true, lat, lon);
103                     setNode(node);
104                 }
105                 registerPOI(node);
106                         }
107
108                         bboxrequests.push(requests);
109         }
110
111         private function error(r:Object):void {}
112
113                 private function loadWay(id:uint):void {
114                         readConnection.call("getway",new Responder(gotWay, error),id);
115                 }
116
117                 private function gotWay(r:Object):void {
118                         var code:uint = r.shift();
119             if (code) {
120                 error(new Array(r.shift()));
121                 return;
122             }
123
124                         var message:String=r.shift();
125             var id:Number = Number(r[0]);
126                         var version:uint = uint(r[3]);
127
128             var way:Way = getWay(id);
129             if ( way != null && way.loaded ) {
130                                 gotRequest(id+"way");
131                 return;
132                         }
133
134             var nodesAMF:Array = r[1];
135                         var tags:Object = r[2];
136                         
137             var nodes:Array = [];
138                         for each (var p:Array in nodesAMF) {
139                 var nodeID:Number = Number(p[2]);
140                 var nodeVersion:uint = uint(p[4]);
141
142                 var node:Node = getNode(nodeID);
143                 if ( node == null ) {
144                     var lon:Number = Number(p[0]);
145                     var lat:Number = Number(p[1]);
146                     var nodeTags:Object = p[3];
147                     node = new Node(nodeID, nodeVersion, nodeTags, true, lat, lon);
148                     setNode(node);
149                 } else if (!node.loaded) {
150                                         node.update(nodeVersion, nodeTags, true, lat, lon);
151                                 }
152                 nodes.push(node);
153                         }
154
155                         if (way==null) {
156                 way = new Way(id, version, tags, true, nodes);
157                 setWay(way);
158                         } else {
159                                 way.update(version, tags, true, nodes);
160                         }
161                         gotRequest(id+"way");
162                 }
163
164
165                 private function loadRelation(id:uint):void {
166                         readConnection.call("getrelation",new Responder(gotRelation, error),id);
167                 }
168
169                 private function gotRelation(r:Object):void {
170                         var code:uint = r.shift();
171             if (code) { error(new Array(r.shift())); return; }
172                         var message:String=r.shift();
173
174             var id:Number = Number(r[0]);
175                         var version:uint = uint(r[3]);
176
177             var relation:Relation = getRelation(id);
178             if ( relation != null && relation.loaded ) {
179                                 gotRequest(id+"rel");
180                                 return;
181                         }
182
183                         var tags:Object = r[1];
184             var membersAMF:Array = r[2];
185                         var members:Array = [];
186                         for each (var p:Array in membersAMF) {
187                                 var type:String=p[0];
188                                 var memid:Number=p[1];
189                                 var role:String=p[2];
190                                 var e:Entity;
191                                 switch (type) {
192                                         case 'Node':
193                                                 e=getNode(memid);
194                                                 if (e==null) { e=new Node(memid,0,{},false,0,0); setNode(Node(e)); }
195                                                 break;
196                                         case 'Way':
197                                                 e=getWay(memid);
198                                                 if (e==null) { e=new Way(memid,0,{},false,[]); setWay(Way(e)); }
199                                                 break;
200                                         case 'Relation':
201                                                 e=getRelation(memid);
202                                                 if (e==null) { e=new Relation(memid,0,{},false,[]); setRelation(Relation(e)); }
203                                                 break;
204                                 }
205                                 members.push(new RelationMember(e,role));
206                         }
207                         if (relation==null) {
208                     relation = new Relation(id, version, tags, true, members);
209                     setRelation(relation);
210                         } else {
211                                 relation.update(version,tags,true,members);
212                         }
213                         gotRequest(id+"rel");
214                 }
215                 
216                 private function gotRequest(n:String):void {
217                         for each (var c:AMFCounter in bboxrequests) {
218                                 if (c.removeRequest(n)) { break; }
219                         }
220                         while (bboxrequests.length>0 && bboxrequests[0].count==0) {
221                                 bboxrequests.shift();
222                         }
223                 }
224         }
225 }