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 16, 2024
1 parent 97388ab commit 596e42b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 97 deletions.
55 changes: 41 additions & 14 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -773,11 +765,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();
Expand Down Expand Up @@ -874,6 +863,10 @@ void CClient::Start()
Sound.Start();
}

/// @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
Expand Down Expand Up @@ -906,6 +899,40 @@ void CClient::Stop()
// reset current signal level and LEDs
bJitterBufferOK = true;
SignalLevelMeter.Reset();

// emit Disconnected() to inform UI of disconnection
emit Disconnected();
}

/// @method
/// @brief Connects to strServerAddress
/// @emit Connecting (strServerName) if the client wasn't running and SetServerAddr was valid.
/// 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 ) )
{
Start();
emit Connecting ( strServerName );
}
else {
throw CGenErr ( "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()
Expand Down
11 changes: 9 additions & 2 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,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,
Expand All @@ -121,6 +120,8 @@ class CClient : public QObject

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

bool IsRunning() { return Sound.IsRunning(); }
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); }
bool SetServerAddr ( QString strNAddr );
Expand Down Expand Up @@ -387,7 +388,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 );
Expand Down Expand Up @@ -427,7 +429,12 @@ protected slots:

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

void ConnectClient ( QString strServerAddress );
void Connecting ( 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 );
Expand Down
116 changes: 41 additions & 75 deletions src/clientdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
/* Implementation *************************************************************/
CClientDlg::CClientDlg ( CClient* pNCliP,
CClientSettings* pNSetP,
const QString& strConnOnStartupAddress,
const QString& strMIDISetup,
const bool bNewShowComplRegConnList,
const bool bShowAnalyzerConsole,
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -473,7 +464,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::Connecting, 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 );

Expand Down Expand Up @@ -608,11 +603,8 @@ void CClientDlg::closeEvent ( QCloseEvent* Event )
ConnectDlg.close();
AnalyzerConsole.close();

// if connected, terminate connection
if ( pClient->IsRunning() )
{
pClient->Stop();
}
// Disconnect if needed
pClient->Stop();

// make sure all current fader settings are applied to the settings struct
MainMixerBoard->StoreAllFaderSettings();
Expand Down Expand Up @@ -730,15 +722,9 @@ 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();
}

// initiate connection
Connect ( strSelectedAddress, strMixerBoardLabel );

pClient->Connect ( strSelectedAddress, strMixerBoardLabel );

// reset flag
bConnectDlgWasShown = false;
Expand All @@ -750,11 +736,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();
}
}
Expand Down Expand Up @@ -859,7 +846,7 @@ void CClientDlg::OnLicenceRequired ( ELicenceType eLicenceType )
// disconnect from that server.
if ( !LicenceDlg.exec() )
{
Disconnect();
pClient->Stop();
}

// unmute the client output stream if local mute button is not pressed
Expand Down Expand Up @@ -1164,7 +1151,7 @@ void CClientDlg::OnSoundDeviceChanged ( QString strError )
// the sound device setup has a problem, disconnect any active connection
if ( pClient->IsRunning() )
{
Disconnect();
pClient->Stop();
}

// show the error message of the device setup
Expand Down Expand Up @@ -1193,65 +1180,41 @@ 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::OnConnectingFailed ( const QString& strError )
{
// 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();
}
QMessageBox::critical ( this, APP_NAME, strError, "Close", nullptr );
}

void CClientDlg::OnDisconnect()
{
// change connect button text to "connect"
butConnect->setText ( tr ( "C&onnect" ) );

Expand Down Expand Up @@ -1293,6 +1256,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
7 changes: 3 additions & 4 deletions src/clientdlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 );

Expand Down Expand Up @@ -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();
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
Loading

0 comments on commit 596e42b

Please sign in to comment.