📄 ss7_mtp2_array.cpp~
字号:
// File Name: SS7_MTP2_Array.cpp/////////////////////////////////////////////////#include <sys/poll.h>#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include "SS7_MTP2_Array.h"#define ReceiveMsg_Buf_Size 1024int inFileIndex; // for UTSS7_MTP2_Array::SS7_MTP2_Array(int _portCount, MTP2Config * _portConfigList) : mtp2_port_count(_portCount), mtp2Array(NULL), toTerminate(0), mtp2ConfigList(_portConfigList), threadTransmit(0), threadReceive(0), threadCheckTimers(0){ if( mtp2_port_count > 0 ) { printf("SS7_MTP2_Array::SS7_MTP2_Array() new %d SS7_MTP2\n", _portCount ); mtp2Array = new SS7_MTP2 [mtp2_port_count]; if( mtp2Array == NULL ) { // error handling } for(int i=0 ; i<mtp2_port_count ; i++ ) { mtp2Array[i].setPortNumber( i ); // use the value from cfg // 0,1,2,...,7 // updating bufferSize (reSize) } } inFileIndex = 0; // for UT}SS7_MTP2_Array::~SS7_MTP2_Array(void){ if( mtp2Array ) { delete [] mtp2Array; }}// Seems it's part of the DAEDT// Now we're NOT running the following thread,// because we send MSU to L1 actively instead of negatively
void * TransmitMsg(void * arg) // L2 downto L1
{
SS7_MTP2_Array * ptrSS7Mtp2Array = (SS7_MTP2_Array *) arg;
while( ! ptrSS7Mtp2Array->toTerminate )
{
// int sentNumber = 0;
for(int i = 0 ; i < ptrSS7Mtp2Array->mtp2_port_count ; i++ )
{
// send one message
// sentNumber += ptrSS7Mtp2Array->mtp2Array[i].sendMessage( 1 );
}
// according to the number of sent messages, determine waiting time
usleep( 20000 ); // 20 ms printf("TransmitMsg() - after usleep\n");
}
return NULL;}
int getNextRevSigUnit(unsigned char * tmpbuf, int head, int tail, RevSigUnit * _pRevSU){// printf( "getNextRevSigUnit() - head=%d,tail=%d\n", head, tail ); if( (unsigned int)(tail - head) < sizeof(unsigned short) ) { printf("data too short for SU Length!\n"); return 0; // == 0 } memcpy( (unsigned char *)& _pRevSU->Length, tmpbuf, sizeof(unsigned short) ); // unsigned short len = *( (unsigned short*)(tmpbuf + head) );// printf( "getNextRevSigUnit() - RevSU Length = %d\n", _pRevSU->Length ); if( (tail - head) < _pRevSU->Length ) // _pRevSU->Length includes all bytes { printf("Data length = %d BIGGER than data in the buffer %d!\n", _pRevSU->Length, tail - head ); return 0; // == 0 } memcpy( (unsigned char *)_pRevSU, tmpbuf + head, _pRevSU->Length ); // printf("Got data at position %d with length = %d!\n", head, _pRevSU->Length ); return (int)(_pRevSU->Length);}// We're now running the following thread, because we must catch up with L1// on receiving all SUs from it.// And we need to measure the waterlevel for Congestion Control.void * ReceiveMsg(void * arg) // L2 from L1
{
printf("beginning ReceiveMsg()!\n"); int n, len; unsigned char tmpbuf[ReceiveMsg_Buf_Size]; int posForNextSU = 0; MTP2_FSM_Event event_out; SS7_MTP2_Array * ptrSS7Mtp2Array = (SS7_MTP2_Array *) arg;
inFileIndex = 0; // for UT char inFileName[256]; // for UT SUWithLength * pSUnLen = (SUWithLength *) malloc( sizeof(unsigned short) + sizeof(SUHeaderL2) + MAX_MSU_LEN );
RevSigUnit * pRevSU = (RevSigUnit *) malloc( REV_SU_HEADER_SIZE + sizeof(SUHeaderL2) + MAX_MSU_LEN ); if( (! pSUnLen) || (! pRevSU) ) { printf("Fail to malloc memory for SUWithLen and RevSigUnit\n" ); if( pSUnLen ) { free( pSUnLen ); pSUnLen = NULL; } if( pRevSU ) { free( pRevSU ); pRevSU = NULL; } return NULL; } printf("To reset the recvBuffer for each Link\n" ); for(int i = 0 ; i < ptrSS7Mtp2Array->mtp2_port_count ; i++ )
{
ptrSS7Mtp2Array->mtp2Array[i].recvBuffer.reset( );
}
while( ! ptrSS7Mtp2Array->toTerminate )
{// // when all the Links are ready// if( ptrSS7Mtp2Array->mtp2Array[0].getDAEDRState() != DAEDRStateInService )// {// printf("ReceiveMsg() - RC has NOT started\n");
// usleep( 200000 );// continue; //timeout
// }
/*************************************** n = select( ptrSS7Mtp2Array->getMaxfd() + 1, ptrSS7Mtp2Array->getRdfdSet(), ptrSS7Mtp2Array->getWrfdSet(), NULL, NULL );
if( n < 0 )
{
perror( "ss7gate: select" );
break;
}
else if( n == 0 )
{ printf( "Selected 0 bytes, wait for a while\n" ); // poll( NULL, 0, 100 ); usleep( 50000 );
continue; //timeout
}
// if there left some bytes which belong to next SU, take care of it
if( FD_ISSET( ptrSS7Mtp2Array->getDevfd(), ptrSS7Mtp2Array->getRdfdSet() ) )
{
len = read( ptrSS7Mtp2Array->getDevfd(), tmpbuf + posForNextSU, ReceiveMsg_Buf_Size - posForNextSU ); printf( "Read out %d bytes\n", len ); len += posForNextSU; // total bytes to process, from the beginning // printf( "Total buffer data len = %d for processing\n", len ); } else { printf( "Read out nothing\n" ); continue; // len = posForNextSU; }
*******************************/ sprintf( inFileName, IN_FILE_NAME_FMT, inFileIndex ); FILE * fp = fopen( (char *)inFileName, "rb" ); if( fp == NULL ) { // printf( "ReceiveMsg() - open file %s failed, wait ...\n", inFileName ); usleep( 100000 ); // printf("ReceiveMsg() - after usleep\n");
continue; //timeout
} inFileIndex ++; // printf("inFileIndex = %d!\n", inFileIndex - 1 ); fseek( fp, 0, SEEK_END ); int file_len = ftell( fp ); fseek( fp, 0, SEEK_SET ); // move to the beginning len = fread( tmpbuf + posForNextSU, 1, file_len , fp ); fclose( fp ); // if there left some bytes which belong to next SU, take care of it
if( len > 0 )
{
// printf("ReceiveMsg() - file len = %d, read out %d\n", file_len, len ); len += posForNextSU; // total bytes to process, from the beginning // printf( "Total buffer data len = %d for processing\n", len ); } else { // printf( "Read out nothing\n" ); inFileIndex --; usleep( 100000 ); continue; // len = posForNextSU; }
// process from the beginning posForNextSU = 0;
// parse the data in the buffer // copy them into corresponding buffer based on
// the port_number within the data
while( true )
{ // get next SU from tmpbuf int lenCurrentSU = getNextRevSigUnit( tmpbuf, posForNextSU, len, pRevSU ); printf( "Get current SU length = %d\n", lenCurrentSU ); if( lenCurrentSU == 0 ) // need to wait for next "read" { // move left bytes to the beginning of the buffer for next "read" memcpy( tmpbuf, tmpbuf + posForNextSU, len - posForNextSU ); posForNextSU = len - posForNextSU;
printf( "Thre are %d bytes left\n", posForNextSU ); break;
} // position for next SU posForNextSU += lenCurrentSU;
if( pRevSU->Type != 0 ) // suppose using 0, this is not a correct SU { printf( "This SU Type = %d\n", pRevSU->Type ); // will improve this later, based on new rule // notify the reception of SU to DAEDR: SU Received wrong SU event_out.FSMType = MTP2_FSM_DAEDR; event_out.name = DAEDREventSUReceived; event_out.param = REV_SU_NOT_CORRECT; ptrSS7Mtp2Array->mtp2Array[pRevSU->ChName].eventQueue.write( (unsigned char*)(&event_out), sizeof(event_out)); } else // correct SU { pSUnLen->len = pRevSU->Length - REV_SU_HEADER_SIZE; memcpy( (unsigned char *)& pSUnLen->header, (unsigned char *) pRevSU->PktBuf, pSUnLen->len ); // printf( "ReceiveMsg() - Corret SU, length=%d, LI=%d\n",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -