Sha256: 9ae1ffd65596e94f9c89f52ba0b385f85789eb3f42204866a68e9832110a282c

Contents?: true

Size: 1.61 KB

Versions: 8

Compression:

Stored size: 1.61 KB

Contents

module Pio
  module OpenFlow
    # Port numbering.
    class Port < BinData::Primitive
      endian :big

      def self.port_size_in_bytes(nbytes)
        class_eval "uint#{nbytes} :port"
      end

      def self.max_port_number(port_number)
        const_set :MAX, port_number
      end

      def self.reserved_ports(port_name_and_number)
        @reserved = port_name_and_number
      end

      def self.reserved_port_number(port_name)
        @reserved.fetch port_name
      end

      def self.reserved_port_name?(port_name)
        @reserved.key? port_name
      end

      def max
        self.class.const_get :MAX
      end

      def reserved_port_number(port_name)
        self.class.reserved_port_number port_name
      end

      def reserved_port_name?(port_name)
        self.class.reserved_port_name? port_name
      end

      def reserved_port_number?(port_number)
        self.class.instance_variable_get(:@reserved).invert.key?(port_number)
      end

      def reserved_port_name(port_number)
        self.class.instance_variable_get(:@reserved).invert.fetch(port_number)
      end

      def get
        if reserved_port_number?(port)
          reserved_port_name(port)
        else
          port
        end
      end

      def set(port)
        if reserved_port_name?(port)
          self.port = reserved_port_number(port)
        else
          port_num = port.to_i
          fail ArgumentError, 'The port should be > 0' if port_num < 1
          if port_num >= max
            fail ArgumentError, "The port should be < #{max.to_hex}"
          end
          self.port = port_num
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
pio-0.30.0 lib/pio/open_flow/port.rb
pio-0.29.0 lib/pio/open_flow/port.rb
pio-0.28.1 lib/pio/open_flow/port.rb
pio-0.28.0 lib/pio/open_flow/port.rb
pio-0.27.2 lib/pio/open_flow/port.rb
pio-0.27.1 lib/pio/open_flow/port.rb
pio-0.27.0 lib/pio/open_flow/port.rb
pio-0.26.0 lib/pio/open_flow/port.rb