summaryrefslogtreecommitdiff
path: root/example/testCongestionTk.pl
blob: aa3d21a2b8bc5c64eccaa271f1c61fc25353e9d0 (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
#!/usr/bin/perl -w

use strict;
use Getopt::Long;
use Ivy;
use Time::HiRes;
use Tk;

sub defaultOption ($$);

my %options;
my $numberOfSentMsg = 0;
my $numberOfSentMsgWhenCongestion = 1e6;
END {Ivy::stop ();}


# cet exemple lance deux agents un qui envoie vite de gros messages, et un autre
# qui les reçoit. Lors des 10 premières receptions il attend une seconde après 
# chaque message, ensuite il depile aussi vite qu'il peut, cet exemple permet
# de tester le bon fonctionnement du mode non bloquant.



#OPTIONS
GetOptions (\%options,  "send", "receive");

unless ((exists $options{send}) || (exists $options{receive})) {
  if (fork () == 0) {
    sleep (1);
    exec (qw (./testCongestionTk.pl -send));
  } else {
    exec (qw (./testCongestionTk.pl -receive));
  }
}

defaultOption ("bus", $ENV{IVYBUS});
if (exists ($options{send})) {
  defaultOption ("ivyname", "TESTSEND");
} else {
  defaultOption ("ivyname", "TESTRECEIVE");
}

my $t0;
my $cbAppelee = 0;
# IVY

Ivy->init (-loopMode => 'TK',
           -appName =>  $options{ivyname},
           -ivyBus => $options{bus},
           -filterRegexp => [$options{ivyname}]
	  ) ;

my $bus = Ivy->new (-statusFunc => \&statusFunc,
		    -slowAgentFunc=> \&congestionFunc,
		    -blockOnSlowAgent => 0,
		    -neededApp => exists $options{send} ?
		    ["TESTRECEIVE"] : ["TESTSEND"]);

my $mw = MainWindow->new;
my $tx1 = $mw->Text;
my $tx2 = $mw->Text  (-height => 3);
$tx2->pack (-fill => 'both', -expand => 'false');
$tx1->pack (-fill => 'both', -expand => 'true');
$mw->title ($options{ivyname});

unless (exists ($options{send})) {
  $bus->bindRegexp ('TESTSEND SEND (\d+) (.*)', [\&receiveSend]);
}

if (exists ($options{send})) {
  $mw->repeat (10, [\&send]);
}

$bus->start ();

$bus->mainLoop ();
#Tk::MainLoop ();


# PROCEDURES


sub receiveSend ($$)
{
   my ($app, $iter) = @_;
   $tx1->insert ('end', "RECEIVE $iter\n");
   $tx1->yviewScroll (1, 'units');
   $tx1->idletasks();
   sleep (1) if ($cbAppelee++ < 10);
}


sub send ()
{
  $t0 = Time::HiRes::gettimeofday;
  #print ("DBG> send $t0\n");

  if ($numberOfSentMsg++ <  ($numberOfSentMsgWhenCongestion+100)) {
    $tx1->insert ('end', "SEND $numberOfSentMsg\n");
    $tx1->yviewScroll (1, 'units');
    $bus->sendAppNameMsgs ("SEND $numberOfSentMsg " . 'a' x 1020);
  }
}


sub defaultOption ($$)
{
  my ($option, $default) = @_;
  unless  (defined $options{$option}) {
#    warn "option $option non spécifiéee : utilision de $default\n";
    $options{$option} = $default;
  }
}


sub statusFunc ($$)
{
  my ($ready, $notReady) = @_;

  if (@{$notReady})  {
    printf "appli manquantes : %s\n", join (' ', @{$notReady});
  } else {
    printf ("Toutes applis OK !!\n");
  }
}

sub congestionFunc ($$$)
{
  my ($name, $addr, $state) = @_;

  if ($state == 1) {
    $tx2->insert ('end', sprintf ("$name [$addr] %s will stop at N=%d\n", $state ? "CONGESTION" : "OK",
	    $numberOfSentMsg+100));
    $numberOfSentMsgWhenCongestion = $numberOfSentMsg;
  } else {
    $tx2->insert ('end', sprintf ("$name [$addr] %s\n", $state ? "CONGESTION" : "OK"));
  }
  $tx2->update();
}