BridgeCommImpl.mesa
Demers, February 20, 1987 10:59:48 am PST
DIRECTORY
BridgeComm USING [],
IO USING [Close, EndOfStream, Error, GetChar, PutChar, PutRope, STREAM],
RefText USING [AppendChar, ObtainScratch, ReleaseScratch],
Rope USING [FromRefText, ROPE],
XNSStream USING [ConnectionClosed, GetStatus, SendClose, SendEndOfMessage, SetTimeouts, State, Timeout];
BridgeCommImpl: CEDAR PROGRAM
IMPORTS IO, RefText, Rope, XNSStream
EXPORTS BridgeComm
~ {
ROPE: TYPE ~ Rope.ROPE;
NetworkStream: TYPE ~ IO.STREAM;
Error: PUBLIC ERROR [msg: ROPE] ~ CODE;
GetRopeToEOM: PROC [s: NetworkStream] RETURNS [rope: ROPE] ~ {
Read to EOM and return chars read as a rope. Clear the EndOfStream condition.
buf: REF TEXT;
state: XNSStream.State;
buf ← RefText.ObtainScratch[200];
DO
c: CHARIO.GetChar[s ! IO.EndOfStream => EXIT];
buf ← RefText.AppendChar[to: buf, from: c];
ENDLOOP;
DO
[state~state] ← XNSStream.GetStatus[s, TRUE];
IF state = open THEN EXIT;
ENDLOOP;
rope ← Rope.FromRefText[buf];
RefText.ReleaseScratch[buf];
};
GetMsg: PUBLIC PROC [s: NetworkStream] RETURNS [msg: CHAR, arg: ROPE] ~ {
ENABLE XNSStream.ConnectionClosed, XNSStream.Timeout, IO.Error, IO.EndOfStream =>
ERROR Error["Communication Failure"];
msg ← IO.GetChar[s];
arg ← GetRopeToEOM[s];
};
PutMsg: PUBLIC PROC [s: NetworkStream, msg: CHAR, arg: ROPENIL] ~ {
ENABLE XNSStream.ConnectionClosed, XNSStream.Timeout, IO.Error =>
ERROR Error["Communication Failure"];
IO.PutChar[s, msg];
IO.PutRope[s, arg];
XNSStream.SendEndOfMessage[s];
};
PutMsgWithReply: PUBLIC PROC [s: NetworkStream, msg: CHAR, arg: ROPENIL]
RETURNS [ansMsg: CHAR, ansArg: ROPE] ~ {
PutMsg[s, msg, arg];
[ansMsg, ansArg] ← GetMsg[s];
};
PutMsgWithAck: PUBLIC PROC [s: NetworkStream, msg: CHAR, arg: ROPENIL] ~ {
ansMsg: CHAR;
ansArg: ROPE;
PutMsg[s, msg, arg];
[ansMsg, ansArg] ← GetMsg[s];
IF ansMsg # 'Y THEN ERROR Error[ IF ansMsg = 'N THEN ansArg ELSE NIL ];
};
CloseConnection: PUBLIC PROC [s: NetworkStream] ~ {
IF s # NIL THEN {
{ ENABLE XNSStream.ConnectionClosed => CONTINUE;
XNSStream.SetTimeouts[s, 10000, 10000];
[] ← XNSStream.SendClose[s] };
IO.Close[s] };
};
}...