Class: SolverWorldModel

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

Constant Summary collapse

KEEP_ALIVE =

Message constants

0
TYPE_ANNOUNCE =
1
START_TRANSIENT =
2
STOP_TRANSIENT =
3
SOLVER_DATA =
4
CREATE_URI =
5
EXPIRE_URI =
6
DELETE_URI =
7
EXPIRE_ATTRIBUTE =
8
DELETE_ATTRIBUTE =
9

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, origin, start_transient_callback = nil, stop_transient_callback = nil) ⇒ SolverWorldModel

Returns a new instance of SolverWorldModel.



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
119
120
121
122
123
# File 'lib/libowl/solver_world_model.rb', line 91

def initialize(host, port, origin, start_transient_callback = nil, stop_transient_callback = nil)
  @origin = origin
  @connected = false
  @host = host
  @port = port
  @socket = TCPSocket.open(host, port)
  handshake = ""
  ver_string = "GRAIL world model 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"
  #Receive a handshake and then send 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

  @name_to_alias = {}
  @alias_to_name = {}

  @start_transient_callback = start_transient_callback
  @stop_transient_callback = stop_transient_callback
end

Instance Attribute Details

#alias_to_nameObject

Returns the value of attribute alias_to_name.



24
25
26
# File 'lib/libowl/solver_world_model.rb', line 24

def alias_to_name
  @alias_to_name
end

#connectedObject

Returns the value of attribute connected.



24
25
26
# File 'lib/libowl/solver_world_model.rb', line 24

def connected
  @connected
end

#name_to_aliasObject

Returns the value of attribute name_to_alias.



24
25
26
# File 'lib/libowl/solver_world_model.rb', line 24

def name_to_alias
  @name_to_alias
end

Instance Method Details

#addSolutionTypes(attributes) ⇒ Object

Add some SolutionType objects to the known list



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/libowl/solver_world_model.rb', line 133

def addSolutionTypes(attributes)
  new_aliases = []
  attributes.each { |attr|
    if (@name_to_alias[attr.name] == nil)
      new_alias = @name_to_alias.length
      @name_to_alias[attr.name] = new_alias
      @alias_to_name[new_alias] = attr.name
      new_aliases.push([attr.name, new_alias])
    end
  }
  if (new_aliases.length > 0)
    makeTypeAnnounce(new_aliases)
  end
end

#closeObject

Close this connection



127
128
129
130
# File 'lib/libowl/solver_world_model.rb', line 127

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

#createURI(uri, creation_time) ⇒ Object

Create an object with the given name in the world model.



194
195
196
197
198
199
200
201
# File 'lib/libowl/solver_world_model.rb', line 194

def createURI(uri, creation_time)
  buff = [CREATE_URI].pack('C')
  buff += strToSizedUTF16(uri)
  buff += packuint64(creation_time)
  buff += strToUnicode(@origin)
  #Send the message with its length prepended to the front
  @socket.send("#{[buff.length].pack('N')}#{buff}", 0)
end

#decodeStartTransient(inbuff) ⇒ Object

Decode a start transient message



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/libowl/solver_world_model.rb', line 56

def decodeStartTransient(inbuff)
  num_aliases = inbuff.unpack('N')[0]
  rest = inbuff[4, inbuff.length - 1]
  new_trans_requests = []
  for i in 1..num_aliases do
    type_alias = rest.unpack('N')[0]
    total_expressions = rest.unpack('N')[0]
    t_request = TransientRequest.new(type_alias, [])
    for j in 1..total_expressions do
      exp, rest = splitURIFromRest(rest[4, rest.length - 1])
      t_request.expressions.push(exp)
    end
    new_trans_requests.push(t_request)
  end
  return new_trans_requests
end

#decodeStopTransient(inbuff) ⇒ Object

Decode a stop transient message



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/libowl/solver_world_model.rb', line 74

def decodeStopTransient(inbuff)
  num_aliases = inbuff.unpack('N')[0]
  rest = inbuff[4, inbuff.length - 1]
  new_trans_requests = []
  for i in 1..num_aliases do
    type_alias = rest.unpack('N')[0]
    total_expressions = rest.unpack('N')[0]
    t_request = TransientRequest.new(type_alias, [])
    for j in 1..total_expressions do
      exp, rest = splitURIFromRest(rest[4, rest.length - 1])
      t_request.expressions.push(exp)
    end
    new_trans_requests.push(t_request)
  end
  return new_trans_requests
end

#deleteURI(uri) ⇒ Object

Delete an object in the world model.



217
218
219
220
221
222
223
# File 'lib/libowl/solver_world_model.rb', line 217

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

#expireURI(uri, expiration_time) ⇒ Object

Expire the object with the given name in the world model, indicating that it is no longer valid after the given time.



206
207
208
209
210
211
212
213
# File 'lib/libowl/solver_world_model.rb', line 206

def expireURI(uri, expiration_time)
  buff = [EXPIRE_URI].pack('C')
  buff += strToSizedUTF16(uri)
  buff += packuint64(expiration_time)
  buff += strToUnicode(@origin)
  #Send the message with its length prepended to the front
  @socket.send("#{[buff.length].pack('N')}#{buff}", 0)
end

#handleMessageObject

Handle a message of currently unknown type



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/libowl/solver_world_model.rb', line 37

def handleMessage()
  #Get the message length as n unsigned integer
  inlen = (@socket.recvfrom(4)[0]).unpack('N')[0]
  inbuff = @socket.recvfrom(inlen)[0]
  #Byte that indicates message type
  control = inbuff.unpack('C')[0]
  if control == START_TRANSIENT
    if (@start_transient_callback != nil)
      @start_transient_callback.call(decodeStartTransient(inbuff[1, inbuff.length - 1]))
    end
  elsif control == STOP_TRANSIENT
    if (@stop_transient_callback != nil)
      @stop_transient_callback.call(decodeStopTransient(inbuff[1, inbuff.length - 1]))
    end
  end
  return control
end

#makeTypeAnnounce(type_pairs) ⇒ Object



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

def makeTypeAnnounce(type_pairs)
  buff = [TYPE_ANNOUNCE].pack('C') + [type_pairs.length].pack('N')
  for pair in type_pairs do
    #TODO Not supporting transient types for now so always write a zero
    #for the transient on/off byte
    buff += [pair[1]].pack('N') + strToSizedUTF16(pair[0]) + [0].pack('C')
  end
  #Add the origin string to the end of the message
  buff += strToUnicode(@origin)
  @socket.send("#{[buff.length].pack('N')}#{buff}", 0)
end

#pushData(wmdata_vector, create_uris = false) ⇒ Object

Push URI attributes, automatically declaring new solution types as non-stremaing types if they were not previously declared.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/libowl/solver_world_model.rb', line 163

def pushData(wmdata_vector, create_uris = false)
  buff = [SOLVER_DATA].pack('C')
  if (create_uris)
    buff += [1].pack('C')
  else
    buff += [0].pack('C')
  end

  #Push back the total number of solutions
  total_solns = wmdata_vector.inject(0){|sum, wmdata|
    sum + wmdata.attributes.length
  }
  buff += [total_solns].pack('N')

  #Now create each solution and push it back into the buffer
  wmdata_vector.each{|wmdata|
    #First make sure all of the solutions types have been declared
    addSolutionTypes(wmdata.attributes)
    #Now push back this attribute's data using an alias for the name
    wmdata.attributes.each{|attr|
      buff += [@name_to_alias[attr.name]].pack('N') +
        packuint64(attr.creation) +
        strToSizedUTF16(wmdata.uri) + [attr.data.length].pack('N') + attr.data
    }
  }
  #Send the message with its length prepended to the front
  @socket.send("#{[buff.length].pack('N')}#{buff}", 0)
end