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);
66 eventManager = new DNSSDEventManager();
67 eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(this.ServiceFound);
68 eventManager.ServiceLost += new _IDNSSDEvents_ServiceLostEventHandler(this.ServiceLost);
69 eventManager.ServiceResolved += new _IDNSSDEvents_ServiceResolvedEventHandler(this.ServiceResolved);
70 eventManager.OperationFailed += new _IDNSSDEvents_OperationFailedEventHandler(this.OperationFailed);
72 service = new DNSSDService();
75 private void Form1_Load(object sender, EventArgs e)
77 typeBox.SelectedItem = "_http._tcp";
82 /// Clean up any resources being used.
84 protected override void Dispose( bool disposing )
88 if (components != null)
108 eventManager.ServiceFound -= new _IDNSSDEvents_ServiceFoundEventHandler(this.ServiceFound);
109 eventManager.ServiceLost -= new _IDNSSDEvents_ServiceLostEventHandler(this.ServiceLost);
110 eventManager.ServiceResolved -= new _IDNSSDEvents_ServiceResolvedEventHandler(this.ServiceResolved);
111 eventManager.OperationFailed -= new _IDNSSDEvents_OperationFailedEventHandler(this.OperationFailed);
113 base.Dispose( disposing );
116 #region Windows Form Designer generated code
118 /// Required method for Designer support - do not modify
119 /// the contents of this method with the code editor.
121 private void InitializeComponent()
123 this.browseList = new System.Windows.Forms.ListBox();
124 this.typeBox = new System.Windows.Forms.ComboBox();
125 this.label1 = new System.Windows.Forms.Label();
126 this.label2 = new System.Windows.Forms.Label();
127 this.label3 = new System.Windows.Forms.Label();
128 this.label4 = new System.Windows.Forms.Label();
129 this.nameField = new System.Windows.Forms.TextBox();
130 this.typeField = new System.Windows.Forms.TextBox();
131 this.domainField = new System.Windows.Forms.TextBox();
132 this.hostField = new System.Windows.Forms.TextBox();
133 this.portField = new System.Windows.Forms.TextBox();
134 this.label5 = new System.Windows.Forms.Label();
135 this.serviceTextField = new System.Windows.Forms.ListBox();
136 this.SuspendLayout();
140 this.browseList.Location = new System.Drawing.Point(8, 48);
141 this.browseList.Name = "browseList";
142 this.browseList.Size = new System.Drawing.Size(488, 147);
143 this.browseList.TabIndex = 0;
144 this.browseList.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
148 this.typeBox.Items.AddRange(new object[]
158 "_animobserver._tcp",
172 "_contactserver._tcp",
183 "_ecbyesfsgksc._tcp",
193 "_fmpro-internal._tcp",
203 "_ica-networking._tcp",
211 "_ipbroadcaster._tcp",
225 "_macfoh-remote._tcp",
232 "_ncsyncserver._tcp",
233 "_net-assistant._tcp",
236 "_nssocketport._tcp",
238 "_omni-bookmark._tcp",
241 "_pdl-datastream._tcp",
251 "_realplayfavs._tcp",
256 "_sallingclicker._tcp",
272 "_ssscreenshare._tcp",
283 "_ticonnectmgr._tcp",
288 "_vue4rendercow._tcp",
301 this.typeBox.Location = new System.Drawing.Point(8, 16);
302 this.typeBox.Name = "typeBox";
303 this.typeBox.Size = new System.Drawing.Size(192, 21);
304 this.typeBox.Sorted = true;
305 this.typeBox.TabIndex = 3;
306 this.typeBox.SelectedIndexChanged += new System.EventHandler(this.typeBox_SelectedIndexChanged);
310 this.label1.Location = new System.Drawing.Point(8, 208);
311 this.label1.Name = "label1";
312 this.label1.Size = new System.Drawing.Size(48, 16);
313 this.label1.TabIndex = 4;
314 this.label1.Text = "Name:";
315 this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
319 this.label2.Location = new System.Drawing.Point(8, 240);
320 this.label2.Name = "label2";
321 this.label2.Size = new System.Drawing.Size(48, 16);
322 this.label2.TabIndex = 5;
323 this.label2.Text = "Type:";
324 this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
328 this.label3.Location = new System.Drawing.Point(8, 272);
329 this.label3.Name = "label3";
330 this.label3.Size = new System.Drawing.Size(48, 16);
331 this.label3.TabIndex = 6;
332 this.label3.Text = "Domain:";
333 this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
337 this.label4.Location = new System.Drawing.Point(8, 304);
338 this.label4.Name = "label4";
339 this.label4.Size = new System.Drawing.Size(48, 16);
340 this.label4.TabIndex = 7;
341 this.label4.Text = "Host:";
342 this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
346 this.nameField.Location = new System.Drawing.Point(56, 208);
347 this.nameField.Name = "nameField";
348 this.nameField.ReadOnly = true;
349 this.nameField.Size = new System.Drawing.Size(168, 20);
350 this.nameField.TabIndex = 8;
351 this.nameField.Text = "";
355 this.typeField.Location = new System.Drawing.Point(56, 240);
356 this.typeField.Name = "typeField";
357 this.typeField.ReadOnly = true;
358 this.typeField.Size = new System.Drawing.Size(168, 20);
359 this.typeField.TabIndex = 9;
360 this.typeField.Text = "";
364 this.domainField.Location = new System.Drawing.Point(56, 272);
365 this.domainField.Name = "domainField";
366 this.domainField.ReadOnly = true;
367 this.domainField.Size = new System.Drawing.Size(168, 20);
368 this.domainField.TabIndex = 10;
369 this.domainField.Text = "";
373 this.hostField.Location = new System.Drawing.Point(56, 304);
374 this.hostField.Name = "hostField";
375 this.hostField.ReadOnly = true;
376 this.hostField.Size = new System.Drawing.Size(168, 20);
377 this.hostField.TabIndex = 11;
378 this.hostField.Text = "";
383 this.portField.Location = new System.Drawing.Point(56, 336);
384 this.portField.Name = "portField";
385 this.portField.ReadOnly = true;
386 this.portField.Size = new System.Drawing.Size(168, 20);
387 this.portField.TabIndex = 12;
388 this.portField.Text = "";
392 this.label5.Location = new System.Drawing.Point(8, 336);
393 this.label5.Name = "label5";
394 this.label5.Size = new System.Drawing.Size(48, 16);
395 this.label5.TabIndex = 14;
396 this.label5.Text = "Port:";
397 this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
401 this.serviceTextField.HorizontalScrollbar = true;
402 this.serviceTextField.Location = new System.Drawing.Point(264, 208);
403 this.serviceTextField.Name = "serviceTextField";
404 this.serviceTextField.Size = new System.Drawing.Size(232, 147);
405 this.serviceTextField.TabIndex = 16;
409 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
410 this.ClientSize = new System.Drawing.Size(512, 365);
411 this.Controls.AddRange(new System.Windows.Forms.Control[] {
412 this.serviceTextField,
426 this.Text = "DNSServices Browser";
427 this.ResumeLayout(false);
433 /// The main entry point for the application.
438 Application.Run(new Form1());
443 // This class is used to store data associated
444 // with a DNSService.Browse() operation
446 public class BrowseData
448 public uint InterfaceIndex;
451 public String Domain;
453 public override String
466 if ((object) this == other)
470 else if (other is BrowseData)
472 BrowseData otherBrowseData = (BrowseData) other;
474 result = (this.Name == otherBrowseData.Name);
484 return Name.GetHashCode();
492 // This class is used to store data associated
493 // with a DNSService.Resolve() operation
495 public class ResolveData
497 public uint InterfaceIndex;
498 public String FullName;
499 public String HostName;
501 public TXTRecord TxtRecord;
503 public override String
513 // Populate this form with data associated with a
514 // DNSService.Resolve() call
517 Populate(BrowseData browseData, ResolveData resolveData)
519 nameField.Text = browseData.Name;
520 typeField.Text = browseData.Type;
521 domainField.Text = browseData.Domain;
522 hostField.Text = resolveData.HostName;
523 portField.Text = resolveData.Port.ToString();
525 serviceTextField.Items.Clear();
527 if (resolveData.TxtRecord != null)
529 for (uint idx = 0; idx < resolveData.TxtRecord.GetCount(); idx++)
534 key = resolveData.TxtRecord.GetKeyAtIndex(idx);
535 bytes = (Byte[])resolveData.TxtRecord.GetValueAtIndex(idx);
543 val = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
546 serviceTextField.Items.Add(key + "=" + val);
553 // called when the type field changes
555 private void typeBox_SelectedIndexChanged(object sender, System.EventArgs e)
557 browseList.Items.Clear();
566 domainField.Text = "";
569 serviceTextField.Items.Clear();
573 browser = service.Browse( 0, 0, typeBox.SelectedItem.ToString(), null, eventManager );
577 MessageBox.Show("Browse Failed", "Error");
582 private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
584 if (resolver != null)
590 if (browseList.SelectedItem != null)
594 BrowseData data = (BrowseData) browseList.SelectedItem;
596 resolver = service.Resolve(0, data.InterfaceIndex, data.Name, data.Type, data.Domain, eventManager);
600 MessageBox.Show("Resolve Failed", "Error");
609 // This call is invoked by the DNSService core. We create
610 // a BrowseData object and invoked the appropriate method
611 // in the GUI thread so we can update the UI
613 public void ServiceFound
623 BrowseData data = new BrowseData();
625 data.InterfaceIndex = ifIndex;
626 data.Name = serviceName;
628 data.Domain = domain;
630 browseList.Items.Add(data);
631 browseList.Invalidate();
634 public void ServiceLost
644 BrowseData data = new BrowseData();
646 data.InterfaceIndex = ifIndex;
647 data.Name = serviceName;
649 data.Domain = domain;
651 browseList.Items.Remove(data);
652 browseList.Invalidate();
655 public void ServiceResolved
666 ResolveData data = new ResolveData();
668 data.InterfaceIndex = ifIndex;
669 data.FullName = fullName;
670 data.HostName = hostName;
672 data.TxtRecord = txtRecord;
677 Populate((BrowseData) browseList.SelectedItem, data);
680 public void OperationFailed
686 MessageBox.Show("Operation failed: error code: " + error, "Error");