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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
package Agent;
use IvyIO;
use FugueConfig;
use strict;
#----------------------------------------------------------------------
#
# class variables and methods
#
#----------------------------------------------------------------------
my ($x0, $y0, $wmax, $dx, $dy, $cmin, $rmax, @matrix, @hosts, $tempo_id,
$mw, $bg, $fg, $selcolor, $pbcolor, $hlbg, $darkbg, $fontspec, @fontspec,
$on_img, $off_img, $pb_img, %instances, @instances, $bus, $coef,
$hosts_tl, $selected_host, $preselected_host, $pingThreshold);
# configure the class : graphic parameters, geometry and ivy bus
sub configure {
shift;
($mw, $bg, $fg, $selcolor, $pbcolor, $hlbg, $darkbg,
$fontspec, $x0, $y0, $wmax, $dy, $cmin, $rmax, $bus, $coef, $pingThreshold) = @_;
@fontspec = @$fontspec if $fontspec;
$off_img = $mw->Bitmap('off', -file => Tk::findINC('led.bmp'),
-foreground => $bg);
$pb_img = $mw->Bitmap('pb', -file => Tk::findINC('led.bmp'),
-foreground => $pbcolor);
$on_img = $mw->Bitmap('on', -file => Tk::findINC('led.bmp'),
-foreground => $selcolor);
&_resetmatrix;
} # end configure
# kill an named agent
sub kill {
my ($class, $appname) = @_;
IvyIO::kill($appname);
} # end kill;
# kill all agents
sub killall {
my ($class) = @_;
my $nb = 0;
for(@instances) {
if ($_->{status} > 0) {
IvyIO::kill($_->{appname});
$nb++;
}
}
return $nb;
} # end killall;
# set usable hosts list for executing agents
sub hosts {
shift;
@hosts = @_;
} # end hosts
# callback called when an agent connects
sub connect {
my ($class, $appname, $host) = @_;
if ($instances{$appname}) {
if ($instances{$appname}->{status} == 1) {
$instances{$appname}->addtwin;
} else {
$instances{$appname}->on;
}
} else {
$class->new($appname, $host, undef, undef, 1);
}
} # end connect
# callback called when an agent disconnects
sub disconnect {
my ($class, $appname, $host) = @_;
if ($instances{$appname}) {
if ($instances{$appname}->{number} > 1) {
$instances{$appname}->removetwin;
} else {
$instances{$appname}->off;
}
} else {
$class->new($appname, $host, undef, undef, 0);
}
} # end disconnect
# callback called when a ping/pong is received
sub pingcb {
my ($class, $appname, $host, $pingtime) = @_;
#print "pingtime=$pingtime\n";
if ($instances{$appname}) {
# if pingtime > $pingThreshold status is changed
if ($pingtime > $pingThreshold) {
$instances{$appname}->pb;
} else {
# print "Stat:".$instances{$appname}->{status}."\n";
if ($instances{$appname}->{status} == 2) { $instances{$appname}->nopb;}
}
}
} # end pingcb
# class constructor
sub new {
my ($class, $appname, $host, $command, $params, $status) = @_;
if ($instances{$appname}) {
$instances{$appname}->addtwin;
return;
}
my $self = {};
bless $self;
# set object attributes
$self->{appname} = $appname;
$self->{status} = $status;
$self->{host} = $host;
$self->{command} = $command;
$self->{params} = $params;
$self->{number} = 1;
$self->{led} = undef;
$self->{label} = undef;
# reset positions before adding new instance
if (@instances >= ($rmax*$cmin)) {
$cmin++;
$class->_updatepositions;
}
# set class variables
$instances{$appname} = $self;
push(@instances, $self);
# graphical updates
$self->createlabel;
$self->on if $status;
$class->_alphabeticsort();
return $self;
} # end new
#----------------------------------------------------------------------
#
# instance methods
#
#----------------------------------------------------------------------
# called when several agent instances are detected
sub addtwin {
my $self = shift;
$self->{number}++;
$self->{label}->configure(-text => $self->formatlabel);
} # end addtwin
# called when an instance of a not single agent dies
sub removetwin {
my $self = shift;
return if $self->{number} == 1;
$self->{number}--;
$self->{label}->configure(-text => $self->formatlabel);
} # end addtwin
# graphical effect when a known agent connects
sub on {
my $self = shift;
$self->nopb;
$self->{label}->raise;
$self->{label}->configure(-highlightthicknes => 2);
$tempo_id = $mw->after(2000, sub {
$self->{label}->configure(-highlightthicknes => 0);
});
} # end on
# graphical effect when a known agent disconnects
sub off {
my $self = shift;
$self->{status} = 0;
$self->{led}->configure(-image => $off_img);
} # end off
# graphical effect when a know agent has a ping problem (latency)
sub pb {
my $self = shift;
$self->{status} = 2;
$self->{led}->configure(-image => $pb_img);
my $i = 0;
my $id;
$id = $mw->repeat(100, sub {
if ($i == 4) {
$mw->afterCancel($id);
} elsif ($i % 2 == 0) {
$self->{led}->configure(-image => $on_img);
} else {
$self->{led}->configure(-image => $pb_img);
}
$i++;
});
} # end pb
sub nopb {
my $self = shift;
$self->{status} = 1;
$self->{led}->configure(-image => $on_img);
} # end pb
# label placement
sub setposition {
my $self = shift;
my ($r, $c);
for (my $i=0; $i<@matrix; $i++) {
for (my $j=0; $j<=$#{$matrix[$i]}; $j++) {
unless ($matrix[$i][$j]->[0]) {
($r, $c) = ($i, $j);
last;
}
}
last if defined $r;
}
$matrix[$r][$c]->[0] = $self;
my ($x, $y) = ($matrix[$r][$c]->[1], $matrix[$r][$c]->[2]);
my $y2 = $y - 5*$coef;
$y2 -= 12*$coef if ($self->{label}->cget(-text) =~ /\n/);
if ($self->{command}) {
$self->{led}->configure(-highlightthickness => 6*$coef);
$self->{led}->place(-x => $x+3*$coef, -y => $y+3*$coef);
} else {
$self->{led}->configure(-highlightthickness => 2*$coef);
$self->{led}->place(-x => $x+6*$coef, -y => $y+6*$coef);
}
$self->{label}->place(-x => $x + 38, -y => $y2);
} # end setposition
# label creation
sub createlabel {
my ($self) = @_;
$self->{led} =
$mw->Label(-image => $off_img,
-highlightbackground => $hlbg,
-borderwidth => 0,
);
$self->{label} =
$mw->Label(-text => $self->formatlabel,
-pady => 10*$coef,
-justify => 'left',
-relief => 'flat',
-highlightthickness => 0,
-background => $darkbg,
-foreground => $fg,
@fontspec,
);
$self->{led}->bind('<1>', [\&_cbOnPress, $self]);
$self->{label}->bind('<1>', [\&_cbOnPress, $self]);
} # end createlabel
# label format : affects too long labels and not single agents
sub formatlabel {
my ($self) = @_;
my $im_width = 38;
my $dx = $dx - $im_width;
my $appname = $self->{appname};
# for not single agents
$appname = '['.$self->{number}.'] '.$appname if $self->{number} > 1;
my $width = $mw->fontMeasure($fontspec[1], $appname);
my $appnametext;
# for too long names
while ($width > 2*$dx) {
$appname = substr($appname, 0, -1);
$width = $mw->fontMeasure($fontspec[1], $appname);
}
if ($width > $dx) {
my $hlen = int(length($appname)/2);
my @fields = split(/:/, $appname);
if (@fields > 1) {
my $len = 0;
my $imax = -1;
for (my $i=0; $i<@fields; $i++) {
$len += length($fields[$i]);
if ($len > $hlen) {
$imax = $i - 1;
last;
}
}
if ($imax >= 0) {
$appnametext = join(':', (@fields)[0..$imax])."\n".
join(':', (@fields)[$imax+1..$#fields]);
}
}
$appnametext = substr($appname, 0, $hlen)."\n".substr($appname, $hlen)
unless $appnametext;
} else {
my @fields = split(/:/, $appname);
if (@fields > 1) {
$appnametext = $fields[0].":\n ".join(':', (@fields)[1..$#fields]);
}
}
$appnametext = $appname unless $appnametext;
return $appnametext;
} # end formatlabel
#----------------------------------------------------------------------
#
# private methods or functions
#
#----------------------------------------------------------------------
sub _resetmatrix {
$dx = $wmax/$cmin;
for(my $r=0; $r<$rmax; $r++) {
for(my $c=0; $c<$cmin; $c++) {
$matrix[$r][$c] = [undef, $x0 + $c*$dx, $y0 + $r*$dy];
}
}
} # end _resetmatrix
sub _updatepositions {
&_resetmatrix;
for (@instances) {
$_->setposition();
$_->{label}->configure(-text => $_->formatlabel);
}
} # end _updatepositions
sub _alphabeticsort {
&_resetmatrix;
for (sort {uc($a->{appname}) cmp uc($b->{appname})} @instances) {
$_->setposition();
}
} # end _alphabeticsort
# callback invoked when user press on label
sub _cbOnPress {
shift;
my $self = shift;
$self->{label}->configure(-foreground => $hlbg);
if ($self->{status} == 1) {
IvyIO::kill($self->{appname});
$mw->after(400, sub {$self->{label}->configure(-foreground => $fg);});
} elsif ($self->{command}) {
my ($x, $y) = ($self->{label}->rootx, $self->{label}->rooty);
my $host = &_hostsmenu($x, $y, $self->{host});
$self->{label}->configure(-foreground => $fg);
return unless $host;
FugueConfig::launchAgent($self->{appname}, $host,
$self->{command}, $self->{params}, $bus);
print "$self->{appname} launched\n";
} else {
$mw->bell;
$mw->after(400, sub {$self->{label}->configure(-foreground => $fg);});
}
} # end _cbOnPress
# create and show hosts menu
sub _hostsmenu {
my $x = shift;
my $y = shift;
$x += 50;
my $preselected_host = shift;
return $preselected_host if @hosts <= 1;
$hosts_tl->destroy if Tk::Exists($hosts_tl);
$hosts_tl = $mw->Toplevel(-background => $darkbg);
$hosts_tl->resizable(0,0);
$hosts_tl->title('ivycontrolpanel');
my $hosts_fm = $hosts_tl->Frame(-background => $darkbg,
-highlightthickness => 3,
-highlightbackground => $hlbg,
)->pack(-side => 'top', -padx => 0, -pady => 0);
my @lattr = (-padx => 10,
-pady => 10,
-relief => 'flat',
-highlightthickness => 0,
-background => $darkbg,
-foreground => $fg,
-borderwidth => 0,
@fontspec);
my @battr = (@lattr,
-width => 4, -height => 1,
-highlightthickness => 3,
-highlightbackground => $hlbg,
-activebackground => $darkbg,
-activeforeground => $fg,
);
my @rattr = (@lattr,
-activebackground => $darkbg,
-activeforeground => $fg,
-selectcolor => $selcolor);
$hosts_fm->Label(@lattr, -text => "restart on :"
)->pack(-side => 'top', -padx => 20, -pady => 20);
for(@hosts) {
$hosts_fm->Radiobutton(@rattr,
-variable => \$preselected_host,
-value => $_,
-text => $_,
)->pack(-side => 'top');
}
my $fm = $hosts_fm->Frame(-background => $darkbg)->pack(-side => 'bottom');
$fm->Button(@battr,
-command => sub {$hosts_tl->destroy},
-text => 'ok')->pack(-side => 'left', -padx => 10, -pady => 20);
$fm->Button(@battr,
-command => sub {$preselected_host = undef; $hosts_tl->destroy},
-text => 'cancel')->pack(-side => 'left', -padx => 10, -pady => 20);
$hosts_tl->update;
my ($X, $Y) = ($mw->rootx, $mw->rooty);
my ($w, $h) = ($hosts_tl->width, $hosts_tl->height);
my ($W, $H) = ($mw->width, $mw->height);
$x = $X + $W - $w if ($x + $w) > $X + $W;
$y = $Y + $H - $h if ($y + $h) > $Y + $H;
$hosts_tl->geometry('+'.$x.'+'.$y);
$hosts_tl->waitWindow();
return $preselected_host;
} # end hostsmenu
1;
|