[threads] Remove unused mono_thread_internal_stop (#4449)
[mono.git] / mcs / class / System.ServiceModel.Discovery / System.ServiceModel.Discovery / DiscoveryMessageSequence.cs
1 //
2 // Author: Atsushi Enomoto <atsushi@ximian.com>
3 //
4 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 // 
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 // 
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25 using System;
26 using System.Collections.Generic;
27 using System.Collections.ObjectModel;
28 using System.ServiceModel;
29 using System.ServiceModel.Channels;
30 using System.ServiceModel.Description;
31 using System.ServiceModel.Dispatcher;
32 using System.Xml;
33 using System.Xml.Schema;
34
35 namespace System.ServiceModel.Discovery
36 {
37         public class DiscoveryMessageSequence : IComparable<DiscoveryMessageSequence>, IEquatable<DiscoveryMessageSequence>
38         {
39                 internal DiscoveryMessageSequence (long instanceId, Uri sequenceId, long messageNumber)
40                 {
41                         InstanceId = instanceId;
42                         SequenceId = sequenceId;
43                         MessageNumber = messageNumber;
44                 }
45
46                 public long InstanceId { get; private set; }
47                 public long MessageNumber { get; private set; }
48                 public Uri SequenceId { get; private set; }
49
50                 public bool CanCompareTo (DiscoveryMessageSequence other)
51                 {
52                         return other != null; // I cannot find any other conditions that return false.
53                 }
54
55                 public int CompareTo (DiscoveryMessageSequence other)
56                 {
57                         return CanCompareTo (other) ? GetHashCode () - other.GetHashCode () : -1;
58                 }
59
60                 public bool Equals (DiscoveryMessageSequence other)
61                 {
62                         if (other == null)
63                                 return false;
64                         return  InstanceId == other.InstanceId &&
65                                 (SequenceId == null && other.SequenceId == null || SequenceId.Equals (other.SequenceId)) &&
66                                 MessageNumber == other.MessageNumber;
67                 }
68
69                 public override bool Equals (object obj)
70                 {
71                         var s = obj as DiscoveryMessageSequence;
72                         return s != null && Equals (s);
73                 }
74
75                 public override int GetHashCode ()
76                 {
77                         return (int) ((InstanceId * (SequenceId != null ? SequenceId.GetHashCode () : 1) << 17) + MessageNumber);
78                 }
79
80                 public override string ToString ()
81                 {
82                         return String.Format ("InstanceId={0}, SequenceId={1}, MessageNumber={2}", InstanceId, SequenceId, MessageNumber);
83                 }
84
85                 public static bool operator == (DiscoveryMessageSequence messageSequence1, DiscoveryMessageSequence messageSequence2)
86                 {
87                         return object.ReferenceEquals (messageSequence1, null) ? object.ReferenceEquals (messageSequence2, null) : messageSequence1.Equals (messageSequence2);
88                 }
89
90                 public static bool operator != (DiscoveryMessageSequence messageSequence1, DiscoveryMessageSequence messageSequence2)
91                 {
92                         return object.ReferenceEquals (messageSequence1, null) ? !object.ReferenceEquals (messageSequence2, null) : !messageSequence1.Equals (messageSequence2);
93                 }
94
95                 internal static DiscoveryMessageSequence ReadXml (XmlReader reader, DiscoveryVersion version)
96                 {
97                         if (reader == null)
98                                 throw new ArgumentNullException ("reader");
99                         if (reader.LocalName != "AppSequence" || reader.NamespaceURI != version.Namespace)
100                                 throw new ArgumentException (String.Format ("AppSequenceType element in namespace '{0}' was expected. Got '{1}' element in '{2}' namespace", version.Namespace, reader.LocalName, reader.NamespaceURI));
101
102                         var instId = reader.GetAttribute ("InstanceId");
103                         var seqId = reader.GetAttribute ("SequenceId");
104                         var msgno = reader.GetAttribute ("MessageNumber");
105                         var source = new DiscoveryMessageSequence (instId != null ? XmlConvert.ToInt64 (instId) : 0, seqId != null ? new Uri (seqId, UriKind.RelativeOrAbsolute) : null, msgno != null ? XmlConvert.ToInt64 (msgno) : 0);
106                         
107                         reader.Skip ();
108                         return source;
109                 }
110
111                 internal void WriteXml (XmlWriter writer)
112                 {
113                         writer.WriteAttributeString ("InstanceId", XmlConvert.ToString (InstanceId));
114                         if (SequenceId != null)
115                                 writer.WriteAttributeString ("SequenceId", SequenceId.ToString ());
116                         writer.WriteAttributeString ("MessageNumber", XmlConvert.ToString (MessageNumber));
117                 }
118
119                 internal static XmlSchema BuildSchema (DiscoveryVersion version)
120                 {
121                         var schema = new XmlSchema () { TargetNamespace = version.Namespace };
122                         var ccr = new XmlSchemaComplexContentRestriction ();
123                         ccr.Attributes.Add (new XmlSchemaAttribute () { Name = "InstanceId", SchemaTypeName = new XmlQualifiedName ("unsignedInt", XmlSchema.Namespace), Use = XmlSchemaUse.Required });
124                         ccr.Attributes.Add (new XmlSchemaAttribute () { Name = "SequenceId", SchemaTypeName = new XmlQualifiedName ("anyURI", XmlSchema.Namespace), Use = XmlSchemaUse.Optional });
125                         ccr.Attributes.Add (new XmlSchemaAttribute () { Name = "MessageNumber", SchemaTypeName = new XmlQualifiedName ("unsignedInt", XmlSchema.Namespace), Use = XmlSchemaUse.Required });
126                         var ct = new XmlSchemaComplexType () { Name = "AppSequenceType", ContentModel = new XmlSchemaComplexContent () { Content = ccr } };
127                         schema.Items.Add (ct);
128
129                         return schema;
130                 }
131         }
132 }