ncr과 에너지 방정식을 기반으로 이기적인 노드를 탐지하려고 합니다. 이기적인 노드를 감지했지만 각각 ncr과 에너지를 기반으로 합니다. 그리고 이 두 조건을 결합하는 데 문제가 있습니다. (노드의 ncr이 30 미만이고 에너지가 임계값 미만인 경우 이기적인 노드로 분류됩니다.)
이기적인 노드의 에너지 감지를 기반으로 한 awk 코드입니다.
BEGIN {
n=38
SelfishCounter=0;
Normalcounter=0;
}
{
# Trace line format: energy
event = $1
time = $2
if (event == "r" || event == "d" || event == "s"|| event == "f") {
node_id = $3
energy=$14
}
if (event=="N"){
node_id = $5
energy=$7
}
# Store remaining energy
finalenergy[node_id]=energy
}
END {
# finalenergy for each node
for (i in finalenergy) {
finalenergy[i]
totalenergy = 0
for (i = 0; i <= n; i = i + 1)
totalenergy += finalenergy[i]
}
###compute average energy
threshold=totalenergy/n
####output
for (i=0; i<n; i++) {
if(threshold >finalenergy[i]){
SelfishCounter++; }
else if(threshold <finalenergy[i]){
Normalcounter++; }
}
printf "number of selfish nodes:%d \n", SelfishCounter;
printf "number of normal nodes:%d \n", Normalcounter;
}
이기적인 노드를 탐지하기 위한 NCR 기반의 awk 코드입니다.. sh 파일을 사용했습니다.
BEGIN {
ReqCounter=0;
RepCounter=0;
NCRCounter=0;
Normalcounter=0;
max_node = 0;
}
{action=$1;
if(action == "s" || action == "r" || action == "f")
{
if(($33 == "REQUEST") && ($5==node))
ReqCounter++;
if(($33 == "REPLY") && ($5==node))
RepCounter++;
}
}
END {
NCR= ((ReqCounter-(ReqCounter-RepCounter))/ReqCounter)*100;
if(NCR <= "30"){
NCRCounter++;
printf "SELFISH\n"; }
else{
Normalcounter++;
printf "NORMAL\n";}
# printf "The Node number is:%d \n", node;
# printf "The NCRis:%d \n", NCR;
#printf "REQUEST Count:%d \n", ReqCounter;
#printf "REPLY Count:%d \n", RepCounter;
}
#!/bin/sh
i=0
SelfishNodes=0
NormalNodes=0
while [ $i -lt 38 ]
do
value=$(awk -v node=$i -f NCR.awk DSR.tr)
echo "Node" $i "is" $value
i=`expr $i + 1`
if [ "$value" = "SELFISH" ]
then
SelfishNodes=` expr $SelfishNodes + 1`
else
NormalNodes=`expr $NormalNodes + 1`
fi
done
echo "Total Number of Sefiish Nodes are:" $SelfishNodes
echo "Totl Number of Normal Nodes are:"$NormalNodes
위에 표시된 두 개의 awk 파일을 함께 혼합하면
BEGIN {
n=38
SelfishCounter=0;
Normalcounter=0;
ReqCounter=0;
RepCounter=0;
}
{
# Trace line format: energy
event = $1
time = $2
if (event == "r" || event == "d" || event == "s"|| event == "f") {
node_id = $3
energy=$14
if(($33 == "REQUEST") && ($5 == node))
ReqCounter++;
if(($33 == "REPLY") && ($5 == node))
RepCounter++;
}
if (event=="N"){
node_id = $5
energy=$7
}
# Store remaining energy
finalenergy[node_id]=energy
}
END {
NCR= ((ReqCounter-(ReqCounter-RepCounter))/ReqCounter)*100;
# finalenergy for each node
for (i in finalenergy) {
finalenergy[i]
totalenergy = 0
for (i = 0; i <= n; i = i + 1)
totalenergy += finalenergy[i]
}
###compute average energy
threshold=totalenergy/n
####output
if((threshold >finalenergy[i]) || (NCR <= 30)){
SelfishCounter++;
printf "SELFISH\n"; }
else{
Normalcounter++;
printf "NORMAL\n";}
}
}
}
#!/bin/sh
i=0
SelfishNodes=0
NormalNodes=0
while [ $i -lt 38 ]
do
value=$(awk -v node=$i -f energy-ncr.awk DSR.tr)
echo "Node" $i "is" $value
i=`expr $i + 1`
if [ "$value" = "SELFISH" ]
then
SelfishNodes=` expr $SelfishNodes + 1`
else
NormalNodes=`expr $NormalNodes + 1`
fi
done
echo "Total Number of Sefiish Nodes are:" $SelfishNodes
echo "Totl Number of Normal Nodes are:"$NormalNodes
다음과 같은 오류가 발생합니다
Node 33 is
awk: energy-ncr.awk:59: }
awk: energy-ncr.awk:59: ^ syntax error
awk: energy-ncr.awk:62: }
awk: energy-ncr.awk:62: ^ syntax error
Node 34 is
awk: energy-ncr.awk:59: }
awk: energy-ncr.awk:59: ^ syntax error
awk: energy-ncr.awk:62: }
awk: energy-ncr.awk:62: ^ syntax error
Node 35 is
awk: energy-ncr.awk:59: }
awk: energy-ncr.awk:59: ^ syntax error
awk: energy-ncr.awk:62: }
awk: energy-ncr.awk:62: ^ syntax error
Node 36 is
awk: energy-ncr.awk:59: }
awk: energy-ncr.awk:59: ^ syntax error
awk: energy-ncr.awk:62: }
awk: energy-ncr.awk:62: ^ syntax error
Node 37 is
Total Number of Sefiish Nodes are: 0
Totl Number of Normal Nodes are:38
편집자: Ed Morton: 다음은 OPs 2의 첫 번째 awk 스크립트입니다. 다음과 같은 형식으로 되어 있어 gawk -o-
코드를 더 쉽게 이해할 수 있도록 들여쓰기 등이 있습니다.
스크립트 1:
BEGIN {
n = 38
SelfishCounter = 0
Normalcounter = 0
}
{
# Trace line format: energy
event = $1
time = $2
if (event == "r" || event == "d" || event == "s" || event == "f") {
node_id = $3
energy = $14
}
if (event == "N") {
node_id = $5
energy = $7
}
# Store remaining energy
finalenergy[node_id] = energy
}
END {
# finalenergy for each node
for (i in finalenergy) {
finalenergy[i]
totalenergy = 0
for (i = 0; i <= n; i = i + 1) {
totalenergy += finalenergy[i]
}
}
###compute average energy
threshold = totalenergy / n
####output
for (i = 0; i < n; i++) {
if (threshold > finalenergy[i]) {
SelfishCounter++
} else if (threshold < finalenergy[i]) {
Normalcounter++
}
}
printf "number of selfish nodes:%d \n", SelfishCounter
printf "number of normal nodes:%d \n", Normalcounter
}
스크립트 2:
BEGIN {
ReqCounter = 0
RepCounter = 0
NCRCounter = 0
Normalcounter = 0
max_node = 0
}
{
action = $1
if (action == "s" || action == "r" || action == "f") {
if (($33 == "REQUEST") && ($5 == node)) {
ReqCounter++
}
if (($33 == "REPLY") && ($5 == node)) {
RepCounter++
}
}
}
END {
NCR = ((ReqCounter - (ReqCounter - RepCounter)) / ReqCounter) * 100
if (NCR <= "30") {
NCRCounter++
printf "SELFISH\n"
} else {
Normalcounter++
printf "NORMAL\n"
}
# printf "The Node number is:%d \n", node;
# printf "The NCRis:%d \n", NCR;
#printf "REQUEST Count:%d \n", ReqCounter;
#printf "REPLY Count:%d \n", RepCounter;
}