%description:
Testing Ethernet communication on EtherBus with 100MBps, with disconnecting and reconnecting some hosts.
The network contains two 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 also reply to requests sent to them.

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

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

%#--------------------------------------------------------------------------------------------------------------
%file: test.ned
import inet.linklayer.ethernet.EtherBus;
import inet.node.ethernet.EtherHost;
import inet.common.scenario.ScenarioManager;
import ned.DatarateChannel;

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

//
// Sample Ethernet LAN: hosts connected via bus
//
network SpeedTest
{
    types:
        channel C100 extends DatarateChannel
        {
            delay = 0.1us;
            datarate = 100Mbps;
        }
    submodules:
        host_1_H_A: EtherHostQ {
            parameters:
                @display("p=70,70");
        }
        host_1_H_B: EtherHostQ {
            parameters:
                @display("p=110,290");
        }
        host_2_H_A: EtherHostQ {
            parameters:
                @display("p=150,70");
        }
        host_2_H_B: EtherHostQ {
            parameters:
                @display("p=190,290");
        }
        host_3_H_A: EtherHostQ {
            parameters:
                @display("p=230,70");
        }
        host_3_H_B: EtherHostQ {
            parameters:
                @display("p=270,290");
        }
        host_4_H_A: EtherHostQ {
            parameters:
                @display("p=310,70");
        }
        host_4_H_B: EtherHostQ {
            parameters:
                @display("p=350,290");
        }
        host_5_H_A: EtherHostQ {
            parameters:
                @display("p=390,70");
        }
        host_5_H_B: EtherHostQ {
            parameters:
                @display("p=430,290");
        }
        bus: EtherBus {
            parameters:
                positions = "10 20 30 40 50 60 70 80 90 100";
                propagationSpeed = 2e8 mps; // 1us = 200m
                @display("p=250,180;b=400,6;o=#408060");
            gates:
                ethg[10];
        }
        scenarioManager: ScenarioManager {
            @display("p=184,180");
        }
    connections:
        bus.ethg++ <--> C100  <--> host_1_H_A.ethg;
        bus.ethg++ <--> C100  <--> host_1_H_B.ethg;
        bus.ethg++ <--> C100  <--> host_2_H_A.ethg;
        bus.ethg++ <--> C100  <--> host_2_H_B.ethg;
        bus.ethg++ <--> C100  <--> host_3_H_A.ethg;
        bus.ethg++ <--> C100  <--> host_3_H_B.ethg;
        bus.ethg++ <--> C100  <--> host_4_H_A.ethg;
        bus.ethg++ <--> C100  <--> host_4_H_B.ethg;
        bus.ethg++ <--> C100  <--> host_5_H_A.ethg;
        bus.ethg++ <--> C100  <--> host_5_H_B.ethg;
}

%#--------------------------------------------------------------------------------------------------------------
%file: scenario.xml
<!-- This file is used by the ScenarioManager. -->
<scenario>
    <at t="1.4">
        <disconnect src-module="host_1_H_A" src-gate="ethg$o" />
        <disconnect src-module="bus" src-gate="ethg$o[0]" />
    </at>
    <at t="1.5">
        <connect src-module="host_1_H_A" src-gate="ethg"
                 dest-module="bus" dest-gate="ethg[0]"
                 channel-type="ned.DatarateChannel">
            <param name="datarate" value="100Mbps" />
            <param name="delay" value="0.1us" />
        </connect>
    </at>

    <at t="1.6">
        <disconnect src-module="host_3_H_A" src-gate="ethg$o" />
        <disconnect src-module="bus" src-gate="ethg$o[4]" />
    </at>
    <at t="1.7">
        <connect src-module="host_3_H_A" src-gate="ethg"
                 dest-module="bus" dest-gate="ethg[4]"
                 channel-type="ned.DatarateChannel">
            <param name="datarate" value="100Mbps" />
            <param name="delay" value="0.1us" />
        </connect>
    </at>

    <at t="1.8">
        <disconnect src-module="host_5_H_B" src-gate="ethg$o" />
        <disconnect src-module="bus" src-gate="ethg$o[9]" />
    </at>
    <at t="1.9">
        <connect src-module="host_5_H_B" src-gate="ethg"
                 dest-module="bus" dest-gate="ethg[9]"
                 channel-type="ned.DatarateChannel">
            <param name="datarate" value="100Mbps" />
            <param name="delay" value="0.1us" />
        </connect>
    </at>

    <at t="3.4">
        <disconnect src-module="host_1_H_A" src-gate="ethg$o" />
        <disconnect src-module="bus" src-gate="ethg$o[0]" />
    </at>
    <at t="3.5">
        <connect src-module="host_1_H_A" src-gate="ethg"
                 dest-module="bus" dest-gate="ethg[0]"
                 channel-type="ned.DatarateChannel">
            <param name="datarate" value="100Mbps" />
            <param name="delay" value="0.1us" />
        </connect>
    </at>

    <at t="3.6">
        <disconnect src-module="host_3_H_A" src-gate="ethg$o" />
        <disconnect src-module="bus" src-gate="ethg$o[4]" />
    </at>
    <at t="3.7">
        <connect src-module="host_3_H_A" src-gate="ethg"
                 dest-module="bus" dest-gate="ethg[4]"
                 channel-type="ned.DatarateChannel">
            <param name="datarate" value="100Mbps" />
            <param name="delay" value="0.1us" />
        </connect>
    </at>

    <at t="3.8">
        <disconnect src-module="host_5_H_B" src-gate="ethg$o" />
        <disconnect src-module="bus" src-gate="ethg$o[9]" />
    </at>
    <at t="3.9">
        <connect src-module="host_5_H_B" src-gate="ethg"
                 dest-module="bus" dest-gate="ethg[9]"
                 channel-type="ned.DatarateChannel">
            <param name="datarate" value="100Mbps" />
            <param name="delay" value="0.1us" />
        </connect>
    </at>
</scenario>

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

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

network = SpeedTest

**.scenarioManager.script = xmldoc("scenario.xml")

*.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"

%#--------------------------------------------------------------------------------------------------------------
%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 <- 6.6
usedlimit <- 92.9

# 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

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