2 // Author: Atsushi Enomoto <atsushi@ximian.com>
4 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
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:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
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.
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;
33 using System.Xml.Schema;
35 namespace System.ServiceModel.Discovery
37 public class DiscoveryMessageSequence : IComparable<DiscoveryMessageSequence>, IEquatable<DiscoveryMessageSequence>
39 internal DiscoveryMessageSequence (long instanceId, Uri sequenceId, long messageNumber)
41 InstanceId = instanceId;
42 SequenceId = sequenceId;
43 MessageNumber = messageNumber;
46 public long InstanceId { get; private set; }
47 public long MessageNumber { get; private set; }
48 public Uri SequenceId { get; private set; }
50 public bool CanCompareTo (DiscoveryMessageSequence other)
52 return other != null; // I cannot find any other conditions that return false.
55 public int CompareTo (DiscoveryMessageSequence other)
57 return CanCompareTo (other) ? GetHashCode () - other.GetHashCode () : -1;
60 public bool Equals (DiscoveryMessageSequence other)
64 return InstanceId == other.InstanceId &&
65 (SequenceId == null && other.SequenceId == null || SequenceId.Equals (other.SequenceId)) &&
66 MessageNumber == other.MessageNumber;
69 public override bool Equals (object obj)
71 var s = obj as DiscoveryMessageSequence;
72 return s != null && Equals (s);
75 public override int GetHashCode ()
77 return (int) ((InstanceId * (SequenceId != null ? SequenceId.GetHashCode () : 1) << 17) + MessageNumber);
80 public override string ToString ()
82 return String.Format ("InstanceId={0}, SequenceId={1}, MessageNumber={2}", InstanceId, SequenceId, MessageNumber);
85 public static bool operator == (DiscoveryMessageSequence messageSequence1, DiscoveryMessageSequence messageSequence2)
87 return object.ReferenceEquals (messageSequence1, null) ? object.ReferenceEquals (messageSequence2, null) : messageSequence1.Equals (messageSequence2);
90 public static bool operator != (DiscoveryMessageSequence messageSequence1, DiscoveryMessageSequence messageSequence2)
92 return object.ReferenceEquals (messageSequence1, null) ? !object.ReferenceEquals (messageSequence2, null) : !messageSequence1.Equals (messageSequence2);
95 internal static DiscoveryMessageSequence ReadXml (XmlReader reader, DiscoveryVersion version)
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));
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);
111 internal void WriteXml (XmlWriter writer)
113 writer.WriteAttributeString ("InstanceId", XmlConvert.ToString (InstanceId));
114 if (SequenceId != null)
115 writer.WriteAttributeString ("SequenceId", SequenceId.ToString ());
116 writer.WriteAttributeString ("MessageNumber", XmlConvert.ToString (MessageNumber));
119 internal static XmlSchema BuildSchema (DiscoveryVersion version)
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);