Class: EPIC::HandleValue

Inherits:
Object
  • Object
show all
Defined in:
src/epic_handlevalue.rb

Overview

Wrapper around net.handle.hdllib.HandleValue.

A Handle, represented in this software system by Handle, is a collection of Handle Values, represented by this class.

See Also:

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (HandleValue) initialize(dbrow = nil)

A new instance of HandleValue

Parameters:

  • dbrow (Hash) (defaults to: nil)

    an optional hash of values to initialize this handle value with.

See Also:



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
191
192
193
194
195
# File 'src/epic_handlevalue.rb', line 164

def initialize dbrow = nil
  # super path
  # matches = %r{/([^/]+/[^/]+)/(\d+)\z}.match path
  # raise "Unexpected path: #{path}" unless matches
  # @handle = matches[1].to_path.unescape
  # @idx = matches[2].to_i
  @handle_value = HS::HDLLIB::HandleValue.new
  if dbrow
    self.idx       = dbrow[:idx].to_i
    self.type      = dbrow[:type].to_s
    self.data      = dbrow[:data].to_s
    self.ttl_type  = dbrow[:ttl_type].to_i
    self.ttl       = dbrow[:ttl].to_i
    self.timestamp = dbrow[:timestamp].to_i
    @handle_value.setReferences(
      dbrow[ :refs ].split("\t").collect do
        |ref|
        ref = ref.split ':', 2
        HS::HDLLIB::ValueReference.new(
          ref[1].to_java_bytes,
          ref[0].to_i
        )
      end.to_java HS::HDLLIB::ValueReference
    )
    self.admin_read  = dbrow[:admin_read]  && 0 != dbrow[:admin_read]
    self.admin_write = dbrow[:admin_write] && 0 != dbrow[:admin_write]
    self.pub_read    = dbrow[:pub_read]    && 0 != dbrow[:pub_read]
    self.pub_write   = dbrow[:pub_write]   && 0 != dbrow[:pub_write]
  else
    self.timestamp = Time.new.to_i
  end
end

Instance Attribute Details

- (Boolean) admin_read

Is the administrator allowed to read this handle value?

Returns:

  • (Boolean)


117
# File 'src/epic_handlevalue.rb', line 117

map_java :admin_read,  :AdminCanRead

- (Boolean) admin_write

Is the administrator allowed to overwrite this handle value?

Returns:

  • (Boolean)


121
# File 'src/epic_handlevalue.rb', line 121

map_java :admin_write, :AdminCanWrite

- (String with Encoding::ASCII_8BIT) data

The binary data in this handle value

Returns:

  • (String with Encoding::ASCII_8BIT)

    the binary data in this handle value



94
# File 'src/epic_handlevalue.rb', line 94

map_java_bytes :data, :Data, Encoding::ASCII_8BIT

- (HDLLIB::HandleValue) handle_value (readonly)

The Java Object wrapped by self.

Returns:

  • (HDLLIB::HandleValue)


134
135
136
# File 'src/epic_handlevalue.rb', line 134

def handle_value
  @handle_value
end

- (Integer) idx

The index of this handle value

Returns:

  • (Integer)

    the index of this handle value



97
# File 'src/epic_handlevalue.rb', line 97

map_java :idx, :Index

- (Array, ...) parsed_data

TODO:

check if the transcodings can also be done with String#force_encoding.

Accesses the data in this handle value, just like #data. While #data accesses the raw octet-stream verbatim, this attribute parses the raw bytes into structured data for certain known value types.

For unknown value types, it returns a UTF-8 encoded string if the data can be interpreted as such. If the binary data doesn’t constitute a valid UTF-8 string, this attribute is nil.

Returns:



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'src/epic_handlevalue.rb', line 228

def parsed_data
  nicetype = type.gsub /\W+/, '_'
  if HS.respond_to? :unpack_#{nicetype}"
    HS.send :unpack_#{nicetype}", self.data
  else
    begin
      retval = self.data.encode( Encoding::UTF_8, Encoding::UTF_8 )
      if %r{[\x00-\x08\x0B\x0C\x0E-\x1F]}.match(retval) ||
         self.data != retval.encode( Encoding::ASCII_8BIT, Encoding::ASCII_8BIT )
        nil
      else
        retval
      end
    rescue
      nil
    end
  end
end

- (Boolean) pub_read

Is the world allowed to read this handle value?

Returns:

  • (Boolean)


125
# File 'src/epic_handlevalue.rb', line 125

map_java :pub_read,    :AnyoneCanRead

- (Boolean) pub_write

Is the world allowed to overwrite this handle value?

Returns:

  • (Boolean)


129
# File 'src/epic_handlevalue.rb', line 129

map_java :pub_write,   :AnyoneCanWrite

- (Array< Hash{ :idx => Integer, :handle => Integer } >) refs

References in this handle value.

Returns:

  • (Array< Hash{ :idx => Integer, :handle => Integer } >)


140
141
142
143
144
145
# File 'src/epic_handlevalue.rb', line 140

def refs
  @handle_value.getReferences.to_a.collect do
    |ref|
    { :idx => ref.index, :handle => String.from_java_bytes( ref.handle ) }
  end
end

- (Integer) timestamp

The timestamp of the last modification of this handle value, in seconds since epoch.

Returns:

  • (Integer)


113
# File 'src/epic_handlevalue.rb', line 113

map_java :timestamp, :Timestamp

- (Integer) ttl

The time-to-live of this handle value

Returns:

  • (Integer)

    the time-to-live of this handle value



100
# File 'src/epic_handlevalue.rb', line 100

map_java :ttl, :TTL

- (0 or 1) ttl_type

Type of attribute #ttl.

0

Relative Interpret #ttl as the number of seconds a client or proxy may/should cache the result.

1

Absolute Interpret #ttl as an absolute timestamp, expressed in seconds since epoch, until which this handle value is current.

Returns:

  • (0 or 1)


108
# File 'src/epic_handlevalue.rb', line 108

map_java :ttl_type, :TTLType

- (String) type

The type of this handle value

Returns:

  • (String)

    the type of this handle value



91
# File 'src/epic_handlevalue.rb', line 91

map_java_bytes :type, :Type

Class Method Details

+ (void) map_java(ruby_name, java_name)

Meta programming helper method.

This private class method can be called inside the class definition of EPIC::HandleValue.

Examples:

# Create a Ruby attribute "timestamp" which is mapped
# to Java methods getTimestamp() and setTimestamp():
map_java_bytes :timestamp, :Timestamp

Parameters:

  • ruby_name (Symbol)

    name of the Ruby attribute to create

  • java_name (Symbol)

    name of the corresponding attribute in net.handle.hdllib.HandleValue

See Also:



42
43
44
45
46
47
48
49
50
51
# File 'src/epic_handlevalue.rb', line 42

def self.map_java ruby_name, java_name
  define_method ruby_name do
    @handle_value.send( :get#{java_name}" )
  end
  define_method :#{ruby_name}=" do
    |value|
    @handle_value.send( :set#{java_name}", value )
    value
  end
end

+ (void) map_java_bytes(ruby_name, java_name, encoding = Encoding::UTF_8)

Meta programming helper method.

Meta programming helper method.

This private class method can be called inside the class definition of EPIC::HandleValue.

The difference between this method and map_java is that map_java works for attributes with primitive Java types, while this method wraps a Ruby String attribute around a Java attribute of type byte[]. In order to do so, we need to define how the Java octet-stream must be interpreted, i.e. which encoding to use.

Examples:

# Create a Ruby attribute "data" which is mapped
# to Java methods getData() and setData():
map_java_bytes :data, :Data, Encoding::BINARY

Parameters:

  • encoding (Encoding) (defaults to: Encoding::UTF_8)

    The encoding to be used when interpreting the Java byte array.

  • ruby_name (Symbol)

    name of the Ruby attribute to create

  • java_name (Symbol)

    name of the corresponding attribute in net.handle.hdllib.HandleValue

See Also:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'src/epic_handlevalue.rb', line 72

def self.map_java_bytes ruby_name, java_name, encoding = Encoding::UTF_8
  define_method ruby_name do
    String.from_java_bytes(
      @handle_value.send( :get#{java_name}" )
    ).force_encoding(encoding)
  end
  define_method :#{ruby_name}=" do
    |value|
    @handle_value.send(
      :set#{java_name}",
      value.force_encoding(Encoding::ASCII_8BIT).to_java_bytes
    )
    value
  end
end