3 # This module impliments the message handling for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
12 @ISA = qw(DXProt DXChannel);
26 use vars qw(%work @msg $msgdir %valid %busy);
28 %work = (); # outstanding jobs
29 @msg = (); # messages we have
30 %busy = (); # station interlocks
31 $msgdir = "$main::root/msg"; # directory contain the msgs
34 fromnode => '9,From Node',
35 tonode => '9,To Node',
38 t => '0,Msg Time,cldatetime',
39 private => '9,Private',
40 subject => '0,Subject',
41 linesreq => '0,Lines per Gob',
42 rrreq => '9,Read Confirm',
45 stream => '9,Stream No',
46 count => '9,Gob Linecnt',
47 file => '9,File?,yesno',
48 gotit => '9,Got it Nodes,parray',
49 lines => '9,Lines,parray',
50 read => '9,Times read',
55 # allocate a new object
56 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
60 my $self = bless {}, $pkg;
61 $self->{msgno} = shift;
63 $self->{from} = shift;
65 $self->{private} = shift;
66 $self->{subject} = shift;
67 $self->{origin} = shift;
68 $self->{read} = shift;
69 $self->{rrreq} = shift;
79 delete $ref->{linesreq};
80 delete $ref->{tonode};
81 delete $ref->{fromnode};
82 delete $ref->{stream};
90 my ($self, $line) = @_;
91 my @f = split /[\^\~]/, $line;
92 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
95 if ($pcno == 28) { # incoming message
96 my $t = cltounix($f[5], $f[6]);
97 my $stream = next_transno($f[2]);
98 my $ref = DXMsg->alloc($stream, $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
100 # fill in various forwarding state variables
101 $ref->{fromnode} = $f[2];
102 $ref->{tonode} = $f[1];
103 $ref->{rrreq} = $f[11];
104 $ref->{linesreq} = $f[10];
105 $ref->{stream} = $stream;
106 $ref->{count} = 0; # no of lines between PC31s
107 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
108 $work{"$f[2]$stream"} = $ref; # store in work
109 $busy{$f[2]} = $ref; # set interlock
110 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
114 if ($pcno == 29) { # incoming text
115 my $ref = $work{"$f[2]$f[3]"};
117 push @{$ref->{lines}}, $f[4];
119 if ($ref->{count} >= $ref->{linesreq}) {
120 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
121 dbg('msg', "stream $f[3]: $ref->{linereq} lines received\n");
128 if ($pcno == 30) { # this is a incoming subject ack
129 my $ref = $work{$f[2]}; # note no stream at this stage
131 $ref->{stream} = $f[3];
133 $ref->{linesreq} = 5;
134 $work{"$f[2]$f[3]"} = $ref; # new ref
135 dbg('msg', "incoming subject ack stream $f[3]\n");
136 $busy{$f[2]} = $ref; # interlock
138 push @{$ref->{lines}}, ($ref->read_msg_body);
139 $ref->send_tranche($self);
143 if ($pcno == 31) { # acknowledge a tranche of lines
144 my $ref = $work{"$f[2]$f[3]"};
146 dbg('msg', "tranche ack stream $f[3]\n");
147 $ref->send_tranche($self);
149 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
154 if ($pcno == 32) { # incoming EOM
155 dbg('msg', "stream $f[3]: EOM received\n");
156 my $ref = $work{"$f[2]$f[3]"};
158 $self->send(DXProt::pc33($f[2], $f[1], $f[3]));# acknowledge it
160 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
161 # store the file or message
162 # remove extraneous rubbish from the hash
163 # remove it from the work in progress vector
164 # stuff it on the msg queue
165 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
166 $ref->{msgno} = next_transno("Msgno") if !$ref->{file};
167 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
168 $ref->store($ref->{lines});
171 $ref->stop_msg($self);
174 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
180 if ($pcno == 33) { # acknowledge the end of message
181 my $ref = $work{"$f[2]$f[3]"};
183 if ($ref->{private}) { # remove it if it private and gone off site#
186 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
187 $ref->store($ref->{lines}); # re- store the file
189 $ref->stop_msg($self);
191 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
197 if ($pcno == 40) { # this is a file request
198 $f[3] =~ s/\\/\//og; # change the slashes
199 $f[3] =~ s/\.//og; # remove dots
200 $f[3] = lc $f[3]; # to lower case;
201 dbg('msg', "incoming file $f[3]\n");
202 last SWITCH if $f[3] =~ /^\/(perl|cmd|local_cmd|src|lib|include|sys|msg)\//; # prevent access to executables
204 # create any directories
205 my @part = split /\//, $f[3];
207 my $fn = "$main::root";
208 pop @part; # remove last part
209 foreach $part (@part) {
212 last SWITCH if !mkdir $fn, 0777;
213 dbg('msg', "created directory $fn\n");
215 my $stream = next_transno($f[2]);
216 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
218 # forwarding variables
219 $ref->{fromnode} = $f[1];
220 $ref->{tonode} = $f[2];
221 $ref->{linesreq} = $f[5];
222 $ref->{stream} = $stream;
223 $ref->{count} = 0; # no of lines between PC31s
225 $work{"$f[2]$stream"} = $ref; # store in work
226 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
231 if ($pcno == 42) { # abort transfer
232 dbg('msg', "stream $f[3]: abort received\n");
233 my $ref = $work{"$f[2]$f[3]"};
235 $ref->stop_msg($self);
245 # store a message away on disc or whatever
247 # NOTE the second arg is a REFERENCE not a list
253 # we only proceed if there are actually any lines in the file
254 if (!$lines || @{$lines} == 0) {
258 if ($ref->{file}) { # a file
259 dbg('msg', "To be stored in $ref->{to}\n");
261 my $fh = new FileHandle "$ref->{to}", "w";
264 foreach $line (@{$lines}) {
268 dbg('msg', "file $ref->{to} stored\n");
270 confess "can't open file $ref->{to} $!";
272 # push @{$ref->{gotit}}, $ref->{fromnode} if $ref->{fromnode};
273 } else { # a normal message
275 # attempt to open the message file
276 my $fn = filename($ref->{msgno});
278 dbg('msg', "To be stored in $fn\n");
280 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
281 my $fh = new FileHandle "$fn", "w";
283 my $rr = $ref->{rrreq} ? '1' : '0';
284 my $priv = $ref->{private} ? '1': '0';
285 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{read}^$rr\n";
286 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
289 foreach $line (@{$lines}) {
290 $ref->{size} += (length $line) + 1;
294 dbg('msg', "msg $ref->{msgno} stored\n");
296 confess "can't open msg file $fn $!";
306 # remove it from the active message list
307 @msg = map { $_ != $self ? $_ : () } @msg;
309 # belt and braces (one day I will ask someone if this is REALLY necessary)
310 delete $self->{gotit};
311 delete $self->{list};
314 unlink filename($self->{msgno});
315 dbg('msg', "deleting $self->{msgno}\n");
318 # read in a message header
328 $file = new FileHandle;
329 if (!open($file, $fn)) {
330 print "Error reading $fn $!\n";
334 $line = <$file>; # first line
336 $size -= length $line;
337 if (! $line =~ /^===/o) {
338 print "corrupt first line in $fn ($line)\n";
342 @f = split /\^/, $line;
343 $ref = DXMsg->alloc(@f);
345 $line = <$file>; # second line
347 $size -= length $line;
348 if (! $line =~ /^===/o) {
349 print "corrupt second line in $fn ($line)\n";
354 @f = split /\^/, $line;
355 push @{$ref->{gotit}}, @f;
356 $ref->{size} = $size;
363 # read in a message header
367 my $msgno = $self->{msgno};
370 my $fn = filename($msgno);
373 $file = new FileHandle;
374 if (!open($file, $fn)) {
375 print "Error reading $fn $!\n";
378 chomp (@out = <$file>);
381 shift @out if $out[0] =~ /^=== /;
382 shift @out if $out[0] =~ /^=== /;
386 # send a tranche of lines to the other end
389 my ($self, $dxchan) = @_;
391 my $to = $self->{tonode};
392 my $from = $self->{fromnode};
393 my $stream = $self->{stream};
396 for ($i = 0; $i < $self->{linesreq} && $self->{count} < @{$self->{lines}}; $i++, $self->{count}++) {
397 push @out, DXProt::pc29($to, $from, $stream, ${$self->{lines}}[$self->{count}]);
399 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
404 # find a message to send out and start the ball rolling
408 my @nodelist = DXProt::get_all_ak1a();
413 # bat down the message list looking for one that needs to go off site and whose
414 # nearest node is not busy.
416 dbg('msg', "queue msg ($sort)\n");
417 foreach $ref (@msg) {
418 # firstly, is it private and unread? if so can I find the recipient
419 # in my cluster node list offsite?
420 if ($ref->{private}) {
421 if ($ref->{read} == 0) {
422 $clref = DXCluster->get($ref->{to});
423 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
424 $dxchan = $clref->{dxchan};
425 $ref->start_msg($dxchan) if $clref && !get_busy($dxchan->call);
428 } elsif ($sort == undef) {
429 # otherwise we are dealing with a bulletin, compare the gotit list with
430 # the nodelist up above, if there are sites that haven't got it yet
431 # then start sending it - what happens when we get loops is anyone's
432 # guess, use (to, from, time, subject) tuple?
434 foreach $noderef (@nodelist) {
435 next if $noderef->call eq $main::mycall;
436 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
438 # if we are here we have a node that doesn't have this message
439 $ref->start_msg($noderef) if !get_busy($noderef->call);
444 # if all the available nodes are busy then stop
445 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
449 # start the message off on its travels with a PC28
452 my ($self, $dxchan) = @_;
454 dbg('msg', "start msg $self->{msgno}\n");
455 $self->{linesreq} = 5;
457 $self->{tonode} = $dxchan->call;
458 $self->{fromnode} = $main::mycall;
459 $busy{$dxchan->call} = $self;
460 $work{"$self->{tonode}"} = $self;
461 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
464 # get the ref of a busy node
477 # get the forwarding queue
483 # stop a message from continuing, clean it out, unlock interlocks etc
486 my ($self, $dxchan) = @_;
487 my $node = $dxchan->call;
489 dbg('msg', "stop msg $self->{msgno} stream $self->{stream}\n");
491 delete $work{"$node$self->{stream}"};
496 # get a new transaction number from the file specified
500 $name =~ s/\W//og; # remove non-word characters
501 my $fn = "$msgdir/$name";
504 my $fh = new FileHandle;
505 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
507 $msgno = $fh->getline;
511 $fh->print("$msgno\n");
512 dbg('msg', "msgno $msgno allocated for $name\n");
515 confess "can't open $fn $!";
520 # initialise the message 'system', read in all the message headers
523 my $dir = new FileHandle;
527 # read in the directory
528 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
529 @dir = readdir($dir);
536 $ref = read_msg_header("$msgdir/$_");
539 # add the message to the available queue
545 # add the message to the directory listing
549 confess "tried to add a non-ref to the msg directory" if !ref $ref;
553 # return all the current messages
559 # get a particular message
564 return $_ if $_->{msgno} == $msgno;
565 last if $_->{msgno} > $msgno;
570 # return the official filename for a message no
573 return sprintf "$msgdir/m%06d", shift;
577 # return a list of valid elements
586 # return a prompt for a field
591 my ($self, $ele) = @_;
596 # send a message state machine
603 if ($self->state eq 'send1') {
605 confess "local var gone missing" if !ref $self->{loc};
606 my $loc = $self->{loc};
607 $loc->{subject} = $line;
609 $self->state('sendbody');
610 #push @out, $self->msg('sendbody');
611 push @out, "Enter Message /EX (^Z) to send or /ABORT (^Y) to exit";
612 } elsif ($self->state eq 'sendbody') {
613 confess "local var gone missing" if !ref $self->{loc};
614 my $loc = $self->{loc};
615 if ($line eq "\032" || uc $line eq "/EX") {
618 if (@{$loc->{lines}} > 0) {
619 foreach $to (@{$loc->{to}}) {
621 my $systime = $main::systime;
622 my $mycall = $main::mycall;
623 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
632 $ref->store($loc->{lines});
634 #push @out, $self->msg('sendsent', $to);
635 push @out, "msgno $ref->{msgno} sent to $to";
638 delete $loc->{lines};
641 $self->state('prompt');
644 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
645 #push @out, $self->msg('sendabort');
646 push @out, "aborted";
647 delete $loc->{lines};
651 $self->state('prompt');
654 # i.e. it ain't and end or abort, therefore store the line
655 push @{$loc->{lines}}, $line;
665 my $name = $AUTOLOAD;
666 return if $name =~ /::DESTROY$/;
669 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
670 @_ ? $self->{$name} = shift : $self->{$name} ;