]> git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/DNSServiceBrowser.VB/DNSServiceBrowser.vb
mDNSResponder-1096.100.3.tar.gz
[apple/mdnsresponder.git] / Clients / DNSServiceBrowser.VB / DNSServiceBrowser.vb
1 '
2 ' Copyright (c) 2010 Apple Inc. All rights reserved.
3 '
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
7 '
8 ' http://www.apache.org/licenses/LICENSE-2.0
9 '
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.
15 '
16
17 Public Class DNSServiceBrowser
18 Public WithEvents MyEventManager As New Bonjour.DNSSDEventManager
19
20 Private m_service As New Bonjour.DNSSDService
21 Private m_browser As Bonjour.DNSSDService
22 Private m_resolver As Bonjour.DNSSDService
23
24 Public Sub New()
25 MyBase.New()
26
27 'This call is required by the Windows Form Designer.
28 InitializeComponent()
29
30 ComboBox1.SelectedIndex = 0
31 End Sub
32
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
35 Dim index As Integer
36 index = ServiceNames.Items.IndexOf(serviceName)
37 '
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.
41 '
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.
44 '
45 If index = -1 Then
46 Dim browseData As New BrowseData
47 browseData.ServiceName = serviceName
48 browseData.RegType = regtype
49 browseData.Domain = domain
50 browseData.Refs = 1
51 ServiceNames.Items.Add(browseData)
52 Else
53 Dim browseData As BrowseData
54 browseData = ServiceNames.Items([index])
55 browseData.Refs += 1
56 End If
57 End Sub
58
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
60 Dim index As Integer
61 '
62 ' See the above about reference counting
63 '
64 index = ServiceNames.Items.IndexOf(serviceName)
65 If index <> -1 Then
66 Dim browseData As BrowseData
67 browseData = ServiceNames.Items([index])
68 browseData.Refs -= 1
69 If browseData.Refs = 0 Then
70 ServiceNames.Items.Remove(serviceName)
71 End If
72 End If
73 End Sub
74
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
76 '
77 ' Once the service is resolved, the resolve operation is stopped. This reduces the burdne on the network
78 '
79 m_resolver.Stop()
80 m_resolver = Nothing
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
86 PortField.Text = port
87
88 '
89 ' The values found in the text record are assumed to be human readable strings.
90 '
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)))
96 End If
97 Next i
98 End If
99 End Sub
100
101 Private Sub ClearServiceInfo()
102 TextRecord.Items.Clear()
103 NameField.Text = ""
104 TypeField.Text = ""
105 DomainField.Text = ""
106 HostField.Text = ""
107 PortField.Text = ""
108 End Sub
109
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
112 m_resolver.Stop()
113 End If
114 Me.ClearServiceInfo()
115 Dim browseData As BrowseData = ServiceNames.Items.Item(ServiceNames.SelectedIndex)
116
117 '
118 ' Selecting a service instance starts a new resolve operation
119 '
120 m_resolver = m_service.Resolve(0, 0, browseData.ServiceName, browseData.RegType, browseData.Domain, MyEventManager)
121 End Sub
122
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
125 m_browser.Stop()
126 End If
127
128 ServiceNames.Items.Clear()
129 Me.ClearServiceInfo()
130
131 '
132 ' Selecting a service type start a new browse operation
133 '
134
135 m_browser = m_service.Browse(0, 0, ComboBox1.Items.Item(ComboBox1.SelectedIndex), "", MyEventManager)
136 End Sub
137
138 Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextRecord.SelectedIndexChanged
139
140 End Sub
141 End Class
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
147
148 Property ServiceName() As String
149 Get
150 Return m_serviceName
151 End Get
152 Set(ByVal Value As String)
153 m_serviceName = Value
154 End Set
155 End Property
156
157 Property RegType() As String
158 Get
159 Return m_regType
160 End Get
161 Set(ByVal Value As String)
162 m_regType = Value
163 End Set
164 End Property
165
166 Property Domain() As String
167 Get
168 Return m_domain
169 End Get
170 Set(ByVal Value As String)
171 m_domain = Value
172 End Set
173 End Property
174
175 Property Refs As Integer
176 Get
177 Return m_refs
178 End Get
179 Set(ByVal Value As Integer)
180 m_refs = Value
181 End Set
182 End Property
183
184 Public Overrides Function Equals(obj As Object) As Boolean
185 If obj Is Nothing Then
186 Return False
187 Else
188 Return m_serviceName.Equals(obj.ToString)
189 End If
190 End Function
191
192 Public Overrides Function ToString() As String
193 Return m_serviceName
194 End Function
195
196 End Class