Ember
Loading...
Searching...
No Matches
ConnectionPanel.cpp
Go to the documentation of this file.
2#include "Network/TCPServer.h"
3
4namespace Ember {
5namespace Monitor {
6
7enum { ID_START_STOP = wxID_HIGHEST + 1, ID_DISCONNECT };
8
11
12 ConnectionPanel::ConnectionPanel(wxWindow *parent, TCPServer *server)
13 : EmberUI::Panel(parent, "ConnectionPanel"), m_server(server) {
14 CreateUI();
15 UpdateStatus();
16}
17
19 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
20
21 wxStaticBox *serverBox = new wxStaticBox(this, wxID_ANY, "Server");
22 wxStaticBoxSizer *serverSizer = new wxStaticBoxSizer(serverBox, wxVERTICAL);
23
24 wxBoxSizer *statusRow = new wxBoxSizer(wxHORIZONTAL);
25 statusRow->Add(new wxStaticText(this, wxID_ANY, "Status:"), 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
26 m_serverStatusLabel = new wxStaticText(this, wxID_ANY, "Stopped");
27 m_serverStatusLabel->SetForegroundColour(*wxRED);
28 statusRow->Add(m_serverStatusLabel, 1, wxALIGN_CENTER_VERTICAL);
29 serverSizer->Add(statusRow, 0, wxEXPAND | wxALL, 5);
30
31 wxBoxSizer *portRow = new wxBoxSizer(wxHORIZONTAL);
32 portRow->Add(new wxStaticText(this, wxID_ANY, "Port:"), 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
33 m_portInput = new wxTextCtrl(this, wxID_ANY, "12345", wxDefaultPosition, wxSize(70, -1));
34 portRow->Add(m_portInput, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);
35 m_startStopButton = new wxButton(this, ID_START_STOP, "Start");
36 portRow->Add(m_startStopButton, 0, wxALIGN_CENTER_VERTICAL);
37 serverSizer->Add(portRow, 0, wxEXPAND | wxALL, 5);
38
39 mainSizer->Add(serverSizer, 0, wxEXPAND | wxALL, 5);
40
41 wxStaticBox *clientBox = new wxStaticBox(this, wxID_ANY, "Client");
42 wxStaticBoxSizer *clientSizer = new wxStaticBoxSizer(clientBox, wxVERTICAL);
43
44 wxBoxSizer *clientStatusRow = new wxBoxSizer(wxHORIZONTAL);
45 clientStatusRow->Add(new wxStaticText(this, wxID_ANY, "Status:"), 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
46 m_clientStatusLabel = new wxStaticText(this, wxID_ANY, "Not Connected");
47 m_clientStatusLabel->SetForegroundColour(wxColour(128, 128, 128));
48 clientStatusRow->Add(m_clientStatusLabel, 1, wxALIGN_CENTER_VERTICAL);
49 clientSizer->Add(clientStatusRow, 0, wxEXPAND | wxALL, 5);
50
51 wxBoxSizer *addrRow = new wxBoxSizer(wxHORIZONTAL);
52 addrRow->Add(new wxStaticText(this, wxID_ANY, "Address:"), 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
53 m_clientAddressLabel = new wxStaticText(this, wxID_ANY, "-");
54 addrRow->Add(m_clientAddressLabel, 1, wxALIGN_CENTER_VERTICAL);
55 clientSizer->Add(addrRow, 0, wxEXPAND | wxALL, 5);
56
57 wxBoxSizer *nameRow = new wxBoxSizer(wxHORIZONTAL);
58 nameRow->Add(new wxStaticText(this, wxID_ANY, "Name:"), 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
59 m_clientNameLabel = new wxStaticText(this, wxID_ANY, "-");
60 nameRow->Add(m_clientNameLabel, 1, wxALIGN_CENTER_VERTICAL);
61 clientSizer->Add(nameRow, 0, wxEXPAND | wxALL, 5);
62
63 wxBoxSizer *treeRow = new wxBoxSizer(wxHORIZONTAL);
64 treeRow->Add(new wxStaticText(this, wxID_ANY, "Tree:"), 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
65 m_treeIdLabel = new wxStaticText(this, wxID_ANY, "-");
66 treeRow->Add(m_treeIdLabel, 1, wxALIGN_CENTER_VERTICAL);
67 clientSizer->Add(treeRow, 0, wxEXPAND | wxALL, 5);
68
69 m_disconnectButton = new wxButton(this, ID_DISCONNECT, "Disconnect");
70 m_disconnectButton->Enable(false);
71 clientSizer->Add(m_disconnectButton, 0, wxALIGN_RIGHT | wxALL, 5);
72
73 mainSizer->Add(clientSizer, 0, wxEXPAND | wxALL, 5);
74
75 mainSizer->AddStretchSpacer(1);
76
77 SetSizer(mainSizer);
78}
79
81 if (m_server->IsRunning()) {
82 m_serverStatusLabel->SetLabel(wxString::Format("Listening on %d", m_server->GetPort()));
83 m_serverStatusLabel->SetForegroundColour(wxColour(0, 128, 0));
84 m_startStopButton->SetLabel("Stop");
85 m_portInput->Enable(false);
86 } else {
87 m_serverStatusLabel->SetLabel("Stopped");
88 m_serverStatusLabel->SetForegroundColour(*wxRED);
89 m_startStopButton->SetLabel("Start");
90 m_portInput->Enable(true);
91 }
92
93 if (m_server->IsClientConnected()) {
94 m_clientStatusLabel->SetLabel("Connected");
95 m_clientStatusLabel->SetForegroundColour(wxColour(0, 128, 0));
96 m_clientAddressLabel->SetLabel(m_server->GetClientAddress());
97
98 wxString clientName = m_server->GetClientName();
99 m_clientNameLabel->SetLabel(clientName.empty() ? "(pending handshake)" : clientName);
100
101 m_disconnectButton->Enable(true);
102 } else {
103 m_clientStatusLabel->SetLabel("Not Connected");
104 m_clientStatusLabel->SetForegroundColour(wxColour(128, 128, 128));
105 m_clientAddressLabel->SetLabel("-");
106 m_clientNameLabel->SetLabel("-");
107 m_treeIdLabel->SetLabel("-");
108 m_disconnectButton->Enable(false);
109 }
110
111 wxWindow::Refresh();
112}
113
114void ConnectionPanel::OnStartStop(wxCommandEvent &event) {
115 if (m_server->IsRunning()) {
116 m_server->Stop();
117 } else {
118 long port = 12345;
119 m_portInput->GetValue().ToLong(&port);
120 if (port < 1 || port > 65535) {
121 wxMessageBox("Invalid port number. Please enter a value between 1 and 65535.", "Error",
122 wxOK | wxICON_ERROR);
123 return;
124 }
125 if (!m_server->Start(static_cast<uint16_t>(port))) {
126 wxMessageBox("Failed to start server. Port may be in use.", "Error", wxOK | wxICON_ERROR);
127 return;
128 }
129 }
130 UpdateStatus();
131}
132
133void ConnectionPanel::OnDisconnect(wxCommandEvent &event) {
134 m_server->DisconnectClient();
135 UpdateStatus();
136}
137
138void ConnectionPanel::SetTreeStatus(const wxString &status) {
139 m_treeIdLabel->SetLabel(status);
140 m_treeIdLabel->SetForegroundColour(wxColour(255, 165, 0));
141 wxWindow::Refresh();
142}
143
144void ConnectionPanel::SetTreeId(const wxString &treeId) {
145 m_treeIdLabel->SetLabel(treeId);
146 m_treeIdLabel->SetForegroundColour(wxColour(0, 128, 0));
147 wxWindow::Refresh();
148}
149
150} // namespace Monitor
151} // namespace Ember
BehaviorTreeProjectDialog::OnProjectNameChanged BehaviorTreeProjectDialog::OnRemoveFiles wxEND_EVENT_TABLE() BehaviorTreeProjectDialog
Base class for all panels with layout, theme, and state management.
Definition Panel.h:11
UI panel for managing TCP server connection, showing server/client status, port input,...
void OnStartStop(wxCommandEvent &event)
void SetTreeStatus(const wxString &status)
Sets the tree status text shown in the panel.
void SetTreeId(const wxString &treeId)
Sets the tree ID text shown in the panel.
void OnDisconnect(wxCommandEvent &event)
void UpdateStatus()
Refreshes server and client status display.
Single-client TCP server for receiving behavior tree data from monitored applications.
Definition TCPServer.h:14
Definition Panel.h:8
wxBEGIN_EVENT_TABLE(MonitorFrame, wxFrame) EVT_CLOSE(MonitorFrame
ConnectionPanel::OnStartStop EVT_BUTTON(ID_DISCONNECT, ConnectionPanel::OnDisconnect) wxEND_EVENT_TABLE() ConnectionPanel