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, $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 # kill a named agent sub kill { my $appname = shift; $ivy->sendDieTo($appname); } # end kill sub _status { 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 #------------------------------------------------------------------------ # # output # #------------------------------------------------------------------------ sub send_rate { my ($rate) = shift; return unless $ivy; $ivy->sendMsgs("SetClock Rate=$rate"); } # end ivy_send_rate sub send_time { my ($time) = shift; return unless $ivy; $ivy->sendMsgs("SetClock Time=$time"); } # end send_time sub send_pause { return unless $ivy; $ivy->sendMsgs("ClockStop"); } # end send_pause sub send_play { return unless $ivy; $ivy->sendMsgs("ClockStart"); } # end send_play_command #------------------------------------------------------------------------ # # input # #------------------------------------------------------------------------ sub bind_for_play_event { my $cb = shift; return unless $cb; return unless $ivy; $ivy->bindRegexp("^ClockStart", [sub { shift; &$cb(); }]); } # end bind_for_play_event sub bind_for_pause_event { my $cb = shift; return unless $cb; return unless $ivy; $ivy->bindRegexp("^ClockStop", [sub { shift; &$cb(); }]); } # end bind_for_pause_event # execute the callback with arguments