{$DOUBLE}

Program MSLoop;

{
  Description: General Message Looper
         File: [message]MSLoop.PAS
       Author: Jim Bostwick  23-NOV-1988 
    Last Edit: 20-OCT-1989 04:07:59 
      History:
    	 20-OCT-1989  JMB - modified header, integrated message_rec
    	 9-OCT-1989 - JMB - major happy munging...
	 3-AUG-1989 - JMB - Add $DOUBLE compiler switch


}

{$nolist}
{[a+,b+,l-,k+,r+] Pasmat }
%INCLUDE 'PAS$EXT:GENERAL.TYP';
%INCLUDE 'PAS$EXT:MESSAGE.pkg';
%INCLUDE 'PAS$EXT:STRING.PKG';
%INCLUDE 'Pas$ext:GTSK.TYP';
%INCLUDE 'Pas$ext:GTSK.Ext';
%INCLUDE 'PAS$EXT:CATR56.EXT';
%INCLUDE 'PAS$EXT:CR56TA.EXT';
%include 'pas$ext:tlon.ext';
%include 'pas$ext:rdef.ext';
%include 'pas$ext:clef.ext';
%include 'pas$ext:stlo.ext';
%include 'pas$ext:srda.ext';
{$list}


CONST
	die_efn = f92;
	rcv_efn = f93;
var
	In_Msg: Message_rec;
	Stat, Len, pos: integer;
	done: boolean;
	r5nam:Rad56;
	Tsk_info: Task_info_rec;
	MyName: CH6;  { name we're running under }
	LogFlag: CH8; 	{ flag string for logging }
	temp_node, node: CH6;    { node we're running on }
	Str,out_str: ch80;    { a useful string }


{ Procedure to dump DSW and header }
Procedure Dump_hdr(Var h:Message_Rec);

VAR
	nam: CH6;
BEGIN
	swrite(output,LogFlag);
	writeln(output,'MSRCVR> Dump of Message Header:');
	writeln(output,'And the status is ',$DSW,'.');
        dmphdr(output,h)
end;



begin  { Main }
	clef(die_efn);
	clef(rcv_efn);
	srda(rcv_efn);
	done := false;
	Gtsk(tsk_Info);		     { find out about self }
	cr56ta(Tsk_info.Name,MyName);  { make it printable }
	sassign(LogFlag,MyName);	{ build log flag string }
	sconcat(LogFlag,'> ');
	Tlon('SYS$NODE',node,len);	{ find out node name }
	sassign(str,'MSLoop operating on ');
	sconcat(str,node);
	sconcat(str,' as task ');
	sconcat(str,Myname);
	schconcat(str,'.');
	swrite(output,str);
	writeln;
	repeat
	    SClear(In_Msg.comment);	{ clear out old junk }
	    stlo([rcv_efn,die_efn]);	{ wait for activity }
	    if rdef(rcv_efn) then 
		begin
		clef(rcv_efn);
		MSRcv(null_task_name,In_Msg);
		Dump_Hdr(In_Msg);
		Writeln('  and the text is:');
		len := slen(In_msg.comment);
		pos := 1;
		While len >= 70 do 
		    begin
		    writeln('len=',len,'   pos=',pos);
		    ssubstr(str,in_msg.comment,pos,70);
		    sassign(out_str,LogFlag);
		    sconcat(out_str,str);
		    sconcat(out_str,'| ');
		    swrite(output,out_str);
		    writeln(output);
		    pos := pos + 70;
		    len := len - 70
		    end;
		if (len > 0) then 
		    begin
		    writeln('len=',len,'   pos=',pos);
		    ssubstr(str,in_msg.comment,pos,len);
		    sassign(out_str,LogFlag);
		    sconcat(out_str,str);
		    sconcat(out_str,'| ');
		    swrite(output,out_str);
		    writeln(output)
		    end;
		{ 
		  To reply, we need exchange source and destination
		  node and task names, and possibly fiddle the router. 
		  ?? DO WE NEED A MSREPLY CALL ??
		 }
		with in_msg do
		    begin
		    dest_task := src_task;
		    src_task := tsk_info.name;
		    dest_node := src_node;
		    src_node := node;
		    end;
		Mssend(in_msg,f0);
		Swrite(output,LogFlag);
		Writeln('MSLOOP> Reply status = ',$dsw,'.')
		end;
	    done := rdef(die_efn)
	until done;	
	writeln( 'Task ',Myname,' done.')
end.
    
 
