summaryrefslogtreecommitdiff
path: root/src/IvyIO.pm
blob: a08a12a7242af4f4f680b14bcdb4cf098219757b (plain)
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
package IvyIO;

use strict;
use Ivy;
use Carp;
use Tk;


my $ivy;
my $MW;
my %appNameByhostAndPort = ();
my %diedApp = ();
my $pingcallback;
my %timerId = ();
my $pingdelay = 3000;
my $pongtimeout = 1000;

# init an ivy bus
sub init {
    my ($appname, $bus, $conncb, $disconncb, $pingcb, $mw) = @_;
    $MW = $mw;
    Ivy->init(-loopMode => 'TK', 
	      -appName => $appname,
	      -ivyBus => $bus,
         );
    
    $pingcallback = $pingcb;
    
    $ivy = Ivy->new(-statusFunc  => sub {&_status($conncb, $disconncb, @_);});
    $ivy->start;
    $mw->repeat ($pingdelay ,\&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 <time> and <rate>
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=.*',
		     [sub { shift; &$cb(@_); }]);
    $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};
    # gestion de timeout : si le pong n'est toujours pas reçu au bout de 500ms
    # on force l'appel à &$pingcallback avec un timestamp égal à -1
    $timerId{$appf} = 
      $MW->after($pongtimeout, sub {
         print "pong timeout on agent $appNameByhostAndPort{$appf} ($pongtimeout ms)\n";
         &$pingcallback($appNameByhostAndPort{$appf}, $appf, -1);
         delete $timerId{$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
  $MW->afterCancel($timerId{$appf}) if $timerId{$appf};
  #printf ("DBG> :$received: $appf [$time]".$appNameByhostAndPort{$appf}."\n");
  &$pingcallback($appNameByhostAndPort{$appf}, $appf, $time);
  
} # end receivePongCb


1;