-- Grapevine: Lily: main control

-- [Indigo]<Grapevine>Lily>LilyControl.mesa

-- Andrew Birrell  17-Dec-82  9:13:26
-- Mike Schroeder, 21-Oct-82  9:19:28

DIRECTORY
Ascii		USING[ ControlT, CR, DEL ],
DisplayDefs	USING[ DisplayOff, StopCursor ],
DriverDefs	USING[ freeQueue ],
HeapDefs	USING[ Compactor, HeapRestart ],
ImageDefs	USING[ BcdTime ],
IODefs		USING[ GetInputStream, SetOutputStream ],
LilyCommandDefs	USING[ LilyCommands ],
LogDefs		USING[ DisplayNumber, SetTypescriptParameters, StatisticsOn,
		       TypescriptOn, WriteChar, WriteLine, WriteLogEntry,
		       WriteString ],
PolicyDefs	USING[ SetOperationLimit ],
ProcessDefs	USING[ Detach ],
ProtocolDefs	USING[ SetTestingMode ],
PupDefs		USING[ AdjustBufferParms, AppendMyName,
                       GetPupPackageUseCount, PupPackageMake ],
PupRouterDefs	USING[ numberOfNetworks ],
RestartDefs	USING[ Enquiry ],
StreamDefs	USING[ DestroyKeyHandler, StreamHandle, StreamObject],
StringDefs	USING[ AppendString ],
TimeDefs	USING[ AppendFullDayTime, UnpackDT ],
VMDefs		USING[ InitializeVM ];

LilyControl: PROGRAM
IMPORTS DisplayDefs, DriverDefs, HeapDefs,
        ImageDefs, IODefs, LilyCommandDefs, LogDefs, PolicyDefs,
	ProcessDefs, ProtocolDefs, PupDefs, PupRouterDefs,
	RestartDefs, StreamDefs, StringDefs, TimeDefs, VMDefs =

BEGIN

inStream: StreamDefs.StreamHandle = IODefs.GetInputStream[];

myStream: StreamDefs.StreamObject ←
   [ reset: NIL, get: NIL, putback: NIL, put: MyPut, endof: NIL,
     destroy: NIL, link: NIL, body: Other[type:0, data:NIL] ];

MyPut: PROC[str: StreamDefs.StreamHandle, c: UNSPECIFIED] =
   { LogDefs.WriteChar[c] };

WriteVersion: PROCEDURE[where: {disk, screen}] =
   BEGIN
   s: STRING = [256];
   StringDefs.AppendString[s, "Grapevine server starting.  Version of "L];
   TimeDefs.AppendFullDayTime[s, TimeDefs.UnpackDT[ImageDefs.BcdTime[]] ];
   IF where = disk
   THEN LogDefs.WriteLogEntry[s]
   ELSE LogDefs.WriteLine[s];
   END;

testMode: BOOLEAN ← FALSE;

SetTestingMode: PROCEDURE =
   BEGIN
   IF NOT inStream.endof[inStream]
   AND inStream.get[inStream] = Ascii.ControlT
   THEN BEGIN
        LogDefs.WriteString["Set testing mode [confirm] "L];
        UNTIL inStream.endof[inStream]
        DO [] ← inStream.get[inStream] ENDLOOP;
        DO c: CHARACTER = inStream.get[inStream];
           SELECT c FROM
             'y, 'Y, Ascii.CR =>
               BEGIN
               LogDefs.WriteLine["Yes"L];
               ProtocolDefs.SetTestingMode[]; testMode ← TRUE;
               EXIT
               END;
             'n, 'N, Ascii.DEL =>
               { LogDefs.WriteLine["No"L]; EXIT };
           ENDCASE => LogDefs.WriteChar['?];
        ENDLOOP;
        END;
   END;

StartStats: PROCEDURE =
   BEGIN
   OPEN StringDefs;
   s: STRING = [256];
   AppendString[s, IF testMode THEN "*** Testing: "L ELSE "Grapevine: "L];
   AppendString[s, "Unintelligent Terminal Server """L];
   PupDefs.AppendMyName[s];
   AppendString[s, """"L];
   LogDefs.StatisticsOn[s];
   LogDefs.WriteLogEntry[s];
   END;

StartCompactor: PROC =
   { START HeapDefs.Compactor };

StartHeap: PROC =
   BEGIN
   initHeap: BOOLEAN =  START HeapDefs.HeapRestart[];
   ProcessDefs.Detach[FORK StartCompactor[]];
   END;


-- Main program --

DisplayDefs.DisplayOff[black]; DisplayDefs.StopCursor[];
LogDefs.SetTypescriptParameters[tsLines: 3];
LogDefs.TypescriptOn[];
WriteVersion[screen];
IODefs.SetOutputStream[@myStream];

[] ← PupDefs.GetPupPackageUseCount[]; -- to start PupRouterCold! --
PupRouterDefs.numberOfNetworks ← 255; -- exclude 0; in PupRouterCold --
PupDefs.AdjustBufferParms[54--buffers--, 64--words--];
-- Beware of OIS routing table packets and Alto I microcode bugs! --
PupDefs.PupPackageMake[];
LogDefs.DisplayNumber["Free PUPs"L, [short[@DriverDefs.freeQueue.length]]];

VMDefs.InitializeVM[min:15--pages--, max:45--pages--];

PolicyDefs.SetOperationLimit[op: lily, limit: 10]; --more Lily connections--

WriteVersion[disk];

SetTestingMode[];

StartHeap[];

StartStats[];

StreamDefs.DestroyKeyHandler[];

START RestartDefs.Enquiry;
START LilyCommandDefs.LilyCommands;

END.