EchoProtocol.Mesa
Copyright Ó 1992 by Xerox Corporation. All rights reserved.
Last Edited by: Spreitzer, March 20, 1985 11:42:33 am PST
Last tweaked by Mike Spreitzer on November 6, 1989 6:31:18 pm PST
DIRECTORY IO, Process, Protocols, RefText, Rope;
EchoProtocol: CEDAR PROGRAM
IMPORTS IO, Process, Protocols, RefText
= {OPEN Protocols;
echo: Protocol ¬ NEW [ProtocolRep ¬ [
name: "Echo",
Instantiate: Instantiate,
StartConnect: StartConnect,
StartDisconnect: StartDisconnect,
GiveUp: GiveUp,
SetOnLine: SetOnLine,
protocolData: NIL]];
Instantiate: PROC [client: Client, logFileName: ROPE ¬ NIL] RETURNS [c: Conversant] ~ {
c ¬ NEW [ConversantPrivate ¬ [echo, client, [FALSE, disconnected], NIL, client.fromClient, NIL]];
TRUSTED {Process.Detach[FORK Copy[from: client.fromClient, to: client.toClient]]};
RETURN};
StartConnect: PROC [c: Conversant, serverName: ROPE, login: BOOL ¬ FALSE] ~ {
c.state.onLine ¬ TRUE;
c.state.connectivity ¬ connected;
c.client.type.NoteState[c];
RETURN};
StartDisconnect: PROC [c: Conversant, verbosely: BOOL ¬ TRUE] = {
c.state.onLine ¬ FALSE;
c.state.connectivity ¬ disconnected;
c.client.type.NoteState[c];
RETURN};
SetOnLine: PROC [c: Conversant, onLine: BOOL] ~ {
c.state.onLine ¬ onLine;
c.client.type.NoteState[c];
RETURN};
GiveUp: PROC [c: Conversant, verbosely: BOOL ¬ TRUE] ~ {RETURN};
Copy: PROC [from, to: IO.STREAM] ~ {
bufsize: NAT ~ bufferSize;
buff: REF TEXT ~ RefText.New[bufsize];
{ENABLE IO.Error, IO.EndOfStream => CONTINUE;
WHILE NOT from.EndOf[] DO
ask: NAT ~ MIN[from.CharsAvail[TRUE], bufsize];
got: NAT ¬ from.GetBlock[buff, 0, ask !IO.EndOfStream => EXIT];
IF got=0 THEN ERROR--yet another bloody fool hasn't implemented an IO class correctly--;
to.PutBlock[buff];
ENDLOOP;
};
from.Close[!IO.Error => CONTINUE];
to.Close[!IO.Error => CONTINUE];
RETURN};
bufferSize: NAT ¬ 1024;
Protocols.RegProtocol[echo];
}.