3 # The Potlatch module provides helper functions for potlatch and its communication with the server
6 # The AMF class is a set of helper functions for encoding and decoding AMF.
9 # Return two-byte integer
11 s.getbyte*256+s.getbyte
14 # Return four-byte long
16 ((s.getbyte*256+s.getbyte)*256+s.getbyte)*256+s.getbyte
19 # Return string with two-byte length
21 len=s.getbyte*256+s.getbyte
23 str.force_encoding("UTF-8") if str.respond_to?("force_encoding")
27 # Return eight-byte double-precision float
29 a=s.read(8).unpack('G') # G big-endian, E little-endian
33 # Return numeric array
46 while (key=getstring(s))
47 if (key=='') then break end
50 s.getbyte # skip the 9 'end of object' value
57 when 0; return getdouble(s) # number
58 when 1; return s.getbyte # boolean
59 when 2; return getstring(s) # string
60 when 3; return getobject(s) # object/hash
61 when 5; return nil # null
62 when 6; return nil # undefined
63 when 8; s.read(4) # mixedArray
64 return getobject(s) # |
65 when 10; return getarray(s) # array
66 else; return nil # error
70 # Envelope data into AMF writeable form
71 def self.putdata(index,n)
72 d =encodestring(index+"/onResult")
73 d+=encodestring("null")
78 # Pack variables as AMF
79 def self.encodevalue(n)
82 a=10.chr+encodelong(n.length)
90 a+=encodestring(k.to_s)+encodevalue(v)
95 when 'Bignum','Fixnum','Float'
100 0.chr+encodedouble(1)
102 0.chr+encodedouble(0)
104 Rails.logger.error("Unexpected Ruby type for AMF conversion: "+n.class.to_s)
108 # Encode string with two-byte length
109 def self.encodestring(n)
110 a,b=n.size.divmod(256)
114 # Encode number as eight-byte double precision float
115 def self.encodedouble(n)
119 # Encode number as four-byte long
120 def self.encodelong(n)
126 # The Dispatcher class handles decoding a series of RPC calls
127 # from the request, dispatching them, and encoding the response
129 def initialize(request, &block)
130 # Get stream for request data
131 @request = StringIO.new(request + 0.chr)
133 # Skip version indicator and client ID
137 AMF.getint(@request).times do # Read number of headers and loop
138 AMF.getstring(@request) # | skip name
139 req.getbyte # | skip boolean
140 AMF.getvalue(@request) # | skip value
143 # Capture the dispatch routine
148 # Read number of message bodies
149 bodies = AMF.getint(@request)
151 # Output response header
152 a,b = bodies.divmod(256)
153 yield 0.chr + 0.chr + 0.chr + 0.chr + a.chr + b.chr
156 bodies.times do # Read each body
157 name = AMF.getstring(@request) # | get message name
158 index = AMF.getstring(@request) # | get index in response sequence
159 bytes = AMF.getlong(@request) # | get total size in bytes
160 args = AMF.getvalue(@request) # | get response (probably an array)
162 result = @dispatch.call(name, *args)
164 yield AMF.putdata(index, result)
169 # The Potlatch class is a helper for Potlatch
174 # does: reads tag preset menus, colours, and autocomplete config files
175 # out: [0] presets, [1] presetmenus, [2] presetnames,
176 # [3] colours, [4] casing, [5] areas, [6] autotags
179 Rails.logger.info(" Message: getpresets")
183 presetmenus={}; presetmenus['point']=[]; presetmenus['way']=[]; presetmenus['POI']=[]
184 presetnames={}; presetnames['point']={}; presetnames['way']={}; presetnames['POI']={}
187 # StringIO.open(txt) do |file|
188 File.open("#{Rails.root}/config/potlatch/presets.txt") do |file|
189 file.each_line {|line|
191 if (t=~/(\w+)\/(\w+)/) then
194 presetmenus[presettype].push(presetcategory)
195 presetnames[presettype][presetcategory]=["(no preset)"]
196 elsif (t=~/^([\w\s]+):\s?(.+)$/) then
198 presetnames[presettype][presetcategory].push(pre)
200 kv.split(',').each {|a|
201 if (a=~/^(.+)=(.*)$/) then presets[pre][$1]=$2 end
207 # Read colours/styling
208 colours={}; casing={}; areas={}
209 File.open("#{Rails.root}/config/potlatch/colours.txt") do |file|
210 file.each_line {|line|
212 if (t=~/(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/) then
214 if ($2!='-') then colours[tag]=$2.hex end
215 if ($3!='-') then casing[tag]=$3.hex end
216 if ($4!='-') then areas[tag]=$4.hex end
221 # Read relations colours/styling
222 relcolours={}; relalphas={}; relwidths={}
223 File.open("#{Rails.root}/config/potlatch/relation_colours.txt") do |file|
224 file.each_line {|line|
226 if (t=~/(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/) then
228 if ($2!='-') then relcolours[tag]=$2.hex end
229 if ($3!='-') then relalphas[tag]=$3.to_i end
230 if ($4!='-') then relwidths[tag]=$4.to_i end
236 icon_list=[]; icon_tags={};
237 File.open("#{Rails.root}/config/potlatch/icon_presets.txt") do |file|
238 file.each_line {|line|
239 (icon,tags)=line.chomp.split("\t")
241 icon_tags[icon]=Hash[*tags.scan(/([^;=]+)=([^;=]+)/).flatten]
247 autotags={}; autotags['point']={}; autotags['way']={}; autotags['POI']={};
248 File.open("#{Rails.root}/config/potlatch/autocomplete.txt") do |file|
249 file.each_line {|line|
251 if (t=~/^([\w:]+)\/(\w+)\s+(.+)$/) then
252 tag=$1; type=$2; values=$3
253 if values=='-' then autotags[type][tag]=[]
254 else autotags[type][tag]=values.split(',').sort.reverse end
259 [presets,presetmenus,presetnames,colours,casing,areas,autotags,relcolours,relalphas,relwidths,icon_list,{},icon_tags]