Class: GwtRpc::Response::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/gwt_rpc/response/reader.rb

Constant Summary collapse

DEBUG =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, json_body) ⇒ Reader

Returns a new instance of Reader.



5
6
7
8
9
10
11
# File 'lib/gwt_rpc/response/reader.rb', line 5

def initialize(client, json_body)
  @client = client
  true_json = demangle_json_body(json_body)
  (@version, placeholder, @string_table, *@data) = JSON.parse(true_json).reverse
  @max_prior_string_location = 0
  @objects = []
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/gwt_rpc/response/reader.rb', line 4

def data
  @data
end

#objectsObject (readonly)

Returns the value of attribute objects.



4
5
6
# File 'lib/gwt_rpc/response/reader.rb', line 4

def objects
  @objects
end

#string_tableObject (readonly)

Returns the value of attribute string_table.



4
5
6
# File 'lib/gwt_rpc/response/reader.rb', line 4

def string_table
  @string_table
end

#versionObject (readonly)

Returns the value of attribute version.



4
5
6
# File 'lib/gwt_rpc/response/reader.rb', line 4

def version
  @version
end

Instance Method Details

#debugObject



69
70
71
72
73
74
75
76
77
# File 'lib/gwt_rpc/response/reader.rb', line 69

def debug
  html = "<table>\n"
  html << @data.map{|i| [i, @string_table[i-1]].map{|val| "<td>#{val}</td>"}}.map{|row| "<tr>#{row}</tr>"}.join("\n")
  html << "</table><table>\n"
  @string_table.each_with_index{|str,i| html << "<tr><td>#{i+1}</td><td>#{str}</td></tr>\n"}
  html << "</table>"
  
  html
end

#demangle_json_body(faux_json) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gwt_rpc/response/reader.rb', line 79

def demangle_json_body(faux_json)
  if faux_json.match(/^\[(.+),\[(.+)\],0,7\]$/)
    data = $1
    string_table = $2
    data.gsub!(/'/,'"')

    "[#{data},[#{string_table}],0,7]"
  else
    raise "can't demangle"
  end
end

#read_intObject



13
14
15
16
17
# File 'lib/gwt_rpc/response/reader.rb', line 13

def read_int
  int = @data.shift
  puts "read int #{int}" if DEBUG
  int
end

#read_objectObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gwt_rpc/response/reader.rb', line 34

def read_object
  int = read_int
  
  if int < 0
    puts "reading obj reference (#{int})" if DEBUG
    obj = @objects[-1 - int]
    # if int == -11
    #   puts obj.inspect
    #   @objects.each_with_index do |doc, i|
    #     puts "#{i} => #{doc.inspect}"
    #   end
    #   exit
    # end
  elsif int > 0
    java_class = read_string(int)
    java_class.sub!(/\/\d+$/,'')
    puts "reading obj #{java_class}" if DEBUG
    ruby_class = @client.class.java_class_to_ruby(java_class)
    
    if ruby_class
      placeholder_position = @objects.count
      @objects << "PLACEHOLDER of type #{java_class}"
      
      obj = ruby_class.constantize.gwt_deserialize(self)
      @objects[placeholder_position] = obj
    else
      raise "unknown java class '#{java_class}'"
    end
  elsif int == 0
    puts "reading obj nil (0)" if DEBUG
    obj = nil
  end
  obj
end

#read_string(position = read_int) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gwt_rpc/response/reader.rb', line 19

def read_string(position=read_int)
  if position > (@max_prior_string_location + 1)
    raise "trying to read #{position}, which is too far ahead; max seen thus far is #{@max_prior_string_location}!"
  end
  
  if position > @max_prior_string_location
    @max_prior_string_location += 1
  end
  
  val = @string_table[position-1]
  puts "read str #{val} (@#{position})" if DEBUG
  
  val
end