1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright (c) 2003-2012 by AG-Software 											 *
3  * All Rights Reserved.																 *
4  * Contact information for AG-Software is available at http://www.ag-software.de	 *
5  *																					 *
6  * Licence:																			 *
7  * The agsXMPP SDK is released under a dual licence									 *
8  * agsXMPP can be used under either of two licences									 *
9  * 																					 *
10  * A commercial licence which is probably the most appropriate for commercial 		 *
11  * corporate use and closed source projects. 										 *
12  *																					 *
13  * The GNU Public License (GPL) is probably most appropriate for inclusion in		 *
14  * other open source projects.														 *
15  *																					 *
16  * See README.html for details.														 *
17  *																					 *
18  * For general enquiries visit our website at:										 *
19  * http://www.ag-software.de														 *
20  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21 
22 using System;
23 
24 namespace agsXMPP.Net.Dns
25 {
26 	/// <summary>
27 	/// Summary description for SRVRecord.
28 	/// </summary>
29    public class SRVRecord : RecordBase, IComparable
30    {
31        /// <summary>
32        /// Constructs a NS record by reading bytes from a return message
33        /// </summary>
34        /// <param name="pointer">A logical pointer to the bytes holding the record</param>
SRVRecord(Pointer pointer)35        internal SRVRecord(Pointer pointer)
36        {
37            m_Priority = pointer.ReadShort();
38            m_Weight = pointer.ReadShort();
39            m_Port = pointer.ReadShort();
40            m_Target = pointer.ReadDomain();
41        }
42 
43         // the fields exposed outside the assembly
44         private int     m_Priority;
45         private int     m_Weight;
46         private int     m_Port;
47         private string  m_Target;
48 
49         public int Priority
50         {
51           get { return m_Priority; }
52         }
53 
54         public int Weight
55         {
56           get { return m_Weight; }
57         }
58 
59         public int Port
60         {
61           get { return m_Port; }
62         }
63 
64         public string Target
65         {
66             get { return m_Target; }
67         }
68 
ToString()69         public override string ToString()
70         {
71 			return string.Format("\n   priority   = {0}\n   weight     = {1}\n   port       = {2}\n   target     = {3}",
72             m_Priority,
73             m_Weight,
74             m_Port,
75             m_Target);
76         }
77 
78         /// <summary>
79         /// Implements the IComparable interface so that we can sort the SRV records by their
80         /// lowest priority
81         /// </summary>
82         /// <param name="other">the other SRVRecord to compare against</param>
83         /// <returns>1, 0, -1</returns>
CompareTo(object obj)84         public int CompareTo(object obj)
85         {
86             SRVRecord srvOther = (SRVRecord)obj;
87 
88             // we want to be able to sort them by priority from lowest to highest.
89             if (m_Priority < srvOther.m_Priority) return -1;
90             if (m_Priority > srvOther.m_Priority) return 1;
91 
92             // if the priority is the same, sort by highest weight to lowest (higher
93             // weighting means that server should get more of the requests)
94             if (m_Weight > srvOther.m_Weight) return -1;
95             if (m_Weight < srvOther.m_Weight) return 1;
96 
97             return 0;
98         }
99     }
100 }