Class: UPnP::SSDP::MulticastConnection
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- UPnP::SSDP::MulticastConnection
- Includes:
- LogSwitch::Mixin, NetworkConstants
- Defined in:
- lib/upnp/ssdp/multicast_connection.rb
Constant Summary
Constants included from NetworkConstants
NetworkConstants::BROADCAST_IP, NetworkConstants::MULTICAST_IP, NetworkConstants::MULTICAST_PORT, NetworkConstants::TTL
Instance Method Summary collapse
-
#initialize(ttl = TTL) ⇒ MulticastConnection
constructor
A new instance of MulticastConnection.
-
#parse(data) ⇒ Hash
Converts the headers to a set of key-value pairs.
-
#peer_info ⇒ Array<String,Fixnum>
Gets the IP and port from the peer that just sent data.
- #set_membership(membership) ⇒ Object
- #set_multicast_ttl(ttl) ⇒ Object
- #set_ttl(ttl) ⇒ Object
-
#setup_multicast_socket ⇒ Object
Sets Socket options to allow for multicasting.
- #switch_multicast_loop(on_off) ⇒ Object
Constructor Details
#initialize(ttl = TTL) ⇒ MulticastConnection
Returns a new instance of MulticastConnection.
18 19 20 21 22 23 24 25 26 |
# File 'lib/upnp/ssdp/multicast_connection.rb', line 18 def initialize ttl=TTL @ttl = ttl @discovery_responses = EM::Channel.new @alive_notifications = EM::Channel.new @byebye_notifications = EM::Channel.new setup_multicast_socket end |
Instance Method Details
#parse(data) ⇒ Hash
Converts the headers to a set of key-value pairs.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/upnp/ssdp/multicast_connection.rb', line 44 def parse(data) new_data = {} unless data =~ /\n/ log "Received response as a single-line String. Discarding." log "Bad response looked like:\n#{data}" return new_data end data.each_line do |line| line =~ /(\S+):(.*)/ unless $1.nil? key = $1 value = $2 key = key.gsub('-', '_').downcase.to_sym new_data[key] = value.strip end end new_data end |
#peer_info ⇒ Array<String,Fixnum>
Gets the IP and port from the peer that just sent data.
31 32 33 34 35 36 37 |
# File 'lib/upnp/ssdp/multicast_connection.rb', line 31 def peer_info peer_bytes = get_peername[2, 6].unpack("nC4") port = peer_bytes.first.to_i ip = peer_bytes[1, 4].join(".") [ip, port] end |
#set_membership(membership) ⇒ Object
81 82 83 |
# File 'lib/upnp/ssdp/multicast_connection.rb', line 81 def set_membership(membership) set_sock_opt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, membership) end |
#set_multicast_ttl(ttl) ⇒ Object
86 87 88 |
# File 'lib/upnp/ssdp/multicast_connection.rb', line 86 def set_multicast_ttl(ttl) set_sock_opt(Socket::IPPROTO_IP, Socket::IP_MULTICAST_TTL, [ttl].pack('i')) end |
#set_ttl(ttl) ⇒ Object
91 92 93 |
# File 'lib/upnp/ssdp/multicast_connection.rb', line 91 def set_ttl(ttl) set_sock_opt(Socket::IPPROTO_IP, Socket::IP_TTL, [ttl].pack('i')) end |
#setup_multicast_socket ⇒ Object
Sets Socket options to allow for multicasting. If ENV is equal to “testing”, then it doesn’t turn off multicast looping.
69 70 71 72 73 74 75 76 77 |
# File 'lib/upnp/ssdp/multicast_connection.rb', line 69 def setup_multicast_socket set_membership(IPAddr.new(MULTICAST_IP).hton + IPAddr.new('0.0.0.0').hton) set_multicast_ttl(@ttl) set_ttl(@ttl) unless ENV["RUBY_UPNP_ENV"] == "testing" switch_multicast_loop :off end end |
#switch_multicast_loop(on_off) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/upnp/ssdp/multicast_connection.rb', line 96 def switch_multicast_loop(on_off) hex_value = case on_off when :on then "\001" when "\001" then "\001" when :off then "\000" when "\000" then "\000" else raise SSDP::Error, "Can't switch IP_MULTICAST_LOOP to '#{on_off}'" end set_sock_opt(Socket::IPPROTO_IP, Socket::IP_MULTICAST_LOOP, hex_value) end |