Class: ClientWorldModel

Inherits:
Object
  • Object
show all
Defined in:
lib/libowl/client_world_model.rb

Overview

This class abstracts the network details of connecting to a World Model. However, this class does not use a thread to handle networking in the background as in the ClientWorldConnection class, so handleMessage must be called manually. If threads are available then the ClientWorldConnection class is simpler to use.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, data_callback = nil, uri_response_callback = nil) ⇒ ClientWorldModel

Returns a new instance of ClientWorldModel.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/libowl/client_world_model.rb', line 49

def initialize(host, port, data_callback = nil, uri_response_callback = nil)
  @connected = false
  @host = host
  @port = port
  @socket = TCPSocket.open(host, port)
  handshake = ""
  ver_string = "GRAIL client protocol"
  #The handshake is the length of the message, the protocol string, and the version (0).
  handshake << [ver_string.length].pack('N') << ver_string << "\x00\x00"
  #Send a handshake and then receive one
  @socket.send(handshake, 0)
  inshake = @socket.recvfrom(handshake.length)[0]
  while (inshake.length < handshake.length)
    #puts "Waiting for #{handshake.length - inshake.length} byte more of handshake."
    inshake += @socket.recvfrom(handshake.length - inshake.length)[0]
  end

  @connected = true
  for i in 1..handshake.length
    if handshake[i] != inshake[i]
      puts "Handshake failure!"
      puts "For byte i we sent #{handshake[i]} but got #{inshake[i]}"
      @connected = false
    end
  end

  @alias_to_attr_name = {}
  @alias_to_origin_name = {}

  @data_callback = data_callback
  @uri_response_callback = uri_response_callback
end

Instance Attribute Details

#alias_to_attr_nameObject

Returns the value of attribute alias_to_attr_name.



40
41
42
# File 'lib/libowl/client_world_model.rb', line 40

def alias_to_attr_name
  @alias_to_attr_name
end

#alias_to_origin_nameObject

Returns the value of attribute alias_to_origin_name.



40
41
42
# File 'lib/libowl/client_world_model.rb', line 40

def alias_to_origin_name
  @alias_to_origin_name
end

#connectedObject (readonly)

Returns the value of attribute connected.



41
42
43
# File 'lib/libowl/client_world_model.rb', line 41

def connected
  @connected
end

Instance Method Details

#closeObject



82
83
84
85
# File 'lib/libowl/client_world_model.rb', line 82

def close()
  @socket.close()
  @connected = false
end

#decodeAttributeAlias(inbuff) ⇒ Object

Decode attribute alias message



121
122
123
124
125
126
127
128
129
130
# File 'lib/libowl/client_world_model.rb', line 121

def decodeAttributeAlias(inbuff)
  num_aliases = inbuff.unpack('N')[0]
  rest = inbuff[4, inbuff.length - 1]
  for i in 1..num_aliases do
    attr_alias = rest.unpack('N')[0]
    name, rest = splitURIFromRest(rest[4, rest.length - 1])
    #Assign this name to the given alias
    @alias_to_attr_name[attr_alias] = name
  end
end

#decodeDataResponse(inbuff) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/libowl/client_world_model.rb', line 161

def decodeDataResponse(inbuff)
  attributes = []
  object_uri, rest = splitURIFromRest(inbuff)
  ticket = rest.unpack('N')[0]
  total_attributes = rest[4, rest.length - 1].unpack('N')[0]
  rest = rest[8, rest.length]
  #puts "Decoding #{total_attributes} attributes"
  for i in 1..total_attributes do
    name_alias = rest.unpack('N')[0]
    creation_date = unpackuint64(rest[4, rest.length - 1])
    expiration_date = unpackuint64(rest[12, rest.length - 1])
    origin_alias = rest[20, rest.length - 1].unpack('N')[0]
    data_len = rest[24, rest.length - 1].unpack('N')[0]
    data = rest[28, data_len]
    rest = rest[28+data_len, rest.length - 1]
    attributes.push(WMAttribute.new(@alias_to_attr_name[name_alias], data, creation_date, expiration_date, @alias_to_origin_name[origin_alias]))
  end
  return WMData.new(object_uri, attributes, ticket)
end

#decodeOriginAlias(inbuff) ⇒ Object

Decode origin alias message



133
134
135
136
137
138
139
140
141
142
# File 'lib/libowl/client_world_model.rb', line 133

def decodeOriginAlias(inbuff)
  num_aliases = inbuff.unpack('N')[0]
  rest = inbuff[4, inbuff.length - 1]
  for i in 1..num_aliases do
    origin_alias = rest.unpack('N')[0]
    name, rest = splitURIFromRest(rest[4, rest.length - 1])
    #Assign this name to the given alias
    @alias_to_origin_name[origin_alias] = name
  end
end

#decodeTicketMessage(inbuff) ⇒ Object

Decode a ticket message or a request complete message.



145
146
147
# File 'lib/libowl/client_world_model.rb', line 145

def decodeTicketMessage(inbuff)
  return inbuff.unpack('N')[0]
end

#decodeURIResponse(inbuff) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/libowl/client_world_model.rb', line 149

def decodeURIResponse(inbuff)
  uris = []
  if (inbuff != nil)
    rest = inbuff
    while (rest.length > 4)
      name, rest = splitURIFromRest(rest)
      uris.push(name)
    end
  end
  return uris
end

#handleMessageObject

Handle a message of currently unknown type



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/libowl/client_world_model.rb', line 88

def handleMessage()
  #puts "Handling message..."
  #Get the message length as n unsigned integer
  inlen = (@socket.recvfrom(4)[0]).unpack('N')[0]
  inbuff = @socket.recvfrom(inlen)[0]
  #Keep reading until the entire packet is read
  #TODO This can block forever if a communication error occurs.
  while (inbuff.length < inlen) 
    inbuff += @socket.recvfrom(inlen-inbuff.length)[0]
  end
  #Byte that indicates message type
  control = inbuff.unpack('C')[0]
  if control == ATTRIBUTE_ALIAS
    decodeAttributeAlias(inbuff[1, inbuff.length - 1])
  elsif control == ORIGIN_ALIAS
    decodeOriginAlias(inbuff[1, inbuff.length - 1])
  elsif control == REQUEST_COMPLETE
    ticket = decodeTicketMessage(inbuff[1, inbuff.length-1])
    #puts "World Model completed request for ticket #{ticket}"
  elsif control == DATA_RESPONSE
    if (@data_callback != nil)
      @data_callback.call(decodeDataResponse(inbuff[1, inbuff.length - 1]))
    end
  elsif control == URI_RESPONSE
    if (@uri_response_callback != nil)
      @uri_response_callback.call(decodeURIResponse(inbuff[1, inbuff.length - 1]))
    end
  end
  #puts "processed message with id #{control}"
  return control
end

#sendRangeRequest(name_pattern, attribute_patterns, ticket, start_time, stop_time) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/libowl/client_world_model.rb', line 199

def sendRangeRequest(name_pattern, attribute_patterns, ticket, start_time, stop_time)
  buff = [RANGE_REQUEST].pack('C')

  buff += [ticket].pack('N')
  buff += strToSizedUTF16(name_pattern)
  buff += [attribute_patterns.length].pack('N')

  attribute_patterns.each{|pattern|
    buff += strToSizedUTF16(pattern)
  }

  buff += packuint64(start_time)
  buff += packuint64(stop_time)

  #Send the message with its length prepended to the front
  @socket.send("#{[buff.length].pack('N')}#{buff}", 0)
end

#sendSnapshotRequest(name_pattern, attribute_patterns, ticket, start_time = 0, stop_time = 0) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/libowl/client_world_model.rb', line 181

def sendSnapshotRequest(name_pattern, attribute_patterns, ticket, start_time = 0, stop_time = 0)
  buff = [SNAPSHOT_REQUEST].pack('C')

  buff += [ticket].pack('N')
  buff += strToSizedUTF16(name_pattern)
  buff += [attribute_patterns.length].pack('N')

  attribute_patterns.each{|pattern|
    buff += strToSizedUTF16(pattern)
  }

  buff += packuint64(start_time)
  buff += packuint64(stop_time)

  #Send the message with its length prepended to the front
  @socket.send("#{[buff.length].pack('N')}#{buff}", 0)
end

#sendStreamRequest(name_pattern, attribute_patterns, update_interval, ticket) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/libowl/client_world_model.rb', line 217

def sendStreamRequest(name_pattern, attribute_patterns, update_interval, ticket)
  buff = [STREAM_REQUEST].pack('C')

  buff += [ticket].pack('N')
  buff += strToSizedUTF16(name_pattern)
  buff += [attribute_patterns.length].pack('N')

  attribute_patterns.each{|pattern|
    buff += strToSizedUTF16(pattern)
  }

  buff += packuint64(0)
  buff += packuint64(update_interval)

  #Send the message with its length prepended to the front
  @socket.send("#{[buff.length].pack('N')}#{buff}", 0)
end

#sendURISearch(name_pattern) ⇒ Object



235
236
237
238
239
240
# File 'lib/libowl/client_world_model.rb', line 235

def sendURISearch(name_pattern)
  buff = [URI_SEARCH].pack('C')
  buff += strToUnicode(name_pattern)
  #Send the message with its length prepended to the front
  @socket.send("#{[buff.length].pack('N')}#{buff}", 0)
end