From a77dcfebb665e5e3160738979a279491eaf85141 Mon Sep 17 00:00:00 2001 From: pgScorpio Date: Fri, 13 Sep 2024 22:42:37 +0200 Subject: [PATCH] Refactor Connect out to CClient This is an extract from https://github.com/jamulussoftware/jamulus/pull/2550 Co-authored-by: ann0see <20726856+ann0see@users.noreply.github.com> --- src/client.cpp | 73 ++++++++++++++++++++++------ src/client.h | 19 +++++++- src/clientdlg.cpp | 118 ++++++++++++++++------------------------------ src/clientdlg.h | 7 ++- src/main.cpp | 32 ++++++------- 5 files changed, 134 insertions(+), 115 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index d3d2c51c9a..fea00807cd 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -27,7 +27,6 @@ /* Implementation *************************************************************/ CClient::CClient ( const quint16 iPortNumber, const quint16 iQosNumber, - const QString& strConnOnStartupAddress, const QString& strMIDISetup, const bool bNoAutoJackConnect, const QString& strNClientName, @@ -122,7 +121,7 @@ CClient::CClient ( const quint16 iPortNumber, QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::OnConClientListMesReceived ); QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::ConClientListMesReceived ); - QObject::connect ( &Channel, &CChannel::Disconnected, this, &CClient::Disconnected ); + QObject::connect ( &Channel, &CChannel::Disconnected, this, &CClient::Stop ); QObject::connect ( &Channel, &CChannel::NewConnection, this, &CClient::OnNewConnection ); @@ -184,13 +183,6 @@ CClient::CClient ( const quint16 iPortNumber, // start the socket (it is important to start the socket after all // initializations and connections) Socket.Start(); - - // do an immediate start if a server address is given - if ( !strConnOnStartupAddress.isEmpty() ) - { - SetServerAddr ( strConnOnStartupAddress ); - Start(); - } } CClient::~CClient() @@ -486,6 +478,9 @@ bool CClient::SetServerAddr ( QString strNAddr ) // apply address to the channel Channel.SetAddress ( HostAddress ); + // By default, set server name to HostAddress. If using the Connect() method, this may be overwritten + SetConnectedServerName ( HostAddress.toString() ); + return true; } else @@ -773,11 +768,8 @@ void CClient::OnHandledSignal ( int sigNum ) { case SIGINT: case SIGTERM: - // if connected, terminate connection (needed for headless mode) - if ( IsRunning() ) - { - Stop(); - } + // if connected, Stop client (needed for headless mode) + Stop(); // this should trigger OnAboutToQuit QCoreApplication::instance()->exit(); @@ -872,8 +864,14 @@ void CClient::Start() // start audio interface Sound.Start(); + + emit Connected ( GetConnectedServerName() ); } +/// @method +/// @brief Stops client and disconnects from server +/// @emit Disconnected +/// Use to set CClientDlg to show not being connected void CClient::Stop() { // stop audio interface @@ -906,6 +904,53 @@ void CClient::Stop() // reset current signal level and LEDs bJitterBufferOK = true; SignalLevelMeter.Reset(); + + // emit Disconnected() to inform UI of disconnection + emit Disconnected(); +} + +/// @method +/// @brief Stops the client if the client is running +/// @emit Disconnected +void CClient::Disconnect() +{ + if ( IsRunning() ) + { + Stop(); + } +} + +/// @method +/// @brief Connects to strServerAddress +/// @emit Connected (strServerName) if the client wasn't running and SetServerAddr was valid. emit happens through Start(). +/// Use to set CClientDlg to show being connected +/// @emit ConnectingFailed (error) if an error occurred +/// Use to display error message in CClientDlg +/// @param strServerAddress - the server address to connect to +/// @param strServerName - the String argument to be passed to Connecting() +void CClient::Connect ( QString strServerAddress, QString strServerName ) +{ + try + { + if ( !IsRunning() ) + { + // Set server address and connect if valid address was supplied + if ( SetServerAddr ( strServerAddress ) ) + { + SetConnectedServerName ( strServerName ); + Start(); + } + else + { + throw CGenErr ( tr ( "Received invalid server address. Please check for typos in the provided server address." ) ); + } + } + } + catch ( const CGenErr& generr ) + { + Stop(); + emit ConnectingFailed ( generr.GetErrorText() ); + } } void CClient::Init() diff --git a/src/client.h b/src/client.h index 3f9e30be6d..49873f9a70 100644 --- a/src/client.h +++ b/src/client.h @@ -111,7 +111,6 @@ class CClient : public QObject public: CClient ( const quint16 iPortNumber, const quint16 iQosNumber, - const QString& strConnOnStartupAddress, const QString& strMIDISetup, const bool bNoAutoJackConnect, const QString& strNClientName, @@ -122,6 +121,13 @@ class CClient : public QObject void Start(); void Stop(); + void Disconnect(); + void Connect ( QString strServerAddress, QString strServerName ); + + // The ConnectedServerName is emitted by Connected() to update the UI with a human readable server name + void SetConnectedServerName ( const QString strServerName ) { strConnectedServerName = strServerName; }; + QString GetConnectedServerName() const { return strConnectedServerName; }; + bool IsRunning() { return Sound.IsRunning(); } bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); } bool SetServerAddr ( QString strNAddr ); @@ -288,6 +294,10 @@ class CClient : public QObject int EvaluatePingMessage ( const int iMs ); void CreateServerJitterBufferMessage(); + // information for the connected server + + QString strConnectedServerName; + // only one channel is needed for client application CChannel Channel; CProtocol ConnLessProtocol; @@ -388,7 +398,8 @@ protected slots: { if ( InetAddr == Channel.GetAddress() ) { - emit Disconnected(); + // Stop client in case it received a Disconnection request + Stop(); } } void OnCLPingReceived ( CHostAddress InetAddr, int iMs ); @@ -428,7 +439,11 @@ protected slots: void CLChannelLevelListReceived ( CHostAddress InetAddr, CVector vecLevelList ); + void Connected ( QString strServerName ); + void ConnectingFailed ( QString errorMessage ); + void DisconnectClient(); void Disconnected(); + void SoundDeviceChanged ( QString strError ); void ControllerInFaderLevel ( int iChannelIdx, int iValue ); void ControllerInPanValue ( int iChannelIdx, int iValue ); diff --git a/src/clientdlg.cpp b/src/clientdlg.cpp index cb6b134bf7..8d60e2312b 100644 --- a/src/clientdlg.cpp +++ b/src/clientdlg.cpp @@ -27,7 +27,6 @@ /* Implementation *************************************************************/ CClientDlg::CClientDlg ( CClient* pNCliP, CClientSettings* pNSetP, - const QString& strConnOnStartupAddress, const QString& strMIDISetup, const bool bNewShowComplRegConnList, const bool bShowAnalyzerConsole, @@ -272,14 +271,6 @@ CClientDlg::CClientDlg ( CClient* pNCliP, TimerCheckAudioDeviceOk.setSingleShot ( true ); // only check once after connection TimerDetectFeedback.setSingleShot ( true ); - // Connect on startup ------------------------------------------------------ - if ( !strConnOnStartupAddress.isEmpty() ) - { - // initiate connection (always show the address in the mixer board - // (no alias)) - Connect ( strConnOnStartupAddress, strConnOnStartupAddress ); - } - // File menu -------------------------------------------------------------- QMenu* pFileMenu = new QMenu ( tr ( "&File" ), this ); @@ -483,7 +474,11 @@ CClientDlg::CClientDlg ( CClient* pNCliP, // other QObject::connect ( pClient, &CClient::ConClientListMesReceived, this, &CClientDlg::OnConClientListMesReceived ); - QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnected ); + QObject::connect ( pClient, &CClient::Connected, this, &CClientDlg::OnConnect ); + + QObject::connect ( pClient, &CClient::ConnectingFailed, this, &CClientDlg::OnConnectingFailed ); + + QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnect ); QObject::connect ( pClient, &CClient::ChatTextReceived, this, &CClientDlg::OnChatTextReceived ); @@ -618,11 +613,8 @@ void CClientDlg::closeEvent ( QCloseEvent* Event ) ConnectDlg.close(); AnalyzerConsole.close(); - // if connected, terminate connection - if ( pClient->IsRunning() ) - { - pClient->Stop(); - } + // Disconnect if needed + pClient->Disconnect(); // make sure all current fader settings are applied to the settings struct MainMixerBoard->StoreAllFaderSettings(); @@ -740,15 +732,11 @@ void CClientDlg::OnConnectDlgAccepted() } } - // first check if we are already connected, if this is the case we have to - // disconnect the old server first - if ( pClient->IsRunning() ) - { - Disconnect(); - } + // Disconnect the client. We could be currently connected. + pClient->Disconnect(); // initiate connection - Connect ( strSelectedAddress, strMixerBoardLabel ); + pClient->Connect ( strSelectedAddress, strMixerBoardLabel ); // reset flag bConnectDlgWasShown = false; @@ -760,11 +748,12 @@ void CClientDlg::OnConnectDisconBut() // the connect/disconnect button implements a toggle functionality if ( pClient->IsRunning() ) { - Disconnect(); - SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() ); + pClient->Stop(); } else { + // If the client isn't running, we assume that we weren't connected. Thus show the connect dialog + // TODO: Refactor to have robust error handling ShowConnectionSetupDialog(); } } @@ -869,7 +858,7 @@ void CClientDlg::OnLicenceRequired ( ELicenceType eLicenceType ) // disconnect from that server. if ( !LicenceDlg.exec() ) { - Disconnect(); + pClient->Disconnect(); } // unmute the client output stream if local mute button is not pressed @@ -1172,10 +1161,7 @@ void CClientDlg::OnSoundDeviceChanged ( QString strError ) if ( !strError.isEmpty() ) { // the sound device setup has a problem, disconnect any active connection - if ( pClient->IsRunning() ) - { - Disconnect(); - } + pClient->Disconnect(); // show the error message of the device setup QMessageBox::critical ( this, APP_NAME, strError, tr ( "Ok" ), nullptr ); @@ -1203,65 +1189,38 @@ void CClientDlg::OnCLPingTimeWithNumClientsReceived ( CHostAddress InetAddr, int ConnectDlg.SetPingTimeAndNumClientsResult ( InetAddr, iPingTime, iNumClients ); } -void CClientDlg::Connect ( const QString& strSelectedAddress, const QString& strMixerBoardLabel ) +void CClientDlg::OnConnect ( const QString& strMixerBoardLabel ) { - // set address and check if address is valid - if ( pClient->SetServerAddr ( strSelectedAddress ) ) - { - // try to start client, if error occurred, do not go in - // running state but show error message - try - { - if ( !pClient->IsRunning() ) - { - pClient->Start(); - } - } - - catch ( const CGenErr& generr ) - { - // show error message and return the function - QMessageBox::critical ( this, APP_NAME, generr.GetErrorText(), "Close", nullptr ); - return; - } - // hide label connect to server - lblConnectToServer->hide(); - lbrInputLevelL->setEnabled ( true ); - lbrInputLevelR->setEnabled ( true ); + // hide label connect to server + lblConnectToServer->hide(); + lbrInputLevelL->setEnabled ( true ); + lbrInputLevelR->setEnabled ( true ); - // change connect button text to "disconnect" - butConnect->setText ( tr ( "&Disconnect" ) ); + // change connect button text to "disconnect" + butConnect->setText ( tr ( "&Disconnect" ) ); - // set server name in audio mixer group box title - MainMixerBoard->SetServerName ( strMixerBoardLabel ); + // set server name in audio mixer group box title + MainMixerBoard->SetServerName ( strMixerBoardLabel ); - // start timer for level meter bar and ping time measurement - TimerSigMet.start ( LEVELMETER_UPDATE_TIME_MS ); - TimerBuffersLED.start ( BUFFER_LED_UPDATE_TIME_MS ); - TimerPing.start ( PING_UPDATE_TIME_MS ); - TimerCheckAudioDeviceOk.start ( CHECK_AUDIO_DEV_OK_TIME_MS ); // is single shot timer + // start timer for level meter bar and ping time measurement + TimerSigMet.start ( LEVELMETER_UPDATE_TIME_MS ); + TimerBuffersLED.start ( BUFFER_LED_UPDATE_TIME_MS ); + TimerPing.start ( PING_UPDATE_TIME_MS ); + TimerCheckAudioDeviceOk.start ( CHECK_AUDIO_DEV_OK_TIME_MS ); // is single shot timer - // audio feedback detection - if ( pSettings->bEnableFeedbackDetection ) - { - TimerDetectFeedback.start ( DETECT_FEEDBACK_TIME_MS ); // single shot timer - bDetectFeedback = true; - } + // audio feedback detection + if ( pSettings->bEnableFeedbackDetection ) + { + TimerDetectFeedback.start ( DETECT_FEEDBACK_TIME_MS ); // single shot timer + bDetectFeedback = true; } } -void CClientDlg::Disconnect() -{ - // only stop client if currently running, in case we received - // the stopped message, the client is already stopped but the - // connect/disconnect button and other GUI controls must be - // updated - if ( pClient->IsRunning() ) - { - pClient->Stop(); - } +void CClientDlg::OnConnectingFailed ( const QString& strError ) { QMessageBox::critical ( this, APP_NAME, strError, tr ( "Close" ), nullptr ); } +void CClientDlg::OnDisconnect() +{ // change connect button text to "connect" butConnect->setText ( tr ( "C&onnect" ) ); @@ -1303,6 +1262,9 @@ void CClientDlg::Disconnect() // clear mixer board (remove all faders) MainMixerBoard->HideAll(); + + // Reset the deco + SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() ); } void CClientDlg::UpdateDisplay() diff --git a/src/clientdlg.h b/src/clientdlg.h index 2a9062a58d..82c69150ec 100644 --- a/src/clientdlg.h +++ b/src/clientdlg.h @@ -76,7 +76,6 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase public: CClientDlg ( CClient* pNCliP, CClientSettings* pNSetP, - const QString& strConnOnStartupAddress, const QString& strMIDISetup, const bool bNewShowComplRegConnList, const bool bShowAnalyzerConsole, @@ -94,8 +93,6 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase void ShowAnalyzerConsole(); void UpdateAudioFaderSlider(); void UpdateRevSelection(); - void Connect ( const QString& strSelectedAddress, const QString& strMixerBoardLabel ); - void Disconnect(); void ManageDragNDrop ( QDropEvent* Event, const bool bCheckAccept ); void SetPingTime ( const int iPingTime, const int iOverallDelayMs, const CMultiColorLED::ELightColor eOverallDelayLEDColor ); @@ -127,6 +124,9 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase CAnalyzerConsole AnalyzerConsole; public slots: + void OnConnect ( const QString& strServerName ); + void OnConnectingFailed ( const QString& strErrorText ); + void OnDisconnect(); void OnConnectDisconBut(); void OnTimerSigMet(); void OnTimerBuffersLED(); @@ -233,7 +233,6 @@ public slots: } void OnConnectDlgAccepted(); - void OnDisconnected() { Disconnect(); } void OnGUIDesignChanged(); void OnMeterStyleChanged(); void OnRecorderStateReceived ( ERecorderState eRecorderState ); diff --git a/src/main.cpp b/src/main.cpp index 42d17a8dea..feeeffa1c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -919,14 +919,7 @@ int main ( int argc, char** argv ) { // Client: // actual client object - CClient Client ( iPortNumber, - iQosNumber, - strConnOnStartupAddress, - strMIDISetup, - bNoAutoJackConnect, - strClientName, - bEnableIPv6, - bMuteMeInPersonalMix ); + CClient Client ( iPortNumber, iQosNumber, strMIDISetup, bNoAutoJackConnect, strClientName, bEnableIPv6, bMuteMeInPersonalMix ); // load settings from init-file (command line options override) CClientSettings Settings ( &Client, strIniFileName ); @@ -950,19 +943,18 @@ int main ( int argc, char** argv ) } // GUI object - CClientDlg ClientDlg ( &Client, - &Settings, - strConnOnStartupAddress, - strMIDISetup, - bShowComplRegConnList, - bShowAnalyzerConsole, - bMuteStream, - bEnableIPv6, - nullptr ); + CClientDlg + ClientDlg ( &Client, &Settings, strMIDISetup, bShowComplRegConnList, bShowAnalyzerConsole, bMuteStream, bEnableIPv6, nullptr ); // show dialog ClientDlg.show(); + // Connect on startup if requested + if ( !strConnOnStartupAddress.isEmpty() ) + { + Client.Connect ( strConnOnStartupAddress, strConnOnStartupAddress ); + } + pApp->exec(); } else @@ -971,6 +963,12 @@ int main ( int argc, char** argv ) // only start application without using the GUI qInfo() << qUtf8Printable ( GetVersionAndNameStr ( false ) ); + // Connect on startup if requested + if ( !strConnOnStartupAddress.isEmpty() ) + { + Client.Connect ( strConnOnStartupAddress, strConnOnStartupAddress ); + } + pApp->exec(); } }