- def talk
- req=StringIO.new(request.raw_post+0.chr) # Get POST data as request
- # (cf http://www.ruby-forum.com/topic/122163)
- req.read(2) # Skip version indicator and client ID
- results={} # Results of each body
- renumberednodes={} # Shared across repeated putways
- renumberedways={} # Shared across repeated putways
-
- # Parse request
-
- headers=AMF.getint(req) # Read number of headers
-
- headers.times do # Read each header
- name=AMF.getstring(req) # |
- req.getc # | skip boolean
- value=AMF.getvalue(req) # |
- header["name"]=value # |
- end
-
- bodies=AMF.getint(req) # Read number of bodies
- bodies.times do # Read each body
- message=AMF.getstring(req) # | get message name
- index=AMF.getstring(req) # | get index in response sequence
- bytes=AMF.getlong(req) # | get total size in bytes
- args=AMF.getvalue(req) # | get response (probably an array)
-
- case message
- when 'getpresets'; results[index]=AMF.putdata(index,getpresets())
- when 'whichways'; results[index]=AMF.putdata(index,whichways(*args))
- when 'whichways_deleted'; results[index]=AMF.putdata(index,whichways_deleted(*args))
- when 'getway'; results[index]=AMF.putdata(index,getway(args[0].to_i))
- when 'getrelation'; results[index]=AMF.putdata(index,getrelation(args[0].to_i))
- when 'getway_old'; results[index]=AMF.putdata(index,getway_old(args[0].to_i,args[1].to_i))
- when 'getway_history'; results[index]=AMF.putdata(index,getway_history(args[0].to_i))
- when 'putway'; r=putway(renumberednodes,*args)
- renumberednodes=r[3]
- if r[1] != r[2]
- renumberedways[r[1]] = r[2]
- end
- results[index]=AMF.putdata(index,r)
- when 'putrelation'; results[index]=AMF.putdata(index,putrelation(renumberednodes, renumberedways, *args))
- when 'deleteway'; results[index]=AMF.putdata(index,deleteway(args[0],args[1].to_i))
- when 'putpoi'; results[index]=AMF.putdata(index,putpoi(*args))
- when 'getpoi'; results[index]=AMF.putdata(index,getpoi(args[0].to_i))
- end
- end
-
- # Write out response
-
- a,b=results.length.divmod(256)
- render :content_type => "application/x-amf", :text => proc { |response, output|
- output.write 0.chr+0.chr+0.chr+0.chr+a.chr+b.chr
- results.each do |k,v|
- output.write(v)
- end
- }
+ def amf_read
+ if request.post?
+ req=StringIO.new(request.raw_post+0.chr)# Get POST data as request
+ # (cf http://www.ruby-forum.com/topic/122163)
+ req.read(2) # Skip version indicator and client ID
+
+ # Parse request
+
+ headers=AMF.getint(req) # Read number of headers
+ headers.times do # Read each header
+ name=AMF.getstring(req) # |
+ req.getc # | skip boolean
+ value=AMF.getvalue(req) # |
+ header["name"]=value # |
+ end
+
+ bodies=AMF.getint(req) # Read number of bodies
+ render :content_type => "application/x-amf", :text => proc { |response, output|
+ a,b=bodies.divmod(256)
+ output.write 0.chr+0.chr+0.chr+0.chr+a.chr+b.chr
+ bodies.times do # Read each body
+ message=AMF.getstring(req) # | get message name
+ index=AMF.getstring(req) # | get index in response sequence
+ bytes=AMF.getlong(req) # | get total size in bytes
+ args=AMF.getvalue(req) # | get response (probably an array)
+ result=''
+ logger.info("Executing AMF #{message}(#{args.join(',')}):#{index}")
+
+ case message
+ when 'getpresets'; result=AMF.putdata(index,getpresets(*args))
+ when 'whichways'; result=AMF.putdata(index,whichways(*args))
+ when 'whichways_deleted'; result=AMF.putdata(index,whichways_deleted(*args))
+ when 'getway'; result=AMF.putdata(index,getway(args[0].to_i))
+ when 'getrelation'; result=AMF.putdata(index,getrelation(args[0].to_i))
+ when 'getway_old'; result=AMF.putdata(index,getway_old(args[0].to_i,args[1]))
+ when 'getway_history'; result=AMF.putdata(index,getway_history(args[0].to_i))
+ when 'getnode_history'; result=AMF.putdata(index,getnode_history(args[0].to_i))
+ when 'findgpx'; result=AMF.putdata(index,findgpx(*args))
+ when 'findrelations'; result=AMF.putdata(index,findrelations(*args))
+ when 'getpoi'; result=AMF.putdata(index,getpoi(*args))
+ end
+ output.write(result)
+ end
+ }
+ else
+ render :nothing => true, :status => :method_not_allowed
+ end
+ end
+
+ def amf_write
+ if request.post?
+ req=StringIO.new(request.raw_post+0.chr)
+ req.read(2)
+ renumberednodes={} # Shared across repeated putways
+ renumberedways={} # Shared across repeated putways
+
+ headers=AMF.getint(req) # Read number of headers
+ headers.times do # Read each header
+ name=AMF.getstring(req) # |
+ req.getc # | skip boolean
+ value=AMF.getvalue(req) # |
+ header["name"]=value # |
+ end
+
+ bodies=AMF.getint(req) # Read number of bodies
+ render :content_type => "application/x-amf", :text => proc { |response, output|
+ a,b=bodies.divmod(256)
+ output.write 0.chr+0.chr+0.chr+0.chr+a.chr+b.chr
+ bodies.times do # Read each body
+ message=AMF.getstring(req) # | get message name
+ index=AMF.getstring(req) # | get index in response sequence
+ bytes=AMF.getlong(req) # | get total size in bytes
+ args=AMF.getvalue(req) # | get response (probably an array)
+ err=false # Abort batch on error
+
+ logger.info("Executing AMF #{message}:#{index}")
+ result=''
+ if err
+ result=[-5,nil]
+ else
+ case message
+ when 'putway'; orn=renumberednodes.dup
+ r=putway(renumberednodes,*args)
+ r[4]=renumberednodes.reject { |k,v| orn.has_key?(k) }
+ if r[0]==0 and r[2] != r[3] then renumberedways[r[2]] = r[3] end
+ result=AMF.putdata(index,r)
+ when 'putrelation'; result=AMF.putdata(index,putrelation(renumberednodes, renumberedways, *args))
+ when 'deleteway'; result=AMF.putdata(index,deleteway(*args))
+ when 'putpoi'; r=putpoi(*args)
+ if r[0]==0 and r[2] != r[3] then renumberednodes[r[2]] = r[3] end
+ result=AMF.putdata(index,r)
+ when 'startchangeset'; result=AMF.putdata(index,startchangeset(*args))
+ end
+ if result[0]==-3 then err=true end # If a conflict is detected, don't execute any more writes
+ end
+ output.write(result)
+ end
+ }
+ else
+ render :nothing => true, :status => :method_not_allowed
+ end