-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_manager.cpp
More file actions
125 lines (104 loc) · 4.28 KB
/
Copy pathprocess_manager.cpp
File metadata and controls
125 lines (104 loc) · 4.28 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
#include "process_manager.h"
#include "socket.h"
#include <wx/sizer.h>
#include <wx/msgdlg.h>
#include <mutex>
#include <unordered_map>
#include <string>
static std::unordered_map<int, std::string> g_ProcessListData;
static std::mutex g_ProcessMutex;
void ProcessManager_UpdateList(int clientId, const std::string& data)
{
std::lock_guard<std::mutex> lk(g_ProcessMutex);
g_ProcessListData[clientId] = data;
}
ProcessManagerDialog::ProcessManagerDialog(wxWindow* parent, const wxString& clientName, int clientId)
: wxDialog(parent, wxID_ANY, "Process Manager - " + clientName,
wxDefaultPosition, wxSize(520, 520),
wxDEFAULT_DIALOG_STYLE | wxMINIMIZE_BOX),
m_clientId(clientId)
{
SetFont(wxFont(9, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, "Segoe UI"));
wxBoxSizer* root = new wxBoxSizer(wxVERTICAL);
wxBoxSizer* toolbar = new wxBoxSizer(wxHORIZONTAL);
m_btnRefresh = new wxButton(this, wxID_ANY, "Refresh", wxDefaultPosition, wxSize(100, 28));
m_btnKill = new wxButton(this, wxID_ANY, "Kill", wxDefaultPosition, wxSize(80, 28));
m_btnKill->SetForegroundColour(wxColour(200, 30, 30));
m_statusLabel = new wxStaticText(this, wxID_ANY, "");
toolbar->Add(m_btnRefresh, 0, wxRIGHT, 6);
toolbar->Add(m_btnKill, 0, wxRIGHT, 12);
toolbar->Add(m_statusLabel, 0, wxALIGN_CENTER_VERTICAL);
root->Add(toolbar, 0, wxEXPAND | wxALL, 6);
m_list = new wxListCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
wxLC_REPORT | wxLC_VRULES | wxLC_HRULES | wxLC_SINGLE_SEL);
m_list->AppendColumn("Process Name", wxLIST_FORMAT_LEFT, 280);
m_list->AppendColumn("PID", wxLIST_FORMAT_RIGHT, 80);
root->Add(m_list, 1, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 6);
SetSizer(root);
Bind(wxEVT_BUTTON, &ProcessManagerDialog::OnRefresh, this, m_btnRefresh->GetId());
Bind(wxEVT_BUTTON, &ProcessManagerDialog::OnKill, this, m_btnKill->GetId());
Bind(wxEVT_CLOSE_WINDOW, &ProcessManagerDialog::OnClose, this);
m_timer.SetOwner(this);
Bind(wxEVT_TIMER, &ProcessManagerDialog::OnTimer, this);
m_timer.Start(400);
DoRefresh();
Centre();
}
ProcessManagerDialog::~ProcessManagerDialog() { m_timer.Stop(); }
void ProcessManagerDialog::DoRefresh()
{
m_statusLabel->SetLabel("Refreshing...");
ServerManager::SendToClient(m_clientId, "{\"cmd\":\"get_processes\"}");
}
void ProcessManagerDialog::OnRefresh(wxCommandEvent&) { DoRefresh(); }
void ProcessManagerDialog::OnTimer(wxTimerEvent&)
{
std::string data;
{
std::lock_guard<std::mutex> lk(g_ProcessMutex);
auto it = g_ProcessListData.find(m_clientId);
if (it == g_ProcessListData.end() || it->second.empty()) return;
data = std::move(it->second);
it->second.clear();
}
UpdateProcessList(wxString::FromUTF8(data));
}
void ProcessManagerDialog::UpdateProcessList(const wxString& data)
{
m_list->DeleteAllItems();
wxArrayString lines = wxSplit(data, '\n');
long row = 0;
for (const auto& line : lines)
{
if (line.empty()) continue;
int sep = line.Find('|');
if (sep == wxNOT_FOUND) continue;
wxString name = line.Left(sep);
wxString pid = line.Mid(sep + 1).Trim();
m_list->InsertItem(row, name);
m_list->SetItem(row, 1, pid);
row++;
}
m_statusLabel->SetLabel(wxString::Format("Processes: %ld", row));
}
void ProcessManagerDialog::OnKill(wxCommandEvent&)
{
long idx = m_list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (idx < 0) { wxMessageBox("Select a process first.", "Kill", wxOK | wxICON_WARNING); return; }
wxString pidStr = m_list->GetItemText(idx, 1);
wxString name = m_list->GetItemText(idx, 0);
if (wxMessageBox("Kill \"" + name + "\" (PID " + pidStr + ")?",
"Confirm Kill", wxYES_NO | wxICON_QUESTION) != wxYES) return;
ServerManager::SendToClient(m_clientId, "{\"cmd\":\"kill_process\",\"pid\":" + pidStr.ToStdString() + "}");
wxMilliSleep(300);
DoRefresh();
}
void ProcessManagerDialog::OnClose(wxCloseEvent&)
{
m_timer.Stop();
{
std::lock_guard<std::mutex> lk(g_ProcessMutex);
g_ProcessListData.erase(m_clientId);
}
Destroy();
}