1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 1997-2004 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 using System.Collections;
21 using System.ComponentModel;
22 using System.Windows.Forms;
27 namespace DNSServiceBrowser_NET
30 /// Summary description for Form1.
32 public class Form1 : System.Windows.Forms.Form
34 private System.Windows.Forms.ComboBox typeBox;
35 private System.Windows.Forms.ListBox browseList;
36 private Bonjour.DNSSDEventManager eventManager = null;
37 private Bonjour.DNSSDService service = null;
38 private Bonjour.DNSSDService browser = null;
39 private Bonjour.DNSSDService resolver = null;
41 /// Required designer variable.
43 private System.ComponentModel.Container components = null;
45 private System.Windows.Forms.Label label1;
46 private System.Windows.Forms.Label label2;
47 private System.Windows.Forms.Label label3;
48 private System.Windows.Forms.Label label4;
49 private System.Windows.Forms.TextBox nameField;
50 private System.Windows.Forms.TextBox typeField;
51 private System.Windows.Forms.TextBox domainField;
52 private System.Windows.Forms.TextBox hostField;
53 private System.Windows.Forms.TextBox portField;
54 private System.Windows.Forms.Label label5;
55 private System.Windows.Forms.ListBox serviceTextField;
60 // Required for Windows Form Designer support
62 InitializeComponent();
64 this.Load += new System.EventHandler(this.Form1_Load);
67 // Create the DNSSDEventManager. You can then associate event handlers
68 // with the instance that will be invoked when the event occurs
70 // In this example, we're associating ServiceFound, ServiceLost,
71 // ServiceResolved, and OperationFailed event handlers with the
72 // event manager instance.
74 eventManager = new DNSSDEventManager();
75 eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(this.ServiceFound);
76 eventManager.ServiceLost += new _IDNSSDEvents_ServiceLostEventHandler(this.ServiceLost);
77 eventManager.ServiceResolved += new _IDNSSDEvents_ServiceResolvedEventHandler(this.ServiceResolved);
78 eventManager.OperationFailed += new _IDNSSDEvents_OperationFailedEventHandler(this.OperationFailed);
80 service = new DNSSDService();
83 private void Form1_Load(object sender, EventArgs e)
85 typeBox.SelectedItem = "_http._tcp";
90 /// Clean up any resources being used.
92 protected override void Dispose( bool disposing )
96 if (components != null)
104 if (resolver != null)
119 eventManager.ServiceFound -= new _IDNSSDEvents_ServiceFoundEventHandler(this.ServiceFound);
120 eventManager.ServiceLost -= new _IDNSSDEvents_ServiceLostEventHandler(this.ServiceLost);
121 eventManager.ServiceResolved -= new _IDNSSDEvents_ServiceResolvedEventHandler(this.ServiceResolved);
122 eventManager.OperationFailed -= new _IDNSSDEvents_OperationFailedEventHandler(this.OperationFailed);
124 base.Dispose( disposing );
127 #region Windows Form Designer generated code
129 /// Required method for Designer support - do not modify
130 /// the contents of this method with the code editor.
132 private void InitializeComponent()
134 this.browseList = new System.Windows.Forms.ListBox();
135 this.typeBox = new System.Windows.Forms.ComboBox();
136 this.label1 = new System.Windows.Forms.Label();
137 this.label2 = new System.Windows.Forms.Label();
138 this.label3 = new System.Windows.Forms.Label();
139 this.label4 = new System.Windows.Forms.Label();
140 this.nameField = new System.Windows.Forms.TextBox();
141 this.typeField = new System.Windows.Forms.TextBox();
142 this.domainField = new System.Windows.Forms.TextBox();
143 this.hostField = new System.Windows.Forms.TextBox();
144 this.portField = new System.Windows.Forms.TextBox();
145 this.label5 = new System.Windows.Forms.Label();
146 this.serviceTextField = new System.Windows.Forms.ListBox();
147 this.SuspendLayout();
151 this.browseList.Location = new System.Drawing.Point(8, 48);
152 this.browseList.Name = "browseList";
153 this.browseList.Size = new System.Drawing.Size(488, 147);
154 this.browseList.TabIndex = 0;
155 this.browseList.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
159 this.typeBox.Items.AddRange(new object[]
169 "_animobserver._tcp",
183 "_contactserver._tcp",
194 "_ecbyesfsgksc._tcp",
204 "_fmpro-internal._tcp",
214 "_ica-networking._tcp",
222 "_ipbroadcaster._tcp",
236 "_macfoh-remote._tcp",
243 "_ncsyncserver._tcp",
244 "_net-assistant._tcp",
247 "_nssocketport._tcp",
249 "_omni-bookmark._tcp",
252 "_pdl-datastream._tcp",
262 "_realplayfavs._tcp",
267 "_sallingclicker._tcp",
283 "_ssscreenshare._tcp",
294 "_ticonnectmgr._tcp",
299 "_vue4rendercow._tcp",
312 this.typeBox.Location = new System.Drawing.Point(8, 16);
313 this.typeBox.Name = "typeBox";
314 this.typeBox.Size = new System.Drawing.Size(192, 21);
315 this.typeBox.Sorted = true;
316 this.typeBox.TabIndex = 3;
317 this.typeBox.SelectedIndexChanged += new System.EventHandler(this.typeBox_SelectedIndexChanged);
321 this.label1.Location = new System.Drawing.Point(8, 208);
322 this.label1.Name = "label1";
323 this.label1.Size = new System.Drawing.Size(48, 16);
324 this.label1.TabIndex = 4;
325 this.label1.Text = "Name:";
326 this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
330 this.label2.Location = new System.Drawing.Point(8, 240);
331 this.label2.Name = "label2";
332 this.label2.Size = new System.Drawing.Size(48, 16);
333 this.label2.TabIndex = 5;
334 this.label2.Text = "Type:";
335 this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
339 this.label3.Location = new System.Drawing.Point(8, 272);
340 this.label3.Name = "label3";
341 this.label3.Size = new System.Drawing.Size(48, 16);
342 this.label3.TabIndex = 6;
343 this.label3.Text = "Domain:";
344 this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
348 this.label4.Location = new System.Drawing.Point(8, 304);
349 this.label4.Name = "label4";
350 this.label4.Size = new System.Drawing.Size(48, 16);
351 this.label4.TabIndex = 7;
352 this.label4.Text = "Host:";
353 this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
357 this.nameField.Location = new System.Drawing.Point(56, 208);
358 this.nameField.Name = "nameField";
359 this.nameField.ReadOnly = true;
360 this.nameField.Size = new System.Drawing.Size(168, 20);
361 this.nameField.TabIndex = 8;
362 this.nameField.Text = "";
366 this.typeField.Location = new System.Drawing.Point(56, 240);
367 this.typeField.Name = "typeField";
368 this.typeField.ReadOnly = true;
369 this.typeField.Size = new System.Drawing.Size(168, 20);
370 this.typeField.TabIndex = 9;
371 this.typeField.Text = "";
375 this.domainField.Location = new System.Drawing.Point(56, 272);
376 this.domainField.Name = "domainField";
377 this.domainField.ReadOnly = true;
378 this.domainField.Size = new System.Drawing.Size(168, 20);
379 this.domainField.TabIndex = 10;
380 this.domainField.Text = "";
384 this.hostField.Location = new System.Drawing.Point(56, 304);
385 this.hostField.Name = "hostField";
386 this.hostField.ReadOnly = true;
387 this.hostField.Size = new System.Drawing.Size(168, 20);
388 this.hostField.TabIndex = 11;
389 this.hostField.Text = "";
394 this.portField.Location = new System.Drawing.Point(56, 336);
395 this.portField.Name = "portField";
396 this.portField.ReadOnly = true;
397 this.portField.Size = new System.Drawing.Size(168, 20);
398 this.portField.TabIndex = 12;
399 this.portField.Text = "";
403 this.label5.Location = new System.Drawing.Point(8, 336);
404 this.label5.Name = "label5";
405 this.label5.Size = new System.Drawing.Size(48, 16);
406 this.label5.TabIndex = 14;
407 this.label5.Text = "Port:";
408 this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
412 this.serviceTextField.HorizontalScrollbar = true;
413 this.serviceTextField.Location = new System.Drawing.Point(264, 208);
414 this.serviceTextField.Name = "serviceTextField";
415 this.serviceTextField.Size = new System.Drawing.Size(232, 147);
416 this.serviceTextField.TabIndex = 16;
420 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
421 this.ClientSize = new System.Drawing.Size(512, 365);
422 this.Controls.AddRange(new System.Windows.Forms.Control[] {
423 this.serviceTextField,
437 this.Text = "DNSServices Browser";
438 this.ResumeLayout(false);
444 /// The main entry point for the application.
449 Application.Run(new Form1());
454 // This class is used to store data associated
455 // with a DNSService.Browse() operation
457 public class BrowseData
459 public uint InterfaceIndex;
462 public String Domain;
465 public override String
478 result = (this.Name == other.ToString());
487 return Name.GetHashCode();
495 // This class is used to store data associated
496 // with a DNSService.Resolve() operation
498 public class ResolveData
500 public uint InterfaceIndex;
501 public String FullName;
502 public String HostName;
504 public TXTRecord TxtRecord;
506 public override String
516 // Populate this form with data associated with a
517 // DNSService.Resolve() call
520 Populate(BrowseData browseData, ResolveData resolveData)
522 nameField.Text = browseData.Name;
523 typeField.Text = browseData.Type;
524 domainField.Text = browseData.Domain;
525 hostField.Text = resolveData.HostName;
526 portField.Text = resolveData.Port.ToString();
528 serviceTextField.Items.Clear();
531 // When we print the text record, we're going to assume that every value
534 if (resolveData.TxtRecord != null)
536 for (uint idx = 0; idx < resolveData.TxtRecord.GetCount(); idx++)
541 key = resolveData.TxtRecord.GetKeyAtIndex(idx);
542 bytes = (Byte[])resolveData.TxtRecord.GetValueAtIndex(idx);
550 val = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
553 serviceTextField.Items.Add(key + "=" + val);
560 // called when the type field changes
562 private void typeBox_SelectedIndexChanged(object sender, System.EventArgs e)
564 browseList.Items.Clear();
567 // Stop the current browse operation
576 domainField.Text = "";
579 serviceTextField.Items.Clear();
584 // Selecting a service type will start a new browse operation.
586 browser = service.Browse( 0, 0, typeBox.SelectedItem.ToString(), null, eventManager );
590 MessageBox.Show("Browse Failed", "Error");
595 private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
597 if (resolver != null)
603 if (browseList.SelectedItem != null)
607 BrowseData data = (BrowseData) browseList.SelectedItem;
610 // Clicking on a service instance results in starting a resolve operation
611 // that will call us back with information about the service
613 resolver = service.Resolve(0, data.InterfaceIndex, data.Name, data.Type, data.Domain, eventManager);
617 MessageBox.Show("Resolve Failed", "Error");
626 // This call is invoked by the DNSService core. We create
627 // a BrowseData object and invoked the appropriate method
628 // in the GUI thread so we can update the UI
630 public void ServiceFound
640 int index = browseList.Items.IndexOf(serviceName);
643 // Check to see if we've seen this service before. If the machine has multiple
644 // interfaces, we could potentially get called back multiple times for the
645 // same service. Implementing a simple reference counting scheme will address
646 // the problem of the same service showing up more than once in the browse list.
650 BrowseData data = new BrowseData();
652 data.InterfaceIndex = ifIndex;
653 data.Name = serviceName;
655 data.Domain = domain;
658 browseList.Items.Add(data);
659 browseList.Invalidate();
663 BrowseData data = (BrowseData) browseList.Items[index];
668 public void ServiceLost
678 int index = browseList.Items.IndexOf(serviceName);
681 // See above comment in ServiceFound about reference counting
685 BrowseData data = (BrowseData) browseList.Items[index];
691 browseList.Items.Remove(data);
692 browseList.Invalidate();
697 public void ServiceResolved
708 ResolveData data = new ResolveData();
710 data.InterfaceIndex = ifIndex;
711 data.FullName = fullName;
712 data.HostName = hostName;
714 data.TxtRecord = txtRecord;
717 // Don't forget to stop the resolver. This eases the burden on the network
722 Populate((BrowseData) browseList.SelectedItem, data);
725 public void OperationFailed
731 MessageBox.Show("Operation failed: error code: " + error, "Error");