1 class AmfController < ApplicationController
6 # - user authentication
7 # - (also pass lat/lon through from view tab to edit tab)
9 # ====================================================================
12 # ---- talk process AMF request
15 req=StringIO.new(request.raw_post) # Get POST data as request
16 req.read(2) # Skip version indicator and client ID
17 results={} # Results of each body
22 headers=getint(req) # Read number of headers
23 for i in (1..headers) # Read each header
24 name=getstring(req) # |
25 req.getc # | skip boolean
26 value=getvalue(req) # |
27 header["name"]=value # |
30 bodies=getint(req) # Read number of bodies
31 for i in (1..bodies) # Read each body
32 message=getstring(req) # | get message name
33 index=getstring(req) # | get index in response sequence
34 bytes=getlong(req) # | get total size in bytes
35 args=getvalue(req) # | get response (probably an array)
38 when 'getpresets'; results[index]=putdata(index,getpresets)
39 when 'whichways'; results[index]=putdata(index,whichways(args))
40 when 'getway'; results[index]=putdata(index,getway(args))
41 when 'putway'; results[index]=putdata(index,putway(args))
42 when 'deleteway'; results[index]=putdata(index,deleteway(args))
49 response.headers["Content-Type"]="application/x-amf"
50 a,b=results.length.divmod(256)
51 ans=0.chr+0.chr+0.chr+0.chr+a.chr+b.chr
60 # ====================================================================
65 presetmenus={}; presetmenus['point']=[]; presetmenus['way']=[]
66 presetnames={}; presetnames['point']={}; presetnames['way']={}
70 File.open("config/potlatch/presets.txt") do |file|
71 file.each_line {|line|
73 if (t=~/(\w+)\/(\w+)/) then
76 presetmenus[presettype].push(presetcategory)
77 presetnames[presettype][presetcategory]=["(no preset)"]
78 elsif (t=~/^(.+):\s?(.+)$/) then
80 presetnames[presettype][presetcategory].push(pre)
82 kv.split(',').each {|a|
83 if (a=~/^(.+)=(.*)$/) then presets[pre][$1]=$2 end
88 [presets,presetmenus,presetnames]
107 # need support functions here too:
108 # database support functions (readwayquery, createuniquesegments)
109 # tag2array, array2tag
112 # ====================================================================
113 # AMF read subroutines
115 # ----- getint return two-byte integer
116 # ----- getlong return four-byte long
117 # ----- getstring return string with two-byte length
118 # ----- getdouble return eight-byte double-precision float
119 # ----- getobject return object/hash
120 # ----- getarray return numeric array
127 ((s.getc*256+s.getc)*256+s.getc)*256+s.getc
131 len=s.getc*256+s.getc
136 a=s.read(8).unpack('G') # G big-endian, E little-endian
151 while (key=getstring(s))
152 if (key=='') then break end
155 s.getc # skip the 9 'end of object' value
159 # ----- getvalue parse and get value
163 when 0; return getdouble(s) # number
164 when 1; return s.getc # boolean
165 when 2; return getstring(s) # string
166 when 3; return getobject(s) # object/hash
167 when 5; return nil # null
168 when 6; return nil # undefined
169 when 8; s.read(4) # mixedArray
170 return getobject(s) # |
171 when 10;return getarray(s) # array
172 else; return nil # error
176 # ====================================================================
177 # AMF write subroutines
179 # ----- putdata envelope data into AMF writeable form
180 # ----- encodevalue pack variables as AMF
183 d =encodestring(index+"/onResult")
184 d+=encodestring("null")
192 a=10.chr+encodelong(n.length)
200 a+=encodestring(k)+encodevalue(v)
204 2.chr+encodestring(n)
205 when 'Bignum','Fixnum','Float'
206 0.chr+encodedouble(n)
212 # ----- encodestring encode string with two-byte length
213 # ----- encodedouble encode number as eight-byte double precision float
214 # ----- encodelong encode number as four-byte long
217 a,b=n.size.divmod(256)
229 # ====================================================================
230 # Co-ordinate conversion
233 -(lat2y(a)-$basey)*$masterscale+250
237 (a-$baselong)*$masterscale+350
241 180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2))
245 y2lat((a-250)/-$masterscale+$basey)
249 (a-350)/$masterscale+$baselong
253 180/Math::PI * (2*Math.atan(Math.exp(a*Math::PI/180))-Math::PI/2)