-- File: AltoFileControl.mesa
-- Last edited by Levin:  27-Feb-81 11:14:20

DIRECTORY
  AltoFile USING [],
  AltoFilePrivate USING [
    Abandon, Close, Destroy, Extend, FileObject, FinalizeDirectory, FinalizeFileList,
    FinalizeKD, GetLength, GetTimes, InitializeDirectory, InitializeFileList,
    InitializeKD, Open, SetCreationTime, SetLength, Truncate],
  FileDefs USING [
    Buffer, Completer, CompleterArg, FileHandle, FSInstance, Operations,
    OperationsRecord, PageNumber],
  FrameDefs USING [IsBound],
  LogDefs USING [DisplayNumber],
  VMStorage USING [longTerm];

AltoFileControl: MONITOR
  IMPORTS AltoFilePrivate, FrameDefs, LogDefs, VMStorage
  EXPORTS AltoFile, AltoFilePrivate, FileDefs =

  BEGIN OPEN AltoFilePrivate;


  -- Global Variables --

  loginCount: CARDINAL;

  -- Miscellaneous --

  InsufficientLogouts: ERROR = CODE;
  TooManyLogouts: ERROR = CODE;
  UseDiskIODefs: ERROR = CODE;


  -- Types Exported to FileDefs --

  FileObject: PUBLIC TYPE = AltoFilePrivate.FileObject;

  -- Procedures Exported to FileDefs --

  InitializeAlto: PUBLIC PROCEDURE RETURNS [FileDefs.Operations] =
    BEGIN
    loggingEnabled ← FrameDefs.IsBound[LogDefs.DisplayNumber];
    altoOps ← VMStorage.longTerm.NEW[FileDefs.OperationsRecord ← [
      login: Login,
      logout: Logout,
      checkpoint: Checkpoint,
      abort: Abort,
      open: Open,
      close: Close,
      abandon: Abandon,
      destroy: Destroy,
      getLength: GetLength,
      setLength: SetLength,
      extend: Extend,
      truncate: Truncate,
      startRead: StartRead,
      startWrite: StartWrite,
      getTimes: GetTimes,
      setCreation: SetCreationTime]];
    InitializeFileList[];
    loginCount ← 0;
    RETURN[altoOps]
    END;

  FinalizeAlto: PUBLIC PROCEDURE =
    BEGIN
    IF loginCount ~= 0 THEN ERROR InsufficientLogouts;
    VMStorage.longTerm.FREE[@altoOps];
    FinalizeFileList[];
    FinalizeKD[];
    END;


  -- Variables Exported to AltoFile --

  altoOps: PUBLIC FileDefs.Operations;


  -- Variables Exported to AltoFilePrivate --

  loggingEnabled: PUBLIC BOOLEAN;


  -- Internal Procedures --

  Login: ENTRY PROCEDURE [server,
    userName, password, secondaryName, secondaryPassword: STRING ← NIL]
    RETURNS [FileDefs.FSInstance] =
    BEGIN
    IF loginCount = 0 THEN {InitializeDirectory[]; InitializeKD[]};
    loginCount ← loginCount + 1;
    RETURN[NIL]
    END;

  Logout: ENTRY PROCEDURE [fs: FileDefs.FSInstance] =
    BEGIN
    SELECT loginCount FROM
      0 => ERROR TooManyLogouts;
      1 => FinalizeDirectory[];
      ENDCASE;
    loginCount ← loginCount - 1;
    END;

  Checkpoint: PROCEDURE [fs: FileDefs.FSInstance] = {NULL};

  Abort: PROCEDURE [fs: FileDefs.FSInstance] = {NULL};

  StartRead, StartWrite: PROCEDURE [
    file: FileDefs.FileHandle, page: FileDefs.PageNumber, buffer: FileDefs.Buffer,
    callback: FileDefs.Completer, arg: FileDefs.CompleterArg] =
    BEGIN
    ERROR UseDiskIODefs
    END;


  END.