Skip to content

Commit

Permalink
Separate connection for monitor and commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kadekcipta committed Dec 25, 2016
1 parent 0fe4ea9 commit 90964c1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/mainframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ MainFrame::MainFrame(wxWindow* parent, wxWindowID id, const wxString& title, con
InitializeControls();
Center();
SetMinClientSize(wxSize(300, 350));
SetBackgroundColour(*wxRED);
}

void MainFrame::InitializeControls()
Expand All @@ -44,7 +45,7 @@ void MainFrame::InitializeControls()
((wxAuiNotebook*)m_mainTab)->SetArtProvider(tabArtProvider);

wxBoxSizer *sizerMain = new wxBoxSizer(wxVERTICAL);
sizerMain->Add(m_mainTab, 1, wxBOTTOM | wxEXPAND, 0);
sizerMain->Add(m_mainTab, 1, wxEXPAND | wxALL, 10);
SetSizer(sizerMain);
Layout();
}
Expand Down
42 changes: 35 additions & 7 deletions src/querypanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ MonitorThread::MonitorThread(ConnectionPanel *handler, const wxString& host, int

MonitorThread::~MonitorThread()
{
wxMessageOutputDebug().Printf("~MonitorThread()");

wxCriticalSectionLocker enter(m_handler->m_csection);
m_handler->m_monitorThread = NULL;
if (m_connection) {
Expand All @@ -52,9 +50,6 @@ MonitorThread::~MonitorThread()

wxThread::ExitCode MonitorThread::Entry()
{
wxMessageOutputDebug().Printf("MonitorThread::Entry()");
wxMessageOutputDebug().Printf("Connecting to %s:%d", m_connection->GetRemoteHost(), m_connection->GetRemotePort());

if (!m_connection->Connect()) {
// report error
auto evtError = new wxThreadEvent(wxEVT_MONITOR_ERROR);
Expand Down Expand Up @@ -148,7 +143,6 @@ ConnectionPanel::ConnectionPanel(wxWindow *parent,
wxTextCtrl *txtKeysSearch = new wxTextCtrl(keysSearchingPanel, ID_TEXT_KEY, wxEmptyString, wxDefaultPosition, wxSize(-1, 28), wxTE_PROCESS_ENTER, wxDefaultValidator);
wxButton *btnKeySearch = new wxButton(keysSearchingPanel, ID_COMMAND_FIND_KEYS, wxT("Find"), wxPoint(-1,-1), wxSize(-1, 28));
wxListBox *lboxKeys = new wxListBox(keysSearchingPanel, ID_LBOX_KEYS, wxDefaultPosition, wxDefaultSize);

wxBoxSizer *hboxKeysSearch = new wxBoxSizer(wxHORIZONTAL);
hboxKeysSearch->Add(txtKeysSearch, 1, wxEXPAND);
hboxKeysSearch->Add(btnKeySearch, 0);
Expand Down Expand Up @@ -219,6 +213,20 @@ ConnectionPanel::~ConnectionPanel()
delete m_rawCommandFont;
}

bool ConnectionPanel::tryConnect()
{
if (m_connection != NULL)
return m_connection->IsConnected();

m_connection = new RedisConnection(m_redisHost, m_redisPort, m_redisPassword);
if (!m_connection->Connect()) {
delete m_connection;
m_connection = NULL;
}

return m_connection->IsConnected();
}

void ConnectionPanel::UpdateServerStatusInfo(const wxString &status)
{
ServerInfoPanel *siPanel = (ServerInfoPanel*)FindWindow(ID_SERVER_INFO);
Expand Down Expand Up @@ -393,6 +401,9 @@ void ConnectionPanel::OnDisconnect(wxCommandEvent& evt)

void ConnectionPanel::ExecuteCommand(const wxString &command)
{
if (!tryConnect())
return;

if (!command.IsEmpty() && m_connection != NULL && m_connection->IsConnected())
{
RedisSimpleValue ret = m_connection->ExecuteCommand(command);
Expand All @@ -409,6 +420,9 @@ void ConnectionPanel::ExecuteCommand(const wxString &command)

void ConnectionPanel::FindKeys(const wxString& keyFilter)
{
if (!tryConnect())
return;

GetKeyListBox()->Clear();
if (m_connection)
{
Expand Down Expand Up @@ -446,11 +460,13 @@ void ConnectionPanel::Cleanup()
m_connection = NULL;
}

wxMessageOutputDebug().Printf("Cleanup");
}

void ConnectionPanel::EditKeyValue()
{
if (!tryConnect())
return;

wxString key = GetSelectedKey();
if (key != wxEmptyString && m_connection != NULL && m_connection->IsConnected())
{
Expand All @@ -464,6 +480,9 @@ void ConnectionPanel::EditKeyValue()

void ConnectionPanel::AddKeyValue()
{
if (!tryConnect())
return;

KeyValueEditorDialog dlg(this, wxT("Add Key-Value Pair"), wxEmptyString, RedisSimpleValue(wxEmptyString));
if (dlg.ShowModal() == wxID_OK && m_connection != NULL && m_connection->IsConnected())
{
Expand All @@ -473,6 +492,9 @@ void ConnectionPanel::AddKeyValue()

void ConnectionPanel::DeleteKey()
{
if (!tryConnect())
return;

wxMessageDialog dlg(this, wxT("Are you sure to delete key: '" + GetSelectedKey() + "'' ?"), wxT("Delete Key"), wxNO_DEFAULT | wxYES_NO | wxCANCEL | wxICON_ASTERISK);
if (dlg.ShowModal() == wxID_YES && m_connection != NULL && m_connection->IsConnected())
{
Expand All @@ -489,6 +511,9 @@ void ConnectionPanel::DeleteKey()

void ConnectionPanel::SelectDb()
{
if (!tryConnect())
return;

SelectDbdialog dlg(this, wxT("Select Database"), m_currentDb);
if (dlg.ShowModal() == wxID_OK && m_connection != NULL && m_connection->IsConnected())
{
Expand All @@ -503,6 +528,9 @@ void ConnectionPanel::SelectDb()

void ConnectionPanel::ExpireKey()
{
if (!tryConnect())
return;

wxNumberEntryDialog dlg(this, wxEmptyString, wxT("Please set the expiration time in seconds"), wxT("Set Key Expiration"), 10, 1, 2147483647);
if (dlg.ShowModal() == wxID_OK && m_connection != NULL && m_connection->IsConnected())
{
Expand Down
3 changes: 3 additions & 0 deletions src/querypanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class ConnectionPanel : public wxPanel

friend class MonitorThread;


bool tryConnect();

wxListBox *GetKeyListBox() const;
wxArrayString GetServerInfoGroupsFromConfig();
void PopulateServerInfoGroups(wxArrayString&);
Expand Down

0 comments on commit 90964c1

Please sign in to comment.