From cb2a72c996ad25310d68efb839e0d1c94c01ea90 Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Mon, 4 Apr 2011 18:57:49 +0000 Subject: [PATCH] some more server list implementation, some code style changes --- src/llconclientdlg.cpp | 4 +++ src/server.h | 17 ++++++----- src/serverlist.cpp | 67 ++++++++++++++++++++++++++++++++++++++++-- src/serverlist.h | 24 ++++++++++++--- src/socket.cpp | 12 ++++++-- src/socket.h | 2 ++ 6 files changed, 109 insertions(+), 17 deletions(-) diff --git a/src/llconclientdlg.cpp b/src/llconclientdlg.cpp index 263a33d8..3a6c9122 100755 --- a/src/llconclientdlg.cpp +++ b/src/llconclientdlg.cpp @@ -894,6 +894,7 @@ void CLlconClientDlg::SetGUIDesign ( const EGUIDesign eNewDesign ) " font: bold; }" "QGroupBox::title { color: rgb(148, 148, 148); }" ); +#ifdef _WIN32 // Workaround QT-Windows problem: This should not be necessary since in the // background frame the style sheet for QRadioButton was already set. But it // seems that it is only applied if the style was set to default and then back @@ -902,6 +903,7 @@ RadioButtonRevSelL->setStyleSheet ( "color: rgb(148, 148, 148);" "font: bold;" ); RadioButtonRevSelR->setStyleSheet ( "color: rgb(148, 148, 148);" "font: bold;" ); +#endif break; @@ -909,9 +911,11 @@ RadioButtonRevSelR->setStyleSheet ( "color: rgb(148, 148, 148);" // reset style sheet and set original paramters backgroundFrame->setStyleSheet ( "" ); +#ifdef _WIN32 // Workaround QT-Windows problem: See above description RadioButtonRevSelL->setStyleSheet ( "" ); RadioButtonRevSelR->setStyleSheet ( "" ); +#endif break; } diff --git a/src/server.h b/src/server.h index 0a48734f..dfc0aa37 100755 --- a/src/server.h +++ b/src/server.h @@ -115,12 +115,13 @@ public: bool GetTimingStdDev ( double& dCurTiStdDev ); bool PutData ( const CVector& vecbyRecBuf, - const int iNumBytesRead, const CHostAddress& HostAdr ); + const int iNumBytesRead, + const CHostAddress& HostAdr ); void GetConCliParam ( CVector& vecHostAddresses, - CVector& vecsName, - CVector& veciJitBufNumFrames, - CVector& veciNetwFrameSizeFact ); + CVector& vecsName, + CVector& veciJitBufNumFrames, + CVector& veciNetwFrameSizeFact ); protected: // access functions for actual channels @@ -135,14 +136,14 @@ protected: CVector CreateChannelList(); void CreateAndSendChanListForAllConChannels(); void CreateAndSendChanListForThisChan ( const int iCurChanID ); - void CreateAndSendChatTextForAllConChannels ( const int iCurChanID, + void CreateAndSendChatTextForAllConChannels ( const int iCurChanID, const QString& strChatText ); void WriteHTMLChannelList(); - CVector ProcessData ( const int iCurIndex, + CVector ProcessData ( const int iCurIndex, CVector >& vecvecsData, - CVector& vecdGains, - CVector& vecNumAudioChannels ); + CVector& vecdGains, + CVector& vecNumAudioChannels ); virtual void customEvent ( QEvent* Event ); diff --git a/src/serverlist.cpp b/src/serverlist.cpp index b16c77d8..052d7e79 100755 --- a/src/serverlist.cpp +++ b/src/serverlist.cpp @@ -29,6 +29,24 @@ CServerListManager::CServerListManager ( const bool NbEbld ) : bEnabled ( NbEbld ) { + // per definition, the very first entry is this server and this entry will + // never be deleted + ServerList.clear(); + +// per definition the client substitudes the IP address of the master server +// itself for his server list +ServerList.append ( CServerListEntry ( + CHostAddress(), + "Master Server", // TEST + "", + QLocale::Germany, // TEST + "Munich", // TEST + 0, // will be updated later + USED_NUM_CHANNELS, + true ) ); // TEST + + + // connections ------------------------------------------------------------- QObject::connect ( &TimerPollList, SIGNAL ( timeout() ), this, SLOT ( OnTimerPollList() ) ); @@ -46,8 +64,9 @@ void CServerListManager::OnTimerPollList() { QMutexLocker locker ( &Mutex ); - // check all list entries if they are still valid - for ( int iIdx = 0; iIdx < ServerList.size(); iIdx++ ) + // check all list entries except of the very first one (which is the master + // server entry) if they are still valid + for ( int iIdx = 1; iIdx < ServerList.size(); iIdx++ ) { // 1 minute = 60 * 1000 ms if ( ServerList[iIdx].RegisterTime.elapsed() > @@ -58,3 +77,47 @@ void CServerListManager::OnTimerPollList() } } } + +void CServerListManager::RegisterServer ( const CHostAddress& InetAddr, + const CServerCoreInfo& ServerInfo ) +{ + QMutexLocker locker ( &Mutex ); + + // define invalid index used as a flag + const int ciInvalidIdx = -1; + + // Check if server is already registered. Use IP number and port number to + // fully identify a server. The very first list entry must not be checked + // since this is per definition the master server (i.e., this server) + int iSelIdx = ciInvalidIdx; // initialize with an illegal value + for ( int iIdx = 1; iIdx < ServerList.size(); iIdx++ ) + { + if ( ServerList[iIdx].HostAddr == InetAddr ) + { + // store entry index + iSelIdx = iIdx; + + // entry found, leave for-loop + continue; + } + } + + // if server is not yet registered, we have to create a new entry + if ( iSelIdx == ciInvalidIdx ) + { + // create a new server list entry and init with received data + ServerList.append ( CServerListEntry ( InetAddr, ServerInfo ) ); + } + else + { + // update all data and call update registration function + ServerList[iSelIdx].strName = ServerInfo.strName; + ServerList[iSelIdx].strTopic = ServerInfo.strTopic; + ServerList[iSelIdx].eCountry = ServerInfo.eCountry; + ServerList[iSelIdx].strCity = ServerInfo.strCity; + ServerList[iSelIdx].iNumClients = ServerInfo.iNumClients; + ServerList[iSelIdx].iMaxNumClients = ServerInfo.iMaxNumClients; + ServerList[iSelIdx].bPermanentOnline = ServerInfo.bPermanentOnline; + ServerList[iSelIdx].UpdateRegistration(); + } +} diff --git a/src/serverlist.h b/src/serverlist.h index 9d17435e..2e92afed 100755 --- a/src/serverlist.h +++ b/src/serverlist.h @@ -74,14 +74,14 @@ class CServerListEntry : public CServerInfo { public: CServerListEntry() : - CServerInfo ( CHostAddress(), + CServerInfo ( CHostAddress(), "", "", QLocale::AnyCountry, "", 0, 0, - false ) { RegisterTime.start(); } + false ) { UpdateRegistration(); } CServerListEntry ( const CHostAddress& NHAddr, const QString& NsName, @@ -91,14 +91,28 @@ public: const int NiNumClients, const int NiMaxNumClients, const bool NbPermOnline) - : CServerInfo ( NHAddr, + : CServerInfo ( NHAddr, NsName, NsTopic, NeCountry, NsCity, NiNumClients, NiMaxNumClients, - NbPermOnline ) { RegisterTime.start(); } + NbPermOnline ) { UpdateRegistration(); } + + CServerListEntry ( const CHostAddress& NHAddr, + const CServerCoreInfo& NewCoreServerInfo ) + : CServerInfo ( NHAddr, + NewCoreServerInfo.strName, + NewCoreServerInfo.strTopic, + NewCoreServerInfo.eCountry, + NewCoreServerInfo.strCity, + NewCoreServerInfo.iNumClients, + NewCoreServerInfo.iMaxNumClients, + NewCoreServerInfo.bPermanentOnline ) + { UpdateRegistration(); } + + void UpdateRegistration() { RegisterTime.start(); } public: // time on which the entry was registered @@ -112,6 +126,8 @@ class CServerListManager : public QObject public: CServerListManager ( const bool NbEbld ); + void RegisterServer ( const CHostAddress& InetAddr, + const CServerCoreInfo& ServerInfo ); protected: QTimer TimerPollList; QMutex Mutex; diff --git a/src/socket.cpp b/src/socket.cpp index f21bb222..446d6b02 100755 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -60,8 +60,10 @@ void CSocket::Init ( const quint16 iPortNumber ) } void CSocket::SendPacket ( const CVector& vecbySendBuf, - const CHostAddress& HostAddr ) + const CHostAddress& HostAddr ) { + QMutexLocker locker ( &Mutex ); + const int iVecSizeOut = vecbySendBuf.Size(); if ( iVecSizeOut != 0 ) @@ -72,7 +74,9 @@ void CSocket::SendPacket ( const CVector& vecbySendBuf, // const char*) SocketDevice.writeDatagram ( (const char*) &( (CVector) vecbySendBuf )[0], - iVecSizeOut, HostAddr.InetAddr, HostAddr.iPort ); + iVecSizeOut, + HostAddr.InetAddr, + HostAddr.iPort ); } } @@ -86,7 +90,9 @@ void CSocket::OnDataReceived() // read block from network interface and query address of sender const int iNumBytesRead = SocketDevice.readDatagram ( (char*) &vecbyRecBuf[0], - MAX_SIZE_BYTES_NETW_BUF, &SenderAddress, &SenderPort ); + MAX_SIZE_BYTES_NETW_BUF, + &SenderAddress, + &SenderPort ); // check if an error occurred if ( iNumBytesRead < 0 ) diff --git a/src/socket.h b/src/socket.h index e42a3b13..a86f3d91 100755 --- a/src/socket.h +++ b/src/socket.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #include "global.h" #include "channel.h" @@ -63,6 +64,7 @@ protected: void Init ( const quint16 iPortNumber = LLCON_DEFAULT_PORT_NUMBER ); QUdpSocket SocketDevice; + QMutex Mutex; CVector vecbyRecBuf; CHostAddress RecHostAddr;