Skip to content

Commit 65e66fa

Browse files
pgScorpioann0see
andcommitted
Refactor Connect out to CClient
This is an extract from jamulussoftware#2550 Co-authored-by: ann0see <[email protected]>
1 parent fe88e3e commit 65e66fa

File tree

5 files changed

+133
-115
lines changed

5 files changed

+133
-115
lines changed

src/client.cpp

Lines changed: 59 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,
@@ -121,7 +120,7 @@ CClient::CClient ( const quint16 iPortNumber,
121120
// The first ConClientListMesReceived handler performs the necessary cleanup and has to run first:
122121
QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::OnConClientListMesReceived );
123122

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

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

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

195187
CClient::~CClient()
@@ -578,6 +570,9 @@ bool CClient::SetServerAddr ( QString strNAddr )
578570
// apply address to the channel
579571
Channel.SetAddress ( HostAddress );
580572

573+
// By default, set server name to HostAddress. If using the Connect() method, this may be overwritten
574+
SetConnectedServerName ( HostAddress.toString() );
575+
581576
return true;
582577
}
583578
else
@@ -865,11 +860,8 @@ void CClient::OnHandledSignal ( int sigNum )
865860
{
866861
case SIGINT:
867862
case SIGTERM:
868-
// if connected, terminate connection (needed for headless mode)
869-
if ( IsRunning() )
870-
{
871-
Stop();
872-
}
863+
// if connected, Stop client (needed for headless mode)
864+
Stop();
873865

874866
// this should trigger OnAboutToQuit
875867
QCoreApplication::instance()->exit();
@@ -979,8 +971,14 @@ void CClient::Start()
979971

980972
// start audio interface
981973
Sound.Start();
974+
975+
emit Connected ( GetConnectedServerName() );
982976
}
983977

978+
/// @method
979+
/// @brief Stops client and disconnects from server
980+
/// @emit Disconnected
981+
/// Use to set CClientDlg to show not being connected
984982
void CClient::Stop()
985983
{
986984
// stop audio interface
@@ -1013,6 +1011,53 @@ void CClient::Stop()
10131011
// reset current signal level and LEDs
10141012
bJitterBufferOK = true;
10151013
SignalLevelMeter.Reset();
1014+
1015+
// emit Disconnected() to inform UI of disconnection
1016+
emit Disconnected();
1017+
}
1018+
1019+
/// @method
1020+
/// @brief Stops the client if the client is running
1021+
/// @emit Disconnected
1022+
void CClient::Disconnect()
1023+
{
1024+
if ( IsRunning() )
1025+
{
1026+
Stop();
1027+
}
1028+
}
1029+
1030+
/// @method
1031+
/// @brief Connects to strServerAddress
1032+
/// @emit Connected (strServerName) if the client wasn't running and SetServerAddr was valid. emit happens through Start().
1033+
/// Use to set CClientDlg to show being connected
1034+
/// @emit ConnectingFailed (error) if an error occurred
1035+
/// Use to display error message in CClientDlg
1036+
/// @param strServerAddress - the server address to connect to
1037+
/// @param strServerName - the String argument to be passed to Connecting()
1038+
void CClient::Connect ( QString strServerAddress, QString strServerName )
1039+
{
1040+
try
1041+
{
1042+
if ( !IsRunning() )
1043+
{
1044+
// Set server address and connect if valid address was supplied
1045+
if ( SetServerAddr ( strServerAddress ) )
1046+
{
1047+
SetConnectedServerName ( strServerName );
1048+
Start();
1049+
}
1050+
else
1051+
{
1052+
throw CGenErr ( tr ( "Received invalid server address. Please check for typos in the provided server address." ) );
1053+
}
1054+
}
1055+
}
1056+
catch ( const CGenErr& generr )
1057+
{
1058+
Stop();
1059+
emit ConnectingFailed ( generr.GetErrorText() );
1060+
}
10161061
}
10171062

10181063
void CClient::Init()

src/client.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ class CClient : public QObject
126126
public:
127127
CClient ( const quint16 iPortNumber,
128128
const quint16 iQosNumber,
129-
const QString& strConnOnStartupAddress,
130129
const QString& strMIDISetup,
131130
const bool bNoAutoJackConnect,
132131
const QString& strNClientName,
@@ -137,6 +136,13 @@ class CClient : public QObject
137136

138137
void Start();
139138
void Stop();
139+
void Disconnect();
140+
void Connect ( QString strServerAddress, QString strServerName );
141+
142+
// The ConnectedServerName is emitted by Connected() to update the UI with a human readable server name
143+
void SetConnectedServerName ( const QString strServerName ) { strConnectedServerName = strServerName; };
144+
QString GetConnectedServerName() const { return strConnectedServerName; };
145+
140146
bool IsRunning() { return Sound.IsRunning(); }
141147
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); }
142148
bool SetServerAddr ( QString strNAddr );
@@ -306,6 +312,9 @@ class CClient : public QObject
306312
void FreeClientChannel ( const int iServerChannelID );
307313
int FindClientChannel ( const int iServerChannelID, const bool bCreateIfNew ); // returns a client channel ID or INVALID_INDEX
308314
bool ReorderLevelList ( CVector<uint16_t>& vecLevelList ); // modifies vecLevelList, passed by reference
315+
// information for the connected server
316+
317+
QString strConnectedServerName;
309318

310319
// only one channel is needed for client application
311320
CChannel Channel;
@@ -417,7 +426,8 @@ protected slots:
417426
{
418427
if ( InetAddr == Channel.GetAddress() )
419428
{
420-
emit Disconnected();
429+
// Stop client in case it received a Disconnection request
430+
Stop();
421431
}
422432
}
423433
void OnCLPingReceived ( CHostAddress InetAddr, int iMs );
@@ -459,7 +469,11 @@ protected slots:
459469

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

472+
void Connected ( QString strServerName );
473+
void ConnectingFailed ( QString errorMessage );
474+
void DisconnectClient();
462475
void Disconnected();
476+
463477
void SoundDeviceChanged ( QString strError );
464478
void ControllerInFaderLevel ( int iChannelIdx, int iValue );
465479
void ControllerInPanValue ( int iChannelIdx, int iValue );

src/clientdlg.cpp

Lines changed: 40 additions & 78 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

@@ -481,7 +472,11 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
481472
// other
482473
QObject::connect ( pClient, &CClient::ConClientListMesReceived, this, &CClientDlg::OnConClientListMesReceived );
483474

484-
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnected );
475+
QObject::connect ( pClient, &CClient::Connected, this, &CClientDlg::OnConnect );
476+
477+
QObject::connect ( pClient, &CClient::ConnectingFailed, this, &CClientDlg::OnConnectingFailed );
478+
479+
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnect );
485480

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

@@ -616,11 +611,8 @@ void CClientDlg::closeEvent ( QCloseEvent* Event )
616611
ConnectDlg.close();
617612
AnalyzerConsole.close();
618613

619-
// if connected, terminate connection
620-
if ( pClient->IsRunning() )
621-
{
622-
pClient->Stop();
623-
}
614+
// Disconnect if needed
615+
pClient->Disconnect();
624616

625617
// make sure all current fader settings are applied to the settings struct
626618
MainMixerBoard->StoreAllFaderSettings();
@@ -738,15 +730,11 @@ void CClientDlg::OnConnectDlgAccepted()
738730
}
739731
}
740732

741-
// first check if we are already connected, if this is the case we have to
742-
// disconnect the old server first
743-
if ( pClient->IsRunning() )
744-
{
745-
Disconnect();
746-
}
733+
// Disconnect the client. We could be currently connected.
734+
pClient->Disconnect();
747735

748736
// initiate connection
749-
Connect ( strSelectedAddress, strMixerBoardLabel );
737+
pClient->Connect ( strSelectedAddress, strMixerBoardLabel );
750738

751739
// reset flag
752740
bConnectDlgWasShown = false;
@@ -758,11 +746,12 @@ void CClientDlg::OnConnectDisconBut()
758746
// the connect/disconnect button implements a toggle functionality
759747
if ( pClient->IsRunning() )
760748
{
761-
Disconnect();
762-
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
749+
pClient->Stop();
763750
}
764751
else
765752
{
753+
// If the client isn't running, we assume that we weren't connected. Thus show the connect dialog
754+
// TODO: Refactor to have robust error handling
766755
ShowConnectionSetupDialog();
767756
}
768757
}
@@ -870,7 +859,7 @@ void CClientDlg::OnLicenceRequired ( ELicenceType eLicenceType )
870859
// disconnect from that server.
871860
if ( !LicenceDlg.exec() )
872861
{
873-
Disconnect();
862+
pClient->Disconnect();
874863
}
875864

876865
// unmute the client output stream if local mute button is not pressed
@@ -1173,10 +1162,7 @@ void CClientDlg::OnSoundDeviceChanged ( QString strError )
11731162
if ( !strError.isEmpty() )
11741163
{
11751164
// the sound device setup has a problem, disconnect any active connection
1176-
if ( pClient->IsRunning() )
1177-
{
1178-
Disconnect();
1179-
}
1165+
pClient->Disconnect();
11801166

11811167
// show the error message of the device setup
11821168
QMessageBox::critical ( this, APP_NAME, strError, tr ( "Ok" ), nullptr );
@@ -1204,65 +1190,38 @@ void CClientDlg::OnCLPingTimeWithNumClientsReceived ( CHostAddress InetAddr, int
12041190
ConnectDlg.SetPingTimeAndNumClientsResult ( InetAddr, iPingTime, iNumClients );
12051191
}
12061192

1207-
void CClientDlg::Connect ( const QString& strSelectedAddress, const QString& strMixerBoardLabel )
1193+
void CClientDlg::OnConnect ( const QString& strMixerBoardLabel )
12081194
{
1209-
// set address and check if address is valid
1210-
if ( pClient->SetServerAddr ( strSelectedAddress ) )
1211-
{
1212-
// try to start client, if error occurred, do not go in
1213-
// running state but show error message
1214-
try
1215-
{
1216-
if ( !pClient->IsRunning() )
1217-
{
1218-
pClient->Start();
1219-
}
1220-
}
1221-
1222-
catch ( const CGenErr& generr )
1223-
{
1224-
// show error message and return the function
1225-
QMessageBox::critical ( this, APP_NAME, generr.GetErrorText(), "Close", nullptr );
1226-
return;
1227-
}
12281195

1229-
// hide label connect to server
1230-
lblConnectToServer->hide();
1231-
lbrInputLevelL->setEnabled ( true );
1232-
lbrInputLevelR->setEnabled ( true );
1196+
// hide label connect to server
1197+
lblConnectToServer->hide();
1198+
lbrInputLevelL->setEnabled ( true );
1199+
lbrInputLevelR->setEnabled ( true );
12331200

1234-
// change connect button text to "disconnect"
1235-
butConnect->setText ( tr ( "&Disconnect" ) );
1201+
// change connect button text to "disconnect"
1202+
butConnect->setText ( tr ( "&Disconnect" ) );
12361203

1237-
// set server name in audio mixer group box title
1238-
MainMixerBoard->SetServerName ( strMixerBoardLabel );
1204+
// set server name in audio mixer group box title
1205+
MainMixerBoard->SetServerName ( strMixerBoardLabel );
12391206

1240-
// start timer for level meter bar and ping time measurement
1241-
TimerSigMet.start ( LEVELMETER_UPDATE_TIME_MS );
1242-
TimerBuffersLED.start ( BUFFER_LED_UPDATE_TIME_MS );
1243-
TimerPing.start ( PING_UPDATE_TIME_MS );
1244-
TimerCheckAudioDeviceOk.start ( CHECK_AUDIO_DEV_OK_TIME_MS ); // is single shot timer
1207+
// start timer for level meter bar and ping time measurement
1208+
TimerSigMet.start ( LEVELMETER_UPDATE_TIME_MS );
1209+
TimerBuffersLED.start ( BUFFER_LED_UPDATE_TIME_MS );
1210+
TimerPing.start ( PING_UPDATE_TIME_MS );
1211+
TimerCheckAudioDeviceOk.start ( CHECK_AUDIO_DEV_OK_TIME_MS ); // is single shot timer
12451212

1246-
// audio feedback detection
1247-
if ( pSettings->bEnableFeedbackDetection )
1248-
{
1249-
TimerDetectFeedback.start ( DETECT_FEEDBACK_TIME_MS ); // single shot timer
1250-
bDetectFeedback = true;
1251-
}
1213+
// audio feedback detection
1214+
if ( pSettings->bEnableFeedbackDetection )
1215+
{
1216+
TimerDetectFeedback.start ( DETECT_FEEDBACK_TIME_MS ); // single shot timer
1217+
bDetectFeedback = true;
12521218
}
12531219
}
12541220

1255-
void CClientDlg::Disconnect()
1256-
{
1257-
// only stop client if currently running, in case we received
1258-
// the stopped message, the client is already stopped but the
1259-
// connect/disconnect button and other GUI controls must be
1260-
// updated
1261-
if ( pClient->IsRunning() )
1262-
{
1263-
pClient->Stop();
1264-
}
1221+
void CClientDlg::OnConnectingFailed ( const QString& strError ) { QMessageBox::critical ( this, APP_NAME, strError, tr ( "Close" ), nullptr ); }
12651222

1223+
void CClientDlg::OnDisconnect()
1224+
{
12661225
// change connect button text to "connect"
12671226
butConnect->setText ( tr ( "C&onnect" ) );
12681227

@@ -1304,6 +1263,9 @@ void CClientDlg::Disconnect()
13041263

13051264
// clear mixer board (remove all faders)
13061265
MainMixerBoard->HideAll();
1266+
1267+
// Reset the deco
1268+
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
13071269
}
13081270

13091271
void CClientDlg::UpdateDisplay()

0 commit comments

Comments
 (0)