lib/pio/arp/request.rb in pio-0.3.0 vs lib/pio/arp/request.rb in pio-0.4.0

- old
+ new

@@ -1,45 +1,46 @@ -# -*- coding: utf-8 -*- -require 'pio/arp/frame' +# encoding: utf-8 + require 'pio/arp/message' require 'pio/mac' +require 'pio/options' module Pio class Arp # ARP Request packet generator class Request < Message OPERATION = 1 + public_class_method :new - BROADCAST_MAC_ADDRESS = Mac.new(0xffffffffffff).to_a - ALL_ZERO_MAC_ADDRESS = Mac.new(0).to_a + # User options for creating an Arp Request. + class Options < Pio::Options + mandatory_option :source_mac + mandatory_option :sender_protocol_address + mandatory_option :target_protocol_address - private + BROADCAST_MAC = Mac.new(0xffffffffffff).freeze + ALL_ZERO_MAC = Mac.new(0).freeze - def default_options - { - :operation => OPERATION, - :destination_mac => BROADCAST_MAC_ADDRESS, - :target_hardware_address => ALL_ZERO_MAC_ADDRESS - } - end + def initialize(options) + validate options + @source_mac = Mac.new(options[:source_mac]).freeze + @sender_protocol_address = + IPv4Address.new(options[:sender_protocol_address]).freeze + @target_protocol_address = + IPv4Address.new(options[:target_protocol_address]).freeze + end - def user_options - @options.merge :sender_hardware_address => @options[:source_mac] + def to_hash + { + operation: OPERATION, + source_mac: @source_mac, + destination_mac: BROADCAST_MAC, + sender_hardware_address: @source_mac, + target_hardware_address: ALL_ZERO_MAC, + sender_protocol_address: @sender_protocol_address, + target_protocol_address: @target_protocol_address + }.freeze + end end - - def mandatory_options - [ - :source_mac, - :sender_hardware_address, - :sender_protocol_address, - :target_protocol_address, - ] - end end end end - -### Local variables: -### mode: Ruby -### coding: utf-8-unix -### indent-tabs-mode: nil -### End: