perm filename NEWFIB.SAI[NEW,AIL] blob sn#408256 filedate 1979-01-08 generic text, type T, neo UTF8
begin "newfib"
   require "cs:std.hdr" source!file;
   require "pltusr.hdr[170,160]" source!file;
   require "sys:proces.def" source!file;
   require "trmprc.hdr[170,160]" source!file;
   require 1000 new!items;
 
! shared names for global modules;
   integer Fib1, Fib2, George, CityHall;
 
! declare the slots used in the test;
 
   integer Object;
   integer Recipient;
   integer Complaint!Dept;
   integer Bitcher;
   string Problem;
   string Action;
   procedure KillThem;
   begin "KillThem"
      itemvar Temp;
      foreach Temp | Temp in Dependents("newfib") do
         terminate(Temp);
   end "KillThem";
   cleanup KillThem;

   recursive procedure Geo;
   begin "Geo"
      integer I, J, NextFib;
       integer hack;
      integer MyTrans1, MyTrans2;
      integer RequestedFib;
      boolean Fib1Alive, Fib2Alive;
      sprout!defaults pstack(5)+stringstack(4);
      MessageVar(Output);
      MessageVar(Input);

      Fib2 := FindModuleNumber(FindSiteNumber(MCCF,DjobName&"FIB2"
              &"kernel"),"fib2");
      Write("Module number for Fib2 = ",cvos(Fib2));

      MyTrans1 := GetNewTransactionNumber;
      MyTrans2 := GetNewTransactionNumber;
      hack := me;
      ConstructMessage(Output,((TTyAction,"NewDefault"),(TTyDefaultRecipient,me),
                               (TTyDefaultTransaction,MyTrans2)));
      SendOff(!!con!output,TTyHandlerProc,MyTrans2);
      ConstructMessage(Output,((TTyAction,"NextDefaultMessage")));
      SendOff(!!con!output,TTyHandlerProc,MyTrans2);


      RequestedFib := Fib2;
      Fib1Alive := true;
      Fib2Alive := true;

      ConstructMessage(Output,((Recipient,Me),(Action,"generate"),
                                  (Complaint!Dept,Me)));
      write("george about to send to a fib");
      SendOff(!!con!output,RequestedFib,MyTrans1);

      while true do
      begin "big while loop"
         write("george about to receive from somebody");
         ReceiveMessage(!!con!input,{ new (AnyModule) ⎇, { new(anytransaction)⎇);
         if Input:Transaction = MyTrans2 then
            begin
               if not missing(TTyInputString,Input) then
                  begin
                     if equ("fib1",Input:TTyInputString) then
                        begin
                           if Fib1Alive then
                              begin
                                 RequestedFib := fib1;
                                 write("Fib1 selected");
                              end
                           else
                              write(["Sorry, Fib1 is dead"]);
                        end
                     else if equ("fib2",Input:TTyInputString) then
                        begin
                           if Fib2Alive then
                              begin
                                 RequestedFib := fib2;
                                 write("Fib2 selected");
                              end
                           else
                              write(["Sorry, Fib2 is dead"]);
                        end
                     else
                        write(Input:TTyInputString, " is an unknown module");
                     ConstructMessage(Output,((TTyAction,
                            "NextDefaultMessage")));
                     SendOff(!!con!output,TTyHandlerProc,MyTrans2);
                     continue "big while loop";
                  end
               else
                  usererr(0,1,"Invalid message from tty server");
            end;
         if not missing(Problem,Input) then
            begin
               if equ(Input:Problem,"overflow") then
                  begin
                     write("overflow of ");
                     if Input:Sender=fib1 then
                        writeon("fib1")
                     else
                        writeon("fib2");
                  end
               else
                  write("unknown error = ", Input:Problem,
                        "I die.");
               ConstructMessage(Output,((Action,"die")));
               SendCopy(!!con!output,Input:Sender,MyTrans1);
               if Input:Sender=Fib1 then
                  begin
                     RequestedFib := Fib2;
                     Fib1Alive := false;
                  end
               else
                  begin
                     RequestedFib := Fib1;
                     Fib2Alive := false;
                  end;
               if not (Fib1Alive or Fib2Alive) then
                  done "big while loop";
               ConstructMessage(Output,((Recipient,Me),(Action,"generate"),
                                  (Complaint!Dept,Me)));
               Write("George about to send to ");
               if RequestedFib = Fib1 then
                  WriteOn("Fib1.")
               else
                  WriteOn("Fib2.");
               SendOff(!!con!output,RequestedFib,MyTrans1);
            end
         else if Missing(Object,Input) then
            begin
               write("no object slot in message to GEO");
               usererr(0,1,"dryrot in GEO");
            end
         else
            begin
               NextFib := Input:Object;
               if Input:Sender=Fib1 then
                  Write("Fib1")
               else
                  Write("Fib2");
               writeon(":NextFib =",NextFib);
               ConstructMessage(Output,((Recipient,Me),(Action,"generate"),
                                  (Complaint!Dept,Me)));
               Write("George about to send to ");
               if RequestedFib = Fib1 then
                  WriteOn("Fib1.")
               else
                  WriteOn("Fib2.");
               SendOff(!!con!output,RequestedFib,MyTrans1);
            end;
      end "big while loop";
      write("Death of George");
      Suicide;
   end "Geo";

   recursive procedure Fib;
   begin "Fib"
      sprout!defaults pstack(5)+stringstack(4);
      integer MyTrans;
      MessageVar(MyComplaint);
      MessageVar(Request);
      MessageVar(Reply);

      integer This, Last, Previous, Biggest;

      Last := 0; This := 1;
      Mytrans := DummyTransaction;  ! transactions not really being used here;
      Biggest := 2↑31 - 1;

      ConstructMessage(MyComplaint,((Problem,"overflow"), (Bitcher, Me),
             (Recipient,CityHall),(Complaint!Dept,CityHall)));

      while true do
      begin
!         write("fib1 about to receive from george");
         ReceiveMessage(!!con!Request,{ new(AnyModule) ⎇,
                         { new(AnyTransaction) ⎇);
         if not Missing(Action,Request) then
            begin
               if equ(Request:Action,"generate") then
                  begin
                     Previous := Last;
                     Last := This;
                     if Biggest - Last > Previous then
                        begin
                           This := Last + Previous;
                           ConstructMessage(Reply,((Object,This)));
!                           write("fib1 about to send to geo");
                           SendCopy(!!con!Reply,Request:Recipient,MyTrans);
                        end
                     else
                        begin
                           MyComplaint:Complaint!Dept := Request:Recipient;
                           if not Missing(Complaint!Dept, Request) then
                              MyComplaint:Complaint!Dept :=
                                                 Request:Complaint!Dept
                           else
                              MyComplaint:Recipient := CityHall;
                           SendCopy(!!con!mycomplaint,Request:Complaint!Dept,
                                    MyTrans);
                        end;
                  end
               else if equ(Request:Action,"die") then
                  begin
                     write("Death of Fib1");
                     Suicide;
                  end
               else
                  begin
                     write("Invalid request to FIB1");
                     usererr(0,1,"dryrot: fib1");
                  end;
            end
         else
            begin
               write("no action slot in request to fib1");
               usererr(0,1,"dryrot2:fib1");
            end;
      end;
   end "Fib";

! must initialize modules;

   begin
      itemvar Temp;
      Temp := new;
      sprout(Temp,Fib,SuspHim);
      Fib1 := GetNewModuleNumber;
      ModuleInsert (Fib1,Temp,"fib1", location(Fib1));
      put Temp in ReadyQueue before 1;
      put Temp in JoinSet; ! don't kill site until fib1 dies;
   end;

   begin
      itemvar Temp;
      Temp := new;
      sprout(Temp,Geo,SuspHim);
      George := GetNewModuleNumber;
      ModuleInsert (George,Temp,"george",location(George));
      put Temp in ReadyQueue before 1;
      put Temp in JoinSet; ! don't kill site until george dies;
   end;

   begin
      itemvar Temp;
      Temp := new;
      sprout(Temp,LowestTTyInputHandler,Susphim);
      TTyInterruptModule := GetNewModuleNumber;
      ModuleInsert(TTyInterruptModule,Temp,"ttyinterruptmodule",
                   location(TTyInterruptModule));
      put Temp in ReadyQueue before 1;
   ! site may die without this process dying;
   end;

   begin
      itemvar Temp;
      Temp := new;
      sprout(Temp,PlitsTTyServer,Susphim);
      TTyHandlerProc := GetNewModuleNumber;
      ModuleInsert(TTyHandlerProc,Temp,"ttyhandlerproc",
                   location(TTyHandlerProc));
      TTyHandlerTrans := GetNewTransactionNumber;
      put Temp in ReadyQueue before 1;
   ! site may die without this process dying;
   end;



   begin
      itemvar Temp;
      Temp := new;
      ModuleInsert (EXTERNALMODULE, Temp,"fib2",location(Fib2));
   end;

! must initialize slot data structure;

   SlotInsert(location(Object),0,Integer36Slot,"OBJECT");
   SlotInsert(location(Recipient),0,ModuleSlot,"RECIPIENT");
   SlotInsert(location(Complaint!Dept),0,ModuleSlot,"COMPLAINTDEPT");
   SlotInsert(location(Bitcher),0,ModuleSlot,"BITCHER");
   SlotInsert(location(Problem),0,StringSlot,"PROBLEM");
   SlotInsert(location(Action),0,StringSlot,"ACTION");
   SlotInsert(location(TTyAction),0,StringSlot,"TTYACTION");
   SlotInsert(location(TTyInputString),0,StringSlot,"TTYINPUTSTRING");
   SlotInsert(location(TTyDefaultRecipient),0,ModuleSlot,"TTyDefaultRecipient");
   SlotInsert(location(TTyDefaultTransaction),0,TransactionSlot,"TTyDefaultTransaction");

   WantHosts[1] := MCCF;
   MyName := "FIB1";

! call kernel to start the whole mess;
   Kernel;

   print(crlf,"successful return from kernel.");
end "newfib"