Skip to content

Commit 596e42b

Browse files
pgScorpioann0see
andcommitted
Refactor Connect and Disconnect out to CClient
This is an extract from jamulussoftware#2550 Co-authored-by: ann0see <[email protected]>
1 parent 97388ab commit 596e42b

File tree

5 files changed

+106
-97
lines changed

5 files changed

+106
-97
lines changed

src/client.cpp

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
/* Implementation *************************************************************/
2828
CClient::CClient ( const quint16 iPortNumber,
2929
const quint16 iQosNumber,
30-
const QString& strConnOnStartupAddress,
3130
const QString& strMIDISetup,
3231
const bool bNoAutoJackConnect,
3332
const QString& strNClientName,
@@ -122,7 +121,7 @@ CClient::CClient ( const quint16 iPortNumber,
122121
QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::OnConClientListMesReceived );
123122
QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::ConClientListMesReceived );
124123

125-
QObject::connect ( &Channel, &CChannel::Disconnected, this, &CClient::Disconnected );
124+
QObject::connect ( &Channel, &CChannel::Disconnected, this, &CClient::Stop );
126125

127126
QObject::connect ( &Channel, &CChannel::NewConnection, this, &CClient::OnNewConnection );
128127

@@ -184,13 +183,6 @@ CClient::CClient ( const quint16 iPortNumber,
184183
// start the socket (it is important to start the socket after all
185184
// initializations and connections)
186185
Socket.Start();
187-
188-
// do an immediate start if a server address is given
189-
if ( !strConnOnStartupAddress.isEmpty() )
190-
{
191-
SetServerAddr ( strConnOnStartupAddress );
192-
Start();
193-
}
194186
}
195187

196188
CClient::~CClient()
@@ -773,11 +765,8 @@ void CClient::OnHandledSignal ( int sigNum )
773765
{
774766
case SIGINT:
775767
case SIGTERM:
776-
// if connected, terminate connection (needed for headless mode)
777-
if ( IsRunning() )
778-
{
779-
Stop();
780-
}
768+
// if connected, Stop client (needed for headless mode)
769+
Stop();
781770

782771
// this should trigger OnAboutToQuit
783772
QCoreApplication::instance()->exit();
@@ -874,6 +863,10 @@ void CClient::Start()
874863
Sound.Start();
875864
}
876865

866+
/// @method
867+
/// @brief Stops client and disconnects from server
868+
/// @emit Disconnected
869+
/// Use to set CClientDlg to show not being connected
877870
void CClient::Stop()
878871
{
879872
// stop audio interface
@@ -906,6 +899,40 @@ void CClient::Stop()
906899
// reset current signal level and LEDs
907900
bJitterBufferOK = true;
908901
SignalLevelMeter.Reset();
902+
903+
// emit Disconnected() to inform UI of disconnection
904+
emit Disconnected();
905+
}
906+
907+
/// @method
908+
/// @brief Connects to strServerAddress
909+
/// @emit Connecting (strServerName) if the client wasn't running and SetServerAddr was valid.
910+
/// Use to set CClientDlg to show being connected
911+
/// @emit ConnectingFailed (error) if an error occurred
912+
/// Use to display error message in CClientDlg
913+
/// @param strServerAddress - the server address to connect to
914+
/// @param strServerName - the String argument to be passed to Connecting()
915+
void CClient::Connect ( QString strServerAddress, QString strServerName )
916+
{
917+
try {
918+
if ( !IsRunning() )
919+
{
920+
// Set server address and connect if valid address was supplied
921+
if ( SetServerAddr ( strServerAddress ) )
922+
{
923+
Start();
924+
emit Connecting ( strServerName );
925+
}
926+
else {
927+
throw CGenErr ( "Received invalid server address. Please check for typos in the provided server address." );
928+
}
929+
}
930+
}
931+
catch ( const CGenErr& generr )
932+
{
933+
Stop();
934+
emit ConnectingFailed ( generr.GetErrorText() );
935+
}
909936
}
910937

911938
void CClient::Init()

src/client.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ class CClient : public QObject
110110
public:
111111
CClient ( const quint16 iPortNumber,
112112
const quint16 iQosNumber,
113-
const QString& strConnOnStartupAddress,
114113
const QString& strMIDISetup,
115114
const bool bNoAutoJackConnect,
116115
const QString& strNClientName,
@@ -121,6 +120,8 @@ class CClient : public QObject
121120

122121
void Start();
123122
void Stop();
123+
void Connect ( QString strServerAddress, QString strServerName );
124+
124125
bool IsRunning() { return Sound.IsRunning(); }
125126
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); }
126127
bool SetServerAddr ( QString strNAddr );
@@ -387,7 +388,8 @@ protected slots:
387388
{
388389
if ( InetAddr == Channel.GetAddress() )
389390
{
390-
emit Disconnected();
391+
// Stop client in case it received a Disconnection request
392+
Stop();
391393
}
392394
}
393395
void OnCLPingReceived ( CHostAddress InetAddr, int iMs );
@@ -427,7 +429,12 @@ protected slots:
427429

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

432+
void ConnectClient ( QString strServerAddress );
433+
void Connecting ( QString strServerName );
434+
void ConnectingFailed ( QString errorMessage );
435+
void DisconnectClient();
430436
void Disconnected();
437+
431438
void SoundDeviceChanged ( QString strError );
432439
void ControllerInFaderLevel ( int iChannelIdx, int iValue );
433440
void ControllerInPanValue ( int iChannelIdx, int iValue );

src/clientdlg.cpp

Lines changed: 41 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
/* Implementation *************************************************************/
2828
CClientDlg::CClientDlg ( CClient* pNCliP,
2929
CClientSettings* pNSetP,
30-
const QString& strConnOnStartupAddress,
3130
const QString& strMIDISetup,
3231
const bool bNewShowComplRegConnList,
3332
const bool bShowAnalyzerConsole,
@@ -272,14 +271,6 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
272271
TimerCheckAudioDeviceOk.setSingleShot ( true ); // only check once after connection
273272
TimerDetectFeedback.setSingleShot ( true );
274273

275-
// Connect on startup ------------------------------------------------------
276-
if ( !strConnOnStartupAddress.isEmpty() )
277-
{
278-
// initiate connection (always show the address in the mixer board
279-
// (no alias))
280-
Connect ( strConnOnStartupAddress, strConnOnStartupAddress );
281-
}
282-
283274
// File menu --------------------------------------------------------------
284275
QMenu* pFileMenu = new QMenu ( tr ( "&File" ), this );
285276

@@ -473,7 +464,11 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
473464
// other
474465
QObject::connect ( pClient, &CClient::ConClientListMesReceived, this, &CClientDlg::OnConClientListMesReceived );
475466

476-
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnected );
467+
QObject::connect ( pClient, &CClient::Connecting, this, &CClientDlg::OnConnect );
468+
469+
QObject::connect ( pClient, &CClient::ConnectingFailed, this, &CClientDlg::OnConnectingFailed );
470+
471+
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnect );
477472

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

@@ -608,11 +603,8 @@ void CClientDlg::closeEvent ( QCloseEvent* Event )
608603
ConnectDlg.close();
609604
AnalyzerConsole.close();
610605

611-
// if connected, terminate connection
612-
if ( pClient->IsRunning() )
613-
{
614-
pClient->Stop();
615-
}
606+
// Disconnect if needed
607+
pClient->Stop();
616608

617609
// make sure all current fader settings are applied to the settings struct
618610
MainMixerBoard->StoreAllFaderSettings();
@@ -730,15 +722,9 @@ void CClientDlg::OnConnectDlgAccepted()
730722
}
731723
}
732724

733-
// first check if we are already connected, if this is the case we have to
734-
// disconnect the old server first
735-
if ( pClient->IsRunning() )
736-
{
737-
Disconnect();
738-
}
739-
740725
// initiate connection
741-
Connect ( strSelectedAddress, strMixerBoardLabel );
726+
727+
pClient->Connect ( strSelectedAddress, strMixerBoardLabel );
742728

743729
// reset flag
744730
bConnectDlgWasShown = false;
@@ -750,11 +736,12 @@ void CClientDlg::OnConnectDisconBut()
750736
// the connect/disconnect button implements a toggle functionality
751737
if ( pClient->IsRunning() )
752738
{
753-
Disconnect();
754-
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
739+
pClient->Stop();
755740
}
756741
else
757742
{
743+
// If the client isn't running, we assume that we weren't connected. Thus show the connect dialog
744+
// TODO: Refactor to have robust error handling
758745
ShowConnectionSetupDialog();
759746
}
760747
}
@@ -859,7 +846,7 @@ void CClientDlg::OnLicenceRequired ( ELicenceType eLicenceType )
859846
// disconnect from that server.
860847
if ( !LicenceDlg.exec() )
861848
{
862-
Disconnect();
849+
pClient->Stop();
863850
}
864851

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

11701157
// show the error message of the device setup
@@ -1193,65 +1180,41 @@ void CClientDlg::OnCLPingTimeWithNumClientsReceived ( CHostAddress InetAddr, int
11931180
ConnectDlg.SetPingTimeAndNumClientsResult ( InetAddr, iPingTime, iNumClients );
11941181
}
11951182

1196-
void CClientDlg::Connect ( const QString& strSelectedAddress, const QString& strMixerBoardLabel )
1183+
void CClientDlg::OnConnect ( const QString& strMixerBoardLabel )
11971184
{
1198-
// set address and check if address is valid
1199-
if ( pClient->SetServerAddr ( strSelectedAddress ) )
1200-
{
1201-
// try to start client, if error occurred, do not go in
1202-
// running state but show error message
1203-
try
1204-
{
1205-
if ( !pClient->IsRunning() )
1206-
{
1207-
pClient->Start();
1208-
}
1209-
}
12101185

1211-
catch ( const CGenErr& generr )
1212-
{
1213-
// show error message and return the function
1214-
QMessageBox::critical ( this, APP_NAME, generr.GetErrorText(), "Close", nullptr );
1215-
return;
1216-
}
1186+
// hide label connect to server
1187+
lblConnectToServer->hide();
1188+
lbrInputLevelL->setEnabled ( true );
1189+
lbrInputLevelR->setEnabled ( true );
12171190

1218-
// hide label connect to server
1219-
lblConnectToServer->hide();
1220-
lbrInputLevelL->setEnabled ( true );
1221-
lbrInputLevelR->setEnabled ( true );
1191+
// change connect button text to "disconnect"
1192+
butConnect->setText ( tr ( "&Disconnect" ) );
12221193

1223-
// change connect button text to "disconnect"
1224-
butConnect->setText ( tr ( "&Disconnect" ) );
1194+
// set server name in audio mixer group box title
1195+
MainMixerBoard->SetServerName ( strMixerBoardLabel );
12251196

1226-
// set server name in audio mixer group box title
1227-
MainMixerBoard->SetServerName ( strMixerBoardLabel );
1197+
// start timer for level meter bar and ping time measurement
1198+
TimerSigMet.start ( LEVELMETER_UPDATE_TIME_MS );
1199+
TimerBuffersLED.start ( BUFFER_LED_UPDATE_TIME_MS );
1200+
TimerPing.start ( PING_UPDATE_TIME_MS );
1201+
TimerCheckAudioDeviceOk.start ( CHECK_AUDIO_DEV_OK_TIME_MS ); // is single shot timer
12281202

1229-
// start timer for level meter bar and ping time measurement
1230-
TimerSigMet.start ( LEVELMETER_UPDATE_TIME_MS );
1231-
TimerBuffersLED.start ( BUFFER_LED_UPDATE_TIME_MS );
1232-
TimerPing.start ( PING_UPDATE_TIME_MS );
1233-
TimerCheckAudioDeviceOk.start ( CHECK_AUDIO_DEV_OK_TIME_MS ); // is single shot timer
1234-
1235-
// audio feedback detection
1236-
if ( pSettings->bEnableFeedbackDetection )
1237-
{
1238-
TimerDetectFeedback.start ( DETECT_FEEDBACK_TIME_MS ); // single shot timer
1239-
bDetectFeedback = true;
1240-
}
1203+
// audio feedback detection
1204+
if ( pSettings->bEnableFeedbackDetection )
1205+
{
1206+
TimerDetectFeedback.start ( DETECT_FEEDBACK_TIME_MS ); // single shot timer
1207+
bDetectFeedback = true;
12411208
}
12421209
}
12431210

1244-
void CClientDlg::Disconnect()
1211+
void CClientDlg::OnConnectingFailed ( const QString& strError )
12451212
{
1246-
// only stop client if currently running, in case we received
1247-
// the stopped message, the client is already stopped but the
1248-
// connect/disconnect button and other GUI controls must be
1249-
// updated
1250-
if ( pClient->IsRunning() )
1251-
{
1252-
pClient->Stop();
1253-
}
1213+
QMessageBox::critical ( this, APP_NAME, strError, "Close", nullptr );
1214+
}
12541215

1216+
void CClientDlg::OnDisconnect()
1217+
{
12551218
// change connect button text to "connect"
12561219
butConnect->setText ( tr ( "C&onnect" ) );
12571220

@@ -1293,6 +1256,9 @@ void CClientDlg::Disconnect()
12931256

12941257
// clear mixer board (remove all faders)
12951258
MainMixerBoard->HideAll();
1259+
1260+
// Reset the deco
1261+
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
12961262
}
12971263

12981264
void CClientDlg::UpdateDisplay()

src/clientdlg.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase
7676
public:
7777
CClientDlg ( CClient* pNCliP,
7878
CClientSettings* pNSetP,
79-
const QString& strConnOnStartupAddress,
8079
const QString& strMIDISetup,
8180
const bool bNewShowComplRegConnList,
8281
const bool bShowAnalyzerConsole,
@@ -94,8 +93,6 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase
9493
void ShowAnalyzerConsole();
9594
void UpdateAudioFaderSlider();
9695
void UpdateRevSelection();
97-
void Connect ( const QString& strSelectedAddress, const QString& strMixerBoardLabel );
98-
void Disconnect();
9996
void ManageDragNDrop ( QDropEvent* Event, const bool bCheckAccept );
10097
void SetPingTime ( const int iPingTime, const int iOverallDelayMs, const CMultiColorLED::ELightColor eOverallDelayLEDColor );
10198

@@ -127,6 +124,9 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase
127124
CAnalyzerConsole AnalyzerConsole;
128125

129126
public slots:
127+
void OnConnect ( const QString& strServerName );
128+
void OnConnectingFailed ( const QString& strErrorText );
129+
void OnDisconnect();
130130
void OnConnectDisconBut();
131131
void OnTimerSigMet();
132132
void OnTimerBuffersLED();
@@ -232,7 +232,6 @@ public slots:
232232
}
233233

234234
void OnConnectDlgAccepted();
235-
void OnDisconnected() { Disconnect(); }
236235
void OnGUIDesignChanged();
237236
void OnMeterStyleChanged();
238237
void OnRecorderStateReceived ( ERecorderState eRecorderState );

0 commit comments

Comments
 (0)