XNSEchoServerImpl.mesa
Copyright © 1986 by Xerox Corporation. All rights reserved.
Demers, June 5, 1986 11:39:22 pm PDT
The XNS Echo Server.
DIRECTORY
Process USING [Abort],
XNS USING [Address, Socket],
XNSBuf USING [Buffer],
XNSEchoBuf USING [Buffer],
XNSEchoServer USING [],
XNSErrorTypes USING [protocolViolationErr],
XNSSocket USING [Create, Destroy, FreeBuffer, Get, Handle, ReturnError, ReturnToSender, SetNoErrors, waitForever],
XNSWKS USING [echo];
XNSEchoServerImpl: CEDAR MONITOR
IMPORTS Process, XNSSocket
EXPORTS XNSEchoServer
~ BEGIN
echoProcess: PROCESS;
Create: PUBLIC ENTRY PROC [buffers: CARDINAL] ~ {
ENABLE UNWIND => NULL; -- No ERRORs
IF echoProcess = NIL THEN {
nGood ← nNotRequest ← nNotEcho ← 0;
echoProcess ← FORK EchoServer[buffers] };
};
Destroy: PUBLIC ENTRY PROC ~ {
ENABLE UNWIND => NULL; -- No ERRORs
IF echoProcess # NIL THEN TRUSTED {
Process.Abort[echoProcess];
JOIN echoProcess;
echoProcess ← NIL };
};
nGood: INT;
nNotEcho: INT;
nNotRequest: INT;
EchoServer: PROC [buffers: CARDINAL] ~ {
b: XNSBuf.Buffer ← NIL;
eb: XNSEchoBuf.Buffer ← NIL;
h: XNSSocket.Handle ← XNSSocket.Create[sendBuffers~0, recvBuffers~buffers, getTimeout~XNSSocket.waitForever, local~XNSWKS.echo];
DO
ENABLE ABORTED => EXIT;
b ← XNSSocket.Get[h];
SELECT b.hdr1.type FROM
echo => {
TRUSTED { eb ← LOOPHOLE[b] };
SELECT eb.hdr2.type FROM
request => {
nGood ← nGood.SUCC;
eb.hdr2.type ← reply;
XNSSocket.ReturnToSender[handle~h, b~b];
b ← NIL }
ENDCASE => {
nNotRequest ← nNotRequest.SUCC;
XNSSocket.ReturnError[handle~h, b~b,
type~XNSErrorTypes.protocolViolationErr];
b ← NIL };
};
ENDCASE => {
nNotEcho ← nNotEcho.SUCC;
XNSSocket.ReturnError[handle~h, b~b,
type~XNSErrorTypes.protocolViolationErr];
b ← NIL };
ENDLOOP;
IF b # NIL THEN XNSSocket.FreeBuffer[handle~h, b~b];
XNSSocket.SetNoErrors[handle~h, noErrors~TRUE];
XNSSocket.Destroy[h];
};
END.