Class: WSDiscovery::Searcher

Inherits:
MulticastConnection show all
Extended by:
LogSwitch
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.



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

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.



13
14
15
# File 'lib/ws_discovery/searcher.rb', line 13

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:



49
50
51
# File 'lib/ws_discovery/searcher.rb', line 49

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.



55
56
57
58
59
# File 'lib/ws_discovery/searcher.rb', line 55

def post_init
    if send_datagram(@search, MULTICAST_IP, MULTICAST_PORT) > 0
        WSDiscovery::Searcher.log("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.



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

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.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.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.



38
39
40
41
42
43
# File 'lib/ws_discovery/searcher.rb', line 38

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