diff --git a/src/connectdlg.cpp b/src/connectdlg.cpp index 76dd7b03..cec328b3 100755 --- a/src/connectdlg.cpp +++ b/src/connectdlg.cpp @@ -245,7 +245,8 @@ void CConnectDlg::SetServerList ( const CHostAddress& InetAddr, // server location (city and country) QString strLocation = vecServerInfo[iIdx].strCity; - if ( !strLocation.isEmpty() ) + if ( ( !strLocation.isEmpty() ) && + ( vecServerInfo[iIdx].eCountry != QLocale::AnyCountry ) ) { strLocation += ", "; } diff --git a/src/llconserverdlgbase.ui b/src/llconserverdlgbase.ui index 93cdf385..dbff253d 100755 --- a/src/llconserverdlgbase.ui +++ b/src/llconserverdlgbase.ui @@ -54,7 +54,7 @@ - Register Server + Register My Server at Central Server @@ -82,7 +82,7 @@ - Server Info + My Server Info diff --git a/src/main.cpp b/src/main.cpp index 97bed536..82bc60f9 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -437,9 +437,12 @@ QString UsageArguments ( char **argv ) " -y, --history enable connection history and set file\n" " name (server only)\n" " -e, --centralserver address of the central server (server only)\n" - " -o, --serverinfo infos of the server in the format:" - " [name];[city];[country as QLocale ID] (server " - " only)\n" + " -o, --serverinfo infos of the server(s) in the format:\n" + " [name];[city];[country as QLocale ID]; ...\n" + " [server1 address];[server1 name]; ...\n" + " [server1 city]; ...\n" + " [server1 country as QLocale ID]; ...\n" + " [server2 address]; ... (server only)\n" " -c, --connect connect to last server on startup (client\n" " only)\n" " -d, --disableleds disable LEDs in main window (client only)\n" diff --git a/src/protocol.cpp b/src/protocol.cpp index 41b335b6..192c709c 100755 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -199,6 +199,12 @@ CONNECTION LESS MESSAGES the QLocale class + +- PROTMESSID_CLM_UNREGISTER_SERVER: Unregister a server + + note: does not have any data -> n = 0 + + - PROTMESSID_CLM_SERVER_LIST: Server list message for each registered server append following data: @@ -597,6 +603,10 @@ bool CProtocol::ParseConnectionLessMessage ( const CVector& vecbyData, case PROTMESSID_CLM_REGISTER_SERVER: bRet = EvaluateCLRegisterServerMes ( InetAddr, vecData ); break; + + case PROTMESSID_CLM_UNREGISTER_SERVER: + bRet = EvaluateCLUnregisterServerMes ( InetAddr ); + break; } } else @@ -1314,6 +1324,20 @@ bool CProtocol::EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr, return false; // no error } +void CProtocol::CreateCLUnregisterServerMes ( const CHostAddress& InetAddr ) +{ + CreateAndImmSendConLessMessage ( PROTMESSID_CLM_UNREGISTER_SERVER, + CVector ( 0 ), + InetAddr ); +} + +bool CProtocol::EvaluateCLUnregisterServerMes ( const CHostAddress& InetAddr ) +{ + // invoke message action + emit CLUnregisterServerReceived ( InetAddr ); + + return false; // no error +} void CProtocol::CreateCLServerListMes ( const CHostAddress& InetAddr, const CVector vecServerInfo ) diff --git a/src/protocol.h b/src/protocol.h index 5ad6f875..cd526a52 100755 --- a/src/protocol.h +++ b/src/protocol.h @@ -59,7 +59,7 @@ #define PROTMESSID_CLM_PING_MS_WITHNUMCLIENTS 1002 // for ping time and num. of clients info #define PROTMESSID_CLM_SERVER_FULL 1003 // server full message #define PROTMESSID_CLM_REGISTER_SERVER 1004 // register server -#define PROTMESSID_CLM_UNREGISTER_SERVER 1005 // unregister server -> TODO +#define PROTMESSID_CLM_UNREGISTER_SERVER 1005 // unregister server #define PROTMESSID_CLM_SERVER_LIST 1006 // server list #define PROTMESSID_CLM_REQ_SERVER_LIST 1007 // request server list #define PROTMESSID_CLM_SEND_EMPTY_MESSAGE 1008 // an empty message shall be send @@ -103,6 +103,7 @@ public: void CreateCLServerFullMes ( const CHostAddress& InetAddr ); void CreateCLRegisterServerMes ( const CHostAddress& InetAddr, const CServerCoreInfo& ServerInfo ); + void CreateCLUnregisterServerMes ( const CHostAddress& InetAddr ); void CreateCLServerListMes ( const CHostAddress& InetAddr, const CVector vecServerInfo ); void CreateCLReqServerListMes ( const CHostAddress& InetAddr ); @@ -206,17 +207,18 @@ protected: bool EvaluateReqNetwTranspPropsMes(); bool EvaluateDisconnectionMes(); - bool EvaluateCLPingMes ( const CHostAddress& InetAddr, - const CVector& vecData ); + bool EvaluateCLPingMes ( const CHostAddress& InetAddr, + const CVector& vecData ); bool EvaluateCLPingWithNumClientsMes ( const CHostAddress& InetAddr, const CVector& vecData ); bool EvaluateCLServerFullMes(); - bool EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr, - const CVector& vecData ); - bool EvaluateCLServerListMes ( const CHostAddress& InetAddr, - const CVector& vecData ); - bool EvaluateCLReqServerListMes ( const CHostAddress& InetAddr ); - bool EvaluateCLSendEmptyMesMes ( const CVector& vecData ); + bool EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr, + const CVector& vecData ); + bool EvaluateCLUnregisterServerMes ( const CHostAddress& InetAddr ); + bool EvaluateCLServerListMes ( const CHostAddress& InetAddr, + const CVector& vecData ); + bool EvaluateCLReqServerListMes ( const CHostAddress& InetAddr ); + bool EvaluateCLSendEmptyMesMes ( const CVector& vecData ); int iOldRecID; int iOldRecCnt; @@ -259,6 +261,7 @@ signals: int iNumClients ); void CLRegisterServerReceived ( CHostAddress InetAddr, CServerCoreInfo ServerInfo ); + void CLUnregisterServerReceived ( CHostAddress InetAddr ); void CLServerListReceived ( CHostAddress InetAddr, CVector vecServerInfo ); void CLReqServerList ( CHostAddress InetAddr ); diff --git a/src/server.cpp b/src/server.cpp index 647833b2..9a38a5df 100755 --- a/src/server.cpp +++ b/src/server.cpp @@ -289,6 +289,10 @@ CServer::CServer ( const QString& strLoggingFileName, SIGNAL ( CLRegisterServerReceived ( CHostAddress, CServerCoreInfo ) ), this, SLOT ( OnCLRegisterServerReceived ( CHostAddress, CServerCoreInfo ) ) ); + QObject::connect ( &ConnLessProtocol, + SIGNAL ( CLUnregisterServerReceived ( CHostAddress ) ), + this, SLOT ( OnCLUnregisterServerReceived ( CHostAddress ) ) ); + QObject::connect ( &ConnLessProtocol, SIGNAL ( CLReqServerList ( CHostAddress ) ), this, SLOT ( OnCLReqServerList ( CHostAddress ) ) ); diff --git a/src/server.h b/src/server.h index ac333d87..53491c21 100755 --- a/src/server.h +++ b/src/server.h @@ -254,6 +254,11 @@ public slots: ServerListManager.RegisterServer ( InetAddr, ServerInfo ); } + void OnCLUnregisterServerReceived ( CHostAddress InetAddr ) + { + ServerListManager.UnregisterServer ( InetAddr ); + } + // CODE TAG: MAX_NUM_CHANNELS_TAG // make sure we have MAX_NUM_CHANNELS connections!!! diff --git a/src/serverlist.cpp b/src/serverlist.cpp index a72e1ad1..8713995c 100755 --- a/src/serverlist.cpp +++ b/src/serverlist.cpp @@ -29,12 +29,26 @@ CServerListManager::CServerListManager ( const QString& sNCentServAddr, const QString& strServerInfo, CProtocol* pNConLProt ) - : bUseDefaultCentralServerAddress ( false ), + : iNumPredefinedServers ( 0 ), + bUseDefaultCentralServerAddress ( false ), pConnLessProtocol ( pNConLProt ) { // set the central server address SetCentralServerAddress ( sNCentServAddr ); + // prepare the server info information + QStringList slServInfoSeparateParams; + int iServInfoNumSplitItems = 0; + + if ( !strServerInfo.isEmpty() ) + { + // split the different parameter strings + slServInfoSeparateParams = strServerInfo.split ( ";" ); + + // get the number of items in the split list + iServInfoNumSplitItems = slServInfoSeparateParams.count(); + } + // per definition, the very first entry is this server and this entry will // never be deleted ServerList.clear(); @@ -49,37 +63,87 @@ CServerListManager::CServerListManager ( const QString& sNCentServAddr, QLocale::system().country(), "", USED_NUM_CHANNELS, - false ); + true ); // parse the server info string according to definition: - // [name];[city];[country as QLocale ID] - if ( !strServerInfo.isEmpty() ) + // [this server name];[this server city]; ... + // [this server country as QLocale ID]; ... + // per definition, we expect at least three parameters + if ( iServInfoNumSplitItems >= 3 ) { - // split the different parameter strings - QStringList slSeparateParameters = strServerInfo.split ( ";" ); + // [this server name] + ThisServerListEntry.strName = slServInfoSeparateParams[0]; - // per definition, we expect three parameters - if ( slSeparateParameters.count() == 3 ) + // [this server city] + ThisServerListEntry.strCity = slServInfoSeparateParams[1]; + + // [this server country as QLocale ID] + const int iCountry = slServInfoSeparateParams[2].toInt(); + if ( ( iCountry >= 0 ) && ( iCountry <= QLocale::LastCountry ) ) { - // [name] - ThisServerListEntry.strName = slSeparateParameters[0]; - - // [city] - ThisServerListEntry.strCity = slSeparateParameters[1]; - - // [country as QLocale ID] - const int iCountry = slSeparateParameters[2].toInt(); - if ( ( iCountry >= 0 ) && ( iCountry <= QLocale::LastCountry ) ) - { - ThisServerListEntry.eCountry = static_cast ( - iCountry ); - } + ThisServerListEntry.eCountry = static_cast ( + iCountry ); } } // per definition, the first entry in the server list it the own server ServerList.append ( ThisServerListEntry ); + // parse the predefined server infos (if any) according to definition: + // [server1 address];[server1 name];[server1 city]; ... + // [server1 country as QLocale ID]; ... + // [server2 address];[server2 name];[server2 city]; ... + // [server2 country as QLocale ID]; ... + // ... + int iCurUsedServInfoSplitItems = 3; // three items are used for this server + + // we always expect four items per new server, also check for maximum + // allowed number of servers in the server list + while ( ( iServInfoNumSplitItems - iCurUsedServInfoSplitItems >= 4 ) && + ( iNumPredefinedServers <= MAX_NUM_SERVERS_IN_SERVER_LIST ) ) + { + // create a new server list entry + CServerListEntry NewServerListEntry ( + CHostAddress(), + "", + "", + QLocale::AnyCountry, + "", + USED_NUM_CHANNELS, + true ); + + // [server n address] + LlconNetwUtil().ParseNetworkAddress ( + slServInfoSeparateParams[iCurUsedServInfoSplitItems], + NewServerListEntry.HostAddr ); + + // [server n name] + NewServerListEntry.strName = + slServInfoSeparateParams[iCurUsedServInfoSplitItems + 1]; + + // [server n city] + NewServerListEntry.strCity = + slServInfoSeparateParams[iCurUsedServInfoSplitItems + 2]; + + // [server n country as QLocale ID] + const int iCountry = + slServInfoSeparateParams[iCurUsedServInfoSplitItems + 3].toInt(); + + if ( ( iCountry >= 0 ) && ( iCountry <= QLocale::LastCountry ) ) + { + NewServerListEntry.eCountry = static_cast ( + iCountry ); + } + + // add the new server to the server list + ServerList.append ( NewServerListEntry ); + + // we have used four items and have created one predefined server + // (adjust counters) + iCurUsedServInfoSplitItems += 4; + iNumPredefinedServers++; + } + // Connections ------------------------------------------------------------- QObject::connect ( &TimerPollList, SIGNAL ( timeout() ), @@ -166,8 +230,8 @@ void CServerListManager::OnTimerPollList() QMutexLocker locker ( &Mutex ); // check all list entries except of the very first one (which is the central - // server entry) if they are still valid - for ( int iIdx = 1; iIdx < ServerList.size(); iIdx++ ) + // server entry) and the predefined servers if they are still valid + for ( int iIdx = 1 + iNumPredefinedServers; iIdx < ServerList.size(); iIdx++ ) { // 1 minute = 60 * 1000 ms if ( ServerList[iIdx].RegisterTime.elapsed() > @@ -232,6 +296,31 @@ void CServerListManager::RegisterServer ( const CHostAddress& InetAddr, } } +void CServerListManager::UnregisterServer ( const CHostAddress& InetAddr ) +{ + QMutexLocker locker ( &Mutex ); + + if ( bIsCentralServer && bEnabled ) + { + const int iCurServerListSize = ServerList.size(); + + // Find the server to unregister in the list. The very first list entry + // must not be checked since this is per definition the central server + // (i.e., this server), also the predefined servers must not be checked + for ( int iIdx = 1 + iNumPredefinedServers; iIdx < iCurServerListSize; iIdx++ ) + { + if ( ServerList[iIdx].HostAddr == InetAddr ) + { + // remove this list entry + ServerList.removeAt ( iIdx ); + + // entry found, leave for-loop + continue; + } + } + } +} + void CServerListManager::QueryServerList ( const CHostAddress& InetAddr ) { QMutexLocker locker ( &Mutex ); diff --git a/src/serverlist.h b/src/serverlist.h index 7a9e5923..95b29698 100755 --- a/src/serverlist.h +++ b/src/serverlist.h @@ -146,6 +146,8 @@ public: void RegisterServer ( const CHostAddress& InetAddr, const CServerCoreInfo& ServerInfo ); + void UnregisterServer ( const CHostAddress& InetAddr ); + void QueryServerList ( const CHostAddress& InetAddr ); @@ -173,6 +175,7 @@ protected: QMutex Mutex; QList ServerList; QString strCentralServerAddress; + int iNumPredefinedServers; bool bEnabled; bool bIsCentralServer; bool bUseDefaultCentralServerAddress;