source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/BMX41000/IHS BMX Framework/IndianHealthService.BMXNet/Sevices/BMXNetRemoteSessionPool.cs@ 1722

Last change on this file since 1722 was 1146, checked in by Sam Habiel, 14 years ago

Initial Import of BMX4

File size: 6.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace IndianHealthService.BMXNet.Services
6{
7 internal class BMXRemoteSessionPool : RemoteSessionPool
8 {
9
10 private Log _log = new NullLog();
11
12 public Log Log
13 {
14 get { return _log; }
15 set { _log = value; }
16 }
17
18 public void Begin(BMXNetSessionConnectionSource aSource)
19 {
20 this.Source = aSource;
21 this.AllowAllocation = true;
22 }
23
24 private bool _allowAllocation = false;
25
26 public bool AllowAllocation
27 {
28 get { return _allowAllocation; }
29 set { _allowAllocation = value; }
30 }
31
32
33 private int _maxSessions = 5;
34
35 public int MaxSessions
36 {
37 get { return _maxSessions; }
38 set { _maxSessions = value; }
39 }
40
41 public int IdleSessionCount
42 {
43 get
44 {
45 return this.IdleConnections.Count;
46 }
47 }
48
49 public int AvailableSessionCount
50 {
51 get
52 {
53 return this.MaxSessions - this.AllocatedSessions.Count;
54 }
55 }
56
57
58 private BMXNetSessionConnectionSource _source = null;
59
60 internal BMXNetSessionConnectionSource Source
61 {
62 get { return _source; }
63 set { _source = value; }
64 }
65
66
67 private List<BMXNetSessionConnection> _idleConnections = new List<BMXNetSessionConnection>();
68
69 public List<BMXNetSessionConnection> IdleConnections
70 {
71 get { return _idleConnections; }
72 set { _idleConnections = value; }
73 }
74
75 private List<BMXNetRemoteSession> _allocatedSessions = new List<BMXNetRemoteSession>();
76
77 public List<BMXNetRemoteSession> AllocatedSessions
78 {
79 get { return _allocatedSessions; }
80 set { _allocatedSessions = value; }
81 }
82
83 #region RemoteSessionPool Members
84
85 public bool HasAvailableSession
86 {
87 get { return this.AvailableSessionCount > 0; }
88 }
89 public RemoteSession OpenSession()
90 {
91 return this.OpenSession(this.Log);
92 }
93
94 public RemoteSession OpenSession(Log aLog)
95 {
96 if (this.HasAvailableSession && this.AllowAllocation)
97 {
98 BMXNetRemoteSession session = new BMXNetRemoteSession();
99 session.Log = aLog;
100 if (this.IdleConnections.Count > 0)
101 {
102 this.Log.Log("BMX NET", "Debug", "Open Idle Session ");
103
104 session.IsPrimary = (this.IdleSessionCount + this.AllocatedSessions.Count) == 1;
105 this.AllocatedSessions.Add(session);
106 BMXNetSessionConnection connection = this.IdleConnections[0];
107 connection.ReceiveTimeout = 0;
108 connection.SendTimeout = 0;
109 this.IdleConnections.Remove(connection);
110 session.Begin(connection, this);
111 }
112 else
113 {
114 this.Log.Log("BMX NET", "Debug", "Open New Session ");
115 session.IsPrimary = (this.IdleSessionCount + this.AllocatedSessions.Count) == 0;
116 this.AllocatedSessions.Add(session);
117 session.Begin(this.Source.OpenConnection(), this);
118 }
119
120 if (!session.IsPrimary)
121 {
122 session.PrimarySession(this.PrimarySession);
123 }
124 return (RemoteSession)session;
125 }
126 else
127 {
128 this.Log.Log("BMX NET", "Info", "No Session Available");
129 return null;
130 }
131 }
132
133
134 private BMXNetRemoteSession PrimarySession
135 {
136 get
137 {
138 foreach (BMXNetRemoteSession each in this.AllocatedSessions)
139 {
140 if (each.IsPrimary)
141 {
142 return each;
143 }
144 }
145 return null;
146 }
147 }
148
149 private void ReleaseSession(RemoteSession aSession)
150 {
151 BMXNetRemoteSession bmxSession = aSession as BMXNetRemoteSession;
152
153 if (bmxSession.IsPrimary)
154 {
155 this.Close();
156 }
157 else
158 {
159 bmxSession.Suspend(this);
160 this.AllocatedSessions.Remove(bmxSession);
161 this.IdleConnections.Add(bmxSession.SessionConnection);
162 }
163 }
164
165 public void CloseSession(RemoteSession aSession)
166 {
167 if (aSession.IsPrimary)
168 {
169 this.TerminateAll();
170 }
171 else
172 {
173 this.ReleaseSession(aSession);
174 }
175 }
176
177 public void Close()
178 {
179 this.AllowAllocation = false;
180 foreach (BMXNetRemoteSession each in new List<BMXNetRemoteSession>(this.AllocatedSessions))
181 {
182 each.IsPrimary = false;
183 this.ReleaseSession(each);
184 }
185 this.TerminateAll();
186 }
187
188 #endregion
189
190
191
192 public void CloseAll()
193 {
194 List<BMXNetRemoteSession> toClose = new List<BMXNetRemoteSession>(this.AllocatedSessions);
195 this.AllocatedSessions = new List<BMXNetRemoteSession>();
196 foreach (BMXNetRemoteSession each in toClose)
197 {
198 this.CloseSession(each);
199 }
200
201 }
202
203
204 private void TerminateAll()
205 {
206 this.AllowAllocation = false;
207
208 this.CloseAll();
209 this.TerminateIdleSessions();
210 }
211
212
213 /// <summary>
214 /// Note: idle session connections are really being terminated. We
215 /// do not re-use BMXNetRemoteSession objects that wrap the session connection
216 /// </summary>
217 public void TerminateIdleSessions()
218 {
219 List<BMXNetSessionConnection> toTerminate = new List<BMXNetSessionConnection>(this.IdleConnections);
220 this.IdleConnections = new List<BMXNetSessionConnection>();
221 foreach (BMXNetSessionConnection each in toTerminate)
222 {
223 this.TerminateConnection(each);
224 }
225 }
226
227 private void TerminateConnection(BMXNetSessionConnection aConnection)
228 {
229 this.Source.CloseConnection(aConnection);
230 }
231
232
233
234 }
235}
Note: See TracBrowser for help on using the repository browser.