summaryrefslogtreecommitdiff
path: root/src/IvyIO.pm
diff options
context:
space:
mode:
Diffstat (limited to 'src/IvyIO.pm')
-rw-r--r--src/IvyIO.pm70
1 files changed, 58 insertions, 12 deletions
diff --git a/src/IvyIO.pm b/src/IvyIO.pm
index 8283a02..76dc968 100644
--- a/src/IvyIO.pm
+++ b/src/IvyIO.pm
@@ -2,19 +2,28 @@ package IvyIO;
use strict;
use Ivy;
+use Carp;
+use Tk;
my $ivy;
+my %appNameByhostAndPort = ();
+my %diedApp = ();
+my $pingcallback;
# init an ivy bus
sub init {
- my ($appname, $bus, $conncb, $disconncb) = @_;
+ my ($appname, $bus, $conncb, $disconncb, $pingcb, $mw) = @_;
Ivy->init(-loopMode => 'TK',
-appName => $appname,
-ivyBus => $bus,
);
+
+ $pingcallback = $pingcb;
+
$ivy = Ivy->new(-statusFunc => sub {&_status($conncb, $disconncb, @_);});
$ivy->start;
+ $mw->repeat (3000 ,[\&sendPings]) if defined $pingcb;
} # end init
@@ -27,13 +36,17 @@ sub kill {
sub _status {
- my ($conncb, $disconncb, $ref_array_present, $ref_array_absent,
- $ref_hash_present, $agent, $status, $host) = @_;
- if ($status eq "new") {
- &$conncb($agent, $host);
- } elsif ($status eq "died") {
- &$disconncb($agent, $host);
- }
+ my ($conncb, $disconncb, $ref_array_present, $ref_array_absent,
+ $ref_hash_present, $agent, $status, $host) = @_;
+ #print "Status : @_\n";
+ if ($status eq "new") {
+ &$conncb($agent, $host);
+ $appNameByhostAndPort{$host} = $agent;
+ delete $diedApp{$host};
+ } elsif ($status eq "died") {
+ &$disconncb($agent, $host);
+ $diedApp{$host} = 1;
+ }
} # end _status
@@ -83,7 +96,7 @@ sub bind_for_play_event {
my $cb = shift;
return unless $cb;
return unless $ivy;
- $ivy->bindRegexp("ClockStart", [sub { shift; &$cb(); }]);
+ $ivy->bindRegexp("^ClockStart", [sub { shift; &$cb(); }]);
} # end bind_for_play_event
@@ -92,7 +105,7 @@ sub bind_for_pause_event {
my $cb = shift;
return unless $cb;
return unless $ivy;
- $ivy->bindRegexp("ClockStop", [sub { shift; &$cb(); }]);
+ $ivy->bindRegexp("^ClockStop", [sub { shift; &$cb(); }]);
} # end bind_for_pause_event
@@ -103,12 +116,45 @@ sub bind_for_clock_and_rate_event {
my $cb = shift;
return unless $cb;
return unless $ivy;
- $ivy->bindRegexp('ClockEvent Time=(\d\d):(\d\d):(\d\d) Rate=(.*) Bs=.*',
+ $ivy->bindRegexp('^ClockEvent Time=(\d\d):(\d\d):(\d\d) Rate=(.*) Bs=.*',
[sub { shift; &$cb(@_); }]);
- $ivy->bindRegexp('ClockDatas Time=(\d\d):(\d\d):(\d\d) Rate=(.*) Bs=.*',
+ $ivy->bindRegexp('^ClockDatas Time=(\d\d):(\d\d):(\d\d) Rate=(.*) Bs=.*',
[sub { shift; &$cb(@_); }]);
} # end bind_for_clock_event
+# execute the callback kill all
+sub bind_for_kill_all {
+ my $cb = shift;
+ return unless $cb;
+ return unless $ivy;
+ $ivy->bindRegexp('^IvyControlPanel KillAll',
+ [sub { shift; &$cb(); }]);
+
+} # end bind_for_clock_event
+
+
+# send a ping to agent(s)
+sub sendPings ()
+{
+ my $appf;
+ foreach $appf (keys (%appNameByhostAndPort)) {
+ next if exists $diedApp{$appf};
+ $ivy->ping ($appf, \&receivePongCb);
+ }
+} # end sendPings
+
+
+# received pong, Agent is notified with the ping/pong time value
+sub receivePongCb($$)
+{
+ my ($time, $appf) = @_; # time = ping/pong duration in ms
+
+ #printf ("DBG> :$received: $appf [$time]".$appNameByhostAndPort{$appf}."\n");
+ &$pingcallback($appNameByhostAndPort{$appf}, $appf, $time);
+
+} # end receivePongCb
+
+
1;