Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob-NY committed Jun 11, 2021
1 parent e5028dd commit b3dac8a
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ int main ( int argc, char** argv )
QString strServerListFilter = "";
QString strWelcomeMessage = "";
QString strClientName = "";
QString strNotifyServer = "";

#if !defined( HEADLESS ) && defined( _WIN32 )
if ( AttachConsole ( ATTACH_PARENT_PROCESS ) )
Expand Down Expand Up @@ -437,6 +438,18 @@ int main ( int argc, char** argv )
continue;
}


// Notification Server xxx.xxx.xxx.xxx:ppp-----------------------------
if ( GetStringArgument ( argc, argv, i, "--notifyserver", "--notifyserver", strArgument ) )
{

strNotifyServer = strArgument;
qInfo() << qUtf8Printable ( QString ( "- notify server: %1" ).arg ( strNotifyServer ) );
CommandLineOptions << "--notifyserver";
continue;
}


// Version number ------------------------------------------------------
if ( ( !strcmp ( argv[i], "--version" ) ) || ( !strcmp ( argv[i], "-v" ) ) )
{
Expand Down Expand Up @@ -645,6 +658,7 @@ int main ( int argc, char** argv )
iPortNumber,
iQosNumber,
strHTMLStatusFileName,
strNotifyServer,
strCentralServer,
strServerInfo,
strServerPublicIP,
Expand Down Expand Up @@ -771,6 +785,7 @@ QString UsageArguments ( char** argv )
" running a slave and your own directory server\n"
" behind the same NAT\n"
" --serverbindip specify the IP address the server will bind to\n"
" --notifyserver specify server to receive UDP notifications IP:PORT\n"
"\n"
"Client only:\n"
" -M, --mutestream starts the application in muted state\n"
Expand Down
37 changes: 37 additions & 0 deletions src/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,10 @@ if ( rand() < ( RAND_MAX / 2 ) ) return false;
case PROTMESSID_CLM_REGISTER_SERVER_RESP:
EvaluateCLRegisterServerResp ( InetAddr, vecbyMesBodyData );
break;

case PROTMESSID_CLM_REQ_SERVER_STATUS:
EvaluateCLReqServerStatus( InetAddr );
break;
}
}

Expand Down Expand Up @@ -1716,6 +1720,32 @@ void CProtocol::CreateCLPingWithNumClientsMes ( const CHostAddress& InetAddr, co
CreateAndImmSendConLessMessage ( PROTMESSID_CLM_PING_MS_WITHNUMCLIENTS, vecData, InetAddr );
}

/**
* Send json encoded server status
*/

void CProtocol::CreateCLServerStatusMes ( const CHostAddress& InetAddr, const QString strServerStatus )
{
int iPos = 0; // init position pointer

// convert server info to utf-8
const QByteArray strUTF8ServerStatus = strServerStatus.toUtf8();

const int iEntrLen = 2 + strUTF8ServerStatus.size();

// build data vector
CVector<uint8_t> vecData ( iEntrLen );

// Server status
PutStringUTF8OnStream ( vecData, iPos, strUTF8ServerStatus );

CreateAndImmSendConLessMessage ( PROTMESSID_CLM_SERVER_STATUS, vecData, InetAddr );

}




bool CProtocol::EvaluateCLPingWithNumClientsMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData )
{
int iPos = 0; // init position pointer
Expand Down Expand Up @@ -2492,6 +2522,13 @@ bool CProtocol::EvaluateCLReqConnClientsListMes ( const CHostAddress& InetAddr )
return false; // no error
}

bool CProtocol::EvaluateCLReqServerStatus( const CHostAddress& InetAddr )
{
emit CLReqServerStatus( InetAddr );
return false;
}


void CProtocol::CreateCLChannelLevelListMes ( const CHostAddress& InetAddr, const CVector<uint16_t>& vecLevelList, const int iNumClients )
{
// This must be a multiple of bytes at four bits per client
Expand Down
7 changes: 7 additions & 0 deletions src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
#define PROTMESSID_CLM_REGISTER_SERVER_EX 1017 // register server with extended information
#define PROTMESSID_CLM_RED_SERVER_LIST 1018 // reduced server list

#define PROTMESSID_CLM_REQ_SERVER_STATUS 1020 // request server status
#define PROTMESSID_CLM_SERVER_STATUS 1021 // server status response

// special IDs
#define PROTMESSID_SPECIAL_SPLIT_MESSAGE 2001 // a container for split messages

Expand Down Expand Up @@ -153,6 +156,8 @@ void CreateReqChannelLevelListMes();
void CreateCLReqConnClientsListMes ( const CHostAddress& InetAddr );
void CreateCLChannelLevelListMes ( const CHostAddress& InetAddr, const CVector<uint16_t>& vecLevelList, const int iNumClients );
void CreateCLRegisterServerResp ( const CHostAddress& InetAddr, const ESvrRegResult eResult );
void CreateCLServerStatusMes ( const CHostAddress& InetAddr, const QString strServerStatus );


static bool ParseMessageFrame ( const CVector<uint8_t>& vecbyData,
const int iNumBytesIn,
Expand Down Expand Up @@ -265,6 +270,7 @@ void CreateReqChannelLevelListMes();
bool EvaluateCLReqVersionAndOSMes ( const CHostAddress& InetAddr );
bool EvaluateCLConnClientsListMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
bool EvaluateCLReqConnClientsListMes ( const CHostAddress& InetAddr );
bool EvaluateCLReqServerStatus ( const CHostAddress& InetAddr );
bool EvaluateCLChannelLevelListMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
bool EvaluateCLRegisterServerResp ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );

Expand Down Expand Up @@ -331,6 +337,7 @@ public slots:
void CLReqVersionAndOS ( CHostAddress InetAddr );
void CLConnClientsListMesReceived ( CHostAddress InetAddr, CVector<CChannelInfo> vecChanInfo );
void CLReqConnClientsList ( CHostAddress InetAddr );
void CLReqServerStatus ( CHostAddress InetAddr );
void CLChannelLevelListReceived ( CHostAddress InetAddr, CVector<uint16_t> vecLevelList );
void CLRegisterServerResp ( CHostAddress InetAddr, ESvrRegResult eStatus );
};
110 changes: 110 additions & 0 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ CServer::CServer ( const int iNewMaxNumChan,
const quint16 iPortNumber,
const quint16 iQosNumber,
const QString& strHTMLStatusFileName,
const QString& strNotifyServer,
const QString& strCentralServer,
const QString& strServerInfo,
const QString& strServerListFilter,
Expand All @@ -238,6 +239,8 @@ CServer::CServer ( const int iNewMaxNumChan,
iFrameCount ( 0 ),
bWriteStatusHTMLFile ( false ),
strServerHTMLFileListName ( strHTMLStatusFileName ),
bNotifyServer( false ),
strNotifyServerAddr( strNotifyServer ),
HighPrecisionTimer ( bNUseDoubleSystemFrameSize ),
ServerListManager ( iPortNumber, strCentralServer, strServerInfo, strServerPublicIP, strServerListFilter, iNewMaxNumChan, &ConnLessProtocol ),
JamController ( this ),
Expand Down Expand Up @@ -374,6 +377,24 @@ CServer::CServer ( const int iNewMaxNumChan,
WriteHTMLChannelList();
}


// Notify Server Setup
if( !strNotifyServerAddr.isEmpty() )
{

if( NetworkUtil::ParseNetworkAddress( strNotifyServerAddr, addrNotifyServer ) )
{
qInfo() << "-Server notifications sent to:" << addrNotifyServer.toString();
bNotifyServer = true;
OnCLReqServerStatus( addrNotifyServer );
}
else
{
qInfo() << "- ** Could not parse notify server **";
bNotifyServer = false; // default is already false - here just for clarity...
}
}

// manage welcome message: if the welcome message is a valid link to a local
// file, the content of that file is used as the welcome message (#361)
SetWelcomeMessage ( strNewWelcomeMessage ); // first use given text, may be overwritten
Expand Down Expand Up @@ -452,6 +473,8 @@ CServer::CServer ( const int iNewMaxNumChan,

QObject::connect ( &ConnLessProtocol, &CProtocol::CLReqConnClientsList, this, &CServer::OnCLReqConnClientsList );

QObject::connect ( &ConnLessProtocol, &CProtocol::CLReqServerStatus, this, &CServer::OnCLReqServerStatus );

QObject::connect ( &ServerListManager, &CServerListManager::SvrRegStatusChanged, this, &CServer::SvrRegStatusChanged );

QObject::connect ( &JamController, &recorder::CJamController::RestartRecorder, this, &CServer::RestartRecorder );
Expand Down Expand Up @@ -549,6 +572,30 @@ void CServer::SendProtMessage ( int iChID, CVector<uint8_t> vecMessage )
Socket.SendPacket ( vecMessage, vecChannels[iChID].GetAddress() );
}

void CServer::OnCLReqServerStatus ( CHostAddress InetAddr )
{
QString strServerStatus;

// If not enabled, bail
if( ! bNotifyServer )
return;

// Only process if the InetAddr is the same as that
// specified in the command line argument.

if( !( InetAddr == addrNotifyServer) )
{
qInfo() << "** Denied server status message request from" << InetAddr.toString() << "only allowed to"<<addrNotifyServer.toString();
return;
}

BuildServerStatusJson(strServerStatus);

ConnLessProtocol.CreateCLServerStatusMes ( InetAddr, strServerStatus );
}



void CServer::OnNewConnection ( int iChID, CHostAddress RecHostAddr )
{
QMutexLocker locker ( &Mutex );
Expand Down Expand Up @@ -1385,6 +1432,9 @@ void CServer::CreateAndSendChanListForAllConChannels()
}
}

// Send Server Status to the designated host, if specified.
OnCLReqServerStatus( addrNotifyServer );

// create status HTML file if enabled
if ( bWriteStatusHTMLFile )
{
Expand Down Expand Up @@ -1439,6 +1489,9 @@ void CServer::CreateAndSendRecorderStateForAllConChannels()
vecChannels[i].CreateRecorderStateMes ( eRecorderState );
}
}

OnCLReqServerStatus( addrNotifyServer );

}

void CServer::CreateOtherMuteStateChanged ( const int iCurChanID, const int iOtherChanID, const bool bIsMuted )
Expand Down Expand Up @@ -1635,6 +1688,63 @@ void CServer::SetWelcomeMessage ( const QString& strNWelcMess )
strWelcomeMessage = strWelcomeMessage.left ( MAX_LEN_CHAT_TEXT );
}

/**
* Build a server status json string that can be used to either
* write to a disk-based status file, or sent via CL message.
*/

void CServer::BuildServerStatusJson( QString& strServerStatus )
{

CHostAddress InetAddr;
CChannelCoreInfo ci;


ERecorderState eRecorderState = JamController.GetRecorderState();

int ccount = GetNumberOfConnectedClients();

QJsonObject jobject;
QJsonArray jclients;


jobject["cc"] = ccount;
jobject["rs"] = eRecorderState;
jobject["ts"] = QDateTime::currentSecsSinceEpoch();

if ( ccount > 0 )
{
// write entry for each connected client
for ( int i = 0; i < iMaxNumChannels; i++ )
{
if ( vecChannels[i].IsConnected() )
{
vecChannels[i].GetAddress ( InetAddr );
ci = vecChannels[i].GetChanInfo();

QJsonObject temp;
temp["ip"] = InetAddr.toString();
temp["in"] = ci.iInstrument;
temp["sk"] = ci.eSkillLevel;
temp["cn"] = ci.eCountry;
temp["ct"] = ci.strCity;
temp["nm"] = vecChannels[i].GetName();

jclients.push_back( QJsonValue(temp) );

}
}

}

jobject["cl"] = jclients;

QJsonDocument jdoc(jobject);
strServerStatus = jdoc.toJson(QJsonDocument::Compact);

}


void CServer::WriteHTMLChannelList()
{
// prepare file and stream
Expand Down
16 changes: 16 additions & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
#include <QHostAddress>
#include <QFileInfo>
#include <QtConcurrent>

#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>

#include <algorithm>
#ifdef USE_OPUS_SHARED_LIB
# include "opus/opus_custom.h"
Expand Down Expand Up @@ -158,6 +163,7 @@ class CServer : public QObject, public CServerSlots<MAX_NUM_CHANNELS>
const quint16 iPortNumber,
const quint16 iQosNumber,
const QString& strHTMLStatusFileName,
const QString& strNotifyServer,
const QString& strCentralServer,
const QString& strServerInfo,
const QString& strServerListFilter,
Expand Down Expand Up @@ -284,6 +290,9 @@ class CServer : public QObject, public CServerSlots<MAX_NUM_CHANNELS>

virtual void customEvent ( QEvent* pEvent );

void BuildServerStatusJson( QString& strServerStatus );


// if server mode is normal or double system frame size
bool bUseDoubleSystemFrameSize;
int iServerFrameSizeSamples;
Expand Down Expand Up @@ -352,6 +361,11 @@ class CServer : public QObject, public CServerSlots<MAX_NUM_CHANNELS>
bool bWriteStatusHTMLFile;
QString strServerHTMLFileListName;

// Notify Server
bool bNotifyServer;
CHostAddress addrNotifyServer;
QString strNotifyServerAddr;

CHighPrecisionTimer HighPrecisionTimer;

// server list
Expand Down Expand Up @@ -431,6 +445,8 @@ public slots:

void OnCLReqConnClientsList ( CHostAddress InetAddr ) { ConnLessProtocol.CreateCLConnClientsListMes ( InetAddr, CreateChannelList() ); }

void OnCLReqServerStatus ( CHostAddress InetAddr );

void OnCLRegisterServerReceived ( CHostAddress InetAddr, CHostAddress LInetAddr, CServerCoreInfo ServerInfo )
{
ServerListManager.CentralServerRegisterServer ( InetAddr, LInetAddr, ServerInfo );
Expand Down

0 comments on commit b3dac8a

Please sign in to comment.