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
|
#!/usr/bin/perl -w
#
# $Source$
# $Revision$
#
# $Author$
# $Date$
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU GPL General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA,
# or refer to http://www.gnu.org/copyleft/gpl.html
#
use strict;
use Ivy;
use Getopt::Long;
use Term::ReadLine;
#use Tk;
use Carp;
my $term = Term::ReadLine->new("ivyprobe.pl");
#$term->tkRunning;
my $OUT = $term->OUT || *STDOUT{IO};
my $appliname = "IVYPROBE.PL";
my $bus;
my $timestamp = 0;
# for each application gives the number of running instances
my %connected_applications;
# for each couple appli:host gives the number of application running on host
my %where_applications;
&check_options;
Ivy->init (-ivyBus => (defined $bus) ? $bus : undef,
-appName => $appliname,
# -loopMode => 'TK',
-loopMode => 'LOCAL',
-messWhenReady => "$appliname READY",
);
my $Ivyobj = Ivy->new(-statusFunc => \&statusFunc,
);
foreach my $regexp (@ARGV) {
print $OUT "binding to $regexp\n";
if ($regexp =~ /'(.*)'/) { $regexp = $1; }
$Ivyobj->bindRegexp($regexp, [ "unused", \&callback] );
}
$Ivyobj->start;
my $IN = *STDIN{IO};
sub cb {
my $line = $term->readline("");
$term->addhistory($line);
chomp $line;
exit if (&interpret_line ($line));
}
Ivy->fileEvent($IN, \&cb);
Ivy->mainLoop;
#my $mw = MainWindow->new();
#$mw->fileevent($IN, 'readable' => \&cb);
#MainLoop;
sub printtime {
return if (!$timestamp);
my ($sec,$min,$hour) = localtime();
printf $OUT "[%02d:%02d:%02d] ", $hour, $min, $sec;
}
# this function has 3 additionnal parameters till Ivy Version 4.6
# and now getting the new/dying applications is straightforward.
# The first 3 parameters are kept only for upward compatibility!
sub statusFunc {
my ($ref_ready, $ref_nonReady, $ref_hashReady, $appname, $status, $host_or_regexp) = @_;
if ($status eq "new") {
&printtime;
print $OUT "$appname connected from $host_or_regexp\n";
$where_applications{"$appname:$host_or_regexp"}++;
}
elsif ($status eq "died") {
&printtime;
print $OUT "$appname disconnected from $host_or_regexp\n";
$where_applications{"$appname:$host_or_regexp"}--;
}
elsif ($status eq 'subscribing') {
&printtime;
print $OUT "$appname subscribed to '$host_or_regexp'\n";
}
elsif ($status eq 'unsubscribing') {
&printtime;
print $OUT "$appname unsubscribed to '$host_or_regexp'\n";
}
else {
&printtime;
print $OUT "Bug: unkown status; $status in &statusFunc\n";
}
%connected_applications = %$ref_hashReady;
}
# return 1 if exit!
sub interpret_line {
my ($str) = @_;
if ($str eq "") { return 0; } ;
if ($str =~ /^([^\.])/ or $str =~ /^\\/) {
my $count=$Ivyobj->sendMsgs($str);
&printtime;
print $OUT "-> Sent to $count peer";
if ($count > 1) { print $OUT "s" }
print $OUT "\n";
return 0;
}
if ($str =~ /^\.q(uit)?\s*$/) {
return 1;
}
if ($str =~ /^\.h(elp)?\s*$/) {
&line_command_usage;
return 0;
}
if ($str =~ /^\.die\s+(.*)/) {
my @applis = split (/\s+/, $1);
foreach my $appli (@applis) {
&printtime;
$Ivyobj->sendDieTo($appli);
}
return 0;
}
if ($str =~ /^.b(ind)?\s+(.*)$/) {
my $regexp = $2;
if ($regexp =~ /'(.*)'/) { $regexp = $1; }
print $OUT "binding $regexp\n";
$Ivyobj->bindRegexp($regexp, [ "unused", \&callback] );
return 0;
}
if ($str =~ /^.u(nbind)?\s+(.*)$/) {
my $regexp = $2;
if ($regexp =~ /'(.*)'/) { $regexp = $1; }
print $OUT "unbinding $regexp\n";
$Ivyobj->bindRegexp($regexp);
return 0;
}
if ($str =~ /^.db(ind)?\s+(.*)$/) {
my $id = $2;
print $OUT "direct binding id $id\n";
$Ivyobj->bindDirect($id, [\&directCallback] );
return 0;
}
if ($str =~ /^.d(irect)?\s+(\S+)\s+(\S+)\s+(.*)$/) {
my $appname = $2;
my $id = $3;
my $data = $4;
&printtime;
print $OUT "send direct to $appname id=$id $data\n";
$Ivyobj->sendDirectMsgs($appname, $id, $data);
return 0;
}
if ($str =~ /^.p(ing)?\s+(\S+)\s+(\d+)\s*$/) {
my $appname = $2;
my $timeout = $3;
&printtime;
print $OUT "ping $appname timeout=$timeout\n";
my $res = $Ivyobj->ping($appname, $timeout);
print $OUT "$res\n";
return 0;
}
if ($str =~ /^.who\s*$/) {
print $OUT "Apps:";
foreach my $app (sort keys %connected_applications) {
for (my $i=0; $i<$connected_applications{$app} ; $i++) {
print $OUT " $app";
}
}
print $OUT "\n";
return 0;
}
if ($str =~ /^.where\s+(\S+)$/) {
my $appli = $1;
my $found = 0;
foreach my $app_host (keys %where_applications) {
my ($app,$host) = $app_host =~ /(.+):(.*)/ ;
if ($app eq $appli) {
for (my $i=0; $i<$where_applications{$app_host}; $i++) {
print $OUT "Application $app on $host\n";
$found = 1;
}
}
}
print $OUT "No Application $appli\n" unless ($found);
return 0;
}
print $OUT "bad command. Type '.help' for a list of commands\n";
return 0;
}
sub callback {
my ($unused, $appname, @param) = @_;
my $paramString = "";
if (scalar @param) { $paramString = join ("' '", @param); }
print $OUT "$appname sent '", $paramString, "'\n";
}
sub directCallback {
my (@param) = @_;
my $paramString = "";
if (scalar @param) { $paramString = join ("|", @param); }
print $OUT "directMessage received '", $paramString, "'\n";
}
sub check_options {
# on traite la ligne de commande
my ($opt_help, $opt_appliname);
GetOptions("help" => \$opt_help,
"b:s" => \$bus,
"name:s" => \$opt_appliname,
"t" => \$timestamp,
);
if (defined $opt_appliname and $opt_appliname =~ /\s/) {
print $OUT "-name value should not contains blanck\n";
&usage;
}
&usage if ($opt_help && $opt_help);
$appliname = $opt_appliname if (defined $opt_appliname);
}
sub usage {
print $OUT "ivyprobe.pl [-h] [ -b <network>:<port> ] ['regexp']*\n";
print $OUT " ivyprobe.pl is a simple test application for the ivy-perl library\n";
print $OUT " Its is based on a similar appplication available with ivy-c\n";
print $OUT " It waits for messages on the bus, messages writtten on the command line\n";
print $OUT " or commands issued on the comnand line\n";
print $OUT " Help for the command line is available through the command\n";
print $OUT " .help or .h\n";
print $OUT " -h print this help\n";
print $OUT " -b <network>:<port>\n";
print $OUT " to defined the network adress and the port number\n";
print $OUT " defaulted to 127:2010\n";
print $OUT " -t print a time stamp when a message is send or received\n";
print $OUT " -name <this_appli_name>\n";
print $OUT " to change the default appliname \'$appliname\'\n";
exit;
}
sub line_command_usage { print "tutu\n";
print $OUT "Commands list:\n";
print $OUT " .h[elp] - this help\n";
print $OUT " .q[uit] - terminate this application\n";
print $OUT " .b[ind] regexp - add a msg to receive\n";
print $OUT " .u[nbind] regexp - remove a msg to receive\n";
print $OUT " .die appname1 appname2 ... - send die msg to appnameN\n";
print $OUT " .db[ind] id - add a direct msg to receive\n";
print $OUT " .d[irect] appname id args - send direct msg to appname\n";
print $OUT " .p[ing] appname timeout - ping appname with a delay of timeout ms NYI\n";
print $OUT " .where appname - on which host is/are appname\n";
print $OUT " .who - who is on the bus\n";
}
__END__
=head1 NAME
ivyprobe.pl - simple application to test ivy, to test other ivy-application or the perl ivy implementation.
=head1 SYNOPSIS
B<ivyprobe.pl> [-h] [-t] [-name <the_appli_name> ] [-b <network>:<port> ] ['regexp']*
=head1 DESCRIPTION
B<ivyprobe.pl> connects to the bus and offers a simple text interface to receive and send messages, and to subscribe to messages. It is very similar to the C and Java version named ivyprobe.
If regexps are given as parameters it subscribes to theses regexp.
To send a message, just type this message on the command line. It will be send to all applications who subscribe to this message. The number of application to which the message is sent is displayed. To send a message starting with a dot, prepend a backslash like this \.xxx
The user can input the following commands:
=over
=item B<.h[elp]>
to get the list of available commands and short explanations
=item B<.q[uit]>
to terminate the application.
=item B<.b[ind] regexp>
to add a subscription to messages matching the regexp.
=item B<.die appname1 appname2 ...>
to send a die msg to appnameN. The distant applications will stop.
=item B<.db[ind] id>
to add a direct msg of type id to receive
=item B<.d[irect] appname id string>
to send a direct msg to appname. The message type is indicated by id
=item B<.u[nbind] regexp>
To unsubscribe to messages matching the regexp
=item B<.where appname>
To get the host on which appname is/are running
=item B<.who>
to get the list of all connected applications
=back
=head1 BUGS
It should be possible to use line editing capabilities, but does not work currently.
No other know bugs at this time. Report them to author.
=head1 SEE ALSO
Ivy(3pm), perl(1), ivy-java(3), ivyprobe(1)
=head1 AUTHORS
Christophe Mertz <mertz@cena.fr>
=head1 COPYRIGHT
CENA (C) 2000-2002
=cut
|