-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSessionCollection.cs
More file actions
212 lines (172 loc) · 6.13 KB
/
SessionCollection.cs
File metadata and controls
212 lines (172 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace LumiSoft.MailServer.API.UserAPI
{
/// <summary>
/// The SessionCollection object represents sessions in LumiSoft Mail Server server.
/// </summary>
public class SessionCollection : IEnumerable
{
private Server m_pOwner = null;
private List<Session> m_pSessions = null;
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="owner">Owner Server object that owns this collection.</param>
internal SessionCollection(Server owner)
{
m_pOwner = owner;
m_pSessions = new List<Session>();
Bind();
}
#region method Refresh
/// <summary>
/// Refreshes sessions.
/// </summary>
public void Refresh()
{
lock(m_pOwner.LockSynchronizer){
m_pSessions.Clear();
Bind();
}
}
#endregion
#region method ConatainsID
/// <summary>
/// Gets if collection contains session with specified ID.
/// </summary>
/// <param name="sessionID">Session ID.</param>
/// <returns></returns>
public bool ConatainsID(string sessionID)
{
foreach(Session session in m_pSessions){
if(session.ID == sessionID){
return true;
}
}
return false;
}
#endregion
#region method GetSessionByID
/// <summary>
/// Gets a Session object in the collection by session ID.
/// </summary>
/// <returns></returns>
public Session GetSessionByID(string sessionID)
{
foreach(Session session in m_pSessions){
if(session.ID == sessionID){
return session;
}
}
throw new Exception("Session with specified session ID '" + sessionID + "' doesn't exist !");
}
#endregion
#region method Bind
/// <summary>
/// Gets server events and binds them to this.
/// </summary>
private void Bind()
{
/* GetSessions
Responses:
+OK <sizeOfData>
<data>
-ERR <errorText>
*/
lock(m_pOwner.LockSynchronizer){
// Call TCP GetEvents
m_pOwner.TcpClient.TcpStream.WriteLine("GetSessions");
string response = m_pOwner.ReadLine();
if(!response.ToUpper().StartsWith("+OK")){
throw new Exception(response);
}
int sizeOfData = Convert.ToInt32(response.Split(new char[]{' '},2)[1]);
MemoryStream ms = new MemoryStream();
m_pOwner.TcpClient.TcpStream.ReadFixedCount(ms,sizeOfData);
// Decompress dataset
DataSet ds = Utils.DecompressDataSet(ms);
if(ds.Tables.Contains("Sessions")){
foreach(DataRow dr in ds.Tables["Sessions"].Rows){
m_pSessions.Add(new Session(
this,
dr["SessionID"].ToString(),
dr["SessionType"].ToString(),
Convert.ToDateTime(dr["SessionStartTime"]),
Convert.ToInt32(dr["ExpectedTimeout"]),
dr["UserName"].ToString(),
dr["LocalEndPoint"].ToString(),
dr["RemoteEndPoint"].ToString(),
Convert.ToInt32(dr["ReadTransferRate"]),
Convert.ToInt32(dr["WriteTransferRate"]),
dr["SessionLog"].ToString()
));
}
}
}
}
#endregion
#region interface IEnumerator
/// <summary>
/// Gets enumerator.
/// </summary>
/// <returns></returns>
public IEnumerator GetEnumerator()
{
return m_pSessions.GetEnumerator();
}
#endregion
#region Properties Implementation
/// <summary>
/// Gets the Server object that is the owner of this collection.
/// </summary>
public Server Server
{
get{ return m_pOwner; }
}
/// <summary>
/// Gets number of sessions in server.
/// </summary>
public int Count
{
get{ return m_pSessions.Count; }
}
/// <summary>
/// Gets a Session object in the collection by index number.
/// </summary>
/// <param name="index">An Int32 value that specifies the position of the Session object in the SessionCollection collection.</param>
/// <returns>A Session object value that represents the session in server.</returns>
public Session this[int index]
{
get{ return m_pSessions[index]; }
}
/// <summary>
/// Gets a Session object in the collection by filter ID.
/// </summary>
/// <param name="sessionID">A String value that specifies the session ID of the Session object in the SessionCollection collection.</param>
/// <returns>A Session object value that represents the session in server.</returns>
public Session this[string sessionID]
{
get{
foreach(Session session in m_pSessions){
if(sessionID.ToLower() == sessionID.ToLower()){
return session;
}
}
throw new Exception("Session with specified ID '" + sessionID + "' doesn't exist !");
}
}
/// <summary>
/// Gets direct access to sessions collection.
/// </summary>
internal List<Session> List
{
get{ return m_pSessions; }
}
#endregion
}
}