Skip to content

Commit

Permalink
Add intermediate refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ann0see committed Sep 15, 2024
1 parent 0b6a2bb commit 4ec9c68
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ EGetDataStat CChannel::GetData ( CVector<uint8_t>& vecbyData, const int iNumByte
Protocol.Reset();

// emit message
emit Disconnected();
emit DisconnectClient();
}

return eGetStatus;
Expand Down
2 changes: 1 addition & 1 deletion src/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public slots:
void LicenceRequired ( ELicenceType eLicenceType );
void VersionAndOSReceived ( COSUtil::EOpSystemType eOSType, QString strVersion );
void RecorderStateReceived ( ERecorderState eRecorderState );
void Disconnected();
void DisconnectClient();

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

Expand Down
49 changes: 23 additions & 26 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,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::DisconnectClient, this, &CClient::Disconnect );

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

Expand Down Expand Up @@ -907,51 +907,48 @@ void CClient::Stop()

/// @method
/// @brief Connects to strServerAddress
/// @emit Connecting (strServerName) if the client wasn't running and SetServerAddr returned true.
/// @emit Connecting (strServerName) if the client wasn't running and SetServerAddr was valid
/// @emit ConnectingFailed (error) if an error occurred
/// @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 )
void CClient::Connect ( QString strServerAddress, QString strServerName )
{
if ( !IsRunning() )
{
// Set server address and connect if valid address was supplied
if ( SetServerAddr ( strServerAddress ) )
try {
if ( !IsRunning() )
{

Start();

emit Connecting ( strServerName );

return true;
// 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." );
}
}
}

return false;
catch ( const CGenErr& generr )
{
Disconnect();
emit ConnectingFailed ( generr.GetErrorText() );
}
}

/// @method
/// @brief Disconnects client
/// @brief Disconnects client. If the client is not running, it just stops Sound
/// @emit Disconnected
/// @result true if client wasn't running, false otherwise
bool CClient::Disconnect()
void CClient::Disconnect()
{
if ( IsRunning() )
{
Stop();

emit Disconnected();

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

emit Disconnected();

return false;
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ class CClient : public QObject

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

bool IsRunning() { return Sound.IsRunning(); }
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); }
Expand Down Expand Up @@ -390,7 +390,7 @@ protected slots:
{
if ( InetAddr == Channel.GetAddress() )
{
emit Disconnected();
Disconnect();
}
}
void OnCLPingReceived ( CHostAddress InetAddr, int iMs );
Expand Down Expand Up @@ -430,7 +430,10 @@ 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 );
Expand Down
22 changes: 16 additions & 6 deletions src/clientdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ CClientDlg::CClientDlg ( CClient* pNCliP,

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 @@ -738,10 +740,7 @@ void CClientDlg::OnConnectDlgAccepted()
// initiate connection
// TODO: Refactor this for failing call on Connect()

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

// reset flag
bConnectDlgWasShown = false;
Expand All @@ -751,9 +750,13 @@ void CClientDlg::OnConnectDlgAccepted()
void CClientDlg::OnConnectDisconBut()
{
// the connect/disconnect button implements a toggle functionality
if ( !pClient->Disconnect() )
if ( pClient->IsRunning() )
{
pClient->Disconnect();
}
else
{
// If the client didn't disconnect, we assume that we weren't connected. Thus show the connect dialog
// 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 @@ -1221,8 +1224,15 @@ void CClientDlg::OnConnect ( const QString& strMixerBoardLabel )
}
}

void CClientDlg::OnConnectingFailed ( const QString& strError )
{
QMessageBox::critical ( this, APP_NAME, strError, "Close", nullptr );
}

void CClientDlg::OnDisconnect()
{
// channel.cpp also issues Disconnected()

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

Expand Down
1 change: 1 addition & 0 deletions src/clientdlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase

public slots:
void OnConnect ( const QString& strServerName );
void OnConnectingFailed ( const QString& strErrorText );
void OnDisconnect();
void OnConnectDisconBut();
void OnTimerSigMet();
Expand Down

0 comments on commit 4ec9c68

Please sign in to comment.