Skip to content

Commit a1d98aa

Browse files
committed
Add intermediate refactoring
1 parent 0b6a2bb commit a1d98aa

File tree

6 files changed

+46
-37
lines changed

6 files changed

+46
-37
lines changed

src/channel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ EGetDataStat CChannel::GetData ( CVector<uint8_t>& vecbyData, const int iNumByte
654654
Protocol.Reset();
655655

656656
// emit message
657-
emit Disconnected();
657+
emit DisconnectClient();
658658
}
659659

660660
return eGetStatus;

src/channel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public slots:
280280
void LicenceRequired ( ELicenceType eLicenceType );
281281
void VersionAndOSReceived ( COSUtil::EOpSystemType eOSType, QString strVersion );
282282
void RecorderStateReceived ( ERecorderState eRecorderState );
283-
void Disconnected();
283+
void DisconnectClient();
284284

285285
void DetectedCLMessage ( CVector<uint8_t> vecbyMesBodyData, int iRecID, CHostAddress RecHostAddr );
286286

src/client.cpp

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ CClient::CClient ( const quint16 iPortNumber,
122122
QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::OnConClientListMesReceived );
123123
QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::ConClientListMesReceived );
124124

125-
QObject::connect ( &Channel, &CChannel::Disconnected, this, &CClient::Disconnected );
125+
QObject::connect ( &Channel, &CChannel::DisconnectClient, this, &CClient::Disconnect );
126126

127127
QObject::connect ( &Channel, &CChannel::NewConnection, this, &CClient::OnNewConnection );
128128

@@ -907,51 +907,48 @@ void CClient::Stop()
907907

908908
/// @method
909909
/// @brief Connects to strServerAddress
910-
/// @emit Connecting (strServerName) if the client wasn't running and SetServerAddr returned true.
910+
/// @emit Connecting (strServerName) if the client wasn't running and SetServerAddr was valid
911+
/// @emit ConnectingFailed (error) if an error occurred
911912
/// @param strServerAddress - the server address to connect to
912913
/// @param strServerName - the String argument to be passed to Connecting()
913-
/// @result true if client wasn't running and SetServerAddr returned true, false otherwise
914-
bool CClient::Connect ( QString strServerAddress, QString strServerName )
914+
void CClient::Connect ( QString strServerAddress, QString strServerName )
915915
{
916-
if ( !IsRunning() )
917-
{
918-
// Set server address and connect if valid address was supplied
919-
if ( SetServerAddr ( strServerAddress ) )
916+
try {
917+
if ( !IsRunning() )
920918
{
921-
922-
Start();
923-
924-
emit Connecting ( strServerName );
925-
926-
return true;
919+
// Set server address and connect if valid address was supplied
920+
if ( SetServerAddr ( strServerAddress ) )
921+
{
922+
Start();
923+
emit Connecting ( strServerName );
924+
}
925+
else {
926+
throw CGenErr ( "Received invalid server address. Please check for typos in the provided server address." );
927+
}
927928
}
928929
}
929-
930-
return false;
930+
catch ( const CGenErr& generr )
931+
{
932+
Disconnect();
933+
emit ConnectingFailed ( generr.GetErrorText() );
934+
}
931935
}
932936

933937
/// @method
934-
/// @brief Disconnects client
938+
/// @brief Disconnects client. If the client is not running, it just stops Sound
935939
/// @emit Disconnected
936-
/// @result true if client wasn't running, false otherwise
937-
bool CClient::Disconnect()
940+
void CClient::Disconnect()
938941
{
939942
if ( IsRunning() )
940943
{
941944
Stop();
942-
943945
emit Disconnected();
944-
945-
return true;
946946
}
947947
else
948948
{
949-
// make sure sound is stopped too
949+
// make sure sound is stopped in any case
950950
Sound.Stop();
951-
952951
emit Disconnected();
953-
954-
return false;
955952
}
956953
}
957954

src/client.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ class CClient : public QObject
121121

122122
void Start();
123123
void Stop();
124-
bool Connect ( QString strServerAddress, QString strServerName );
125-
bool Disconnect();
124+
void Connect ( QString strServerAddress, QString strServerName );
125+
void Disconnect();
126126

127127
bool IsRunning() { return Sound.IsRunning(); }
128128
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); }
@@ -390,7 +390,7 @@ protected slots:
390390
{
391391
if ( InetAddr == Channel.GetAddress() )
392392
{
393-
emit Disconnected();
393+
Disconnect();
394394
}
395395
}
396396
void OnCLPingReceived ( CHostAddress InetAddr, int iMs );
@@ -430,7 +430,10 @@ protected slots:
430430

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

433+
void ConnectClient ( QString strServerAddress );
433434
void Connecting ( QString strServerName );
435+
void ConnectingFailed ( QString errorMessage );
436+
void DisconnectClient();
434437
void Disconnected();
435438

436439
void SoundDeviceChanged ( QString strError );

src/clientdlg.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,8 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
481481

482482
QObject::connect ( pClient, &CClient::Connecting, this, &CClientDlg::OnConnect );
483483

484+
QObject::connect ( pClient, &CClient::ConnectingFailed, this, &CClientDlg::OnConnectingFailed );
485+
484486
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnect );
485487

486488
QObject::connect ( pClient, &CClient::ChatTextReceived, this, &CClientDlg::OnChatTextReceived );
@@ -738,10 +740,7 @@ void CClientDlg::OnConnectDlgAccepted()
738740
// initiate connection
739741
// TODO: Refactor this for failing call on Connect()
740742

741-
if ( pClient->Connect ( strSelectedAddress, strMixerBoardLabel ) )
742-
{
743-
OnConnect ( strMixerBoardLabel );
744-
}
743+
pClient->Connect ( strSelectedAddress, strMixerBoardLabel );
745744

746745
// reset flag
747746
bConnectDlgWasShown = false;
@@ -751,9 +750,13 @@ void CClientDlg::OnConnectDlgAccepted()
751750
void CClientDlg::OnConnectDisconBut()
752751
{
753752
// the connect/disconnect button implements a toggle functionality
754-
if ( !pClient->Disconnect() )
753+
if ( pClient->IsRunning() )
755754
{
756-
// If the client didn't disconnect, we assume that we weren't connected. Thus show the connect dialog
755+
pClient->Disconnect();
756+
}
757+
else
758+
{
759+
// If the client isn't running, we assume that we weren't connected. Thus show the connect dialog
757760
// TODO: Refactor to have robust error handling
758761
ShowConnectionSetupDialog();
759762
}
@@ -1221,6 +1224,11 @@ void CClientDlg::OnConnect ( const QString& strMixerBoardLabel )
12211224
}
12221225
}
12231226

1227+
void CClientDlg::OnConnectingFailed ( const QString& strError )
1228+
{
1229+
QMessageBox::critical ( this, APP_NAME, strError, "Close", nullptr );
1230+
}
1231+
12241232
void CClientDlg::OnDisconnect()
12251233
{
12261234
// change connect button text to "connect"

src/clientdlg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase
126126

127127
public slots:
128128
void OnConnect ( const QString& strServerName );
129+
void OnConnectingFailed ( const QString& strErrorText );
129130
void OnDisconnect();
130131
void OnConnectDisconBut();
131132
void OnTimerSigMet();

0 commit comments

Comments
 (0)