<commit_before>package Functions::Shell;

#######################################
# Here will be store all the functions that will be used to change shell behaviour
# and environment
#######################################

use strict;
use warnings;
use threads;
use Functions::Help qw(help);
use Proc::Daemon;
use Fcntl ':mode';
use Getopt::Long;
use IO::Interface::Simple;
use Functions::Setup qw(setup fixperm);
use Functions::ShellSocket qw(connect_shell_socket send_shell_msg receive_shell_msg close_shell_socket term_shell_ctxt bind_shell_socket);
use Term::ANSIColor;
use Time::HiRes qw(sleep);
use Functions::Virtualization qw(start_distributed);

# Next lines are needed to export subroutines to the main package
use base 'Exporter';
use vars qw/ @EXPORT_OK /;
@EXPORT_OK = qw(check_daemon check_command start_daemon get_daemon_output exit_shell);

# The next line is here to help me find the directory of the script
# if you have a better method, let me know.
use FindBin qw($RealBin);

# This function will check whether the daemon is running.
sub check_daemon {
	my $running = `ps -ef | grep cvmfs-testdwrapper | grep -v grep | grep -v defunct`;
	return $running and $main::daemon_ip ne "127.0.0.1";
}

sub check_process {
	my $process_name = shift;
	my $running = `ps -fu cvmfs-test | grep -i $process_name | grep -v grep`;
	return $running;
}

# This function is used to check if the command typed has to be ran by the shell
# or by the daemon. If the command is bundled in the shell, it launches the
# corresponding function.
sub check_command {
	my $socket = shift;
	my $ctxt = shift;
	my $daemon_path = shift;
	my $command = shift;
	
	# Variables to memorize if the command was found and executed
	my $executed = 0;
	
	# Switching the value of $command
	for ($command){
		if ($_ eq 'exit' or $_ eq 'quit' or $_ eq 'q') { exit_shell($socket, $ctxt) }
		elsif ($_ eq 'status') { print_status(); $executed = 1 }
		elsif ($_ =~ m/^start\s*.*/ ) { ($socket, $ctxt) = start_daemon($daemon_path, undef, undef, $command); $executed = 1 }
		elsif ($_ =~ m/^help\s*.*/ or $_ =~ m/^h\s.*/) { help($command), $executed = 1 }
		elsif ($_ eq 'setup' ) { setup(); $executed = 1 }
		elsif ($_ eq 'fixperm') { fixperm(); $executed = 1 }
		elsif ($_ =~ m/^restart\s*.*/ ) { ($socket, $ctxt) = restart_daemon($socket, $ctxt, $daemon_path, $command); $executed = 1 }
		elsif ($_ =~ m/^wait-daemon\s*.*/ ) { ($socket, $ctxt) = wait_daemon($socket, $ctxt); $executed = 1 }
	}
	
	# If the daemon is not running and no command was executed, print on screen a message
	# saying that the command was not found
	if(!check_daemon and !$executed){
		print "Command not found. Type 'help' for a list of available commands.\n";
	}
	
	# Returning the value of $executed to check if the command was found and executed
	return ($executed, $socket, $ctxt);
}

sub wait_daemon {
	my $socket = shift;
	my $ctxt = shift;
	
	# Closing old socket and context
	if (defined($socket) and defined($ctxt)) {
		close_shell_socket($socket);
		term_shell_ctxt($ctxt);
	}
	
	# Opening the socket to wait for the daemon to send its ip
	my ($shell_socket, $shell_ctxt) = bind_shell_socket();
	
	# Wait fo the daemon message
	my $answer = receive_shell_msg($shell_socket);
	
	# Splitting the message and assigning it to variables
	my @ip_port = split /:/, $answer;
	my ($daemon_ip, $daemon_port) = ($ip_port[0], $ip_port[1]);
	my $daemon_path = "$daemon_ip:$daemon_port";<commit_msg>log added to wait-daemon function<commit_after>package Functions::Shell;

#######################################
# Here will be store all the functions that will be used to change shell behaviour
# and environment
#######################################

use strict;
use warnings;
use threads;
use Functions::Help qw(help);
use Proc::Daemon;
use Fcntl ':mode';
use Getopt::Long;
use IO::Interface::Simple;
use Functions::Setup qw(setup fixperm);
use Functions::ShellSocket qw(connect_shell_socket send_shell_msg receive_shell_msg close_shell_socket term_shell_ctxt bind_shell_socket);
use Term::ANSIColor;
use Time::HiRes qw(sleep);
use Functions::Virtualization qw(start_distributed);

# Next lines are needed to export subroutines to the main package
use base 'Exporter';
use vars qw/ @EXPORT_OK /;
@EXPORT_OK = qw(check_daemon check_command start_daemon get_daemon_output exit_shell);

# The next line is here to help me find the directory of the script
# if you have a better method, let me know.
use FindBin qw($RealBin);

# This function will check whether the daemon is running.
sub check_daemon {
	my $running = `ps -ef | grep cvmfs-testdwrapper | grep -v grep | grep -v defunct`;
	return $running and $main::daemon_ip ne "127.0.0.1";
}

sub check_process {
	my $process_name = shift;
	my $running = `ps -fu cvmfs-test | grep -i $process_name | grep -v grep`;
	return $running;
}

# This function is used to check if the command typed has to be ran by the shell
# or by the daemon. If the command is bundled in the shell, it launches the
# corresponding function.
sub check_command {
	my $socket = shift;
	my $ctxt = shift;
	my $daemon_path = shift;
	my $command = shift;
	
	# Variables to memorize if the command was found and executed
	my $executed = 0;
	
	# Switching the value of $command
	for ($command){
		if ($_ eq 'exit' or $_ eq 'quit' or $_ eq 'q') { exit_shell($socket, $ctxt) }
		elsif ($_ eq 'status') { print_status(); $executed = 1 }
		elsif ($_ =~ m/^start\s*.*/ ) { ($socket, $ctxt) = start_daemon($daemon_path, undef, undef, $command); $executed = 1 }
		elsif ($_ =~ m/^help\s*.*/ or $_ =~ m/^h\s.*/) { help($command), $executed = 1 }
		elsif ($_ eq 'setup' ) { setup(); $executed = 1 }
		elsif ($_ eq 'fixperm') { fixperm(); $executed = 1 }
		elsif ($_ =~ m/^restart\s*.*/ ) { ($socket, $ctxt) = restart_daemon($socket, $ctxt, $daemon_path, $command); $executed = 1 }
		elsif ($_ =~ m/^wait-daemon\s*.*/ ) { ($socket, $ctxt) = wait_daemon($socket, $ctxt); $executed = 1 }
	}
	
	# If the daemon is not running and no command was executed, print on screen a message
	# saying that the command was not found
	if(!check_daemon and !$executed){
		print "Command not found. Type 'help' for a list of available commands.\n";
	}
	
	# Returning the value of $executed to check if the command was found and executed
	return ($executed, $socket, $ctxt);
}

sub wait_daemon {
	my $socket = shift;
	my $ctxt = shift;
	
	# Closing old socket and context
	if (defined($socket) and defined($ctxt)) {
		close_shell_socket($socket);
		term_shell_ctxt($ctxt);
	}
	
	# Opening the socket to wait for the daemon to send its ip
	my ($shell_socket, $shell_ctxt) = bind_shell_socket();
	
	# Wait for the daemon message
	print "Waiting for the daemon...\n";
	my $answer = receive_shell_msg($shell_socket);
	print "Received a connection from $answer.\n";
	
	# Splitting the message and assigning it to variables
	my @ip_port = split /:/, $answer;
	my ($daemon_ip, $daemon_port) = ($ip_port[0], $ip_port[1]);
	my $daemon_path = "$daemon_ip:$daemon_port";
{'fraction_non_alphanumeric': 0.14679423104383835, 'fraction_numerical': 0.004569470227045552, 'mean_word_length': 4.484729835552075, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1968395', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2329, 'n_words': 824}