forked from dimbor-ru/opennx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionList.cpp
311 lines (277 loc) · 8.9 KB
/
SessionList.cpp
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// $Id: SessionList.cpp 687 2012-02-18 00:00:24Z felfert $
//
// Copyright (C) 2006 The OpenNX Team
// Author: Fritz Elfert
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if defined(__GNUG__) && !defined(__APPLE__)
#pragma implementation "SessionList.h"
#endif
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "SessionList.h"
#include "MySession.h"
#include <wx/dir.h>
#include <wx/filename.h>
#include "trace.h"
ENABLE_TRACE;
DEFINE_LOCAL_EVENT_TYPE(wxEVT_SESSIONLIST_ACTION);
class SessionTraverser : public wxDirTraverser
{
public:
SessionTraverser(wxArrayString& dirs) : m_dirs(dirs) { }
virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename))
{
return wxDIR_CONTINUE;
}
virtual wxDirTraverseResult OnDir(const wxString& dirpath)
{
wxString name = wxFileName::FileName(dirpath).GetFullName();
m_dirs.Add(name);
return wxDIR_IGNORE;
}
private:
wxArrayString& m_dirs;
};
class RmRfTraverser : public wxDirTraverser
{
public:
RmRfTraverser() { }
~RmRfTraverser()
{
int n = m_aFiles.GetCount() - 1;
while (n >= 0) {
myLogTrace(MYTRACETAG, wxT("Removing file %s"), VMB(m_aFiles[n]));
::wxRemoveFile(m_aFiles[n--]);
}
n = m_aDirs.GetCount() - 1;
while (n >= 0) {
myLogTrace(MYTRACETAG, wxT("Removing dir %s"), VMB(m_aDirs[n]));
::wxRmdir(m_aDirs[n--]);
}
}
virtual wxDirTraverseResult OnFile(const wxString& filename)
{
myLogTrace(MYTRACETAG, wxT("going to delete file %s"), VMB(filename));
m_aFiles.Add(filename);
return wxDIR_CONTINUE;
}
virtual wxDirTraverseResult OnDir(const wxString& dirpath)
{
myLogTrace(MYTRACETAG, wxT("going to delete dir %s"), VMB(dirpath));
m_aDirs.Add(dirpath);
return wxDIR_CONTINUE;
}
private:
wxArrayString m_aDirs;
wxArrayString m_aFiles;
};
WX_DECLARE_STRING_HASH_MAP(MySession, SessionHash);
SessionList::SessionList(wxString dir, wxEvtHandler* h)
: wxThreadHelper()
, m_dirName(dir)
, m_pAdminHandler(h)
, m_dir(NULL)
, m_reValid(false)
{
m_sessions = new SessionHash();
m_re = new wxRegEx();
if (m_re->Compile(wxT("(([TF])-)?([SC])-(.*)-([[:digit:]]+)-([[:xdigit:]]{32})$"), wxRE_ADVANCED))
m_reValid = true;
if (m_reValid && (!m_dirName.IsEmpty())) {
CreateThread();
m_thread->Run();
}
}
SessionList::~SessionList()
{
m_thread->Delete();
while (m_thread->IsRunning())
wxThread::Sleep(100);
if (m_dir != NULL)
delete m_dir;
delete m_re;
m_sessions->clear();
delete m_sessions;
}
void SessionList::SetDir(wxString dir)
{
m_dirName = dir;
if (m_reValid && (!m_dirName.IsEmpty())) {
if (m_thread == NULL) {
CreateThread();
m_thread->Run();
} else {
m_csDir.Enter();
if (m_dir)
delete m_dir;
m_dir = NULL;
m_csDir.Leave();
}
}
}
void SessionList::ScanDir()
{
wxCriticalSectionLocker dlocker(m_csDir);
if (m_dir == NULL) {
if (m_dirName.IsEmpty())
return;
if (!wxDir::Exists(m_dirName)) {
m_dirName = wxEmptyString;
return;
}
m_dir = new wxDir(m_dirName);
}
// get the names of all session directories
wxArrayString sdirs;
SessionTraverser traverser(sdirs);
m_dir->Traverse(traverser);
size_t cnt = sdirs.GetCount();
size_t i;
// Format of session dir name:
//
// ([TF]-)?[SC]-(.*)-[:digit:]+-[:xdigit:]{32}
//
// 1. element is session status
// "T-" resp. "F-" stands for terminated or failed respectively.
// If it is missing, the session is undefined (probably running).
//
// 2. element is session type
// S- resp. C- stands for server resp. client session type
//
// 3. element is host name of the NX server
// 4. element is port name
// 5. element is an md5sum. (//FE: For what data?)
//
bool changed = false;
SessionHash::iterator it;
size_t oldcount = m_sessions->size();
for (it = m_sessions->begin(); it != m_sessions->end(); ++it)
it->second.bSetTouched(false);
for (i = 0; i < cnt; i++) {
wxString tmp = sdirs[i];
if (m_re->Matches(tmp)) {
wxString md5 = m_re->GetMatch(tmp, 6);
it = m_sessions->find(md5);
if (it == m_sessions->end()) {
// New session found
long port;
m_re->GetMatch(tmp,5).ToLong(&port);
myLogTrace(MYTRACETAG,
wxT("State='%s', Type='%s', Host='%s', Port=%d, MD5='%s'"),
VMB(m_re->GetMatch(tmp,2)),
VMB(m_re->GetMatch(tmp,3)),
VMB(m_re->GetMatch(tmp,4)),
(int)port, VMB(md5));
// Create new hash entry
MySession s(m_dirName + wxFileName::GetPathSeparator() + tmp,
m_re->GetMatch(tmp,2), m_re->GetMatch(tmp,3),
m_re->GetMatch(tmp,4), port, md5);
(*m_sessions)[md5] = s;
if (m_pAdminHandler) {
wxCommandEvent ev(wxEVT_SESSIONLIST_ACTION, wxID_ANY);
ev.SetInt(SessionAdded);
ev.SetClientData(&(m_sessions->find(md5)->second));
m_pAdminHandler->AddPendingEvent(ev);
changed = true;
}
} else {
// Existing session found, mark it
it->second.bSetTouched(true);
}
}
}
bool finished = false;
while (!finished) {
finished = true;
for (it = m_sessions->begin(); it != m_sessions->end(); ++it) {
if (it->second.bGetTouched()) {
MySession::tSessionStatus st = it->second.eGetSessionStatus();
it->second.CheckState();
if (it->second.eGetSessionStatus() != st) {
if (m_pAdminHandler) {
wxCommandEvent ev(wxEVT_SESSIONLIST_ACTION, wxID_ANY);
ev.SetInt(SessionChanged);
ev.SetClientData(&it->second);
m_pAdminHandler->AddPendingEvent(ev);
changed = true;
}
}
} else {
wxString md5 = it->second.sGetMd5();
myLogTrace(MYTRACETAG, wxT("Session '%s' disappeared"), VMB(md5));
finished = false;
if (m_pAdminHandler) {
wxCommandEvent ev(wxEVT_SESSIONLIST_ACTION, wxID_ANY);
ev.SetInt(SessionRemoved);
ev.SetClientData(&it->second);
m_pAdminHandler->AddPendingEvent(ev);
changed = true;
}
RemoveFromList(md5);
break;
}
}
}
if (changed && (m_pAdminHandler != NULL)) {
wxCommandEvent ev(wxEVT_SESSIONLIST_ACTION, wxID_ANY);
ev.SetInt(UpdateList);
m_pAdminHandler->AddPendingEvent(ev);
}
if (m_sessions->size() != oldcount)
myLogTrace(MYTRACETAG, wxT("SessionList: Now %d sessions"), (int)m_sessions->size());
}
void
SessionList::RemoveFromList(wxString md5)
{
m_sessions->erase(md5);
}
void
SessionList::CleanupDir(wxString &dir)
{
if (!dir.IsEmpty()) {
{
myLogTrace(MYTRACETAG, wxT("CleanupDir '%s'"), VMB(dir));
wxDir d(dir);
RmRfTraverser t;
d.Traverse(t);
}
::wxRmdir(dir);
}
}
wxThread::ExitCode
SessionList::Entry()
{
int cnt = 0;
while (!m_thread->TestDestroy()) {
if (cnt-- == 0) {
ScanDir();
cnt = 20;
}
wxThread::Sleep(100);
}
return 0;
}