#!/usr/bin/env python

# An example script using the rumba package

from rumba.model import Node, UnicastLayer, EthDixLayer
from rumba.storyboard import *
from rumba.utils import ExperimentManager, PROMPT_SWAPOUT
from rumba.topologies import build_star

# import testbed plugins
import rumba.testbeds.jfed as jfed

# import Ouroboros prototype plugin
import rumba.prototypes.ouroboros as our

import rumba.log as log


__all__ = ["exp", "sb", "nodes", "main", "run"]

log.set_logging_level('DEBUG')

n1 = UnicastLayer("n1")

leaves, routerNode = build_star(
    ["client1", "client2", "server"], n1, hub_name="router")
clientNode1, clientNode2, serverNode = leaves

nodes = ["client1", "client2", "router", "server"]

tb = jfed.Testbed(exp_name='cc2',
                  cert_file='/path/to/cert.pem',
                  authority='wall1.ilabt.iminds.be',
                  image='UBUNTU18-64-STD',
                  username='username',
                  exp_hours='1',
                  proj_name='ouroborosrocks')

exp = our.Experiment(tb,
                     nodes=[clientNode1, clientNode2, routerNode, serverNode],
                     # git_repo='https://codeberg.org/o7s/ouroboros',
                     git_branch='be',
                     build_options='-DCMAKE_BUILD_TYPE=Debug '
                                   '-DSHM_BUFFER_SIZE=131072 '
                                   '-DIPCP_ETH_PAD_RUNT_FRAMES=false '
                                   '-DDISABLE_CORE_LOCK=false '
                                   '-DPCP_SCHED_THR_MUL=1',
                     add_packages=['ethtool'],
                     influxdb={
                         'ip': '127.0.0.1',
                         'port': 8086,
                         'org': "Ouroboros",
                         'token': "your-influxdb-token"
                     })

sb = StoryBoard(experiment=exp, duration=1500, servers=[])


def run():
    sb.run_command("server",
                   'irm bind prog ocbr name ocbr_server;'
                   'irm name register ocbr_server layer n1;'
                   'ocbr --listen > /dev/null 2>&1 &')
    sb.run_command("client1",
                   'irm bind prog ocbr name ocbr_client1;'
                   'irm name register ocbr_client1 layer n1;'
                   'ocbr --listen > /dev/null 2>&1 &')
    sb.run_command("server",
                   'irm bind prog oping name oping_server;'
                   'irm name register oping_server layer n1;'
                   'oping --listen > /dev/null 2>&1 &')
    sb.run_command("client1", "for sz in `seq 0 10 200 1000`;"
                              "do oping -n oping_server -i 0ms -s $sz -d 120;"
                              "done")
    sb.run_command("client1", "ocbr -n ocbr_server -r 70M -d 600 --spin > /dev/null 2>&1 &")
    sb.run_command("server", "ocbr -n ocbr_client1 -r 70M -d 600 --spin > /dev/null 2>&1 &")
    time.sleep(30)
    sb.run_command("client2", "ocbr -n ocbr_server -r 70M -d 30 --spin > /dev/null 2>&1 &")
    time.sleep(45)
    sb.run_command("client2", "ocbr -n ocbr_server -r 20M -d 30 --spin > /dev/null 2>&1 &")
    time.sleep(45)
    sb.run_command("client2", "ocbr -n ocbr_server -r 60M -d 30 -s 1400 --spin > /dev/null 2>&1 &")
    time.sleep(45)
    sb.run_command("client2", "ocbr -n ocbr_server -r 60M -d 30 -s 1200 --spin > /dev/null 2>&1 &")
    time.sleep(45)
    sb.run_command("client2", "ocbr -n ocbr_server -r 60M -d 30 -s 1000 --spin > /dev/null 2>&1 &")
    time.sleep(45)
    sb.run_command("client2", "ocbr -n ocbr_server -r 60M -d 30 -s 800 --spin > /dev/null 2>&1 &")
    time.sleep(35)
    sb.run_command("client2", "ocbr -n ocbr_server -r 60M -d 30 -s 500 --spin > /dev/null 2>&1 &")
    time.sleep(35)
    sb.run_command("client2", "ocbr -n ocbr_server -r 60M -d 30 -s 200 --spin > /dev/null 2>&1 &")
    time.sleep(200)
#    sb.ouroboros_stop_metrics_exporter(nodes)
#    exp.terminate_prototype()


def main():
    with ExperimentManager(exp, swap_out_strategy=PROMPT_SWAPOUT):
        exp.swap_in()
        exp.install_prototype()
        exp.set_phy_link_rate_between("router", "server", 100)
        exp.start_metrics_exporter(nodes, interval=300)
        exp.bootstrap_prototype()
        run()


if __name__ == "__main__":
    main()
