summaryrefslogtreecommitdiff
path: root/example/ivyprobe.pl
blob: 2cc1d5112754e0d29f4bbe7114233d5bbd341172 (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
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
#!/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;

my $appliname = "IVYPROBE.PL";
my $bus;

# 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;

if (defined $bus) {
    Ivy->init (-ivyBus => $bus,
	       -appName => $appliname,
#	       -loopMode => 'TK',
	       -loopMode => 'LOCAL',
	       -messWhenReady => "$appliname READY",
	       ); }
else {
    Ivy->init (-appName => $appliname,
#	       -loopMode => 'TK',
	       -loopMode => 'LOCAL',
	       -messWhenReady => "$appliname READY",
	       );
}

my $Ivyobj = Ivy->new(-statusFunc => \&statusFunc,
		      );

foreach my $regexp (@ARGV) {
    print "binding to $regexp\n";
    if ($regexp =~ /'(.*)'/) { $regexp = $1; }
    $Ivyobj->bindRegexp($regexp, [ "unused", \&callback] );
}

my $term = Term::ReadLine->new("ivyprobe.pl");

$Ivyobj->start;

no strict 'subs';
my $IN = $term->IN;
use strict 'subs';


Ivy->fileEvent($IN, \&cb);

Ivy->mainLoop;


# 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") {
	print "$appname connected from $host_or_regexp\n";
	$where_applications{"$appname:$host_or_regexp"}++;
    }
    elsif ($status eq "died") {
	print "$appname disconnected from $host_or_regexp\n";
	$where_applications{"$appname:$host_or_regexp"}--;
    }
    elsif ($status eq 'subscribing') {
	print "$appname subscribed to '$host_or_regexp'\n";
    }
    elsif ($status eq 'unsubscribing') {
	print "$appname unsubscribed to '$host_or_regexp'\n";
    }
    else {
	print "Bug: unkown status; $status in &statusFunc\n";
    }
    
    %connected_applications = %$ref_hashReady;
}


sub cb {
    my $str = <$IN>;
    chomp $str;
    exit if (&interpret_line ($str));
}



# return 1 if exit!
sub interpret_line {
    my ($str) = @_;
    if ($str eq "") { return 0; }
    if ($str =~ /^([^\.])/) {
	my $count=$Ivyobj->sendMsgs($str);
	print "-> Sent to $count peer";
	if ($count > 1) { print "s" }
	print "\n";
	return 0;
    }
    if ($str =~ /^\.q(uit)?\s*$/) {
	return 1;
    }
    if ($str =~ /^\.h(elp)?\s*$/) {
	&line_command_usage;
	return 0;
    }

    if ($str =~ /^\.die\s+(\w+)$/) {
	$Ivyobj->sendDieTo($2);
	return 0;
    }

    if ($str =~ /^.b(ind)?\s+(.*)$/) {
	my $regexp = $2;
	if ($regexp =~ /'(.*)'/) { $regexp = $1; }
	print "binding $regexp\n";
	$Ivyobj->bindRegexp($regexp, [ "unused", \&callback] );
	return 0;
    }

    if ($str =~ /^.u(nbind)?\s+(.*)$/) {
	my $regexp = $2;
	if ($regexp =~ /'(.*)'/) { $regexp = $1; }
	print "unbinding $regexp\n";
	$Ivyobj->bindRegexp($regexp);
	return 0;
    }

    if ($str =~ /^.db(ind)?\s+(.*)$/) {
	my $id = $2;
	print "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;
	print "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;
	print "ping $appname timeout=$timeout\n";
	my $res = $Ivyobj->ping($appname, $timeout);
	print "$res\n";
	return 0;
    }

    if ($str =~ /^.who\s*$/) {
	print "Apps:";
	foreach my $app (sort keys %connected_applications) {
	    for (my $i=0; $i<$connected_applications{$app} ; $i++) {
		print " $app";
	    }
	}
	print "\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 "Application $app on $host\n";
		    $found = 1;
		}
	    }
	}
	print "No Application $appli\n" unless ($found);
	return 0;
    }

    print "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 "$appname sent '", $paramString, "'\n";
}

sub directCallback {
    my (@param) = @_;

    my $paramString = "";
    if (scalar @param) { $paramString = join ("|", @param); }
    print "directMessage received '", $paramString, "'\n";
}


sub check_options {
    # on traite la ligne de commande
    my ($opt_help, $opt_gf);
    GetOptions("help" => \$opt_help,
	       "b:s"    => \$bus,
	       );

    &usage if ($opt_help && $opt_help);
}


sub usage {
    print "ivyprobe [-h] [ -b <network>:<port> ]  ['regexp']*\n";
    print " ivyprobe is a simple test application for the ivy-perl library\n";
    print " Its is based on a similar appplication available with ivy-c\n";
    print " It waits for messages on the bus, messages writtten on the command line\n";
    print " or commands issued on the comnand line\n";
    print " a help for the command line is available through\n";
    print " the command .help or .h\n";
    print "   -h   print this help\n";
    print "   -b   <network>:<port>\n";
    print "        to defined the network adress and the port number\n";
    print "        defaulted to 127:2010\n";
    exit;
}


sub line_command_usage {
    print "Commands list:\n";
    print "	.h[elp]				- this help\n";
    print "	.q[uit]				- terminate this application\n";
    print "	.b[ind] regexp			- add a msg to receive\n";
    print "	.u[nbind] regexp		- remove a msg to receive\n";
    print "	.die appname			- send die msg to appname\n";
    print "	.db[ind] id			- add a direct msg to receive\n";
    print "	.d[irect] appname id args	- send direct msg to appname\n";
    print "	.p[ing] appname timeout		- ping appname with a delay of timeout ms NYI\n";
    print "	.where appname			- on which host is/are appname\n";
    print "	.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] [-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 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.

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 appname>

to send a die msg to appname. This application 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 is currently not possible to send a message which begin with a dot. It will be interpreted as a command (usually unknown!)

No other know bugs at this time. Report them to author.


=head1 SEE ALSO

ivy-perl(1), perl(1)

=head1 AUTHORS

Christophe Mertz <mertz@cena.fr>

=head1 COPYRIGHT

CENA (C) 2000-2001

=cut