Skip to content

Commit

Permalink
Refactor Connect and Disconnect out to CClient
Browse files Browse the repository at this point in the history
This is an extract from #2550
Co-authored-by: ann0see <[email protected]>
  • Loading branch information
pgScorpio authored and ann0see committed Sep 13, 2024
1 parent a1c0765 commit 0b6a2bb
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 79 deletions.
57 changes: 52 additions & 5 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,11 +773,8 @@ void CClient::OnHandledSignal ( int sigNum )
{
case SIGINT:
case SIGTERM:
// if connected, terminate connection (needed for headless mode)
if ( IsRunning() )
{
Stop();
}
// if connected, disconnect (needed for headless mode)
Disconnect();

// this should trigger OnAboutToQuit
QCoreApplication::instance()->exit();
Expand Down Expand Up @@ -908,6 +905,56 @@ void CClient::Stop()
SignalLevelMeter.Reset();
}

/// @method
/// @brief Connects to strServerAddress
/// @emit Connecting (strServerName) if the client wasn't running and SetServerAddr returned true.
/// @param strServerAddress - the server address to connect to
/// @param strServerName - the String argument to be passed to Connecting()
/// @result true if client wasn't running and SetServerAddr returned true, false otherwise
bool CClient::Connect ( QString strServerAddress, QString strServerName )
{
if ( !IsRunning() )
{
// Set server address and connect if valid address was supplied
if ( SetServerAddr ( strServerAddress ) )
{

Start();

emit Connecting ( strServerName );

return true;
}
}

return false;
}

/// @method
/// @brief Disconnects client
/// @emit Disconnected
/// @result true if client wasn't running, false otherwise
bool CClient::Disconnect()
{
if ( IsRunning() )
{
Stop();

emit Disconnected();

return true;
}
else
{
// make sure sound is stopped too
Sound.Stop();

emit Disconnected();

return false;
}
}

void CClient::Init()
{
// check if possible frame size factors are supported
Expand Down
5 changes: 5 additions & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class CClient : public QObject

void Start();
void Stop();
bool Connect ( QString strServerAddress, QString strServerName );
bool Disconnect();

bool IsRunning() { return Sound.IsRunning(); }
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); }
bool SetServerAddr ( QString strNAddr );
Expand Down Expand Up @@ -427,7 +430,9 @@ protected slots:

void CLChannelLevelListReceived ( CHostAddress InetAddr, CVector<uint16_t> vecLevelList );

void Connecting ( QString strServerName );
void Disconnected();

void SoundDeviceChanged ( QString strError );
void ControllerInFaderLevel ( int iChannelIdx, int iValue );
void ControllerInPanValue ( int iChannelIdx, int iValue );
Expand Down
116 changes: 45 additions & 71 deletions src/clientdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
{
// initiate connection (always show the address in the mixer board
// (no alias))
Connect ( strConnOnStartupAddress, strConnOnStartupAddress );

// initiate connection
// TODO: Refactor this for failing call on Connect()

pClient->Connect ( strConnOnStartupAddress, strConnOnStartupAddress );
// TODO: Find out why without this the mixer status issue still occurs.
OnConnect ( strConnOnStartupAddress );
}

// File menu --------------------------------------------------------------
Expand Down Expand Up @@ -473,7 +479,9 @@ 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::Connecting, this, &CClientDlg::OnConnect );

QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnect );

QObject::connect ( pClient, &CClient::ChatTextReceived, this, &CClientDlg::OnChatTextReceived );

Expand Down Expand Up @@ -608,11 +616,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();
Expand Down Expand Up @@ -730,16 +735,14 @@ 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() )
// initiate connection
// TODO: Refactor this for failing call on Connect()

if ( pClient->Connect ( strSelectedAddress, strMixerBoardLabel ) )
{
Disconnect();
OnConnect ( strMixerBoardLabel );
}

// initiate connection
Connect ( strSelectedAddress, strMixerBoardLabel );

// reset flag
bConnectDlgWasShown = false;
}
Expand All @@ -748,13 +751,10 @@ void CClientDlg::OnConnectDlgAccepted()
void CClientDlg::OnConnectDisconBut()
{
// the connect/disconnect button implements a toggle functionality
if ( pClient->IsRunning() )
{
Disconnect();
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
}
else
if ( !pClient->Disconnect() )
{
// If the client didn't disconnect, we assume that we weren't connected. Thus show the connect dialog
// TODO: Refactor to have robust error handling
ShowConnectionSetupDialog();
}
}
Expand Down Expand Up @@ -859,7 +859,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
Expand Down Expand Up @@ -1164,7 +1164,7 @@ void CClientDlg::OnSoundDeviceChanged ( QString strError )
// 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
Expand Down Expand Up @@ -1193,65 +1193,36 @@ 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()
void CClientDlg::OnDisconnect()
{
// 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();
}

// change connect button text to "connect"
butConnect->setText ( tr ( "C&onnect" ) );

Expand Down Expand Up @@ -1293,6 +1264,9 @@ void CClientDlg::Disconnect()

// clear mixer board (remove all faders)
MainMixerBoard->HideAll();

// Reset the deco
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
}

void CClientDlg::UpdateDisplay()
Expand Down
5 changes: 2 additions & 3 deletions src/clientdlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,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 );

Expand Down Expand Up @@ -127,6 +125,8 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase
CAnalyzerConsole AnalyzerConsole;

public slots:
void OnConnect ( const QString& strServerName );
void OnDisconnect();
void OnConnectDisconBut();
void OnTimerSigMet();
void OnTimerBuffersLED();
Expand Down Expand Up @@ -232,7 +232,6 @@ public slots:
}

void OnConnectDlgAccepted();
void OnDisconnected() { Disconnect(); }
void OnGUIDesignChanged();
void OnMeterStyleChanged();
void OnRecorderStateReceived ( ERecorderState eRecorderState );
Expand Down

0 comments on commit 0b6a2bb

Please sign in to comment.