-- File: PressNetSendFile.mesa: Sends a Press.bits file to a printing server.
-- Last Edited: December 10, 1981 6:52 PM By: GWilliams
--fixed stream addressing bug.
DIRECTORY
IODefs USING[CR, ReadChar, ReadLine, WriteChar, WriteLine, WriteString],
PressDefs USING [PressPassword],
PressNetDefs USING [PageAttributes,
--PressNetErr,
Printer,
PrinterAttributes,
CloseConnection,
GetPrinterAttributes,
PrinterReady,
SendPageAttributes,
SetRetries
,
SendLine
],
SegmentDefs USING[DataSegmentHandle, DefaultBase, DeleteDataSegment, NewDataSegment, Read, SegmentAddress],
StreamDefs USING[NewWordStream, DiskHandle, ReadBlock, SetPosition, StreamPosition],
StringDefs USING [UpperCase];
PressNetSendFile: PROGRAM
IMPORTS IODefs, PressNetDefs, SegmentDefs, StreamDefs, StringDefs
=
BEGIN OPEN IODefs, PressDefs, PressNetDefs, SegmentDefs, StreamDefs, StringDefs;

IllegalPressFile: ERROR = CODE;
NoImageInFile: ERROR = CODE;
FileTooShort: ERROR = CODE;

--Pack Variables
--Procs
SendImage: PROC[]=
--Raises no signals or errors
BEGIN
attr: PageAttributes;
bandWidth: CARDINAL = 16;
bitPage: LONG CARDINAL;
ch: CHARACTER;
pa: PrinterAttributes;
bufferWordLen: CARDINAL = 1024;
bufferByteLen: CARDINAL = 2*bufferWordLen;
h: DiskHandle;
bufferSeg: DataSegmentHandle;
wordsRead: CARDINAL;
filename: STRING ← [200];
inCoreAddress: POINTER TO ARRAY [0..bufferWordLen) OF CARDINAL;
streamPosition: StreamPosition;
scratch: CARDINAL;
firstBand, lastBand, bitMargin, bitWordCt, frstScan, lstScan: CARDINAL;
scanNum: CARDINAL;
totalBands: LONG CARDINAL;
bandNum: LONG CARDINAL;
roundedBytesPerBand: LONG CARDINAL;

pa ← GetPrinterAttributes[Stinger];
--
first see that there is a file on the disk

WriteString["Filename to read: "];
ReadLine[filename];

[h] ← NewWordStream[filename, Read];--open file for testing.
bufferSeg ← NewDataSegment[DefaultBase, (bufferWordLen+255)/256];
inCoreAddress ← SegmentAddress[bufferSeg];

streamPosition ← 3*2;--address by bytes, not words
SetPosition[h, streamPosition];

scratch ← h.get[h];
IF scratch # PressPassword THEN ERROR IllegalPressFile;
bandNum ← firstBand ← h.get[h];
lastBand ← h.get[h];
bitMargin ← h.get[h];
bitWordCt ← h.get[h];
IF ~(lastBand > firstBand) THEN ERROR NoImageInFile;
IF ~(bitWordCt>0) THEN ERROR NoImageInFile;

FOR i: CARDINAL IN [1..3] DO scratch ← h.get[h]; ENDLOOP; --get bitPage

bitPage ← scratch;
totalBands ← lastBand-firstBand+1;--calculate figures for scan line transfers
roundedBytesPerBand ← ((bitWordCt * bandWidth + 1023)/1024) * 1024 * 2;--i.e., # of bytes in band, rounded up to 1K bound

--
set up this record before calling the printer (this sequence useful for debugging)
frstScan ← firstBand*bandWidth; lstScan ← (lastBand*bandWidth) + bandWidth-1;
attr ← [firstScan: frstScan, lastScan: lstScan, margin: bitMargin, bitWc: bitWordCt];

--OK, we think the file is fine, try to get the printer.

ch ← ’Y;
WHILE ch=’Y DO
IF PrinterReady[Stinger, 2] THEN EXIT;
ch ← ’r;
WHILE ~(ch=’Y OR ch=’N) DO
WriteString["Stinger not available, retry? (Y/N): "L];
ch ← UpperCase[ReadChar[]]; WriteChar[ch]; WriteChar[CR];
ENDLOOP;
ENDLOOP;

IF ch#’Y THEN GOTO exit;

WriteLine["Sending file"];
SendPageAttributes[@attr];--send the attributes of this page
--transfer the file.
SetRetries[60000];--for debugging on remote end

FOR bandNum IN [0..totalBands)
DO
streamPosition ← bandNum*roundedBytesPerBand+(2048 * bitPage);--i.e., in TfsPages, bandNum*tfsPagesPerBand + 1
SetPosition[h, streamPosition];--now positioned to first word of band.

FOR scanNum IN [0..bandWidth)
DO
IF h.endof[h] THEN ERROR FileTooShort;
wordsRead ← ReadBlock[h, inCoreAddress, bitWordCt];

SendLine[inCoreAddress];
ENDLOOP;
ENDLOOP;
DeleteDataSegment[bufferSeg];
h.destroy[h];

CloseConnection[];
WriteLine["All ok"L];

EXITS
exit=>NULL;
END;
--of SendImage
--def these locally for debugging and to avoid the net

SendImage[];
--debugging.

END.
-- PressNetSendFile.mesa
-- Last Edited: November 20, 1981 7:17 PM By: GWilliams