Commit 5c42938a authored by Anne's avatar Anne

Merged develop into ba_2016_heimes

parents 4a5429e5 7acee70d
......@@ -54,6 +54,7 @@ public:
static const int NP_CLIENT_OPEN = 100;
static const int NP_CLIENT_CLOSE = 101;
static const int NP_CLIENT_WAITING_FOR_SAMPLES = 102;
static const int NP_SERVER_CLOSE = 200;
static const int NP_SERVER_OPEN = 201;
......
......@@ -25,13 +25,13 @@
#include <ITASampleFrame.h>
#include <string>
#include <vector>
class VistaTCPSocket;
class CITANetAudioStreamingServer;
class VistaConnectionIP;
class VistaTCPServer;
class VistaTCPSocket;
//! Realizes server functionality for network audio streaming
/**
......@@ -46,10 +46,11 @@ public:
std::string GetServerAddress() const;
int GetNetworkPort() const;
bool Start(const std::string& sAddress, int iPort);
VistaConnectionIP* GetConnetion() const;
void Disconnect();
bool Start( const std::string& sAddress, int iPort );
void Stop();
VistaConnectionIP* GetConnection() const;
bool IsConnected() const;
......
......@@ -21,6 +21,8 @@
#include <ITADataSourcesDefinitions.h>
#include <ITANetAudioProtocol.h>
#include <string>
#include <vector>
......@@ -33,6 +35,8 @@ class ITADatasource;
class CITANetAudioMessage;
class CITANetAudioProtocol;
class CITANetAudioServer;
class CITANetAudioMessage;
class VistaTCPSocket;
//! Network audio sample server (for connecting a net audio stream)
/**
......@@ -70,6 +74,8 @@ public:
void SetAutomaticUpdateRate();
bool LoopBody();
protected:
int Transmit( const ITASampleFrame& sfNewSamples, int iNumSamples );
ITADatasource* GetInputStream() const;
......@@ -78,12 +84,10 @@ private:
CITANetAudioServer* m_pNetAudioServer;
ITASampleFrame m_sfTempTransmitBuffer;
ITADatasource* m_pInputStream;
CITANetAudioProtocol* m_pProtocol;
CITANetAudioMessage* m_pMessage;
VistaConnectionIP* m_pConnection;
CITANetAudioProtocol::StreamingParameters m_oServerParams;
CITANetAudioMessage* m_pMessage;
int m_iUpdateStrategy;
int m_iClientRingBufferFreeSamples;
......
......@@ -10,8 +10,8 @@
static int S_nMessageIds = 0;
CITANetAudioMessage::CITANetAudioMessage( VistaSerializingToolset::ByteOrderSwapBehavior bSwapBuffers )
: m_vecIncomingBuffer()
, m_oOutgoing()
: m_vecIncomingBuffer( 2048 )
, m_oOutgoing( 2048 )
, m_pConnection( NULL )
{
m_oOutgoing.SetByteorderSwapFlag( bSwapBuffers );
......@@ -37,7 +37,6 @@ void CITANetAudioMessage::ResetMessage()
m_oOutgoing.ClearBuffer();
m_oOutgoing.WriteInt32( 0 ); // size dummy
m_oOutgoing.WriteInt32( 0 ); // type dummy
m_oOutgoing.WriteInt32( 0 ); // exceptmode dummy
m_oOutgoing.WriteInt32( 0 ); // ID
m_oIncoming.SetBuffer( NULL, 0 );
......@@ -82,19 +81,15 @@ void CITANetAudioMessage::WriteMessage()
try
{
int iSize = m_oOutgoing.GetBufferSize();
int nRet = m_pConnection->WriteRawBuffer( m_oOutgoing.GetBuffer(), iSize );
int iRawBufferSize = m_oOutgoing.GetBufferSize();
int nRet = m_pConnection->WriteRawBuffer( m_oOutgoing.GetBuffer(), iRawBufferSize );
m_pConnection->WaitForSendFinish();
if( nRet != m_oOutgoing.GetBufferSize() )
{
VistaExceptionBase ex( "ITANetAudioMessage::WriteMessage: Connection error", "CITANetAudioMessage", -1, -1 );
//throw( ex );
}
ITA_EXCEPT1( NETWORK_ERROR, "Could not write the expected number of bytes" );
}
catch( VistaExceptionBase& ex )
{
std::string sExceptionText = ex.GetExceptionText();
std::cerr << sExceptionText << std::endl;
//ITA_EXCEPT1(UNKNOWN, sExceptionText.c_str());
ITA_EXCEPT1( NETWORK_ERROR, ex.GetExceptionText() );
}
}
......@@ -114,7 +109,7 @@ void CITANetAudioMessage::ReadMessage()
nReturn = m_pConnection->ReadRawBuffer( &m_vecIncomingBuffer[ 0 ], nMessageSize );
if( nReturn != nMessageSize )
ITA_EXCEPT1( UNKNOWN, "Protokoll error, Received less bytes than expected" );
ITA_EXCEPT1( NETWORK_ERROR, "Protokoll error, Received less bytes than expected" );
m_oIncoming.SetBuffer( &m_vecIncomingBuffer[ 0 ], nReturn );
......@@ -161,12 +156,15 @@ void CITANetAudioMessage::WriteAnswer()
VistaSerializingToolset::Swap4( &iSwapDummy );
memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
try {
try
{
int nRet = m_pConnection->WriteRawBuffer( m_oOutgoing.GetBuffer(), m_oOutgoing.GetBufferSize() );
m_pConnection->WaitForSendFinish();
if( nRet != m_oOutgoing.GetBufferSize() )
ITA_EXCEPT1( UNKNOWN, "Could not write the expected number of bytes" );
}
catch( VistaExceptionBase& ex ) {
catch( VistaExceptionBase& ex )
{
ITA_EXCEPT1( UNKNOWN, ex.GetExceptionText() );
}
}
......@@ -186,7 +184,8 @@ void CITANetAudioMessage::ReadAnswer()
nReturn = -1; // Network connection error
}
if( nReturn != sizeof( VistaType::sint32 ) ) {
if( nReturn != sizeof( VistaType::sint32 ) )
{
ITA_EXCEPT1( UNKNOWN, "Protokoll error, Received less bytes than expected" );
}
......@@ -196,6 +195,7 @@ void CITANetAudioMessage::ReadAnswer()
if( nMessageSize > ( int ) m_vecIncomingBuffer.size() )
m_vecIncomingBuffer.resize( nMessageSize );
// jst: hier nicht while( nReturn < nMessageSize) ReadRawBuffer??
nReturn = m_pConnection->ReadRawBuffer( &m_vecIncomingBuffer[ 0 ], nMessageSize );
if( nReturn != nMessageSize )
ITA_EXCEPT1( UNKNOWN, "Protokoll error, Received less bytes than expected" );
......@@ -216,7 +216,6 @@ void CITANetAudioMessage::ReadAnswer()
try
{
m_nAnswerType = ReadInt(); // TODO: assert weg, dafr Kontrolle falls Server crasht<
ReadInt(); // protocol overhead - just read and ignore
int nMessageID = ReadInt();
assert( nMessageID == m_nMessageId );
m_nMessageId = nMessageID;
......@@ -245,6 +244,11 @@ void CITANetAudioMessage::SetAnswerType( int nType )
m_nAnswerType = nType;
}
int CITANetAudioMessage::GetAnswerType() const
{
return m_nAnswerType;
}
int CITANetAudioMessage::GetIncomingMessageSize() const
{
return m_oIncoming.GetTailSize();
......
......@@ -22,6 +22,7 @@
CITANetAudioServer::CITANetAudioServer()
: m_pServer( NULL )
, m_pSocket( NULL )
, m_pConnection( NULL )
, m_iServerPort( -1 )
, m_pConnection( NULL )
{
......@@ -29,6 +30,8 @@ CITANetAudioServer::CITANetAudioServer()
CITANetAudioServer::~CITANetAudioServer()
{
delete m_pConnection;
delete m_pServer;
}
std::string CITANetAudioServer::GetServerAddress() const
......@@ -52,30 +55,35 @@ bool CITANetAudioServer::Start(const std::string& sAddress, int iPort)
// blocking wait for connection
m_pSocket = m_pServer->GetNextClient();
if( !m_pSocket )
return false;
if( m_pSocket->GetIsConnected() )
m_pConnection = new VistaConnectionIP( m_pSocket );
if( !m_pConnection )
return false;
if (m_pSocket != NULL)
m_pConnection = new VistaConnectionIP(m_pSocket);
return true;
return false;
return true;
}
VistaConnectionIP* CITANetAudioServer::GetConnetion() const
VistaConnectionIP* CITANetAudioServer::GetConnection() const
{
return m_pConnection;
}
void CITANetAudioServer::Disconnect()
void CITANetAudioServer::Stop()
{
delete m_pConnection;
m_pConnection = NULL;
m_pSocket = NULL;
delete m_pServer;
m_pServer = NULL;
}
bool CITANetAudioServer::IsConnected() const
{
if( !m_pSocket )
return false;
return m_pSocket->GetIsConnected();
}
\ No newline at end of file
return m_pConnection ? true : false;
}
......@@ -13,16 +13,17 @@ CITANetAudioStreamingClient::CITANetAudioStreamingClient( CITANetAudioStream* pP
{
m_pClient = new CITANetAudioClient();
m_oParams.iChannels = pParent->GetNumberOfChannels();
m_oParams.dSampleRate = pParent->GetSampleRate();
m_oParams.iBlockSize = pParent->GetBlocklength();
m_oClientParams.iChannels = pParent->GetNumberOfChannels();
m_oClientParams.dSampleRate = pParent->GetSampleRate();
m_oClientParams.iBlockSize = pParent->GetBlocklength();
m_pMessage = new CITANetAudioMessage( VistaSerializingToolset::SWAPS_MULTIBYTE_VALUES );
}
CITANetAudioStreamingClient::~CITANetAudioStreamingClient()
{
if( m_pConnection )
{
m_pMessage->ResetMessage();
m_pMessage->SetMessageType( CITANetAudioProtocol::NP_CLIENT_CLOSE );
m_pMessage->WriteAnswer();
}
......@@ -38,19 +39,24 @@ bool CITANetAudioStreamingClient::Connect( const std::string& sAddress, int iPor
m_pConnection = m_pClient->GetConnection();
m_pMessage->ResetMessage();
m_pMessage->SetConnection( m_pConnection );
// Validate streaming parameters of server and client
m_pMessage->ResetMessage();
m_pMessage->SetMessageType( CITANetAudioProtocol::NP_CLIENT_OPEN );
m_pMessage->WriteStreamingParameters( m_oParams );
//m_pMessage->WriteStreamingParameters( m_oClientParams ); // Not yet
m_pMessage->WriteInt( 42 );
m_pMessage->WriteMessage();
m_pMessage->ReadAnswer();
// NOTE: Wieso speichern wir beie paras, nach einmal vergleichen, sollten doch sowieso beide gleich sein
assert( m_pMessage->GetAnswerType() == CITANetAudioProtocol::NP_SERVER_OPEN );
int i42 = m_pMessage->ReadInt();
/* Not yet
CITANetAudioProtocol::StreamingParameters oServerParams = m_pMessage->ReadStreamingParameters();
if (!(oServerParams == m_oParams))
ITA_EXCEPT1( INVALID_PARAMETER, "Streaming parameters of network audio server and client do not match." );
*/
Run();
......
#include <ITANetAudioStreamingServer.h>
#include <ITANetAudioServer.h>
#include <ITANetAudioMessage.h>
// ITA includes
#include <ITADataSource.h>
......@@ -10,7 +11,6 @@
// Vista includes
#include <VistaInterProcComm/Concurrency/VistaThreadLoop.h>
#include <VistaInterProcComm/Connections/VistaConnectionIP.h>
#include <VistaInterProcComm/IPNet/VistaTCPServer.h>
#include <VistaInterProcComm/IPNet/VistaTCPSocket.h>
#include <VistaBase/VistaTimeUtils.h>
#include <VistaInterProcComm/IPNet/VistaIPAddress.h>
......@@ -25,33 +25,35 @@ CITANetAudioStreamingServer::CITANetAudioStreamingServer()
, m_pConnection( NULL )
{
m_pNetAudioServer = new CITANetAudioServer();
// TODO: Init members
}
bool CITANetAudioStreamingServer::Start(const std::string& sAddress, int iPort)
bool CITANetAudioStreamingServer::Start( const std::string& sAddress, int iPort )
{
// TODO: vorrckgabe noch anfangen zu senden (Samples)
if (!m_pNetAudioServer->Start(sAddress, iPort))
// TODO: vorrckgabe noch anfangen zu senden (Samples)
if( !m_pNetAudioServer->Start( sAddress, iPort ) ) // blocking
return false;
m_pConnection = m_pNetAudioServer->GetConnetion();
m_pConnection = m_pNetAudioServer->GetConnection();
m_pMessage = new CITANetAudioMessage( m_pConnection->GetByteorderSwapFlag() );
m_pMessage->ResetMessage();
m_pMessage->SetConnection( m_pConnection );
m_pMessage->ReadMessage(); // blocking
// Get Streaming Parameters from Client
m_pMessage->ReadAnswer();
CITANetAudioProtocol::StreamingParameters m_oServerParams = m_pMessage->ReadStreamingParameters();
// Send Streaming Parameters from Client back
m_pMessage->ResetMessage();
m_pMessage->SetMessageType(CITANetAudioProtocol::NP_SERVER_OPEN);
m_pMessage->WriteStreamingParameters(m_oServerParams);
m_pMessage->WriteMessage();
int nMT = m_pMessage->GetMessageType();
assert( nMT == CITANetAudioProtocol::NP_CLIENT_OPEN );
int i42 = m_pMessage->ReadInt();
//CITANetAudioProtocol::StreamingParameters oClientParams = m_pMessage->ReadStreamingParameters();
Run();
return true;
m_pMessage->SetAnswerType( CITANetAudioProtocol::NP_SERVER_OPEN );
m_pMessage->WriteInt( 2 * 42 );
m_pMessage->WriteAnswer();
if( m_pInputStream )
Run();
return true;
}
bool CITANetAudioStreamingServer::IsClientConnected() const
......@@ -75,12 +77,19 @@ bool CITANetAudioStreamingServer::LoopBody()
void CITANetAudioStreamingServer::Stop()
{
m_pNetAudioServer->Disconnect();
m_pNetAudioServer->Stop();
}
void CITANetAudioStreamingServer::SetInputStream( ITADatasource* pInStream )
{
if( VistaThreadLoop::IsRunning() )
ITA_EXCEPT1( MODAL_EXCEPTION, "Streaming loop already running, can not change input stream" );
m_pInputStream = pInStream;
m_sfTempTransmitBuffer.init( m_pInputStream->GetNumberOfChannels(), m_pInputStream->GetBlocklength(), true );
if( m_pConnection )
Run();
}
int CITANetAudioStreamingServer::GetNetStreamBlocklength() const
......@@ -98,6 +107,33 @@ void CITANetAudioStreamingServer::SetAutomaticUpdateRate()
m_iUpdateStrategy = AUTO;
}
bool CITANetAudioStreamingServer::LoopBody()
{
m_pMessage->ResetMessage();
m_pMessage->SetConnection( m_pConnection );
m_pMessage->ReadMessage(); // blocking
switch( m_pMessage->GetMessageType() )
{
case CITANetAudioProtocol::NP_CLIENT_WAITING_FOR_SAMPLES:
if( m_pInputStream )
{
for( int i = 0; i < m_pInputStream->GetNumberOfChannels(); i++ )
{
ITAStreamInfo oStreamInfo;
const float* pfData = m_pInputStream->GetBlockPointer( i, &oStreamInfo );
m_sfTempTransmitBuffer[ i ].write( pfData, m_pInputStream->GetBlocklength() );
}
}
//m_pMessage->WriteSampleFrame( &m_sfTempTransmitBuffer );
m_pMessage->WriteAnswer();
break;
}
return true;
}
ITADatasource* CITANetAudioStreamingServer::GetInputStream() const
{
return m_pInputStream;
......
......@@ -20,7 +20,7 @@ int main( int , char** )
//oStreamingServer.SetInputStream( &oGenerator );
cout << "Starting server and waiting for connections on '" << g_sServerName << "' on port " << g_iServerPort << endl;
//oStreamingServer.Start(g_sServerName, g_iServerPort);
oStreamingServer.Start( g_sServerName, g_iServerPort );
int iKey;
cin >> iKey;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment