Class: WSDiscovery::Searcher

Inherits:
MulticastConnection show all
Includes:
SemanticLogger::Loggable
Defined in:
lib/ws_discovery/searcher.rb

Constant Summary

Constants included from NetworkConstants

NetworkConstants::MULTICAST_IP, NetworkConstants::MULTICAST_PORT, NetworkConstants::TTL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Searcher

Returns a new instance of Searcher.

Parameters:

  • options (Hash) (defaults to: {})

    The options for the probe.

Options Hash (options):

  • :env_namespaces (Hash<String>)

    Additional envelope namespaces.

  • :type_attributes (Hash<String>)

    Type attributes.

  • :types (String)

    Types.

  • :scope_attributes (Hash<String>)

    Scope attributes.

  • :scopes (String)

    Scopes.

  • :ttl (Fixnum)

    TTL for the probe.



20
21
22
23
24
25
26
# File 'lib/ws_discovery/searcher.rb', line 20

def initialize(options={})
  options[:ttl] ||= TTL

  @search = probe(options)

  super options[:ttl]
end

Instance Attribute Details

#discovery_responsesEventMachine::Channel (readonly)

Returns Provides subscribers with responses from their search request.

Returns:

  • (EventMachine::Channel)

    Provides subscribers with responses from their search request.



11
12
13
# File 'lib/ws_discovery/searcher.rb', line 11

def discovery_responses
  @discovery_responses
end

Instance Method Details

#parse(data) ⇒ WSDiscovery::Response

Converts the headers to a set of key-value pairs.

Parameters:

  • data (String)

    The data to convert.

Returns:



47
48
49
# File 'lib/ws_discovery/searcher.rb', line 47

def parse(data)
  WSDiscovery::Response.new(data)
end

#post_initObject

Sends the probe that was built during init. Logs what was sent if the send was successful.



53
54
55
56
57
# File 'lib/ws_discovery/searcher.rb', line 53

def post_init
  if send_datagram(@search, MULTICAST_IP, MULTICAST_PORT) > 0
    logger.info("Sent datagram search:\n#{@search}")
  end
end

#probe(options = {}) ⇒ String

Probe for target services supporting WS-Discovery.

Parameters:

  • options (Hash) (defaults to: {})

    The options for the probe.

Options Hash (options):

  • :env_namespaces (Hash<String>)

    Additional envelope namespaces.

  • :type_attributes (Hash<String>)

    Type attributes.

  • :types (String)

    Types.

  • :scope_attributes (Hash<String>)

    Scope attributes.

  • :scopes (String)

    Scopes.

Returns:

  • (String)

    Probe SOAP message.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ws_discovery/searcher.rb', line 68

def probe(options={})
  namespaces = {
    'xmlns:a' => 'http://schemas.xmlsoap.org/ws/2004/08/addressing',
    'xmlns:d' => 'http://schemas.xmlsoap.org/ws/2005/04/discovery',
    'xmlns:s' => 'http://www.w3.org/2003/05/soap-envelope'
  }
  namespaces.merge! options[:env_namespaces] if options[:env_namespaces]

  Builder::XmlMarkup.new.s(:Envelope, namespaces) do |xml|
    xml.s(:Header) do |xml|
      xml.a(:Action, 'http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe')
      xml.a(:MessageID, "uuid:#{UUID.generate}")
      xml.a(:To, 'urn:schemas-xmlsoap-org:ws:2005:04:discovery')
    end

    xml.s(:Body) do |xml|
      xml.d(:Probe) do
        xml.d(:Types, options[:type_attributes], options[:types])
        xml.d(:Scopes, options[:scope_attributes], options[:scopes])
      end
    end
  end
end

#receive_data(response) ⇒ Object

This is the callback called by EventMachine when it receives data on the socket that’s been opened for this connection. In this case, the method parses the probe matches into WSDiscovery::Responses and adds them to the appropriate EventMachine::Channel (provided as accessor methods). This effectively means that in each Channel, you get a WSDiscovery::Response for each response that comes in on the socket.

Parameters:

  • response (String)

    The data received on this connection’s socket.



36
37
38
39
40
41
# File 'lib/ws_discovery/searcher.rb', line 36

def receive_data(response)
  ip, port = peer_info
  logger.info "<#{self.class}> Response from #{ip}:#{port}:\n#{response}\n"
  parsed_response = parse(response)
  @discovery_responses << parsed_response
end