%description:
Testing Ethernet communication on EtherHub with 100MBps.
Simulation has 5 pairs of hosts that communicate with each other.
All hosts use EtherMAC implementation with half duplex mode.
All hosts have same ethernet configurations.
All hosts send requests and responses to requests for its pair, too.

checks:
 - the idle state of rx is less than or equals to 4% in all hosts
 - the utilization state of rx is more than or equals to 96% in all hosts

%#--------------------------------------------------------------------------------------------------------------
%testprog: opp_run

%#--------------------------------------------------------------------------------------------------------------
%file: test.ned
import ned.DatarateChannel;
import inet.node.ethernet.EtherHost;
import inet.linklayer.ethernet.EtherHub;

module EtherHostQ extends EtherHost
{
    parameters:
        queueType = "DropTailQueue";
}

//
// Sample Ethernet LAN: hosts connected via hub
//
network SpeedTest
{
    types:
        channel C100 extends DatarateChannel
        {
            delay = 0s;
            datarate = 100Mbps;
        }
    submodules:
        host_1_H_A: EtherHostQ {
            parameters:
                @display("p=70,70");
        }
        host_1_H_B: EtherHostQ {
            parameters:
                @display("p=210,70");
        }
        host_2_H_A: EtherHostQ {
            parameters:
                @display("p=70,140");
        }
        host_2_H_B: EtherHostQ {
            parameters:
                @display("p=210,140");
        }
        host_3_H_A: EtherHostQ {
            parameters:
                @display("p=70,210");
        }
        host_3_H_B: EtherHostQ {
            parameters:
                @display("p=210,210");
        }
        host_4_H_A: EtherHostQ {
            parameters:
                @display("p=70,280");
        }
        host_4_H_B: EtherHostQ {
            parameters:
                @display("p=210,280");
        }
        host_5_H_A: EtherHostQ {
            parameters:
                @display("p=70,350");
        }
        host_5_H_B: EtherHostQ {
            parameters:
                @display("p=210,350");
        }
        hub: EtherHub {
            parameters:
                @display("p=140,210");
            gates:
                ethg[10];
        }
    connections:
        hub.ethg++ <--> C100 <--> host_1_H_A.ethg;
        hub.ethg++ <--> C100 <--> host_1_H_B.ethg;
        hub.ethg++ <--> C100 <--> host_2_H_A.ethg;
        hub.ethg++ <--> C100 <--> host_2_H_B.ethg;
        hub.ethg++ <--> C100 <--> host_3_H_A.ethg;
        hub.ethg++ <--> C100 <--> host_3_H_B.ethg;
        hub.ethg++ <--> C100 <--> host_4_H_A.ethg;
        hub.ethg++ <--> C100 <--> host_4_H_B.ethg;
        hub.ethg++ <--> C100 <--> host_5_H_A.ethg;
        hub.ethg++ <--> C100 <--> host_5_H_B.ethg;
}

%#--------------------------------------------------------------------------------------------------------------
%inifile: omnetpp.ini
[General]
sim-time-limit = 5s

tkenv-plugin-path = ../../../etc/plugins
#record-eventlog = true
**.vector-recording = false

network = SpeedTest

*.host_1_H_A.cli.destAddress = "host_1_H_B"
*.host_1_H_B.cli.destAddress = "host_1_H_A"

*.host_2_H_A.cli.destAddress = "host_2_H_B"
*.host_2_H_B.cli.destAddress = "host_2_H_A"

*.host_3_H_A.cli.destAddress = "host_3_H_B"
*.host_3_H_B.cli.destAddress = "host_3_H_A"

*.host_4_H_A.cli.destAddress = "host_4_H_B"
*.host_4_H_B.cli.destAddress = "host_4_H_A"

*.host_5_H_A.cli.destAddress = "host_5_H_B"
*.host_5_H_B.cli.destAddress = "host_5_H_A"


**.cli.reqLength = 1250B       # 10.000 bit
**.cli.respLength = 1250B      # 10.000 bit
*.host_*_*.cli.startTime = 0s

*.host_*_H_*.mac.duplexMode = false

*.host_*_H_*.cli.sendInterval  = 0.1ms        # 10.000 / speed [ / nodecount when halfduplex]

**.mac.address = "auto"

# Check: "rx channel idle (%)" <= 4.0
# Check: "rx channel utilization (%)" >= 96.0

%#--------------------------------------------------------------------------------------------------------------
%postrun-command: Rscript check.r
%#--------------------------------------------------------------------------------------------------------------
%file: check.r
#!/usr/bin/env Rscript

options(echo=FALSE)
options(width=160)
library("omnetpp", warn.conflicts=FALSE)

#TEST parameters
scafile <- 'results/General-#0.sca'
linecount <- 10
idlelimit <- 4.0
usedlimit <- 96.0

# begin TEST:

idle <- loadDataset(scafile, add(type='scalar', select='name("rx channel idle *")'))
used <- loadDataset(scafile, add(type='scalar', select='name("rx channel utilization *")'))

cat("\nOMNETPP TEST RESULT: ")

if(length(idle$scalars$value) == linecount & max(idle$scalars$value) <= idlelimit)
{
    cat("IDLE OK\n")
} else {
    cat("IDLE BAD:\n")
    print(idle$scalars[idle$scalars$value > idlelimit,])
}

cat("\nOMNETPP TEST RESULT: ")

if(length(used$scalars$value) == linecount & min(used$scalars$value) >= usedlimit)
{
    cat("USED OK\n")
} else {
    cat("USED BAD:\n")
    print(used$scalars[used$scalars$value < usedlimit,])
}

cat("\n")

%#--------------------------------------------------------------------------------------------------------------
%contains: postrun-command(1).out

OMNETPP TEST RESULT: IDLE OK

OMNETPP TEST RESULT: USED OK

%#--------------------------------------------------------------------------------------------------------------
