jamulus/src/testbench.h

149 lines
4.6 KiB
C
Raw Normal View History

2009-05-09 14:24:33 +02:00
/******************************************************************************\
2010-01-03 14:40:46 +01:00
* Copyright (c) 2004-2010
2009-05-09 14:24:33 +02:00
*
* Author(s):
* Volker Fischer
*
******************************************************************************
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
\******************************************************************************/
#if !defined ( TESTBENCH_HOIHJH8_3_43445KJIUHF1912__INCLUDED_ )
#define TESTBENCH_HOIHJH8_3_43445KJIUHF1912__INCLUDED_
#include <qobject.h>
#include <qtimer.h>
#include <qdatetime.h>
#include <qhostaddress.h>
#include "global.h"
#include "socket.h"
#include "protocol.h"
2009-05-09 14:24:33 +02:00
#include "util.h"
/* Classes ********************************************************************/
class CTestbench : public QObject
{
Q_OBJECT
public:
2009-05-12 09:33:11 +02:00
CTestbench ( QString sNewAddress, quint16 iNewPort ) :
sAddress ( sNewAddress ), iPort ( iNewPort )
2009-05-09 14:24:33 +02:00
{
2009-05-09 22:55:25 +02:00
// bind socket
UdpSocket.bind ( QHostAddress ( QHostAddress::Any ), 22222 );
// connect protocol signal
QObject::connect ( &Protocol, SIGNAL ( MessReadyForSending ( CVector<uint8_t> ) ),
this, SLOT ( OnSendProtMessage ( CVector<uint8_t> ) ) );
2009-05-09 14:24:33 +02:00
// connect and start the timer (testbench heartbeat)
QObject::connect ( &Timer, SIGNAL ( timeout() ),
this, SLOT ( OnTimer() ) );
2009-10-10 10:32:54 +02:00
2009-05-09 14:24:33 +02:00
Timer.start ( 1 ); // 1 ms
}
protected:
int GenRandomIntInRange ( const int iStart, const int iEnd ) const
{
return static_cast<int> ( iStart +
2009-05-09 22:55:25 +02:00
( ( static_cast<double> ( iEnd - iStart + 1 ) * rand() ) / RAND_MAX ) );
}
2009-05-12 09:33:11 +02:00
QString sAddress;
quint16 iPort;
2009-05-09 22:55:25 +02:00
QTimer Timer;
CProtocol Protocol;
QUdpSocket UdpSocket;
2009-05-09 14:24:33 +02:00
public slots:
void OnTimer()
{
2009-05-09 22:55:25 +02:00
// generate random protocol message
2009-10-10 10:32:54 +02:00
switch ( GenRandomIntInRange ( 0, 11 ) )
2009-05-09 22:55:25 +02:00
{
case 0:
Protocol.CreateJitBufMes ( GenRandomIntInRange ( 0, 10 ) );
break;
case 1:
Protocol.CreateReqJitBufMes();
break;
case 2:
Protocol.CreateChanGainMes ( GenRandomIntInRange ( 0, 20 ),
GenRandomIntInRange ( -100, 100 ) );
break;
2009-09-04 10:03:48 +02:00
case 3:
2009-05-09 22:55:25 +02:00
Protocol.CreateServerFullMes();
break;
2009-09-04 10:03:48 +02:00
case 4:
2009-05-09 22:55:25 +02:00
Protocol.CreateReqConnClientsList();
break;
2009-09-04 10:03:48 +02:00
case 5:
2009-05-09 22:55:25 +02:00
Protocol.CreateChanNameMes ( QString ( "test%1" ).arg (
GenRandomIntInRange ( 0, 1000 ) ) );
break;
2009-05-09 14:24:33 +02:00
2009-09-04 10:03:48 +02:00
case 6:
2009-05-09 22:55:25 +02:00
Protocol.CreateChatTextMes ( QString ( "test%1" ).arg (
GenRandomIntInRange ( 0, 1000 ) ) );
break;
2009-09-04 10:03:48 +02:00
case 7:
2009-05-09 22:55:25 +02:00
Protocol.CreatePingMes ( GenRandomIntInRange ( 0, 100000 ) );
break;
2009-09-04 10:03:48 +02:00
case 8:
2009-05-09 22:55:25 +02:00
Protocol.CreateReqNetwTranspPropsMes();
break;
2009-09-04 10:03:48 +02:00
case 9:
Protocol.CreateAndImmSendAcknMess ( GenRandomIntInRange ( -10, 100 ),
2009-05-09 22:55:25 +02:00
GenRandomIntInRange ( -100, 100 ) );
break;
2009-09-04 10:03:48 +02:00
case 10:
2009-10-10 10:32:54 +02:00
Protocol.CreateAndImmSendDisconnectionMes();
break;
case 11:
2009-05-09 22:55:25 +02:00
// arbitrary "audio" packet (with random sizes)
CVector<uint8_t> vecMessage ( GenRandomIntInRange ( 1, 1000 ) );
OnSendProtMessage ( vecMessage );
break;
}
}
void OnSendProtMessage ( CVector<uint8_t> vecMessage )
{
UdpSocket.writeDatagram (
(const char*) &( (CVector<uint8_t>) vecMessage )[0],
2009-05-12 09:33:11 +02:00
vecMessage.Size(), QHostAddress ( sAddress ), iPort );
2009-05-09 14:24:33 +02:00
2009-05-09 22:55:25 +02:00
// reset protocol so that we do not have to wait for an acknowledge to
// send the next message
Protocol.Reset();
2009-05-09 14:24:33 +02:00
}
};
#endif /* !defined ( TESTBENCH_HOIHJH8_3_43445KJIUHF1912__INCLUDED_ ) */