#!/usr/bin/env bash
# $Id$
#
# Thomas Dreibholz's PlanetLab Script Collection
# Copyright (C) 2005-2023 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: thomas.dreibholz@gmail.com


. ./planetlab-config

PRsPerRegion=3
PR_LIST=`( head -n$PRsPerRegion pr-europe.ip ; head -n$PRsPerRegion pr-america.ip ) | grep "^[a-Z0-9]"|sort -u`   # ACHTUNG: Eindeutigkeit unbedingt notwendig!!!
PE_LIST=`cat alive-planetlab.ip|grep "^[a-Z0-9]"|sort -u`

# PR_LIST=`( head -n$PRsPerRegion pr-europe.ip ) | grep "^[a-Z0-9]"|sort -u`   # ACHTUNG: Eindeutigkeit unbedingt notwendig!!!
# PE_LIST=`cat pe-europe.ip|grep "^[a-Z0-9]"|sort -u`


TESTID=fractal-scenario2
PR_MISCOPT="-asapannounce=off -enrpannounce=off -endpointkeepalivetransmissioninterval=30000 -endpointkeepalivetimeoutinterval=15000 -cspinterval=1000 -loglevel=1"
PE_MISCOPT="-asapannounce=off -cspinterval=0 -fractal -fgpfailureafter=0 -rereginterval=30000 -policy=RoundRobin -loglevel=1 "
PU_MISCOPT=""
VALGRIND=""  # "/usr/bin/valgrind  -v --tool=memcheck --leak-check=yes --show-reachable=no --leak-resolution=high --num-callers=40 --freelist-vol=4000000"

resetAll ()
{
   ./stop-processes Registrar
   ./stop-processes PoolElement
}

startNetwork ()
{
   optPeers=""
   for peer in $PR_LIST ; do
      optPeers="$optPeers -peer=$peer:9901"
   done
   number=1
   for pr in $PR_LIST ; do
      echo "Starting PR on $pr ..."
      ./start-process "Registrar-$number" "./rootshell $VALGRIND ./registrar $OPT_CSP $PR_MISCOPT -identifier=100$number -asap=0.0.0.0:3863 -enrp=0.0.0.0:9901 $optPeers -statsfile=statistics-$pr.data -statsinterval=1000 -actionlogfile=actions-$pr.data >$TESTID-pr$number-$pr.log 2>&1" $pr &
      let number=$number+1
   done

   optRegistrars=""
   for pr in $PR_LIST ; do
      optRegistrars="$optRegistrars -registrar=$pr:3863"
   done
   number=1
   for pe in $PE_LIST ; do
      echo "Starting PE on $pe ..."
      ./start-process "PoolElement-$number" "./rootshell ./server $OPT_CSP $PE_MISCOPT $optRegistrars -identifier=$number >$TESTID-pe$number-$pe.log 2>&1" $pe &
      let number=$number+1
   done
}

stopNetwork ()
{
   number=1
   for pe in $PE_LIST ; do
      ./stop-process "PoolElement-$number" $pe
      let number=$number+1
   done
   number=1
   for pr in $PR_LIST ; do
      ./stop-process "Registrar-$number" $pr
      let number=$number+1
   done
}



if [ $# -lt 1 ] ; then
   echo "Usage: fractal-scenario2 [start|stop|reset|user]"
   exit 1
fi

if [ "x$1" = "xstart" ] ; then
   startNetwork
elif [ "x$1" = "xstop" ] ; then
   stopNetwork
elif [ "x$1" = "xreset" ] ; then
   echo "Resetting everything!"
   resetAll
elif [ "x$1" = "xuser" ] ; then
   optRegistrars=""
   for pr in $PR_LIST ; do
      optRegistrars="$optRegistrars -registrar=$pr:3863"
   done
   shift
   cmd="../fractalpooluser $optRegistrars -loglevel=1 $PU_MISCOPT $OPT_CSP -configdir=../fgpconfig $@"
   echo "# $cmd"
   $cmd
else
   echo "ERROR: Bad parameter!"
fi
