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
14 # Return four-byte long
16 ((s.getc*256+s.getc)*256+s.getc)*256+s.getc
19 # Return string with two-byte length
25 # Return eight-byte double-precision float
27 a=s.read(8).unpack('G') # G big-endian, E little-endian
31 # Return numeric array
44 while (key=getstring(s))
45 if (key=='') then break end
48 s.getc # skip the 9 'end of object' value
55 when 0; return getdouble(s) # number
56 when 1; return s.getc # boolean
57 when 2; return getstring(s) # string
58 when 3; return getobject(s) # object/hash
59 when 5; return nil # null
60 when 6; return nil # undefined
61 when 8; s.read(4) # mixedArray
62 return getobject(s) # |
63 when 10;return getarray(s) # array
64 else; return nil # error
68 # Envelope data into AMF writeable form
69 def self.putdata(index,n)
70 d =encodestring(index+"/onResult")
71 d+=encodestring("null")
76 # Pack variables as AMF
77 def self.encodevalue(n)
80 a=10.chr+encodelong(n.length)
88 a+=encodestring(k.to_s)+encodevalue(v)
93 when 'Bignum','Fixnum','Float'
100 0.chr+encodedouble(0)
102 Rails.logger.error("Unexpected Ruby type for AMF conversion: "+n.class.to_s)
106 # Encode string with two-byte length
107 def self.encodestring(n)
108 a,b=n.size.divmod(256)
112 # Encode number as eight-byte double precision float
113 def self.encodedouble(n)
117 # Encode number as four-byte long
118 def self.encodelong(n)
124 # The Dispatcher class handles decoding a series of RPC calls
125 # from the request, dispatching them, and encoding the response
127 def initialize(request, &block)
128 # Get stream for request data
129 @request = StringIO.new(request + 0.chr)
131 # Skip version indicator and client ID
135 AMF.getint(@request).times do # Read number of headers and loop
136 AMF.getstring(@request) # | skip name
137 req.getc # | skip boolean
138 AMF.getvalue(@request) # | skip value
141 # Capture the dispatch routine
146 # Read number of message bodies
147 bodies = AMF.getint(@request)
149 # Output response header
150 a,b = bodies.divmod(256)
151 yield 0.chr + 0.chr + 0.chr + 0.chr + a.chr + b.chr
154 bodies.times do # Read each body
155 name = AMF.getstring(@request) # | get message name
156 index = AMF.getstring(@request) # | get index in response sequence
157 bytes = AMF.getlong(@request) # | get total size in bytes
158 args = AMF.getvalue(@request) # | get response (probably an array)
160 result = @dispatch.call(name, *args)
162 yield AMF.putdata(index, result)
167 # The Potlatch class is a helper for Potlatch
172 # does: reads tag preset menus, colours, and autocomplete config files
173 # out: [0] presets, [1] presetmenus, [2] presetnames,
174 # [3] colours, [4] casing, [5] areas, [6] autotags
177 Rails.logger.info(" Message: getpresets")
181 presetmenus={}; presetmenus['point']=[]; presetmenus['way']=[]; presetmenus['POI']=[]
182 presetnames={}; presetnames['point']={}; presetnames['way']={}; presetnames['POI']={}
185 # StringIO.open(txt) do |file|
186 File.open("#{Rails.root}/config/potlatch/presets.txt") do |file|
187 file.each_line {|line|
189 if (t=~/(\w+)\/(\w+)/) then
192 presetmenus[presettype].push(presetcategory)
193 presetnames[presettype][presetcategory]=["(no preset)"]
194 elsif (t=~/^([\w\s]+):\s?(.+)$/) then
196 presetnames[presettype][presetcategory].push(pre)
198 kv.split(',').each {|a|
199 if (a=~/^(.+)=(.*)$/) then presets[pre][$1]=$2 end
205 # Read colours/styling
206 colours={}; casing={}; areas={}
207 File.open("#{Rails.root}/config/potlatch/colours.txt") do |file|
208 file.each_line {|line|
210 if (t=~/(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/) then
212 if ($2!='-') then colours[tag]=$2.hex end
213 if ($3!='-') then casing[tag]=$3.hex end
214 if ($4!='-') then areas[tag]=$4.hex end
219 # Read relations colours/styling
220 relcolours={}; relalphas={}; relwidths={}
221 File.open("#{Rails.root}/config/potlatch/relation_colours.txt") do |file|
222 file.each_line {|line|
224 if (t=~/(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/) then
226 if ($2!='-') then relcolours[tag]=$2.hex end
227 if ($3!='-') then relalphas[tag]=$3.to_i end
228 if ($4!='-') then relwidths[tag]=$4.to_i end
234 icon_list=[]; icon_tags={};
235 File.open("#{Rails.root}/config/potlatch/icon_presets.txt") do |file|
236 file.each_line {|line|
237 (icon,tags)=line.chomp.split("\t")
239 icon_tags[icon]=Hash[*tags.scan(/([^;=]+)=([^;=]+)/).flatten]
245 autotags={}; autotags['point']={}; autotags['way']={}; autotags['POI']={};
246 File.open("#{Rails.root}/config/potlatch/autocomplete.txt") do |file|
247 file.each_line {|line|
249 if (t=~/^([\w:]+)\/(\w+)\s+(.+)$/) then
250 tag=$1; type=$2; values=$3
251 if values=='-' then autotags[type][tag]=[]
252 else autotags[type][tag]=values.split(',').sort.reverse end
257 [presets,presetmenus,presetnames,colours,casing,areas,autotags,relcolours,relalphas,relwidths,icon_list,{},icon_tags]