2c7b38ad18e373e1cf216f7f619074ec367f8b17
[mono.git] / mcs / class / System.Runtime.Remoting / MonoHttp / HttpListenerPrefixCollection.cs
1 #define EMBEDDED_IN_1_0
2
3 //
4 // System.Net.HttpListenerPrefixCollection.cs
5 //
6 // Author:
7 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
8 //
9 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 #if !NET_2_0
31
32 using System.Collections;
33 using System.Collections.Generic;
34 using System; using System.Net; namespace MonoHttp {
35 #if EMBEDDED_IN_1_0
36         internal class HttpListenerPrefixCollection : IEnumerable, ICollection {
37                 ArrayList prefixes;
38                 
39 #else
40         internal class HttpListenerPrefixCollection : ICollection<string>, IEnumerable<string>, IEnumerable {
41                 List<string> prefixes = new List<string> ();
42 #endif
43                 HttpListener listener;
44
45                 internal HttpListenerPrefixCollection (HttpListener listener)
46                 {
47                         this.listener = listener;
48                 }
49
50                 public int Count {
51                         get { return prefixes.Count; }
52                 }
53
54                 public bool IsReadOnly {
55                         get { return false; }
56                 }
57
58                 public bool IsSynchronized {
59                         get { return false; }
60                 }
61
62                 public void Add (string uriPrefix)
63                 {
64                         listener.CheckDisposed ();
65                         ListenerPrefix.CheckUri (uriPrefix);
66                         if (prefixes.Contains (uriPrefix))
67                                 return;
68
69                         prefixes.Add (uriPrefix);
70                         if (listener.IsListening)
71                                 EndPointManager.AddPrefix (uriPrefix, listener);
72                 }
73
74                 public void Clear ()
75                 {
76                         listener.CheckDisposed ();
77                         prefixes.Clear ();
78                         if (listener.IsListening)
79                                 EndPointManager.RemoveListener (listener);
80                 }
81
82                 public bool Contains (string uriPrefix)
83                 {
84                         listener.CheckDisposed ();
85                         return prefixes.Contains (uriPrefix);
86                 }
87
88                 public void CopyTo (string [] array, int offset)
89                 {
90                         listener.CheckDisposed ();
91                         prefixes.CopyTo (array, offset);
92                 }
93
94                 public void CopyTo (Array array, int offset)
95                 {
96                         listener.CheckDisposed ();
97                         ((ICollection) prefixes).CopyTo (array, offset);
98                 }
99
100 #if !EMBEDDED_IN_1_0
101                 public IEnumerator<string> GetEnumerator ()
102                 {
103                         return prefixes.GetEnumerator ();
104                 }
105 #else
106                 object ICollection.SyncRoot { get { return this; } }
107 #endif
108         
109                 IEnumerator IEnumerable.GetEnumerator ()
110                 {
111                         return prefixes.GetEnumerator ();
112                 }
113
114                 public bool Remove (string uriPrefix)
115                 {
116                         listener.CheckDisposed ();
117                         if (uriPrefix == null)
118                                 throw new ArgumentNullException ("uriPrefix");
119
120                         bool result = prefixes.Contains (uriPrefix); if (result) prefixes.Remove (uriPrefix);;
121                         if (result && listener.IsListening)
122                                 EndPointManager.RemovePrefix (uriPrefix, listener);
123
124                         return result;
125                 }
126         }
127 }
128 #endif
129