Copyright © 1984 by Xerox Corporation. All rights reserved.
Schroeder, November 8, 1983 1:53 pm
Levin, June 24, 1983 5:38 pm
Russ Atkinson, November 7, 1984 10:59:08 am PST
Doug Wyatt (DKW), November 27, 1984 2:18:36 pm PST -- changed SetState
DIRECTORY
BasicTime USING [FromPupTime, GMT, Now, nullGMT, Period],
FSBackdoor USING [Version],
FSRemoteFile USING [LookupResult],
PrincOps USING [zEXCH],
Process USING [Detach, SecondsToTicks],
PupDefs USING [GetFreePupBuffer, GetHopsToNetwork, GetPupAddress, MsToTocks, PupAddress, PupNameTrouble, PupBuffer, PupSocket, PupSocketDestroy, PupSocketMake, ReturnFreePupBuffer, SetPupContentsBytes],
PupTypes USING [fillInSocketID, Pair, PupSocketID, PupType],
Rope USING [Equal, Fetch, Length, ROPE],
RuntimeError USING [UNCAUGHT];
FSFileLookupImpl:
CEDAR
MONITOR
IMPORTS BasicTime, Process, PupDefs, Rope, RuntimeError
EXPORTS FSRemoteFile
= {
CARD: TYPE = LONG CARDINAL;
GMT: TYPE = BasicTime.GMT;
ROPE: TYPE = Rope.ROPE;
FileLookup protocol implementation
serverList: REF ServerInfo ← NIL; -- list of known servers;
packetCount: CARD ← 0; -- outgoing; also used as pupID
ReverseCardinal: TYPE = MACHINE DEPENDENT RECORD [high, low: CARDINAL];
ServerInfo:
TYPE =
RECORD [
name: ROPE,
addr: PupDefs.PupAddress,
state: FSRemoteFile.LookupResult, -- can be noResponse, noSuchPort, or ok
time: GMT, -- GMT of last ok or first not ok
next: REF ServerInfo];
FindServer:
ENTRY
PROC [server:
ROPE]
RETURNS [result: FSRemoteFile.LookupResult, info:
REF ServerInfo] =
TRUSTED {
ENABLE UNWIND => NULL;
result ← ok;
FOR info ← serverList, info.next
UNTIL info =
NIL
DO
IF Rope.Equal[info.name, server, FALSE] THEN {result ← info.state; RETURN};
ENDLOOP;
Didn't find an entry, so make one
{
LookupFileSocket: PupTypes.PupSocketID = [0, 61B];
addr: PupDefs.PupAddress;
addr ← PupDefs.GetPupAddress[LookupFileSocket, server
! PupDefs.PupNameTrouble => {
SELECT code
FROM
errorFromServer => result ← noSuchServer;
ENDCASE => result ← noResponse;
CONTINUE } ];
IF result # noSuchServer
THEN {
info ← NEW [ServerInfo ← [name: server, addr: addr, state: result, time: BasicTime.Now[], next: serverList]];
IF serverList = NIL THEN Process.Detach[FORK ServerCollector[]];
serverList ← info;
};
};
};
DoFileLookup:
PROC [addr: PupDefs.PupAddress, file:
ROPE]
RETURNS [result: FSRemoteFile.LookupResult, version: FSBackdoor.Version, create:
GMT, count:
INT] =
TRUSTED {
LookupFileType: PupTypes.PupType = LOOPHOLE[200B];
LookupFileReplyType: PupTypes.PupType = LOOPHOLE[201B];
LookupFileErrorType: PupTypes.PupType = LOOPHOLE[202B];
tries: CARDINAL = 4;
msWait: CARDINAL = 2000+500*MIN[8, PupDefs.GetHopsToNetwork[addr.net]];
s: PupDefs.PupSocket;
fileNameChars: CARDINAL = Rope.Length[file];
result ← noResponse; -- assume result
s ← PupDefs.PupSocketMake [local: PupTypes.fillInSocketID, remote: addr, ticks: PupDefs.MsToTocks[msWait] ];
THROUGH [1..tries]
DO
b: PupDefs.PupBuffer = PupDefs.GetFreePupBuffer[];
b.pupType ← LookupFileType;
b.pupID ← PupIDFromLongCardinal[packetCount ← packetCount + 1];
FOR i:
CARDINAL
IN [0 .. fileNameChars)
DO
b.pupChars[i] ← Rope.Fetch[file, i];
ENDLOOP;
PupDefs.SetPupContentsBytes[b, fileNameChars];
s.put[b];
{
r: PupDefs.PupBuffer = s.get[];
IF r = NIL THEN LOOP; -- no response yet
SELECT r.pupType
FROM
LookupFileReplyType => {
rPtr: LONG POINTER TO MACHINE DEPENDENT RECORD[v: CARDINAL, c, l: ReverseCardinal] = LOOPHOLE[@r.pupBody];
result ← ok;
version ← [rPtr.v];
create ← BasicTime.FromPupTime[
LongCardFromReverseCardinal[rPtr.c]
! RuntimeError.UNCAUGHT => {create ← BasicTime.nullGMT; CONTINUE}];
count ← LongCardFromReverseCardinal[rPtr.l];
};
LookupFileErrorType => result ← noSuchFile;
error =>
SELECT r.errorCode
FROM
noProcessPupErrorCode => result ← noSuchPort;
cantGetTherePupErrorCode => NULL;
ENDCASE => {PupDefs.ReturnFreePupBuffer[r]; LOOP};
ENDCASE => {PupDefs.ReturnFreePupBuffer[r]; LOOP};
PupDefs.ReturnFreePupBuffer[r];
EXIT;
};
ENDLOOP;
PupDefs.PupSocketDestroy[s];
};
PupIDFromLongCardinal:
PROC [s:
CARD]
RETURNS [PupTypes.Pair] =
TRUSTED
MACHINE CODE {PrincOps.zEXCH};
LongCardFromReverseCardinal:
PROC [r: ReverseCardinal]
RETURNS [
CARD] =
TRUSTED
MACHINE CODE {PrincOps.zEXCH};
SetState:
ENTRY
PROC [info:
REF ServerInfo, state: FSRemoteFile.LookupResult] = {
ENABLE UNWIND => NULL;
info.time ← BasicTime.Now[];
SELECT state
FROM
noResponse, noSuchPort => info.state ← state;
ENDCASE;
DKW: formerly IF state = noSuchPort THEN info.state ← noSuchPort;
};
ServerCollector:
ENTRY
PROC = {
ENABLE UNWIND => NULL;
forOneMinute: CONDITION ← [Process.SecondsToTicks[60]];
UNTIL serverList =
NIL
DO
last: REF ServerInfo ← NIL;
info: REF ServerInfo;
WAIT forOneMinute;
FOR info ← serverList, info.next
UNTIL info =
NIL
DO
age: INT = BasicTime.Period[info.time, BasicTime.Now[]];
IF (info.state # ok AND age > 300) OR (age > 1800)
THEN { IF last=NIL THEN serverList ← info.next ELSE last.next ← info.next; }
ELSE last ← info;
ENDLOOP;
ENDLOOP;
};
}.