2 ' Copyright (c) 2010 Apple Inc. All rights reserved.
4 ' Licensed under the Apache License, Version 2.0 (the "License");
5 ' you may not use this file except in compliance with the License.
6 ' You may obtain a copy of the License at
8 ' http://www.apache.org/licenses/LICENSE-2.0
10 ' Unless required by applicable law or agreed to in writing, software
11 ' distributed under the License is distributed on an "AS IS" BASIS,
12 ' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ' See the License for the specific language governing permissions and
14 ' limitations under the License.
17 Public Class DNSServiceBrowser
18 Public WithEvents MyEventManager As New Bonjour.DNSSDEventManager
20 Private m_service As New Bonjour.DNSSDService
21 Private m_browser As Bonjour.DNSSDService
22 Private m_resolver As Bonjour.DNSSDService
27 'This call is required by the Windows Form Designer.
30 ComboBox1.SelectedIndex = 0
33 'Called when a service is found as a result of a browse operation
34 Public Sub MyEventManager_ServiceFound(ByVal browser As Bonjour.DNSSDService, ByVal flags As Bonjour.DNSSDFlags, ByVal ifIndex As UInteger, ByVal serviceName As String, ByVal regtype As String, ByVal domain As String) Handles MyEventManager.ServiceFound
36 index = ServiceNames.Items.IndexOf(serviceName)
38 ' A simple reference counting scheme is implemented so that the same service
39 ' does not show up more than once in the browse list. This can happen
40 ' if the machine has more than one network interface.
42 ' If we have not seen this service before, then it is added to the browse list
43 ' Otherwise it's reference count is bumped up.
46 Dim browseData As New BrowseData
47 browseData.ServiceName = serviceName
48 browseData.RegType = regtype
49 browseData.Domain = domain
51 ServiceNames.Items.Add(browseData)
53 Dim browseData As BrowseData
54 browseData = ServiceNames.Items([index])
59 Public Sub MyEventManager_ServiceLost(ByVal browser As Bonjour.DNSSDService, ByVal flags As Bonjour.DNSSDFlags, ByVal ifIndex As UInteger, ByVal serviceName As String, ByVal regtype As String, ByVal domain As String) Handles MyEventManager.ServiceLost
62 ' See the above about reference counting
64 index = ServiceNames.Items.IndexOf(serviceName)
66 Dim browseData As BrowseData
67 browseData = ServiceNames.Items([index])
69 If browseData.Refs = 0 Then
70 ServiceNames.Items.Remove(serviceName)
75 Public Sub MyEventManager_ServiceResolved(ByVal resolver As Bonjour.DNSSDService, ByVal flags As Bonjour.DNSSDFlags, ByVal ifIndex As UInteger, ByVal fullname As String, ByVal hostname As String, ByVal port As UShort, ByVal record As Bonjour.TXTRecord) Handles MyEventManager.ServiceResolved
77 ' Once the service is resolved, the resolve operation is stopped. This reduces the burdne on the network
81 Dim browseData As BrowseData = ServiceNames.Items.Item(ServiceNames.SelectedIndex)
82 NameField.Text = browseData.ServiceName
83 TypeField.Text = browseData.RegType
84 DomainField.Text = browseData.Domain
85 HostField.Text = hostname
89 ' The values found in the text record are assumed to be human readable strings.
91 If record IsNot Nothing Then
92 For i As UInteger = 0 To record.GetCount() - 1
93 Dim key As String = record.GetKeyAtIndex(i)
94 If key.Length() > 0 Then
95 TextRecord.Items.Add(key + "=" + System.Text.Encoding.ASCII.GetString(record.GetValueAtIndex(i)))
101 Private Sub ClearServiceInfo()
102 TextRecord.Items.Clear()
105 DomainField.Text = ""
110 Private Sub ServiceNames_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ServiceNames.SelectedIndexChanged
111 If m_resolver IsNot Nothing Then
114 Me.ClearServiceInfo()
115 Dim browseData As BrowseData = ServiceNames.Items.Item(ServiceNames.SelectedIndex)
118 ' Selecting a service instance starts a new resolve operation
120 m_resolver = m_service.Resolve(0, 0, browseData.ServiceName, browseData.RegType, browseData.Domain, MyEventManager)
123 Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
124 If m_browser IsNot Nothing Then
128 ServiceNames.Items.Clear()
129 Me.ClearServiceInfo()
132 ' Selecting a service type start a new browse operation
135 m_browser = m_service.Browse(0, 0, ComboBox1.Items.Item(ComboBox1.SelectedIndex), "", MyEventManager)
138 Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextRecord.SelectedIndexChanged
142 Public Class BrowseData
143 Private m_serviceName As String
144 Private m_regType As String
145 Private m_domain As String
146 Private m_refs As Integer
148 Property ServiceName() As String
152 Set(ByVal Value As String)
153 m_serviceName = Value
157 Property RegType() As String
161 Set(ByVal Value As String)
166 Property Domain() As String
170 Set(ByVal Value As String)
175 Property Refs As Integer
179 Set(ByVal Value As Integer)
184 Public Overrides Function Equals(obj As Object) As Boolean
185 If obj Is Nothing Then
188 Return m_serviceName.Equals(obj.ToString)
192 Public Overrides Function ToString() As String