2002-10-25 Tim Coleman (tim@tmicoleman.com)
[mono.git] / mcs / class / Mono.Data.TdsClient / Mono.Data.TdsClient / TdsConnectionPool.cs
1 //
2 // Mono.Data.TdsClient.TdsConnectionPool.cs
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) 2002 Tim Coleman
8 //
9
10 using Mono.Data.TdsClient.Internal;
11 using System;
12 using System.Collections;
13 using System.Threading;
14
15 namespace Mono.Data.TdsClient {
16         internal class TdsConnectionPool : MarshalByRefObject, IList, ICollection, IEnumerable
17         {
18                 #region Fields
19
20                 ArrayList list = new ArrayList ();
21
22                 int maxSize;
23                 int minSize;
24                 int packetSize;
25                 int port;
26
27                 string dataSource;
28
29                 #endregion // Fields
30
31                 #region Constructors
32
33                 public TdsConnectionPool (string dataSource, int port, int packetSize, int minSize, int maxSize)
34                 {
35                         this.dataSource = dataSource;
36                         this.port = port;
37                         this.packetSize = packetSize;
38                         this.minSize = minSize;
39                         this.maxSize = maxSize;
40                 }
41
42                 #endregion // Constructors
43
44                 #region Properties
45
46                 public Tds this[int index] {
47                         get { return (Tds) list[index]; }
48                 }
49
50                 object IList.this[int index] {
51                         get { return this[index]; }
52                         set { throw new InvalidOperationException (); }
53                 }
54
55                 public int Count {
56                         get { return list.Count; }
57                 }
58
59                 public bool IsFixedSize {
60                         get { return false; }
61                 }
62
63                 public bool IsReadOnly {
64                         get { return true; }
65                 }
66
67                 public bool IsSynchronized {
68                         get { return false; }
69                 }
70
71                 public int MaxSize {
72                         get { return maxSize; }
73                 }
74
75                 public int MinSize {
76                         get { return minSize; }
77                 }
78
79                 public object SyncRoot {
80                         get { throw new InvalidOperationException (); }
81                 }
82
83                 #endregion // Properties
84
85                 #region Methods
86
87                 public int Add (object o)
88                 {
89                         return list.Add ((Tds) o);
90                 }
91
92                 public void Clear ()
93                 {
94                         list.Clear ();
95                 }
96
97                 public bool Contains (object o)
98                 {
99                         return list.Contains ((Tds) o);
100                 }
101
102                 public void CopyTo (Array array, int index)
103                 {
104                         list.CopyTo (array, index);
105                 }
106
107                 public IEnumerator GetEnumerator ()
108                 {
109                         return list.GetEnumerator ();
110                 }
111
112                 [MonoTODO]
113                 public ITds AllocateConnection ()
114                 {
115                         // make sure we have the minimum count (really only useful the first time)
116                         lock (list) {
117                                 for (int i = Count; i < minSize; i += 1)
118                                         Add (new Tds42 (dataSource, port, packetSize));
119                         }
120
121                         // Try to obtain a lock
122                         foreach (object o in list)
123                                 if (Monitor.TryEnter (o))
124                                         return (ITds) o;
125
126                         if (Count < maxSize) {
127                                 Tds tds = new Tds42 (dataSource, port, packetSize);
128                                 Monitor.Enter (tds);
129                                 Add (tds);
130                                 return tds;
131                         }
132
133                         // else we have to wait for one to be available
134                         
135                         return null;
136                 }
137
138                 public void ReleaseConnection (ITds tds)
139                 {
140                         Monitor.Exit (tds);
141                 }
142
143                 public int IndexOf (object o)
144                 {
145                         return list.IndexOf ((Tds) o);
146                 }
147
148                 public void Insert (int index, object o)
149                 {
150                         list.Insert (index, (Tds) o);
151                 }
152
153                 public void Remove (object o)
154                 {
155                         list.Remove ((Tds) o);
156                 }
157
158                 public void RemoveAt (int index)
159                 {
160                         list.RemoveAt (index);
161                 }
162
163                 #endregion // Methods
164         }
165 }