-- LUserImpl.mesa
-- Last trashed by D. Swinehart, September 22, 1982 8:32 am
-- Last modified by L. Stewart, October 4, 1982 2:55 pm

DIRECTORY
	ConvertUnsafe USING [ ToRope ],
	Inline USING [LowByte],
	IO,
	LarkServer,
	LarkServerRpcControl USING [ ImportInterface ],
	LarkUser USING [ Machine ],
	LarkUserRpcControl USING [ ExportInterface ],
	Names USING [ CurrentPasskey, CurrentRName ],
	Process USING [ Detach, Pause, SecondsToTicks ],
	PupDefs USING [ AppendPupAddress, PupAddress, PupAddressLookup, PupNameTrouble ],
	Rope USING [ROPE],
	RPC USING [ ShortROPE ],
	RPCBcstListen USING [ BcstListenInitialize ]
	;

LUserImpl: PROGRAM
	IMPORTS
		ConvertUnsafe, Inline, IO, LarkServer, LarkServerRpcControl, LarkUserRpcControl, Names,
			Process, PupDefs, RPCBcstListen
	EXPORTS LarkUser = {

in, out: IO.STREAM;

Register: PUBLIC PROC [ machine: LarkUser.Machine ] RETURNS [ instance: RPC.ShortROPE] = {
	pa: PupDefs.PupAddress = [ net: [machine.net], host: [machine.host], socket:  [a: 0, b: 0]];
	res: STRING←[100];
	res.length←0;
	PupDefs.PupAddressLookup[res, pa!PupDefs.PupNameTrouble => {
		PupDefs.AppendPupAddress[res, pa]; res.length←res.length-1;  CONTINUE}];
	instance ← ConvertUnsafe.ToRope[LONG[res]];
	Process.Detach[FORK LarkServerProc[instance]];	-- start Diagnostic user.
 	};
 

GetChar: PUBLIC PROC [ value: CARDINAL ] = {
	c: CHAR = LOOPHOLE[Inline.LowByte[value]];
	out.PutF["%c", IO.char[c]];
	};

LarkServerProc: PROC[instance: RPC.ShortROPE] = {
	c: CHAR;
	Process.Pause[Process.SecondsToTicks[1]]; -- let Register results settle.
	LarkServerRpcControl.ImportInterface[interfaceName: [ type: "LarkServer.lark", instance: instance ]];
	-- This here's the main loop.
	out.PutF["LarkServer ready for business.\n"];
	DO ENABLE IO.Signal => TRUSTED { CONTINUE; };
		out.PutF["%% "];
		c ← in.GetChar[];
		IF c # '  THEN LarkServer.PutChar[LOOPHOLE[c]]
		ELSE LarkServer.PutString[in.GetLine[]];
		IF c = '← THEN EXIT;
		ENDLOOP; -- for example.
	};

-- Set up user interface
[in: in, out: out] ← IO.CreateTTYStreams[name: "LarkServer"];

LarkUserRpcControl.ExportInterface[
	interfaceName: [ type: "LarkUser.lark", instance: "Einstein.lark"],
	user: Names.CurrentRName[],
	password: Names.CurrentPasskey[]
	];

RPCBcstListen.BcstListenInitialize[];
}.