FSFileLookupImpl.mesa
Copyright © 1984, 1985 by Xerox Corporation. All rights reserved.
Schroeder, November 8, 1983 1:53 pm
Levin, June 24, 1983 5:38 pm
Russ Atkinson, March 18, 1985 10:41:54 pm PST
Doug Wyatt (DKW), November 27, 1984 2:18:36 pm PST -- changed SetState
Bob Hagmann April 30, 1985 1:05:25 pm PDT
Hal Murray, June 3, 1986 4:33:20 pm PDT
DIRECTORY
BasicTime USING [FromPupTime, GMT, Now, nullGMT, Period],
Endian USING [FWORD, CardFromF],
FSBackdoor USING [Version],
FSRemoteFile USING [GetServerPupName, LookupResult],
Process USING [Detach, Ticks, SecondsToTicks],
Pup USING [Address],
PupBuffer USING [Buffer],
PupHop USING [InitialTimeout],
PupName USING [NameLookup, Error],
PupSocket USING [AllocBuffer, CopyRope, CreateEphemeral, Destroy, FreeBuffer, Get, GetUniqueID, Send, SetNoErrors, Socket],
PupType USING [fileLookup, fileLookupError, fileLookupReply],
PupWKS USING [fileLookup],
Rope USING [Equal, ROPE],
RuntimeError USING [UNCAUGHT];
FSFileLookupImpl: CEDAR MONITOR
IMPORTS BasicTime, Endian, FSRemoteFile, Process, PupHop, PupName, PupSocket, Rope, RuntimeError
EXPORTS FSRemoteFile
= {
CARD: TYPE = LONG CARDINAL;
GMT: TYPE = BasicTime.GMT;
ROPE: TYPE = Rope.ROPE;
Exported to FSRemoteFile
Lookup: PUBLIC PROC [server, file: ROPE] RETURNS [result: FSRemoteFile.LookupResult, version: FSBackdoor.Version, create: GMT, count: INT] = {
sI: REF ServerInfo;
[result, sI] ← FindServer[server];
IF result = ok THEN {
[result, version, create, count] ← DoFileLookup[sI.addr, file];
SetState[sI, result];
};
};
FileLookup protocol implementation
serverList: REF ServerInfo ← NIL; -- list of known servers;
keepingList: BOOLTRUE;
keepingList => cache responses to try to reduce name lookup traffic and useless traffic when a server is obviously down
packetCount: CARD ← 0; -- outgoing; also used as pup id
ServerInfo: TYPE = RECORD [
name: ROPE,
addr: Pup.Address,
state: FSRemoteFile.LookupResult, -- can be noResponse, noSuchPort, or ok
timeOK: GMT, -- GMT of last ok
timeErr: GMT, -- GMT of last error
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
{
name: ROPE ← FSRemoteFile.GetServerPupName[server];
addr: Pup.Address;
addr ← PupName.NameLookup[name, PupWKS.fileLookup
! PupName.Error => {
SELECT code FROM
errorFromServer => result ← noSuchServer;
ENDCASE => result ← noResponse;
CONTINUE } ];
IF result # noSuchServer THEN {
time: GMT ← BasicTime.Now[];
info ← NEW [ServerInfo ← [
name: server, addr: addr, state: result,
timeOK: IF result = ok THEN time ELSE BasicTime.nullGMT,
timeErr: IF result # ok THEN time ELSE BasicTime.nullGMT,
next: serverList]];
IF serverList = NIL THEN Process.Detach[FORK ServerCollector[]];
IF keepingList THEN serverList ← info;
};
};
};
DoFileLookup: PROC [addr: Pup.Address, file: ROPE] RETURNS [result: FSRemoteFile.LookupResult, version: FSBackdoor.Version, create: GMT, count: INT] = TRUSTED {
tries: CARDINAL = 4;
msWait: CARDINAL ← PupHop.InitialTimeout[addr.net, 2000];
id: Endian.FWORD ← PupSocket.GetUniqueID[];
socket: PupSocket.Socket;
result ← noResponse; -- assume result
IF msWait = 0 THEN RETURN;
socket ← PupSocket.CreateEphemeral[remote: addr, getTimeout: msWait];
PupSocket.SetNoErrors[socket];
FOR i: NAT IN [0..tries) DO
b: PupBuffer.Buffer ← PupSocket.AllocBuffer[socket];
b.type ← PupType.fileLookup;
b.id ← id;
PupSocket.CopyRope[b, file];
PupSocket.Send[socket, b, addr];
b ← PupSocket.Get[socket];
IF b = NIL THEN LOOP; -- no response yet
IF b.id # id THEN { PupSocket.FreeBuffer[b]; LOOP; };
SELECT b.type FROM
PupType.fileLookupReply => {
result ← ok;
version ← [b.fileLookupReply.version];
create ← BasicTime.FromPupTime[Endian.CardFromF[b.fileLookupReply.createTime]
! RuntimeError.UNCAUGHT => {create ← BasicTime.nullGMT; CONTINUE}];
count ← Endian.CardFromF[b.fileLookupReply.length]; };
PupType.fileLookupError => result ← noSuchFile;
error => SELECT b.error.code FROM
noSocket => result ← noSuchPort;
cantGetThere => NULL;
ENDCASE => { PupSocket.FreeBuffer[b]; LOOP; };
ENDCASE => { PupSocket.FreeBuffer[b]; LOOP; };
PupSocket.FreeBuffer[b];
EXIT;
ENDLOOP;
PupSocket.Destroy[socket];
};
SetState: ENTRY PROC [info: REF ServerInfo, state: FSRemoteFile.LookupResult] = {
ENABLE UNWIND => NULL;
time: GMT ← BasicTime.Now[];
IF state = ok THEN info.timeOK ← time ELSE info.timeErr ← time;
SELECT state FROM
noResponse, noSuchPort => info.state ← state;
ENDCASE;
DKW: formerly IF state = noSuchPort THEN info.state ← noSuchPort;
};
pollingInterval: NAT ← 5;
seconds to wait before polling
okTimeout: NAT ← 1800;
seconds to wait before having a cached OK server evaporate
noResponseTimeout: NAT ← 30;
seconds to wait before having a noResponse evaporate
noSuchPortTimeout: NAT ← 30;
seconds to wait before having a noSuchPort evaporate
errorTimeout: NAT ← 30;
seconds to wait before having any other error evaporate
ServerCollector: ENTRY PROC = {
ENABLE UNWIND => NULL;
UNTIL serverList = NIL DO
last: REF ServerInfo ← NIL;
forPolling: CONDITION ← [Process.SecondsToTicks[pollingInterval]];
WAIT forPolling;
FOR info: REF ServerInfo ← serverList, info.next UNTIL info = NIL DO
time: GMT = BasicTime.Now[];
ageOK: INT = BasicTime.Period[info.timeOK, time];
ageErr: INT = BasicTime.Period[info.timeErr, time];
{
SELECT info.state FROM
ok => IF ageOK > okTimeout THEN GO TO Splice;
noResponse => IF ageErr > noResponseTimeout THEN GO TO Splice;
noSuchPort => IF ageErr > noSuchPortTimeout THEN GO TO Splice;
ENDCASE => IF ageErr > errorTimeout THEN GO TO Splice;
last ← info;
EXITS Splice =>
IF last=NIL THEN serverList ← info.next ELSE last.next ← info.next; };
ENDLOOP;
ENDLOOP;
};
}.
Bob Hagmann April 29, 1985 10:25:49 am PDT
changes to: DIRECTORY, FindServer