📄 asmrulep.cpp
字号:
" \
#(16000 > $Bandwidth) && (($Bandwidth >= 8000)), \
TolerablePacketLoss=2, \
AverageBandwidth=8000, \
AverageBandwidthStd=0, \
Foo=\"This is a test\", \
Bar=\"This wu'z a test\", \
Priority=7; \
"
/* Rule 4 */
" \
# ($Bandwidth > 8000), \
AverageBandwidth=4000, \
Priority=7; \
"
};
ASMRuleBook r(pRuleBook);
for (int i = 0; i < 25000; i += 1000)
{
IHXValues* pVal = new CHXHeader;
IHXBuffer* pBuffer = new CHXBuffer;
char s[1024]; /* Flawfinder: ignore */
//printf ("%d\n", i);
sprintf (s, "%d", i); /* Flawfinder: ignore */
pBuffer->Set((unsigned char *)s, 5);
pVal->SetPropertyCString("Bandwidth", pBuffer);
pVal->AddRef();
pBuffer = new CHXBuffer;
sprintf (s, "%d", 60); /* Flawfinder: ignore */
pBuffer->Set((unsigned char *)s, 5);
pVal->SetPropertyCString("Cpu", pBuffer);
pVal->AddRef();
if (i == 0)
{
printf ("Threshold Points: ");
float pThreshold[1024];
memset (pThreshold, 0xffffffff, sizeof(float) * 1024);
UINT32 ulNum = 0;
r.GetPreEvaluate(pThreshold, ulNum, pVal, "Bandwidth");
for (int j = 0; j < ulNum; j++)
{
printf ("%.2f ", pThreshold[j]);
}
printf ("\n");
}
BOOL pSubInfo[1024];
r.GetSubscription(pSubInfo, pVal);
printf (" bw %5d: %d %d %d %d\n", i,
pSubInfo[0], pSubInfo[1], pSubInfo[2], pSubInfo[3], pSubInfo[4]);
pVal->Release();
}
return 0;
}
#endif
HX_RESULT
ASMRuleBook::InitRulesArray()
{
if( !m_pValidRulesArray )
{
m_pValidRulesArray = new BOOL[ m_unNumRules ];
if(!m_pValidRulesArray)
{
return HXR_OUTOFMEMORY;
}
for( int ii=0; ii<m_unNumRules; ii++ )
{
m_pValidRulesArray[ii] = TRUE;
}
}
if( !m_pDeletedRulesArray )
{
m_pDeletedRulesArray = new BOOL[ m_unNumRules ];
if(!m_pDeletedRulesArray)
{
HX_VECTOR_DELETE(m_pValidRulesArray);
return HXR_OUTOFMEMORY;
}
for( int ii=0; ii<m_unNumRules; ii++ )
{
m_pDeletedRulesArray[ii] = FALSE;
}
}
return HXR_OK;
}
HX_RESULT
ASMRuleBook::Enable( UINT16 nRule )
{
// Allocate and initialized m_pValidRulesArray if necessary.
if( HXR_OUTOFMEMORY == InitRulesArray() )
{
return HXR_OUTOFMEMORY;
}
else
{
m_pValidRulesArray[nRule] = TRUE;
}
return HXR_OK;
}
HX_RESULT
ASMRuleBook::Disable( UINT16 nRule )
{
// Allocate and initialized m_pValidRulesArray if necessary.
if( HXR_OUTOFMEMORY == InitRulesArray() )
{
return HXR_OUTOFMEMORY;
}
else
{
m_pValidRulesArray[nRule] = FALSE;
}
return HXR_OK;
}
HX_RESULT
ASMRuleBook::ReCompute()
{
// We should call a Reset function here
if( HXR_OUTOFMEMORY == Reset() )
{
return HXR_OUTOFMEMORY;
}
for( int ii=0; ii<m_unNumRules; ii++ )
{
if( m_pValidRulesArray[ ii ] == FALSE )
{
DeleteRule( ii );
}
}
return HXR_OK;
}
// Remove a rule from the rulebook and 'collapse' the surrounding rules
// to cover the bandwidth range that up to now was covered by this rule.
HX_RESULT
ASMRuleBook::DeleteRule( int uRuleToDel )
{
int nLeftEdge = 0;
int nRightEdge = 0;
ULONG32 uNumActiveRules = 0;
int ii;
// The rule should not be greater the max, of course.
if( uRuleToDel > m_unNumRules )
{
return HXR_FAIL;
}
// Find the "right edge" rule.
for( ii=0; ii<m_unNumRules; ii++ )
{
if( !m_pDeletedRulesArray[ii] && m_pRules[ii].m_pRuleExpression->IsRightEdge() )
{
nRightEdge = ii;
break;
}
}
// Find the "left edge" rule.
for( ii=m_unNumRules-1; ii>=0; ii-- )
{
if( ( m_pDeletedRulesArray[ii] == FALSE ) && ( m_pRules[ ii ].m_pRuleExpression->GetLeft() || m_pRules[ ii ].m_pRuleExpression->GetRight() ) )
{
nLeftEdge = ii;
}
}
// Determine the number of active rules.
for( ii=0; ii<m_unNumRules; ii++ )
{
if( !m_pDeletedRulesArray[ii] )
{
uNumActiveRules++;
}
}
// We handle the 3 possible cases separately: 1) Left Edge, 2) Right Edge,
// and 3) Non-Edge.
if( uRuleToDel == nLeftEdge )
{
// Left edge case
// Modify the next rule(s) to the right so that it's > value is 0.
// But only if this is the only rule covering uRuleToDel's bw range.
if( CheckCurrentRangeEmpty( uRuleToDel ) == TRUE )
{
for( ii=uRuleToDel+1; ii<m_unNumRules; ii++ )
{
if( !m_pDeletedRulesArray[ ii ] && (
( m_pRules[ ii ].m_pRuleExpression->GetLeft() == m_pRules[ uRuleToDel ].m_pRuleExpression->GetRight() ) ) )
{
m_pRules[ ii ].m_pRuleExpression->SetLeft( 0 );
}
}
}
}
else if( uRuleToDel >= nRightEdge )
{
// Right edge case
// Set the right value (less than x) to infinity
// But only if this is the only rule covering uRuleToDel's bw range.
if( CheckCurrentRangeEmpty( uRuleToDel ) == TRUE )
{
for( ii=uRuleToDel-1; ii>=0; ii-- )
{
if( !m_pDeletedRulesArray[ ii ] && (
( m_pRules[ ii ].m_pRuleExpression->GetRight() == m_pRules[ uRuleToDel ].m_pRuleExpression->GetRight() ) ) )
{
m_pRules[ ii ].m_pRuleExpression->SetRight( RULE_VAL_INFINITY );
}
}
}
}
else if( uRuleToDel > nLeftEdge )
{
// Non-edge case
// Set all "left-adjacent" rules "right-hand" value to nRight.
// But only if this is the only rule covering uRuleToDel's bw range.
if( CheckCurrentRangeEmpty( uRuleToDel ) == FALSE )
{
// Determine the value of our right-hand value.
int nRight = (int) m_pRules[ uRuleToDel ].m_pRuleExpression->GetRight();
for( ii=uRuleToDel-1; ii>=0; ii-- )
{
if( !m_pDeletedRulesArray[ ii ] && (
( m_pRules[ ii ].m_pRuleExpression->GetRight() == m_pRules[ uRuleToDel ].m_pRuleExpression->GetLeft() ) ) )
{
m_pRules[ ii ].m_pRuleExpression->SetRight( nRight );
}
}
}
}
m_pDeletedRulesArray[uRuleToDel] = TRUE;
return HXR_OK;
}
void
ASMRuleExpression::SetLeft( int nLeft )
{
int nRight = ((IntegerNode*) ((VariableNode*)m_pHead->m_pRight))->m_Data;
if( nRight == 1 )
{
((IntegerNode*)(m_pHead->m_pLeft->m_pRight))->m_Data = nLeft;
}
}
void
ASMRuleExpression::SetRight( int nRight )
{
int nTmpRight = 0;
if( m_pHead && m_pHead->m_pRight )
{
nTmpRight = ((IntegerNode*) ((VariableNode*)m_pHead->m_pRight))->m_Data;
}
if( nTmpRight == 1 )
{
( (IntegerNode*)((VariableNode*) ((VariableNode*)m_pHead->m_pRight)->m_pRight) )->m_Data = nRight;
}
else if( nTmpRight != 0 )
{
((VariableNode*)m_pHead->m_pRight)->m_Data = (char*)nRight;
}
}
float
ASMRuleExpression::GetLeft()
{
int nLeft = 0;
int nRight = 0;
if( m_pHead && m_pHead->m_pRight )
{
nRight = ((IntegerNode*) ((VariableNode*)m_pHead->m_pRight))->m_Data;
}
if( nRight == 1 )
{
nLeft = ((IntegerNode*) ((VariableNode*)m_pHead->m_pLeft->m_pRight))->m_Data;
}
return (float)nLeft;
}
float
ASMRuleExpression::GetRight()
{
int nRight = 0;
if( m_pHead && m_pHead->m_pRight )
{
nRight = ((IntegerNode*) ((VariableNode*)(m_pHead->m_pRight)))->m_Data;
}
if( nRight == 1 )
{
nRight = ((IntegerNode*) ((VariableNode*)m_pHead->m_pRight->m_pRight))->m_Data;
}
return (float)nRight;
}
int
ASMRuleExpression::GetOperatorAsInt()
{
return (int) ((OperatorNode*) m_pHead)->m_Data;
}
// This function checks to see if the current rule's operator is GREATER or
// GREATEROREQUAL, both of which would suggest that we are the "right edge"
// rule.
BOOL
ASMRuleExpression::IsRightEdge()
{
if( m_pHead )
{
switch(((OperatorNode*)m_pHead)->m_Data)
{
case HX_RE_GREATER:
case HX_RE_GREATEREQUAL:
return TRUE;
break;
default:
return FALSE;
break;
}
}
else
{
return FALSE;
}
}
HX_RESULT
ASMRuleBook::Reset()
{
int i = 0;
{ //XXXSMPNOW
BOOL bSeenExpression = 0;
//printf ("******* Rule %d\n", i);
const char* pRuleBook = m_pRuleBook;
while (*pRuleBook)
{
char pTemp[2048 + 1];
int n = 0;
BOOL bSingleQuote = 0;
BOOL bDoubleQuote = 0;
for (;((*pRuleBook) && (bDoubleQuote || ((*pRuleBook != ',') && (*pRuleBook != ';')))); pRuleBook++)
{
if ((*pRuleBook == '\'') && (!bDoubleQuote))
bSingleQuote = !bSingleQuote;
if ((*pRuleBook == '"') && (!bSingleQuote))
bDoubleQuote = !bDoubleQuote;
// Kill whitespace outside of quotes
if (bSingleQuote || bDoubleQuote || (
(*pRuleBook != ' ') &&
(*pRuleBook != '\n') &&
(*pRuleBook != '\r') &&
(*pRuleBook != '\t')))
{
pTemp[n++] = *pRuleBook;
if (n >= 2048)
{
//printf ("Panic: Rule Property %d too long\n", i);
HX_ASSERT(0);
break;
}
}
}
pTemp[n] = 0;
if ((*pRuleBook == ',') || (*pRuleBook == ';'))
{
// Rule is Valid!
if (*pTemp == '#')
{
// This part of the rule is an expression
if (!bSeenExpression)
{
m_pRules[i].SetExpression(pTemp + 1);
m_pRules[i].Dump();
bSeenExpression = 1;
m_ulNumThresholds += m_pRules[i].m_pRuleExpression->GetNumThresholds();
}
else
{
//printf ("Panic: Two expressions in Rule %d\n", i);
HX_ASSERT(0);
}
}
else
{
char *pProp;
char *pValue;
pProp = pTemp;
pValue = pTemp;
while ((*pValue != '=') && (pValue-pTemp < ((int) strlen(pTemp))))
pValue++;
if (*pValue == '=')
{
*pValue++ = '\0';
}
else
{
pValue = NULL;
}
if (pValue)
{
IHXBuffer* pBuffer = new CHXBuffer;
if(!pBuffer)
{
return HXR_OUTOFMEMORY;
}
pBuffer->AddRef();
/* Strip Outside Quotes */
if (*pValue == '"')
{
pValue++;
int end = (strlen(pValue) ? strlen(pValue)-1 : 0);
HX_ASSERT(pValue[end] == '"');
pValue[end] = 0;
}
HX_RESULT theErr =
pBuffer->Set((const unsigned char *)pValue,
strlen(pValue) + 1);
if( theErr == HXR_OUTOFMEMORY )
{
pBuffer->Release();
return theErr;
}
//printf ("Property '%s'--'%s'\n", pProp, pValue);
if( m_pRules[i].m_pRuleProps )
{
m_pRules[i].m_pRuleProps->
SetPropertyCString(pProp, pBuffer);
}
pBuffer->Release();
}
}
if (*pRuleBook == ';')
{
i++;
bSeenExpression = 0;
if (i >= m_unNumRules)
{
break;
}
//printf ("******* Rule %d\n", i);
}
pRuleBook++;
}
}
}
return InitRulesArray();
}
BOOL
ASMRuleBook::CheckCurrentRangeEmpty( int uRuleToDel )
{
for( int ii=0; ii<m_unNumRules; ii++ )
{
// But only if this is the only rule covering uRuleToDel's bw range.
if( uRuleToDel != ii && !m_pDeletedRulesArray[ ii ] &&
( m_pRules[ ii ].m_pRuleExpression->GetLeft() == m_pRules[ uRuleToDel ].m_pRuleExpression->GetLeft() ) &&
( m_pRules[ ii ].m_pRuleExpression->GetRight() == m_pRules[ uRuleToDel ].m_pRuleExpression->GetRight() ) &&
( m_pRules[ ii ].m_pRuleExpression->GetOperatorAsInt() == m_pRules[ uRuleToDel ].m_pRuleExpression->GetOperatorAsInt() ) )
{
return FALSE;
}
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -