]> git.openstreetmap.org Git - rails.git/blob - lib/potlatch.rb
Potlatch 2 localisation updates
[rails.git] / lib / potlatch.rb
1 require 'stringio'
2
3 # The Potlatch module provides helper functions for potlatch and its communication with the server
4 module Potlatch
5
6   # The AMF class is a set of helper functions for encoding and decoding AMF.
7   class AMF
8     
9     # Return two-byte integer
10     def self.getint(s) 
11       s.getbyte*256+s.getbyte
12     end
13
14     # Return four-byte long
15     def self.getlong(s) 
16       ((s.getbyte*256+s.getbyte)*256+s.getbyte)*256+s.getbyte
17     end
18
19     # Return string with two-byte length 
20     def self.getstring(s) 
21       len=s.getbyte*256+s.getbyte
22       str=s.read(len)
23       str.force_encoding("UTF-8") if str.respond_to?("force_encoding")
24       str
25     end
26
27     # Return eight-byte double-precision float 
28     def self.getdouble(s) 
29       a=s.read(8).unpack('G')                   # G big-endian, E little-endian
30       a[0]
31     end
32
33     # Return numeric array
34     def self.getarray(s) 
35       len=getlong(s)
36       arr=[]
37       for i in (0..len-1)
38         arr[i]=getvalue(s)
39       end
40       arr
41     end
42
43     # Return object/hash 
44     def self.getobject(s) 
45       arr={}
46       while (key=getstring(s))
47         if (key=='') then break end
48         arr[key]=getvalue(s)
49       end
50       s.getbyte         # skip the 9 'end of object' value
51       arr
52     end
53
54     # Parse and get value
55     def self.getvalue(s) 
56       case s.getbyte
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
67       end
68     end
69
70     # Envelope data into AMF writeable form
71     def self.putdata(index,n) 
72       d =encodestring(index+"/onResult")
73       d+=encodestring("null")
74       d+=[-1].pack("N")
75       d+=encodevalue(n)
76     end
77
78     # Pack variables as AMF
79     def self.encodevalue(n) 
80       case n.class.to_s
81       when 'Array'
82         a=10.chr+encodelong(n.length)
83         n.each do |b|
84           a+=encodevalue(b)
85         end
86         a
87       when 'Hash'
88         a=3.chr
89         n.each do |k,v|
90           a+=encodestring(k.to_s)+encodevalue(v)
91         end
92         a+0.chr+0.chr+9.chr
93       when 'String'
94         2.chr+encodestring(n)
95       when 'Bignum','Fixnum','Float'
96         0.chr+encodedouble(n)
97       when 'NilClass'
98         5.chr
99           when 'TrueClass'
100         0.chr+encodedouble(1)
101           when 'FalseClass'
102         0.chr+encodedouble(0)
103       else
104         Rails.logger.error("Unexpected Ruby type for AMF conversion: "+n.class.to_s)
105       end
106     end
107
108     # Encode string with two-byte length
109     def self.encodestring(n) 
110       a,b=n.size.divmod(256)
111       a.chr+b.chr+n
112     end
113
114     # Encode number as eight-byte double precision float 
115     def self.encodedouble(n) 
116       [n].pack('G')
117     end
118
119     # Encode number as four-byte long
120     def self.encodelong(n) 
121       [n].pack('N')
122     end
123
124   end
125
126   # The Dispatcher class handles decoding a series of RPC calls
127   # from the request, dispatching them, and encoding the response
128   class Dispatcher
129     def initialize(request, &block)
130       # Get stream for request data
131       @request = StringIO.new(request + 0.chr)
132
133       # Skip version indicator and client ID
134       @request.read(2)
135
136       # Skip headers
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
141       end
142
143       # Capture the dispatch routine
144       @dispatch = Proc.new
145     end
146
147     def each(&block)
148       # Read number of message bodies
149       bodies = AMF.getint(@request)
150
151       # Output response header
152       a,b = bodies.divmod(256)
153       yield 0.chr + 0.chr + 0.chr + 0.chr + a.chr + b.chr
154
155       # Process the bodies
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)
161
162         result = @dispatch.call(name, *args)
163
164         yield AMF.putdata(index, result)
165       end
166     end
167   end
168
169   # The Potlatch class is a helper for Potlatch
170   class Potlatch
171
172     # ----- getpresets
173     #             in:   none
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
177     #                           (all hashes)
178     def self.get_presets
179       Rails.logger.info("  Message: getpresets")
180
181       # Read preset menus
182       presets={}
183       presetmenus={}; presetmenus['point']=[]; presetmenus['way']=[]; presetmenus['POI']=[]
184       presetnames={}; presetnames['point']={}; presetnames['way']={}; presetnames['POI']={}
185       presettype=''
186       presetcategory=''
187       # StringIO.open(txt) do |file|
188       File.open("#{Rails.root}/config/potlatch/presets.txt") do |file|
189         file.each_line {|line|
190           t=line.chomp
191           if (t=~/(\w+)\/(\w+)/) then
192             presettype=$1
193             presetcategory=$2
194             presetmenus[presettype].push(presetcategory)
195             presetnames[presettype][presetcategory]=["(no preset)"]
196           elsif (t=~/^([\w\s]+):\s?(.+)$/) then
197             pre=$1; kv=$2
198             presetnames[presettype][presetcategory].push(pre)
199             presets[pre]={}
200             kv.split(',').each {|a|
201               if (a=~/^(.+)=(.*)$/) then presets[pre][$1]=$2 end
202             }
203           end
204         }
205       end
206
207       # Read colours/styling
208       colours={}; casing={}; areas={}
209       File.open("#{Rails.root}/config/potlatch/colours.txt") do |file|
210         file.each_line {|line|
211           t=line.chomp
212           if (t=~/(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/) then
213             tag=$1
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
217           end
218         }
219       end
220
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|
225           t=line.chomp
226           if (t=~/(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/) then
227             tag=$1
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
231           end
232         }
233       end
234
235       # Read POI presets
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")
240           icon_list.push(icon)
241           icon_tags[icon]=Hash[*tags.scan(/([^;=]+)=([^;=]+)/).flatten]
242         }
243       end
244       icon_list.reverse!
245       
246       # Read auto-complete
247       autotags={}; autotags['point']={}; autotags['way']={}; autotags['POI']={};
248       File.open("#{Rails.root}/config/potlatch/autocomplete.txt") do |file|
249         file.each_line {|line|
250           t=line.chomp
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
255           end
256         }
257       end
258
259       [presets,presetmenus,presetnames,colours,casing,areas,autotags,relcolours,relalphas,relwidths,icon_list,{},icon_tags]
260     end
261   end
262
263 end
264