EchoTool.mesa
Hal Murray December 30, 1985 5:47:23 pm PST
Hal Murray, January 13, 1986 7:10:54 pm PST
DIRECTORY
Atom USING [GetPName],
BasicTime USING [GetClockPulses, GMT, Now, Period, Pulses, PulsesToMicroseconds],
Buttons USING [Button, ButtonProc, Create, Destroy, SetDisplayStyle],
Commander USING [CommandProc, Register],
Containers USING [ChildXBound, ChildYBound, Create],
Convert USING [Error, IntFromRope, RopeFromInt],
IO USING [Flush, PutChar, PutF, PutRope, STREAM, Value],
Labels USING [Create],
Loader USING [BCDBuildTime],
PupDefs USING [GetFreePupBuffer, GetHostName, GetPupAddress, GetPupContentsBytes, MsToTocks, PupAddress, PupBuffer, PupAddressToRope, PupNameTrouble, PupRouterSendThis, PupSocket, PupSocketDestroy, PupSocketKick, PupSocketMake, ReturnFreePupBuffer, ReturnPup, SetPupContentsBytes, SwapPupSourceAndDest, veryLongWait],
PupTypes USING [echoSoc, fillInPupAddress, fillInSocketID, maxDataBytesPerGatewayPup],
Rope USING [ROPE],
Rules USING [Create],
TypeScript USING [ChangeLooks, Create],
ViewerClasses USING [Viewer],
ViewerEvents USING [EventProc, RegisterEventProc],
ViewerIO USING [CreateViewerStreams],
ViewerOps USING [AddProp, ComputeColumn, CreateViewer, FetchProp, MoveViewer, OpenIcon, SetOpenHeight],
ViewerTools USING [GetContents, MakeNewTextViewer, SetContents, SetSelection],
DriverDefs USING [GetDeviceChain, Network],
EthernetDriverStats USING [EtherStats, MaxTries],
PupEchoServerDefs USING [echoStatsReply, echoStatsRequest, EchoStatsEntry, echoVersion],
PupRouterDefs USING [EnumerateRoutingTable, RoutingTableEntry],
PupWireFormat USING [MesaToBcplLongNumber];
EchoTool: CEDAR MONITOR
IMPORTS
Atom, BasicTime, Buttons, Commander, Containers, Convert, IO, Labels, Loader, PupDefs, Rules, TypeScript, ViewerEvents, ViewerIO, ViewerOps, ViewerTools,
DriverDefs, PupRouterDefs, PupWireFormat =
BEGIN
ROPE: TYPE = Rope.ROPE;
STREAM: TYPE = IO.STREAM;
Viewer: TYPE = ViewerClasses.Viewer;
Viewer layout parameters
buttonHeight: INTEGER ← 0;
buttonWidth: INTEGER ← 0;
Server data (global)
Note: This server goes in on top of the builtin one
pupsEchoed: INT ← 0;
serverLog: STREAMNIL;
pleaseStopServer: BOOLEANFALSE;
server: PROCESSNIL;
showDollar: REF BOOL ← NEW [BOOL ← FALSE];
serverSoc: PupDefs.PupSocket ← NIL;
ClientData: TYPE = REF ClientDataRep;
ClientDataRep: TYPE = RECORD [
log: STREAMNIL,
in: STREAMNIL,
pleaseStop: BOOLEANFALSE,
user: PROCESSNIL,
where: PupDefs.PupAddress ← PupTypes.fillInPupAddress,
echo: REF BOOLNEW [BOOL ← TRUE],
miss: REF BOOLNEW [BOOL ← TRUE],
late: REF BOOLNEW [BOOL ← TRUE],
dataChecking: REF BOOLNEW [BOOL ← TRUE],
fixedLength: REF BOOLNEW [BOOL ← FALSE],
target, length, delay, pause: Viewer ← NIL,
good: INT ← 0,
minDelay, maxDelay: INT ← 0,
delayHist: ARRAY DelayRange OF INTALL[0] ];
DelayRange: TYPE = {
d1, d1a, d2, d2a, d3, d3a, d4, d4a, d5, d5a, d6, d6a, d7, d7a, d8, d8a, d9, d9a,
d10, d14, d20, d28, d50, d70, d100, d140, d200, d280, d500, d700,
d1000, d1400, d2000, d2800, d5000, d7000, d10000, d14000, d20000, d28000, more};
delayTime: ARRAY DelayRange OF INT = [
d1: 1000, d1a: 1500, d2: 2000, d2a: 2500,
d3: 3000, d3a: 3500, d4: 4000, d4a: 4500,
d5: 5000, d5a: 5500, d6: 6000, d6a: 6500,
d7: 7000, d7a: 7500, d8: 8000, d8a: 8500,
d9: 9000, d9a: 9500,
d10: 10000, d14: 14000, d20: 20000, d28: 28000, d50: 50000, d70: 70000,
d100: 100000, d140: 140000, d200: 200000, d280: 280000, d500: 500000, d700: 700000,
d1000: 1000000, d1400: 1400000, d2000: 2000000, d2800: 2800000, d5000: 5000000, d7000: 7000000,
d10000: 10000000, d14000: 14000000, d20000: 20000000, d28000: 28000000, more: LAST[INT]];
global: ClientData ← NIL; -- debugging
Create: Commander.CommandProc = TRUSTED {
viewer, buttons, log: Viewer ← NIL;
data: ClientData ← NEW[ClientDataRep ← []];
global ← data;
viewer ← ViewerOps.CreateViewer [
flavor: $Container,
info: [name: "EchoTool", column: right, iconic: TRUE, scrollable: FALSE]];
[] ← ViewerEvents.RegisterEventProc[Poof, destroy, viewer, TRUE];
ViewerOps.AddProp[viewer, $PupEcho, data];
{ -- Kludge to find Button size
temp: Buttons.Button = Buttons.Create[
info: [name: "Length:", parent: viewer, border: FALSE,
wx: 0, wy: 0],
proc: NIL, clientData: NIL, fork: FALSE, paint: FALSE];
buttonWidth ← temp.ww;
buttonHeight ← temp.wh;
Buttons.Destroy[temp]; };
log ← TypeScript.Create[
[name: "EchoTool.log", wy: 27+4, parent: viewer, border: FALSE], FALSE];
[data.in, data.log] ← ViewerIO.CreateViewerStreams [
name: "EchoTool.log", backingFile: "EchoTool.log", viewer: log, editedStream: FALSE];
IF serverLog = NIL THEN { serverLog ← data.log; StartServer[]; };
Containers.ChildXBound[viewer, log];
Containers.ChildYBound[viewer, log];
CreateButtons[data, viewer, log];
TypeScript.ChangeLooks[log, 'f];
IO.PutF[data.log, "EchoTool of %G.\n", [time[Loader.BCDBuildTime[Create]]]];
ViewerOps.OpenIcon[viewer]; };
CreateButtons: ENTRY PROC[data: ClientData, parent, log: Viewer] = {
child: Viewer ← NIL;
kids: Viewer = Containers.Create[
info: [parent: parent, border: FALSE, scrollable: FALSE, wx: 0, wy: -9999, ww: 9999, wh: 0] ];
Containers.ChildXBound[parent, kids];
child ← MakeBool[name: "!", init: data.echo, clientData: data, parent: kids, x: 2, y: 1];
child ← MakeBool[name: "?", init: data.miss, clientData: data, parent: kids, x: child.wx + child.ww + 10, y: child.wy];
child ← MakeBool[name: "#", init: data.late, clientData: data, parent: kids, x: child.wx + child.ww + 10, y: child.wy];
child ← MakeBool[name: "$", init: showDollar, clientData: data, parent: kids, x: child.wx + child.ww + 10, y: child.wy];
child ← MakeBool[name: "D-ck", init: data.dataChecking, clientData: data, parent: kids, x: child.wx + child.ww + 10, y: child.wy];
child ← MakeBool[name: "FixedLength", init: data.fixedLength, clientData: data, parent: kids, x: child.wx + child.ww + 10, y: child.wy];
child ← data.length ← MakeLabeledText[
parent: kids,
sibling: child,
name: "Length:",
data: Convert.RopeFromInt[PupTypes.maxDataBytesPerGatewayPup],
prev: data.length,
newline: FALSE ];
child ← MakeRule[kids, child];
child ← data.target ← MakeLabeledText[
parent: kids,
sibling: child,
name: "Target:",
data: "Target",
prev: data.target ];
child ← MakeRule[kids, child];
child ← MakeLabel[kids, child, "What:"];
child ← MakeButton[kids, child, data, "Poke", PokeProc];
child ← MakeButton[kids, child, data, "Start", StartProc];
child ← MakeButton[kids, child, data, "Hist", HistProc];
child ← MakeButton[kids, child, data, "Stop", StopProc];
child ← MakeButton[kids, child, data, "Route", RouteProc];
child ← MakeButton[kids, child, data, "Stats", StatsProc];
child ← MakeRule[kids, child];
{
kidsY: INTEGER = 2;
kidsH: INTEGER = child.wy + child.wh + 2;
ViewerOps.MoveViewer[viewer: log, x: 0, y: kidsY + kidsH, w: log.ww, h: parent.ch - (kids.wy + kidsH), paint: FALSE];
ViewerOps.SetOpenHeight[parent, kidsY + kidsH + 12 * buttonHeight];
IF ~parent.iconic THEN ViewerOps.ComputeColumn[parent.column];
ViewerOps.MoveViewer[viewer: kids, x: kids.wx, y: kidsY, w: kids.ww, h: kidsH]; };
};
Poof: ViewerEvents.EventProc = {
[viewer: ViewerClasses.Viewer, event: ViewerEvent, before: BOOL]
RETURNS[abort: BOOLFALSE]
data: ClientData ← NARROW[ViewerOps.FetchProp[viewer, $PupEcho]];
IF event # destroy OR before # TRUE THEN ERROR;
Stop[data];
IF data.log = serverLog THEN StopServer[]; };
StartServer: ENTRY PROC = {
IF server # NIL THEN RETURN;
pleaseStopServer ← FALSE;
server ← FORK Server[]; };
StopServer: ENTRY PROC = TRUSTED {
IF server = NIL THEN RETURN;
pleaseStopServer ← TRUE;
IF server # NIL THEN {PupDefs.PupSocketKick[serverSoc]; JOIN server; };
server ← NIL; };
Server: PROC = TRUSTED {
serverSoc ← PupDefs.PupSocketMake[
PupTypes.echoSoc, PupTypes.fillInPupAddress, PupDefs.veryLongWait];
UNTIL pleaseStopServer DO
b: PupDefs.PupBuffer ← serverSoc.get[];
IF b # NIL THEN {
SELECT b.pupType FROM
echoMe => {
b.pupType ← iAmEcho;
PupDefs.SwapPupSourceAndDest[b];
PupDefs.PupRouterSendThis[b];
pupsEchoed ← pupsEchoed.SUCC;
IF showDollar^ THEN IO.PutChar[serverLog, '$]; };
PupEchoServerDefs.echoStatsRequest => {
EchoStatsEntry: TYPE = PupEchoServerDefs.EchoStatsEntry;
ese: LONG POINTER TO EchoStatsEntry ← LOOPHOLE[@b.pupWords];
ese^ ← [
version: PupEchoServerDefs.echoVersion,
pupsEchoed: PupWireFormat.MesaToBcplLongNumber[pupsEchoed]];
PupDefs.ReturnPup[
b, PupEchoServerDefs.echoStatsReply, 2*SIZE[EchoStatsEntry]]; };
ENDCASE => PupDefs.ReturnFreePupBuffer[b]; };
ENDLOOP;
PupDefs.PupSocketDestroy[serverSoc];
serverSoc ← NIL; };
StartProc: Buttons.ButtonProc = {
parent: REF, clientData: REF, mouseButton: MouseButton, shift, control: BOOL
data: ClientData ← NARROW[clientData];
overlap: NAT;
SELECT TRUE FROM
shift AND control => overlap ← 5;
control => overlap ← 2;
shift => overlap ← 1;
ENDCASE => overlap ← 0;
Start[data, overlap]; };
Start: PROC [data: ClientData, overlap: NAT] = {
target: ROPE = ViewerTools.GetContents[data.target];
IF data.user # NIL THEN Stop[data];
Report[data, "\nEchoing to ", target];
IF ~FindPath[data, target] THEN RETURN;
IF overlap # 0 THEN IO.PutF[data.log, "%g packets will be kept in flight.\n", [integer[overlap+1]]];
data.pleaseStop ← FALSE;
data.user ← FORK User[data, overlap]; };
HistProc: Buttons.ButtonProc = {
parent: REF, clientData: REF, mouseButton: MouseButton, shift, control: BOOL
data: ClientData ← NARROW[clientData];
Stop[data];
PrintDelayHist[data]; };
StopProc: Buttons.ButtonProc = {
parent: REF, clientData: REF, mouseButton: MouseButton, shift, control: BOOL
data: ClientData ← NARROW[clientData];
Stop[data]; };
Stop: PROC [data: ClientData] = TRUSTED {
data.pleaseStop ← TRUE;
IF data.user # NIL THEN JOIN data.user;
data.user ← NIL; };
RouteProc: Buttons.ButtonProc = {
parent: REF, clientData: REF, mouseButton: MouseButton, shift, control: BOOL
data: ClientData ← NARROW[clientData];
Route[data]; };
Route: PROC [data: ClientData] = TRUSTED {
PrintOne: PROC [rte: PupRouterDefs.RoutingTableEntry] = TRUSTED {
network: DriverDefs.Network ← rte.network;
IF network = NIL THEN RETURN;
nets ← nets + 1;
IF k = 0 THEN IO.PutF[data.log, "|"];
IO.PutF[data.log, "%3B%4B#%3B#%4D |",
[integer[rte.net]],
[integer[network.netNumber.b]],
[integer[IF rte.hop # 0 THEN rte.route ELSE network.hostNumber]],
[integer[rte.hop]] ];
IF (k ← k + 1) = 3 THEN { IO.PutF[data.log, "\n"]; k ← 0; }; };
k, nets: INT ← 0;
IO.PutF[data.log, "\n Local Pup Routing Table.\n"];
IO.PutF[data.log, "| Net Via Hops | Net Via Hops | Net Via Hops |\n"];
IO.PutF[data.log, "|-----------------|-----------------|-----------------|\n"];
PupRouterDefs.EnumerateRoutingTable[PrintOne];
IF k # 0 THEN IO.PutF[data.log, "\n"];
IF nets > 1 THEN IO.PutF[data.log, "There are %D active networks.\n", [integer[nets]]];
};
StatsProc: Buttons.ButtonProc = {
parent: REF, clientData: REF, mouseButton: MouseButton, shift, control: BOOL
data: ClientData ← NARROW[clientData];
Stats[data]; };
Stats: PROC [data: ClientData] = TRUSTED {
first: DriverDefs.Network ← DriverDefs.GetDeviceChain[];
FOR network: DriverDefs.Network ← first, network.next UNTIL network = NIL DO
SELECT network.device FROM
ethernet, ethernetOne => {
stats: EthernetDriverStats.EtherStats ← LOOPHOLE[network.stats];
IO.PutF[data.log, "Ethernet"];
IF network.device = ethernetOne THEN IO.PutF[data.log, "One"];
IO.PutF[data.log, " Statistics from %B#%B#.\n",
[integer[network.netNumber.b]], [integer[network.hostNumber]] ];
IO.PutF[data.log, " Rcv: pkts %G, words %G, bad %G, missed %G\n",
[integer[stats.packetsRecv]],
[integer[stats.wordsRecv]],
[integer[stats.badRecvStatus]],
[integer[stats.inputOff]]];
IO.PutF[data.log, " Xmit: pkts %G, words %G, bad %G\n",
[integer[stats.packetsSent]],
[integer[stats.wordsSent]],
[integer[stats.badSendStatus]]];
IO.PutF[data.log, " Lds:"];
FOR i: NAT IN [0..EthernetDriverStats.MaxTries] DO
IO.PutF[data.log, " %G", [integer[stats.loadTable[i]]]];
ENDLOOP;
IO.PutF[data.log, "\n"]; };
ENDCASE => NULL;
ENDLOOP;
};
User: PROC [data: ClientData, overlap: NAT] = TRUSTED {
ENABLE UNWIND => NULL;
start, finish: BasicTime.GMT;
sent, mashed, missed, late, error, funny: INT ← 0;
bytes: INT ← 0;
sendPacketNumber, recvPacketNumber: INTEGER ← 0;
dataChecking: BOOL = data.dataChecking^;
fixedLength: BOOL = data.fixedLength^;
lengthText: ROPE = ViewerTools.GetContents[data.length];
lengthCard: CARDINAL ← 0;
mySoc: PupDefs.PupSocket ← PupDefs.PupSocketMake[
PupTypes.fillInSocketID, data.where, PupDefs.MsToTocks[3000]];
sizeOfStartArray: NAT = 25;
packetStart: ARRAY [0..sizeOfStartArray) OF BasicTime.Pulses;
cond: CONDITION;
grabber: PROCESS;
longest: CARDINAL = PupTypes.maxDataBytesPerGatewayPup;
Kick: ENTRY PROC = TRUSTED {NOTIFY cond; };
Wait: ENTRY PROC = TRUSTED {WAIT cond; };
Grabber: PROC = TRUSTED {
DO
b: PupDefs.PupBuffer ← mySoc.get[];
start, packetStop: BasicTime.Pulses;
recvStartIndex: NAT;
length, pupLength: CARDINAL;
arrivedPacketNumber: INTEGER;
IF b = NIL THEN {
IF data.pleaseStop THEN EXIT; -- Wait for last packet to keep stats clean
missed ← missed + 1;
IF data.miss^ THEN IO.PutRope[data.log, "?"];
recvPacketNumberrecvPacketNumber + 1;
Kick[];
LOOP; };
IF b.pupType = error THEN {
error ← error + 1;
PrintErrorPup[data, b];
PupDefs.ReturnFreePupBuffer[b];
Kick[];
LOOP;};
IF b.pupType # iAmEcho THEN {
funny ← funny + 1;
IO.PutRope[data.log, "%"];
PupDefs.ReturnFreePupBuffer[b];
Kick[];
LOOP;};
arrivedPacketNumber ← LOOPHOLE[b.pupID.a];
IF (recvPacketNumber-arrivedPacketNumber) > 0 THEN {
late ← late + 1;
IF data.late^ THEN IO.PutRope[data.log, "#"];
PupDefs.ReturnFreePupBuffer[b];
Kick[];
LOOP; };
UNTIL arrivedPacketNumber = recvPacketNumber DO
missed ← missed + 1;
IF data.miss^ THEN IO.PutRope[data.log, "?"];
recvPacketNumberrecvPacketNumber + 1;
ENDLOOP;
length ← IF fixedLength THEN lengthCard ELSE recvPacketNumber MOD longest;
pupLength ← PupDefs.GetPupContentsBytes[b];
IF pupLength # length THEN {
funny ← funny + 1;
IO.PutRope[data.log, "%"];
PupDefs.ReturnFreePupBuffer[b];
Kick[];
LOOP; };
packetStop ← BasicTime.GetClockPulses[];
recvStartIndex ← LOOPHOLE[recvPacketNumber, CARDINAL] MOD sizeOfStartArray;
start ← packetStart[recvStartIndex];
AddToDelayHist[data, BasicTime.PulsesToMicroseconds[packetStop-start]];
FOR k: CARDINAL IN [0..length) WHILE dataChecking DO
IF b.pupBytes[k] # k MOD 100H THEN {
IO.PutRope[data.log, "~"];
mashed ← mashed + 1;
EXIT; };
REPEAT FINISHED => {
data.good ← data.good + 1;
bytes ← bytes + length;
recvPacketNumberrecvPacketNumber + 1;
IF data.echo^ THEN IO.PutRope[data.log, "!"];
IF recvPacketNumber MOD 256 = 0 THEN {
IF ~data.echo^ THEN IO.PutRope[data.log, "."] ELSE IO.PutRope[data.log, "\n"]; }; };
ENDLOOP;
PupDefs.ReturnFreePupBuffer[b];
Kick[];
IF data.pleaseStop AND recvPacketNumber = sendPacketNumber THEN EXIT;
ENDLOOP;
Kick[];
};
IF fixedLength THEN {
temp: INT;
temp ← Convert.IntFromRope[lengthText ! Convert.Error => {
IO.PutF[data.log, "Can't parse Length field.\n"];
lengthCard ← 0;
CONTINUE; } ];
temp ← MAX[temp, 0];
temp ← MIN[temp, longest];
lengthCard ← temp;
IO.PutF[data.log, "Packet length is %G bytes.\n", [integer[lengthCard]]]; };
InitDelayHist[data];
data.good ← 0;
grabber ← FORK Grabber[];
start ← BasicTime.Now[];
UNTIL data.pleaseStop DO
b: PupDefs.PupBuffer ← PupDefs.GetFreePupBuffer[];
length: CARDINALIF fixedLength THEN lengthCard ELSE sendPacketNumber MOD longest;
sendStartIndex: NAT;
b.pupID.a ← b.pupID.b ← LOOPHOLE[sendPacketNumber];
b.pupType ← echoMe;
FOR k: CARDINAL IN [0..length) DO b.pupBytes[k] ← k MOD 100H; ENDLOOP;
PupDefs.SetPupContentsBytes[b, length];
sendStartIndex ← LOOPHOLE[sendPacketNumber, CARDINAL] MOD sizeOfStartArray;
packetStart[sendStartIndex] ← BasicTime.GetClockPulses[];
mySoc.put[b];
sent ← sent + 1;
sendPacketNumber ← sendPacketNumber + 1;
WHILE (sendPacketNumber-recvPacketNumber-overlap) > 0 DO
IF data.pleaseStop THEN EXIT;
Wait[];
ENDLOOP;
ENDLOOP;
IO.PutRope[data.log, "\n"];
finish ← BasicTime.Now[];
JOIN grabber;
PupDefs.PupSocketDestroy[mySoc];
BEGIN
packetsPerSecond, msPerPacket: REAL;
seconds: LONG CARDINAL ← BasicTime.Period[from: start, to: finish];
IF seconds = 0 THEN seconds ← 1;
packetsPerSecond ← REAL[sent]/REAL[seconds];
IF data.good = 0 THEN msPerPacket ← 0
ELSE msPerPacket ← 1000.0*seconds/data.good;
IO.PutF[data.log, "%G packets sent in %G seconds =>\n\t", [integer[sent]], [cardinal[seconds]]];
IO.PutF[data.log, "%1.2F packets/second.\n", [real[packetsPerSecond]] ];
IO.PutF[data.log, "%G bytes received in %G seconds =>\n\t", [integer[bytes]], [cardinal[seconds]]];
IO.PutF[data.log, "%1.0F bits/second => ", [real[8.0*bytes/seconds]] ];
IO.PutF[data.log, "%G ms/packet.\n", [real[msPerPacket]] ];
PrintPercent[data, data.good, sent, "Packets echoed OK"];
PrintPercent[data, mashed, sent, "Packets with bad data"];
PrintPercent[data, missed, sent, "Packets missed"];
PrintPercent[data, late, sent, "Late Packets"];
PrintPercent[data, error, sent, "Error Packets"];
PrintPercent[data, funny, sent, "Funny Packets"];
IO.Flush[data.log];
END; };
InitDelayHist: PROC [data: ClientData] = {
data.minDelay ← INT.LAST;
data.maxDelay ← INT.FIRST;
data.delayHist ← ALL[0]; };
AddToDelayHist: PROC [data: ClientData, micro: INT] = {
FOR d: DelayRange IN DelayRange DO -- Slow but clean
IF delayTime[d] < micro THEN LOOP;
data.delayHist[d] ← data.delayHist[d] + 1;
EXIT;
REPEAT FINISHED => ERROR;
ENDLOOP;
data.minDelay ← MIN[data.minDelay, micro];
data.maxDelay ← MAX[data.maxDelay, micro]; };
PrintDelayHist: PROC [data: ClientData] = {
total: INT ← 0;
IF data.good = 0 THEN RETURN;
IO.PutF[data.log, " Incremental Cumulative Microseconds\n"];
FOR d: DelayRange IN DelayRange DO
IF data.delayHist[d] = 0 THEN LOOP;
total ← total + data.delayHist[d];
IO.PutF[data.log, "%8G", [integer[data.delayHist[d]]]];
IO.PutF[data.log, "%7.2F%%", [real[100.0*data.delayHist[d]/data.good]]];
IO.PutF[data.log, "%8G", [integer[total]]];
IO.PutF[data.log, "%7.2F%%", [real[100.0*total/data.good]]];
IO.PutF[data.log, "%9G\n", [integer[delayTime[d]]]];
ENDLOOP;
IO.PutF[data.log, "The min delay was %G microseconds.\n", [integer[data.minDelay]]];
IO.PutF[data.log, "The max delay was %G microseconds.\n", [integer[data.maxDelay]]]; };
PokeProc: Buttons.ButtonProc = {
parent: REF, clientData: REF, mouseButton: MouseButton, shift, control: BOOL
data: ClientData ← NARROW[clientData];
target: ROPE = ViewerTools.GetContents[data.target];
Poke[data, target]; };
Poke: PROC [data: ClientData, target: ROPE] = TRUSTED {
b: PupDefs.PupBuffer;
mySoc: PupDefs.PupSocket;
Report[data, "\nPoking to ", target];
IF ~FindPath[data, target] THEN RETURN;
mySoc ← PupDefs.PupSocketMake[
PupTypes.fillInSocketID, data.where, PupDefs.MsToTocks[5000]];
b ← PupDefs.GetFreePupBuffer[];
b.pupID.a ← b.pupID.b ← 0;
b.pupType ← echoMe;
PupDefs.SetPupContentsBytes[b, 0];
mySoc.put[b];
UNTIL (b ← mySoc.get[]) = NIL DO
SELECT TRUE FROM
(b.pupType = error) => PrintErrorPup[data, b];
(b.pupType # iAmEcho) => IO.PutRope[data.log, "%"];
(b.pupID.a # 0) OR (b.pupID.b # 0) => IO.PutRope[data.log, "#"];
ENDCASE => {
IO.PutRope[data.log, "Response from: "];
IO.PutRope[data.log, PupDefs.GetHostName[b.source]];
IO.PutRope[data.log, " = "];
IO.PutRope[data.log, PupDefs.PupAddressToRope[b.source]];
IO.PutRope[data.log, "\n"]; };
PupDefs.ReturnFreePupBuffer[b];
ENDLOOP;
PupDefs.PupSocketDestroy[mySoc]; };
PrintPercent: PROC [data: ClientData, x, sent: INT, name: ROPE] = {
IF x = 0 THEN RETURN;
IO.PutF[data.log, "%8G %7.2F%% %G\n", [integer[x]], [real[100.0*x/sent]], [rope[name]] ]; };
PrintErrorPup: PROC [data: ClientData, b: PupDefs.PupBuffer] = TRUSTED {
length: CARDINAL ← PupDefs.GetPupContentsBytes[b];
IO.PutRope[data.log, "Error Pup from: "];
IO.PutRope[data.log, PupDefs.PupAddressToRope[b.source]];
IO.PutRope[data.log, ": "];
FOR i:CARDINAL IN [0..length - 2*(10 + 1 + 1)) DO
IO.PutChar[data.log, b.errorText[i]];
ENDLOOP;
IO.PutRope[data.log, "\n"]; };
FindPath: PROC [data: ClientData, target: ROPE] RETURNS [BOOLEAN] = TRUSTED {
data.where ← PupDefs.GetPupAddress[PupTypes.echoSoc, target !
PupDefs.PupNameTrouble =>
BEGIN Report[data, " Oops: ", e, ".\n"]; GOTO Trouble; END];
Report[data, " = ", PupDefs.PupAddressToRope[data.where], ".\n"];
RETURN[TRUE];
EXITS Trouble => RETURN[FALSE]; };
Report: PROC [data: ClientData, r1,r2,r3,r4: ROPENIL] = TRUSTED {
IF r1 # NIL THEN {IO.PutRope[data.log, r1]};
IF r2 # NIL THEN {IO.PutRope[data.log, r2]};
IF r3 # NIL THEN {IO.PutRope[data.log, r3]};
IF r4 # NIL THEN {IO.PutRope[data.log, r4]}; };
MakeRule: PROC [parent, sibling: Viewer] RETURNS [child: Viewer] = {
child ← Rules.Create[
info: [parent: parent, border: FALSE,
wy: IF sibling = NIL THEN 0 ELSE sibling.wy + sibling.wh + 2, wx: 0, ww: parent.ww, wh: 1],
paint: FALSE ];
Containers.ChildXBound[parent, child]; };
MakeButton: PROC [parent, sibling: Viewer, data: REF ANY, name: ROPE, proc: Buttons.ButtonProc] RETURNS[child: Viewer] = {
child ← Buttons.Create[
info: [name: name, parent: parent, border: TRUE,
wy: sibling.wy, wx: sibling.wx + buttonWidth - 1, ww: buttonWidth],
proc: proc,
clientData: data,
fork: TRUE,
paint: FALSE]; };
SelectorProc: TYPE = PROC [parent: Viewer, clientData: REF, value: ATOM];
Selector: TYPE = REF SelectorRec;
SelectorRec: TYPE = RECORD [
value: REF ATOM,
change: PROC [parent: Viewer, clientData: REF, value: ATOM],
clientData: REF,
buttons: LIST OF Buttons.Button,
values: LIST OF ATOM ];
MakeSelector: PROC
[name: ROPE, values: LIST OF ATOM, init: REF ATOMNIL, change: SelectorProc ← NIL, clientData: REFNIL, parent: Viewer, x, y: INTEGER]
RETURNS [child: Viewer] = {
selector: Selector ← NEW [SelectorRec ← [
value: IF init # NIL THEN init ELSE NEW [ATOM ← values.first],
change: change,
clientData: clientData,
buttons: NIL,
values: values ] ];
last: LIST OF Buttons.Button ← NIL;
child ← Labels.Create[info: [name: name, parent: parent, border: FALSE, wx: x, wy: y] ];
FOR a: LIST OF ATOM ← values, a.rest UNTIL a = NIL DO
child ← Buttons.Create[
info: [name: Atom.GetPName[a.first], parent: parent, border: TRUE, wx: child.wx + child.ww + 2, wy: child.wy],
proc: SelectorHelper, clientData: selector, fork: TRUE, paint: TRUE];
IF last = NIL THEN last ← selector.buttons ← CONS[first: child, rest: NIL]
ELSE { last.rest ← CONS[first: child, rest: NIL]; last ← last.rest };
IF a.first = selector.value^ THEN Buttons.SetDisplayStyle[child, $WhiteOnBlack];
ENDLOOP; };
SelectorHelper: Buttons.ButtonProc = {
parent: REF, clientData: REF, mouseButton: MouseButton, shift, control: BOOL
self: Buttons.Button = NARROW[parent];
selector: Selector = NARROW[clientData];
buttons: LIST OF Buttons.Button ← selector.buttons;
FOR a: LIST OF ATOM ← selector.values, a.rest UNTIL a = NIL DO
IF self = buttons.first THEN {
selector.value^ ← a.first;
IF selector.change # NIL THEN selector.change[self.parent, selector.clientData, a.first];
Buttons.SetDisplayStyle[buttons.first, $WhiteOnBlack]; }
ELSE Buttons.SetDisplayStyle[buttons.first, $BlackOnWhite];
buttons ← buttons.rest;
ENDLOOP; };
BoolProc: TYPE = PROC [parent: Viewer, clientData: REF, value: BOOL];
Bool: TYPE = REF BoolRec;
BoolRec: TYPE = RECORD [
value: REF BOOL,
change: BoolProc,
clientData: REF,
button: Viewer ];
MakeBool: PROC
[name: ROPE, init: REF BOOL, change: BoolProc ← NIL, clientData: REFNIL, parent: Viewer, x, y: INTEGER]
RETURNS [child: Viewer] = {
bool: Bool ← NEW [BoolRec ← [
value: IF init # NIL THEN init ELSE NEW [BOOLTRUE],
change: change,
clientData: clientData,
button: NIL ] ];
child ← Buttons.Create[
info: [name: name, parent: parent, border: TRUE, wx: x, wy: y],
proc: BoolHelper, clientData: bool, fork: TRUE, paint: TRUE];
bool.button ← child;
IF bool.value^ THEN Buttons.SetDisplayStyle[child, $WhiteOnBlack]; };
BoolHelper: Buttons.ButtonProc = {
parent: REF, clientData: REF, mouseButton: MouseButton, shift, control: BOOL
self: Buttons.Button = NARROW[parent];
bool: Bool = NARROW[clientData];
bool.value^ ← ~bool.value^;
IF bool.value^ THEN Buttons.SetDisplayStyle[bool.button, $WhiteOnBlack]
ELSE Buttons.SetDisplayStyle[bool.button, $BlackOnWhite];
IF bool.change # NIL THEN bool.change[self.parent, bool.clientData, bool.value^]; };
MakeLabel: PROC [parent, sibling: Viewer, name: ROPE] RETURNS [child: Viewer] = {
child ← Labels.Create[
info: [name: name, parent: parent, border: FALSE,
wy: sibling.wy + sibling.wh + (IF sibling.class.flavor = $Button THEN -1 ELSE 2),
wx: 2],
paint: FALSE ]; };
MakeLabeledText: PROC [
parent, sibling: Viewer, name, data: ROPE, prev: Viewer, newline: BOOLTRUE] RETURNS [child: Viewer] = {
x: INTEGER = IF newline THEN 2 ELSE sibling.wx + sibling.ww + 10;
y: INTEGER = IF newline THEN sibling.wy + sibling.wh + 1 ELSE sibling.wy;
child ← ViewerTools.MakeNewTextViewer[
info: [parent: parent, wh: buttonHeight, ww: 999, scrollable: TRUE,
data: IF prev = NIL THEN data ELSE ViewerTools.GetContents[prev],
border: FALSE,
wx: x + buttonWidth + 2, wy: y],
paint: FALSE ];
Containers.ChildXBound[parent, child];
[] ← Buttons.Create[
info: [name: name, parent: parent, wh: buttonHeight, border: FALSE, wx: x, wy: y],
proc: LabeledTextProc, clientData: child, fork: FALSE, paint: FALSE];
RETURN[child]; };
LabeledTextProc: Buttons.ButtonProc = {
parent: REF, clientData: REF, mouseButton: MouseButton, shift, control: BOOL
text: Viewer = NARROW[clientData];
SELECT mouseButton FROM
red => ViewerTools.SetSelection[text, NIL];
yellow => NULL;
blue => { ViewerTools.SetContents[text, NIL]; ViewerTools.SetSelection[text, NIL] };
ENDCASE => ERROR; };
Commander.Register["EchoTool", Create, "Echo Pups to another machine."];
END.