-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQUEST.client.pl
More file actions
194 lines (160 loc) · 5.22 KB
/
QUEST.client.pl
File metadata and controls
194 lines (160 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/perl
# -d
use strict;
use warnings;
# to make STDOUT flush immediately, simply set the variable
# this can be useful if you are writing to STDOUT in a loop
# many times the buffering will cause unexpected output results
$| = 1;
# paths di librerie personali
use lib $ENV{HOME};
# il metodo Dumper restituisce una stringa (con ritorni a capo \n)
# contenente la struttura dati dell'oggetto in esame.
#
# Esempio:
# $obj = Class->new();
# print Dumper($obj);
use Data::Dumper;
###################################################################
use Cwd;
use Carp;
use IO::Socket::INET;
## GLOBS ##
our $conf_file = '/etc/QUEST.conf'; # file di configurazione
our %confs; # parametri di configurazione del socket
our $socket;
our $options = { };
## SBLOG ##
SPLASH: {
my $splash = <<END
********************************************************************************
QUEST - QUEue your ScripT
release 20.12.a
Copyright (c) 2011-2020, Dario CORRADA <dario.corrada\@gmail.com>
This work is licensed under GNU GENERAL PUBLIC LICENSE Version 3.
********************************************************************************
END
;
print $splash;
}
USAGE: {
use Getopt::Long;no warnings;
GetOptions($options, 'help|h', 'list|l','threads|n=i', 'killer|k=s', 'queue|q=s', 'details|d=s' );
my $usage = <<END
SYNOPSYS
$0 -n 4 ./script.sh
$0 -l
$0 -k 752ENRBU
OPTIONS
-n <int> suggested number of threads used (default: 1)
-l list of jobs running/queued
-k <jobid> kill a job
-d <jobid> details of a job
-q <fast|slow> specify the queue type, "fast" jobs have priority over
"slow" jobs(default: slow)
END
;
if (exists $options->{'help'}) {
print $usage;
goto FINE;
}
}
INIT: {
# configurazione del socket
open(CONF, '<' . $conf_file) or croak("E- unable to open <$conf_file>\n\t");
while (my $newline = <CONF>) {
if ($newline =~ /^#/) {
next; # skippo le righe di commento
} elsif ($newline =~ / = /) {
chomp $newline;
my ($key, $value) = $newline =~ m/([\w\d_\.]+) = ([\w\d_\.\/]+)/;
$confs{$key} = $value;
} else {
next;
}
}
close CONF;
# configurazione delle opzioni
unless ($ARGV[0] || exists $options->{'list'} || exists $options->{'killer'} || exists $options->{'details'}) {
print "\nNothing to do";
goto FINE;
}
if ($ARGV[0]) {
$options->{'user'} = $ENV{'USER'};
$options->{'workdir'} = getcwd();
$options->{'threads'} = 1 unless ($options->{'threads'});
$options->{'script'} = qx/readlink -f $ARGV[0]/;
chomp $options->{'script'};
unless (-x $options->{'script'}) {
croak(sprintf("\nE- File [%s] is not executable\n\t", $options->{'script'}));
}
if ($options->{'threads'} > $confs{'threads'}) {
croak(sprintf("\nE- Number of threads required (%d) is higher than allowed (%d)\n\t", $options->{'threads'}, $confs{'threads'}));
}
if (exists $options->{'queue'}) {
$options->{'queue'} = 'slow' unless ($options->{'queue'} =~ /(fast|slow)/);
} else {
$options->{'queue'} = 'slow';
}
} elsif (exists $options->{'killer'}) {
$options->{'user'} = $ENV{'USER'};
}
}
CORE: {
# accesso al server
$socket = new IO::Socket::INET (
'PeerAddr' => $confs{'host'},
'PeerPort' => $confs{'port'},
'Proto' => 'tcp'
) or croak("\nE- Cannot open socket, maybe the server is down?\n\t");
my $mess; # messaggio da spedire al server
# richiedo la lista dei job attivi
if (exists $options->{'list'}) {
$mess = 'status';
# ammazzo un job
} elsif (exists $options->{'killer'}) {
$mess = sprintf("%s|%s;%s|%s",
'killer', $options->{'killer'},
'user', $options->{'user'}
);
# dettagli su di un job
} elsif (exists $options->{'details'}) {
$mess = sprintf("%s|%s",
'details', $options->{'details'}
);
# sottometto un job
} else {
foreach my $key (keys %{$options}) {
$mess .= "$key|$options->{$key};";
}
chop($mess);
}
$socket->send($mess); # mando un messaggio sul server
# rimango in attesa di una risposta
my $recieved_data = 'null';
while (1) {
$socket->recv($recieved_data,1024);
my $log = $recieved_data;
$log =~ s/(QUEST.over&out)$//;
print $log;
last if ($recieved_data =~ /QUEST.over&out/); # ho ricevuto il segnale "passo e chiudo" dal server, posso uscire
}
}
FINE: {
close $socket if ($socket);
# citazione conclusiva
my $quote_file = qx/which QUEST.server.pl/;
chomp $quote_file;
$quote_file =~ s/QUEST\.server\.pl$/quotes\.txt/;
my $sep = $/;
$/ = ">>\n";
open(CITA, "<$quote_file") or do { print "\n"; exit; };
my @quotes = <CITA>;
close CITA;
my $tot = scalar @quotes;
$/ = $sep;
my $end = "\n\n--\n" . $quotes[int(rand($tot))] . "\n";
$end =~ s/(<<|>>)//g;
print $end;
exit;
}