⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cxx

📁 performance analysis in H.323 environments. Open H.323 Call Generator是一个OpenH323 (www.openh323.org)子
💻 CXX
📖 第 1 页 / 共 2 页
字号:
}

///////////////////////////////////////////////////////////////////////////////
COH323Capability::COH323Capability(PINDEX codec_index, 
				   unsigned sub_type, 
				   const char *name)
: H323AudioCapability(64,64), m_CodecIndex(codec_index), m_SubType(sub_type), m_Name(name)
{ 
}

COH323Capability::~COH323Capability() 
{ 
}

///////////////////////////////////////////////////////////////////////////////
COH323LineDevice::COH323LineDevice(const char *name, 
				   COH323EndPoint &endpt, 
				   ECallDirection dir)
: m_Endpt(endpt), m_Name(name), m_Dir(dir), m_State(STATE_IDLE)
{
}

BOOL COH323LineDevice::IsReadyForIncomingCall() const
{
    return m_ConnectionToken.IsEmpty()==TRUE;
}

BOOL COH323LineDevice::IsReadyForOutgoingCall() const
{
    return m_ConnectionToken.IsEmpty()==TRUE;
}

STATUS COH323LineDevice::Connect(const char *remote_host)
{
    PAssert(IsReadyForOutgoingCall(),PLogicError);

    cout << m_Name << " Connecting to " << remote_host << endl;

    m_ConnectionToken = m_Endpt.MakeOutgoingCall(*this, remote_host);
    if (!m_ConnectionToken.IsEmpty()) {
	m_State = STATE_BUSY;
	return OK;
    } else
	return NOT_OK;
}

STATUS COH323LineDevice::Listen()
{
    return m_Endpt.ListenForIncomingCalls() ? OK : NOT_OK;
}

STATUS COH323LineDevice::Hangup()
{
    if (m_ConnectionToken.IsEmpty()) {
	cout << m_Name << "Can't hangup because connection token is empty" << endl;
	return OK;
    }

    cout << m_Name << " Hanging up..." << endl;
    if (!m_Endpt.ClearCall(m_ConnectionToken))
	cout << m_Name << "Could not hangup" << endl;
    return OK;
}

void COH323LineDevice::OnConnectionEstablished(const PString &token)
{
    cout << m_Name << " Connection established" << endl;

    m_ConnectionToken = token;

    m_Mutex.Wait();
    m_State=STATE_CONNECTED;
    ++m_NConnected;
    m_Mutex.Signal();
}

void COH323LineDevice::OnConnectionCleared(const PString&)
{
    cout << m_Name << " Connection cleared" << endl;
    m_ConnectionToken = "";
    
    if (m_State==STATE_CONNECTED) {
	m_Mutex.Wait();
	--m_NConnected;
	m_Mutex.Signal();
    }

    m_State=STATE_IDLE;
}

void COH323LineDevice::OnIncomingConnection(const PString &token)
{
    cout << m_Name << " Incoming connection" << endl;
    m_ConnectionToken = token;
    m_State=STATE_BUSY;
}

void COH323LineDevice::OnAnswerCall()
{
}

/*
void COH323LineDevice::
OnUserInputString( const PString & value )
{
    cout << m_Name << " OnUserInputString: " << value << endl;
}
*/

PObject *COH323LineDevice::Clone() const
{
    COH323LineDevice *newline=new COH323LineDevice(m_Name,m_Endpt,m_Dir);
    newline->m_ConnectionToken = m_ConnectionToken;
    newline->m_State = m_State;
    return newline;
}

///////////////////////////////////////////////////////////////////////////////
void MyMain::Main()
{
    PArgList & args = GetArguments();
    args.Parse(	"l-listen."
		"n-lines:"
		"t-trace-level:"
		"1-d1:"
		"2-d2:"
		"3-d3:"
		"4-d4:"
		"r-repeat:"
		"g-gatekeeper:"
		"i-interface:"
		"u-user:"
		"o-output:"
		"N-no-gatekeeper."
		"-require-gatekeeper."
		, FALSE);

    if (args.GetCount()==0 && !args.HasOption('l')) {
	cout << "Usage:\n"
	        "  callgen323 [options] -l\n"
	        "  callgen323 [options] dest-host\n"
	        "where options:\n"
		"  -l                   Passive/listening mode.\n"
	        "  -n --lines   number  Number of lines [1]\n"
	        "  -1 --d1     second   Timeout to wait until all lines are connected [60]\n"
		"  -2 --d2     second   Delay before tearing down the calls after connected [2]\n"
                "  -3 --d3     second   Timeout to wait until all lines are disconnected [60]\n"
                "  -4 --d4     second   Delay to wait before starting the next batch [2]\n"
	        "  -r --repeat number   Number of batches [1]\n"
	        "  -t --trace-level lvl Trace level [0]\n"
                "  -g --gatekeeper host Specify gatekeeper host\n"
                "  -i --interface addr  Specify IP address to bind to\n"
		"  -u --user username   Specify local username\n"
		"  -o --output file     Specify filename for trace output [stdout]\n"
                "  -N --no-gatekeeper   Disable gatekeeper discovery [false]\n"
                "  --require-gatekeeper Exit if gatekeeper discovery fails\n";
	return;
    }

    const int number = args.HasOption('n') ? args.GetOptionString('n').AsUnsigned() : 1;
    const int trace_level = args.HasOption('t') ? args.GetOptionString('t').AsUnsigned() : 0;

    //PTrace::SetOptions(PTrace::Thread);
    PTrace::SetLevel(trace_level);
    if (args.HasOption('o')) {
    /* This is a bit tricky. We allocate the file for tracing on the heap, and
       we never actually deallocate it. So it come up as a leak on exit if
       memory checking is on. We cannot deallocate it as during termination
       some PTRACE's may get executed and attempt to trace to a closed (and
       deleted!) file object which crashes. Not good.
     */
	PTextFile * output = new PTextFile;
	if (output->Open(args.GetOptionString('o'), PFile::WriteOnly))
	    PTrace::SetStream(output);
	else {
	    cout << "Warning: could not open trace output file \""
		 << args.GetOptionString('o') << '"' << endl;
	    delete output;
	}
    }

    COH323EndPoint endpt;
    endpt.Initialise(args);

    if (args.HasOption('l')) {
	for (int i=0; i<number; ++i) {
	    char name[16];

	    sprintf( name, "line%d", i);
	    COH323LineDevice *line_device = endpt.CreateLineDevice(name, eCD_INCOMING);
	    line_device->Listen();
	}
	char key[10];
	cout << "Endpoint is listening for incoming calls. Number of lines=" << number << endl;
	cout << "Press ENTER at any time to quit" << endl;
	fgets( key, 9, stdin);

    } else {
	const int repeat=args.HasOption('r') ? args.GetOptionString('r').AsUnsigned() : 1;
	const int d1=args.HasOption('1') ? args.GetOptionString('1').AsUnsigned() : 60;
	const int d2=args.HasOption('2') ? args.GetOptionString('2').AsUnsigned() : 2;
	const int d3=args.HasOption('3') ? args.GetOptionString('3').AsUnsigned() : 60;
	const int d4=args.HasOption('4') ? args.GetOptionString('4').AsUnsigned() : 2;
	const int timer_ms_divider=50;

	PTimeInterval total_calling_time, total_hangingup_time;
	int n_successfull_calls=0, n_failed_calls=0;

	LineDeviceList line_devices;
	line_devices.DisallowDeleteObjects();

	// creating line devices
	for (int i=0; i<number; ++i) {
	    char name[16];
	    sprintf( name, "line%d", i);
	    COH323LineDevice *line_device = endpt.CreateLineDevice(name, eCD_OUTGOING);
	    line_devices.Append(line_device);
	}

	// repeating batches
	for (int r=1; r<=repeat; ++r) {
	    unsigned int l;

	    cout << "Repeat " << r << " of " << repeat << endl;

	    PTimeInterval t_calling_time, t_hangingup_time;
	    PTimeInterval t_begin = PTimer::Tick();

	    // connect all lines
	    for (l=0; l<line_devices.GetSize(); ++l) {
		if (line_devices[l].IsReadyForOutgoingCall())
		    line_devices[l].Connect( args[0] );
		else
		    cout << "Can't dial: " 
			 << line_devices[l].GetName() << " is busy." << endl;
	    }

	    // wait until all lines are connected
	    int d;
	    cout << "Waiting until all lines are connected" << endl;
	    for (d=0; d<d1*timer_ms_divider && COH323LineDevice::GetNConnected()!=number; ++d) {
		PThread::Current()->Sleep(1000/timer_ms_divider);
	    }
	    if (COH323LineDevice::GetNConnected()!=number) {
		t_calling_time = PTimeInterval(0,d1);

		cout << number-COH323LineDevice::GetNConnected() 
		     << " line(s) is still not connected after "
		     << d1 << " seconds." << endl;

		n_successfull_calls += COH323LineDevice::GetNConnected();
		n_failed_calls += number-COH323LineDevice::GetNConnected();

	    } else {
		PTimeInterval t_end = PTimer::Tick();
		t_calling_time = t_end - t_begin;
		cout << "Connected after " << t_calling_time << " milliseconds." << endl;
		n_successfull_calls += COH323LineDevice::GetNConnected();
		cout << "Sleeping for " << d2 << " second(s)." << endl;
		PThread::Current()->Sleep(d2 * 1000);
		cout << "Hanging up lines" << endl;
	    }

	    t_begin = PTimer::Tick();

	    // hangup all lines
	    for (l=0; l<line_devices.GetSize(); ++l) 
		line_devices[l].Hangup();

	    // wait until all lines are free
	    cout << "Waiting until all lines are free" << endl;
	    d = 0;
	    while (COH323LineDevice::GetNConnected() && d < d3*timer_ms_divider) {
		PThread::Current()->Sleep(1000/timer_ms_divider);
		endpt.ClearAllCalls();
		++d;
	    }

	    PTimeInterval t_end = PTimer::Tick();
	    t_hangingup_time = t_end - t_begin;

	    total_calling_time += t_calling_time;
	    total_hangingup_time += t_hangingup_time;

	    cout << "Hangup is finished in " << t_hangingup_time << " milliseconds" << endl;

	    // summary
	    cout << "Total successfull calls: " << n_successfull_calls << endl
		 << "Total failed calls: " << n_failed_calls << endl
		 << endl;

	    cout << "Sleeping for " << d4 << " second(s)." << endl;
	    PThread::Current()->Sleep(d4*1000);

	}
	cout << "Total time to make "
	     << (n_successfull_calls + n_failed_calls)
	     << " calls is " << total_calling_time << " ms" << endl;

        cout << "Total time to hangup calls is "
	     << total_hangingup_time << " ms" << endl;
    }

    endpt.ClearAllCalls();


    /*
    cout << "Waiting until all lines are free" << endl;
    while (COH323LineDevice::GetNConnected()) {
	PThread::Current()->Sleep(1000);
    }
    */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -