3 # The Potlatch module provides helper functions for potlatch and its communication with the server
5 # The AMF class is a set of helper functions for encoding and decoding AMF.
7 # Return two-byte integer
9 s.getbyte * 256 + s.getbyte
12 # Return four-byte long
14 ((s.getbyte * 256 + s.getbyte) * 256 + s.getbyte) * 256 + s.getbyte
17 # Return string with two-byte length
19 len = s.getbyte * 256 + s.getbyte
21 str.force_encoding("UTF-8") if str.respond_to?("force_encoding")
25 # Return eight-byte double-precision float
27 a = s.read(8).unpack("G") # G big-endian, E little-endian
31 # Return numeric array
33 getlong(s).times.collect do
41 while (key = getstring(s))
43 arr[key] = getvalue(s)
45 s.getbyte # skip the 9 'end of object' value
52 when 0 then return getdouble(s) # number
53 when 1 then return s.getbyte # boolean
54 when 2 then return getstring(s) # string
55 when 3 then return getobject(s) # object/hash
56 when 5 then return nil # null
57 when 6 then return nil # undefined
58 when 8 then s.read(4) # mixedArray
59 return getobject(s) # |
60 when 10 then return getarray(s) # array
61 else return nil # error
65 # Envelope data into AMF writeable form
66 def self.putdata(index, n)
67 d = encodestring(index + "/onResult")
68 d += encodestring("null")
74 # Pack variables as AMF
75 def self.encodevalue(n)
78 a = 10.chr + encodelong(n.length)
86 a += encodestring(k.to_s) + encodevalue(v)
88 a + 0.chr + 0.chr + 9.chr
90 2.chr + encodestring(n)
91 when "Bignum", "Fixnum", "Float"
92 0.chr + encodedouble(n)
96 0.chr + encodedouble(1)
98 0.chr + encodedouble(0)
100 Rails.logger.error("Unexpected Ruby type for AMF conversion: " + n.class.to_s)
104 # Encode string with two-byte length
105 def self.encodestring(n)
106 n = n.dup.force_encoding("ASCII-8BIT") if n.respond_to?("force_encoding")
107 a, b = n.size.divmod(256)
111 # Encode number as eight-byte double precision float
112 def self.encodedouble(n)
116 # Encode number as four-byte long
117 def self.encodelong(n)
122 # The Dispatcher class handles decoding a series of RPC calls
123 # from the request, dispatching them, and encoding the response
125 def initialize(request, &_block)
126 # Get stream for request data
127 @request = StringIO.new(request + 0.chr)
129 # Skip version indicator and client ID
133 AMF.getint(@request).times do # Read number of headers and loop
134 AMF.getstring(@request) # | skip name
135 req.getbyte # | skip boolean
136 AMF.getvalue(@request) # | skip value
139 # Capture the dispatch routine
144 # Read number of message bodies
145 bodies = AMF.getint(@request)
147 # Output response header
148 a, b = bodies.divmod(256)
149 yield 0.chr + 0.chr + 0.chr + 0.chr + a.chr + b.chr
152 bodies.times do # Read each body
153 name = AMF.getstring(@request) # | get message name
154 index = AMF.getstring(@request) # | get index in response sequence
155 AMF.getlong(@request) # | get total size in bytes
156 args = AMF.getvalue(@request) # | get response (probably an array)
158 result = @dispatch.call(name, *args)
160 yield AMF.putdata(index, result)
165 # The Potlatch class is a helper for Potlatch
169 # does: reads tag preset menus, colours, and autocomplete config files
170 # out: [0] presets, [1] presetmenus, [2] presetnames,
171 # [3] colours, [4] casing, [5] areas, [6] autotags
174 Rails.logger.info(" Message: getpresets")
178 presetmenus = { "point" => [], "way" => [], "POI" => [] }
179 presetnames = { "point" => {}, "way" => {}, "POI" => {} }
182 # StringIO.open(txt) do |file|
183 File.open("#{Rails.root}/config/potlatch/presets.txt") do |file|
184 file.each_line do|line|
186 if t =~ %r{(\w+)/(\w+)}
189 presetmenus[presettype].push(presetcategory)
190 presetnames[presettype][presetcategory] = ["(no preset)"]
191 elsif t =~ /^([\w\s]+):\s?(.+)$/
194 presetnames[presettype][presetcategory].push(pre)
196 kv.split(",").each do|a|
197 presets[pre][$1] = $2 if a =~ /^(.+)=(.*)$/
203 # Read colours/styling
207 File.open("#{Rails.root}/config/potlatch/colours.txt") do |file|
208 file.each_line do |line|
209 next unless line.chomp =~ /(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/
212 colours[tag] = $2.hex if $2 != "-"
213 casing[tag] = $3.hex if $3 != "-"
214 areas[tag] = $4.hex if $4 != "-"
218 # Read relations colours/styling
222 File.open("#{Rails.root}/config/potlatch/relation_colours.txt") do |file|
223 file.each_line do |line|
224 next unless line.chomp =~ /(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/
227 relcolours[tag] = $2.hex if $2 != "-"
228 relalphas[tag] = $3.to_i if $3 != "-"
229 relwidths[tag] = $4.to_i if $4 != "-"
236 File.open("#{Rails.root}/config/potlatch/icon_presets.txt") do |file|
237 file.each_line do |line|
238 (icon, tags) = line.chomp.split("\t")
240 icon_tags[icon] = Hash[*tags.scan(/([^;=]+)=([^;=]+)/).flatten]
246 autotags = { "point" => {}, "way" => {}, "POI" => {} }
247 File.open("#{Rails.root}/config/potlatch/autocomplete.txt") do |file|
248 file.each_line do|line|
249 next unless line.chomp =~ %r{^([\w:]+)/(\w+)\s+(.+)$}
255 autotags[type][tag] = []
257 autotags[type][tag] = values.split(",").sort.reverse
262 [presets, presetmenus, presetnames, colours, casing, areas, autotags, relcolours, relalphas, relwidths, icon_list, {}, icon_tags]