2 // System.ServiceModel.MessageFilterTable.cs
4 // Author: Duncan Mak (duncan@novell.com)
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System
.Collections
;
30 using System
.Collections
.Generic
;
31 using System
.Runtime
.Serialization
;
32 using System
.ServiceModel
;
33 using System
.ServiceModel
.Channels
;
35 namespace System
.ServiceModel
.Dispatcher
38 public class MessageFilterTable
<TFilterData
> : IMessageFilterTable
<TFilterData
>, IDictionary
<MessageFilter
,TFilterData
>,
39 ICollection
<KeyValuePair
<MessageFilter
, TFilterData
>>, IEnumerable
<KeyValuePair
<MessageFilter
,TFilterData
>>, IEnumerable
43 Dictionary
<MessageFilter
, TFilterData
> table
;
44 Dictionary
<MessageFilter
, int> priority_list
;
46 public MessageFilterTable ()
47 : this (0) {} // 0 is the default
49 public MessageFilterTable (int default_priority
)
51 this.default_priority
= default_priority
;
52 table
= new Dictionary
<MessageFilter
, TFilterData
> ();
53 priority_list
= new Dictionary
<MessageFilter
, int> ();
56 public void Add (KeyValuePair
<MessageFilter
, TFilterData
> item
)
58 this.Add (item
.Key
, item
.Value
, default_priority
);
61 public void Add (MessageFilter filter
, TFilterData data
)
63 this.Add (filter
, data
, default_priority
);
66 public void Add (MessageFilter filter
, TFilterData data
, int priority
)
68 table
.Add (filter
, data
);
69 priority_list
.Add (filter
, priority
);
75 priority_list
.Clear ();
78 public bool Contains (KeyValuePair
<MessageFilter
, TFilterData
> item
)
80 return ContainsKey (item
.Key
);
83 public bool ContainsKey (MessageFilter filter
)
85 return table
.ContainsKey (filter
);
88 public void CopyTo (KeyValuePair
<MessageFilter
, TFilterData
> [] array
, int index
)
90 ((ICollection
) table
).CopyTo (array
, index
);
93 protected virtual IMessageFilterTable
<TFilterData
> CreateFilterTable (MessageFilter filter
)
95 return filter
.CreateFilterTable
<TFilterData
> ();
98 public IEnumerator
<KeyValuePair
<MessageFilter
, TFilterData
>> GetEnumerator ()
100 return table
.GetEnumerator ();
103 public int GetPriority (MessageFilter filter
)
105 return priority_list
[filter
];
108 public bool GetMatchingFilter (Message message
, out MessageFilter result
)
111 throw new ArgumentNullException ("message is null");
115 foreach (MessageFilter f
in table
.Keys
) {
116 if (!f
.Match (message
))
120 throw new MultipleFilterMatchesException (
121 "More than one filter matches.");
129 public bool GetMatchingFilter (MessageBuffer buffer
, out MessageFilter result
)
131 return GetMatchingFilter (buffer
.CreateMessage (), out result
);
134 [MonoTODO ("Consider priority")]
135 public bool GetMatchingFilters (Message message
, ICollection
<MessageFilter
> results
)
138 throw new ArgumentNullException ("message is null.");
141 foreach (MessageFilter f
in table
.Keys
)
142 if (f
.Match (message
)) {
149 public bool GetMatchingFilters (MessageBuffer buffer
, ICollection
<MessageFilter
> results
)
151 return GetMatchingFilters (buffer
.CreateMessage (), results
);
154 [MonoTODO ("Consider priority")]
155 public bool GetMatchingValue (Message message
, out TFilterData data
)
158 throw new ArgumentNullException ("message is null");
159 data
= default (TFilterData
);
162 foreach (MessageFilter f
in table
.Keys
) {
163 if (!f
.Match (message
))
167 throw new MultipleFilterMatchesException (
168 "More than one filter matches.");
176 public bool GetMatchingValue (MessageBuffer buffer
, out TFilterData data
)
178 return GetMatchingValue (buffer
.CreateMessage (), out data
);
181 public bool GetMatchingValues (Message message
, ICollection
<TFilterData
> results
)
184 throw new ArgumentNullException ("message is null.");
187 foreach (MessageFilter f
in table
.Keys
)
188 if (f
.Match (message
)) {
189 results
.Add (table
[f
]);
195 public bool GetMatchingValues (MessageBuffer buffer
, ICollection
<TFilterData
> results
)
197 return GetMatchingValues (buffer
.CreateMessage (), results
);
200 public bool Remove (MessageFilter filter
)
203 throw new ArgumentNullException ("filter is null.");
205 if (!table
.ContainsKey (filter
))
208 table
.Remove (filter
);
209 priority_list
.Remove (filter
);
213 public bool Remove (KeyValuePair
<MessageFilter
, TFilterData
> item
)
215 return Remove (item
.Key
);
218 IEnumerator IEnumerable
.GetEnumerator ()
220 return (IEnumerator
) table
.GetEnumerator ();
223 public bool TryGetValue (MessageFilter filter
, out TFilterData data
)
225 return table
.TryGetValue (filter
, out data
);
228 public int Count { get { return table.Count; }
}
231 public int DefaultPriority
{
232 get { return default_priority; }
233 set { default_priority = value; }
236 public bool IsReadOnly { get { return false; }
}
238 public TFilterData
this [MessageFilter filter
] {
239 get { return table [filter]; }
240 set { table [filter] = value; }
243 public ICollection
<MessageFilter
> Keys
{
244 get { return table.Keys; }
247 public ICollection
<TFilterData
> Values
{
248 get { return table.Values; }