home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 11 / develop 11 code / The NetWork Project / Examples (Sources) / NetSim / task.impl.p < prev    next >
Encoding:
Text File  |  1992-07-15  |  9.8 KB  |  395 lines  |  [TEXT/MPS ]

  1. {© G. Sawitzki, StatLab Heidelberg 1986-1991}    
  2.  
  3. {----------------- Main Routines    ------------------}
  4.  
  5.  
  6. PROCEDURE MyInit; {(var TheState : TaskStateType)}
  7.  
  8. const 
  9.     chsize=128; chdelta=5;
  10.     cvsize=128; cvdelta=5;    
  11.     cvtsize=40;
  12. var myTaskHandler:tTaskHandler;
  13.     myMasterTaskHandler:tMasterTaskHandler;
  14.     mySlaveTaskHandler:tSlaveTaskHandler;
  15.     myTaskGenerator:tMyTaskGenerator;
  16.     myResultHandler:tReplyResultHandler;
  17.     h:MenuHandle;
  18.     s:str255;
  19.     i:integer;
  20.     myNamePtr:^str31;
  21.     ptyp:integer;
  22.     hc:tConfigurationHandle;err:oserr;
  23.     h1:handle;
  24.     myPic:PicHandle;
  25.  
  26. procedure fatal;
  27. var
  28.     scrl:longint;
  29. begin
  30.         progressreport(0,'NetSim ');
  31.         progressreport(1,'could not be launched');
  32.         progressreport(3,'This program needs a NetWork Processor');
  33.         progressreport(2,'This program needs ColorQuickdraw');
  34.         progressreport(4,'Please consult the documentation.');
  35.         ShowProgress;{make shure all is drawn and visible}
  36.         Delay(300,scrl);
  37.         halt;
  38. end;
  39.  
  40. BEGIN
  41.     h1:=GetResource('NSSY',128);
  42.     if GetHandleSize(h1)<>sizeOf(tConfiguration) then ProgramBreak('bad NSSY resource size');
  43.     hc:=tConfigurationHandle(h1);
  44.     err:=ResError;if (err<>noErr) | (hc=nil) then ProgramBreak('could not load NSSY configuration');
  45.     if hc^^.version<>5 then ProgramBreak('bad NSSY configuration version') 
  46.     else theConfiguration:=hc^^;
  47.  
  48.     ReleaseResource(handle(hc));
  49.  
  50.     gNextNetRefresh:=tickcount+CNetRefreshDelta;
  51.  
  52.     gSoldResults:='';
  53.  
  54.     nextTicks:=tickCount+TicksDelta;
  55.     repeat until ReadDateTime(oldTime)=noErr;
  56.     goldtime:=oldtime;
  57.     nrResults:=0;
  58.     gnrResults:=0;
  59.     gProtocolOn:=false;
  60.     with theConfiguration do begin
  61.         if     ShowControlMenu then begin 
  62.             InsertMenu(GetMenu(128),0);
  63.             DrawMenuBar;
  64.         end;
  65.     end;
  66.  
  67.     {prepare your workarea here}
  68.     gTaskState := TaskNew;
  69.  
  70.     if InitNetwork(NetWorkEvt)<>NoErr then fatal;
  71.     if NlInit<>noErr then fatal;
  72.  
  73.     {NetWorkScheduler installation}
  74.     new(NetWorkScheduler);
  75.     if NetWorkScheduler=nil then fatal;
  76.  
  77.     NetWorkScheduler.init;
  78.     
  79.     if NetWorkScheduler.err=NoErr then 
  80.     begin 
  81.         NetWorkScheduler.TaskIterations:=theconfiguration.samplesize;
  82.         StatisticWindow:=nil;
  83.  
  84.  
  85.         with nform do begin
  86.             style:= FixedDecimal;
  87.             digits:=2;
  88.         END;
  89.  
  90.         if master then begin 
  91.             new(myMasterTaskHandler);    myTaskHandler:=tTaskHandler(myMasterTaskHandler); 
  92.  
  93.  
  94.         end else begin 
  95.             new(mySlaveTaskHandler);    new(myResultHandler);
  96.             if myResultHandler<> nil then myResultHandler.init;
  97.             mySlaveTaskHandler.ResultHandler:=myResultHandler;
  98.             myTaskHandler:=tTaskHandler(mySlaveTaskHandler);
  99.  
  100.  
  101.         end;
  102.  
  103.         {new(myTaskHandler);}
  104.         if myTaskHandler<>nil then NetWorkScheduler.InitTaskHandler(myTaskHandler);
  105.     end;{if NetWorkScheduler.err=NoErr then }
  106.  
  107.  
  108.     {end of NetWorkScheduler installation}
  109.  
  110.     randseed:=tickCount;
  111.     tToy_init(theConfiguration.SampleSize);
  112.     
  113.     
  114.     doopen(cnew); {for example, fake a new-command}
  115.     BaseWindow:=Frontwindow; {no close box on this one, so it will always exist}
  116.     baserect:=BaseWindow^.portrect;
  117.     baserect.left:=baserect.left+200;
  118.     fillRect(baserect,ltgray);
  119.  
  120.     system.windowcount:=3;{leave some space}
  121.     MakeAWindow('Record Min/Max Samples', 300, 100, -noGrowDocProc);
  122.     RecordWindow:=Frontwindow;
  123.     {$Ifc false}
  124.     minrect:=RecordWindow^.portrect;
  125.     minrect.bottom:=minrect.bottom-50;
  126.     maxrect:=RecordWindow^.portrect;
  127.     maxrect.top:=maxrect.top+50;
  128.     setrect(minrect,0,0,300,50);
  129.     {$Endc}
  130.         setrect(minrect,0,10,100,40);
  131.     
  132. setrect(maxrect,0,60,100,90);
  133.     setport(RecordWindow);
  134.     
  135.     minPic:=openPicture(minrect);
  136.     showpen;
  137.     framerect(minrect);
  138.     fillrect(minrect,ltGray);
  139.     hidepen;
  140.     closePicture;
  141.     
  142.     maxPic:=openPicture(maxrect);
  143.     showpen;
  144.     framerect(maxrect);
  145.     fillrect(maxrect,dkGray);
  146.     hidepen;
  147.     closePicture;
  148.     
  149.     NewRecordPicture;
  150.     
  151.     minRecord:=+inf;
  152.     maxRecord:=-inf;
  153.     if system.GraficModel<>ColorQuickdrawModel 
  154.     THEN fatal;
  155.  
  156.     IF master THEN BEGIN
  157.         new(myTaskGenerator);
  158.         IF myTaskGenerator<>NIL THEN BEGIN
  159.  
  160.             NetWorkScheduler.initTaskGenerator(myTaskGenerator);
  161.         END;
  162.  
  163.         StatisticWindow:=GetNewWindow(129,@StatisticWindowRec,Pointer(-1));{should go to open}
  164.         initstatistics;
  165.         setrect(localhrect,0,0,chsize,cvsize);offsetrect(localhrect,chdelta,cvdelta);
  166.         setrect(totalhrect,0,0,chsize,cvsize);offsetrect(totalhrect,chsize+2*chdelta,cvdelta);
  167.  
  168.         setrect(localsrect,0,0,chsize,cvtsize);offsetrect(localsrect,chdelta,cvsize+2*cvdelta);
  169.         setrect(totalsrect,0,0,chsize,cvtsize);offsetrect(totalsrect,chsize+2*chdelta,cvsize+2*cvdelta);
  170.  
  171.         setrect(pcomprect,0,0,chsize,cvsize);offsetrect(pcomprect,chdelta,
  172.         cvsize+3*cvdelta+cvtsize);
  173.         setrect(qcomprect,0,0,chsize,cvsize);offsetrect(qcomprect,chsize+2*chdelta,
  174.         cvsize+3*cvdelta+cvtsize);
  175.     END;
  176.  
  177.     with gMsgHeader do begin
  178.         traceinfo:=0;
  179.         version:=cMyMsgHeaderVersion;
  180.         action:=aNoop;
  181.         ContentsToken:=longint('NONE');
  182.         DistributionToken:=longint(cDefaultDistribution);
  183.         DisplayToken:=longint(cDefaultDisplay);
  184.         SampleSize:=50;
  185.     end;
  186. END;
  187.  
  188. {Application equivalent of system Task: this is your main program}
  189. PROCEDURE MyTask(phase : BackPhase);
  190. const 
  191.     cForeGroundSleep=0;
  192.     cBackGroundSleep=10;
  193. BEGIN
  194.     {do your job here} 
  195.     CASE phase OF
  196.         BackBegin :  begin{we are going to the background}
  197.             {progressreport(1,'Template is in background')};
  198.             if gWaitNextEventSleep>=cForeGroundSleep then gWaitNextEventSleep:=cForeGroundSleep;
  199.         end;
  200.         BackContinue : {we are called again}
  201.         begin 
  202.             if gInBackground then {if you want special background tasks done}
  203.             begin if gWaitNextEventSleep>cBackGroundSleep then gWaitNextEventSleep:=cBackGroundSleep;
  204.                 if master then DoLocalTask;
  205.                 {invertrect(thePort^.portrect)     }{ do it here.}
  206.                 { this is just a quick&dirty example }
  207.             end else begin
  208.                 if master then DoLocalTask;
  209.                 if gWaitNextEventSleep>cForeGroundSleep then gWaitNextEventSleep:=cForeGroundSleep;
  210.             end;
  211.         end;
  212.         BackEnd : {we are coming from background}
  213.         begin
  214.             {hideprogress;}
  215.             { this was just a quick&dirty example, let's clean up}
  216.             if gWaitNextEventSleep>cForeGroundSleep then gWaitNextEventSleep:=cForeGroundSleep;
  217.             eraserect(thePort^.portrect);
  218.             invalrect(thePort^.portbits.bounds);
  219.         end;
  220.         OTHERWISE         {should not occur}
  221.     END;
  222.     if TickCount>gNextNetRefresh then begin
  223.         DrawStatisticWindow;
  224.         gNextNetRefresh:=TickCount+cNetRefreshDelta;
  225.     end;
  226.     if master then gTaskState := TaskOk else gTaskState:=TaskIdle; {if all is ok}
  227. END;
  228.  
  229. PROCEDURE MyCleanUp;
  230. var msg:msgPtr;
  231.     me,Dest:msgAddr;
  232. BEGIN
  233.     {clean up any mess you have done here. Save your results}
  234.     if gProtocolOn then     close(OutFile);
  235.     
  236.     {send a goodbye message via broadcast to release all partners}
  237.     preparemessageHeader(gMsgHeader,aDone);
  238.     me:=NetWorkScheduler.MySelf;
  239.     Dest.p:=NetWorkScheduler.MySelf.p;
  240.     Dest.a := -1; { broadcast }
  241.     if master then if postmsg(msg,nil,cMsgNAttention+cMustBeLaunched,
  242.     NetWorkScheduler.TaskGenerator.ContextStamp,
  243.     Dest,me,
  244.     @gMsgHeader,sizeof(gMsgHeader),nil,0)<>noErr then;
  245.     if NetWorkScheduler<>nil then NetWorkScheduler.free;
  246.  
  247. END;
  248.  
  249. {-------------------- routines on request    -------------------------}
  250.  
  251. PROCEDURE doopen;
  252. CONST
  253.     height = 100;{just for example}
  254.     width = 300;
  255. VAR
  256.     anyStr,s : Str255;
  257.     anyindex : integer;
  258.     anyFaktor, anyScale : real;
  259.     anyHandle : PicHandle;
  260.     val:extended;
  261.     mytime:longint;q:extended;
  262. BEGIN
  263.     CASE itsCmdNumber OF
  264.         cnew : 
  265.         BEGIN
  266.             {save old results}
  267.             if ReadDateTime(myTime)=NoErr then 
  268.             begin
  269.                 q:=(mytime-goldTime);
  270.                 NumToString(round(q),s);
  271.                 numtostring(gnrResults,gsOldResults);
  272.                 
  273.                 gsOldResults:=concat('Zeit: ',s,' s #: ',gsOldResults);
  274.             end;
  275.  
  276.             nextTicks:=tickCount+TicksDelta;
  277.  
  278.             repeat until ReadDateTime(oldTime)=noErr;
  279.  
  280.             gnrResults:=0;
  281.             nrResults:=0;
  282.  
  283.             {just an example- make a sample window}
  284.  
  285.             anyStr := ('Local Sample');
  286.             if system.WindowCount =0 then
  287.             { two types, for example: with no Grow, no GoAway}
  288.             MakeAWindow(anyStr, width, height, -noGrowDocProc);
  289.  
  290.             {define your own real 2d coordinates}
  291.             setcoord(-system.WindowCount, 1, system.WindowCount, -1);
  292.             
  293.             sInit;
  294.         END;
  295.         cOpen : 
  296.         ;
  297.         OTHERWISE {should not occur}
  298.     END;
  299. END;
  300.  
  301. {=============================================================================}
  302.  
  303. {take a picture ask for a file name, and store the picture in file. Fake MacDraw file}
  304.  
  305. procedure saveas(ThePic: PicHandle);
  306. type
  307.     TMDHdr = record {PICT 1 header}
  308.         ftype: OSType;
  309.         hdrid: integer;
  310.         version: integer;
  311.         prRec: array[1..60] of integer;
  312.         xorigin: Fixed;
  313.         yorigin: point;
  314.         xscale: point;
  315.         yscale: point;
  316.         atrstate: array[1..31] of integer;
  317.         lcnt: integer;
  318.         ltot: Integer;
  319.         lsiz: longint;
  320.         lr2d: rect;
  321.         filler1: array[1..141] of integer
  322.     end;
  323. var
  324.     header: TMDHdr;
  325.     tempReply: SFReply;
  326.     Helpfi: TFileinfo;
  327.  
  328.     temperr: oserr;
  329.     tempref: integer;
  330.     Proposal: str255;
  331.     P: ptr;
  332.     count: longint;
  333.     r: rect;
  334. begin
  335.  
  336.     numtostring(tickcount, proposal);
  337.     proposal := concat( 'Template.', proposal);
  338.     with header do begin
  339.         ftype := 'PICT';
  340.         hdrid := 0;
  341.         version := 0;
  342.     end;
  343.     if ThePic <> nil then begin
  344.         SFPutFile(System.SFPutPoint, 'Save graph as…', Proposal, nil, tempReply);
  345.         if tempreply.good then begin {try to open file}
  346.             if create(tempreply.fname, tempreply.VRefNum, 'MDPL', 'PICT') <> noerr then
  347.             else begin
  348.                 temperr := FSOpen(tempreply.fname, tempreply.VRefNum, tempref);
  349.                 hlock(handle(thePic));
  350.                 count := 512;
  351.                 temperr := FsWrite(tempref, count, @header);
  352.                 count := gethandlesize(Handle(ThePic));
  353.                 temperr := FsWrite(tempref, count, Ptr(thepic^));{write picture contents}
  354.                 hunlock(handle(thePic));
  355.                 temperr := fsclose(tempref);{close  file}
  356.             end;
  357.         end;
  358.  
  359.     end;
  360. end;
  361.  
  362. {=============================================================================}
  363.  
  364.  
  365. PROCEDURE dosave;
  366. var ThePic:PicHandle;
  367.     TheFrontWindow:WindowPtr;
  368. BEGIN
  369.     CASE itsCmdNumber OF
  370.         cSave : 
  371.         ;
  372.         cSaveAs : {this is a bad, but helpful example. if the frontwindow has a picture,
  373.         we save it in a Pict file, and claim to be MacDraw}
  374.         begin
  375.             TheFrontWindow:=frontwindow;
  376.             if TheFrontWindow<>nil then begin
  377.                 ThePic:=PicHandle(GetWindowPic(TheFrontWindow));
  378.                 if ThePic<>nil then saveas(ThePic);
  379.             end;
  380.         end;
  381.         cSaveCopy : 
  382.         ;
  383.         OTHERWISE {should not occur}
  384.     END;
  385. END;
  386.  
  387. PROCEDURE doclose;
  388. BEGIN
  389.     CASE itsCmdNumber OF
  390.         cClose : 
  391.         ;
  392.         OTHERWISE {should not occur}
  393.     END;
  394. END;
  395.