Title VMESS -- Get A PHONEy Directory
.DecSave

;1..4 JSYS Acs

x==12				;Loop var
n==13				;Counter
;p==17

MaxNod==512.
HstAdr=100000
HstLen==6*MaxNod
StrLen==100

S:	Jfcl			;Tradition
	RESET			;Stop the world!!

	Setzm ZerBeg		;Clear static data
	Move 1,[ZerBeg,,ZerBeg+1]
	Blt 1,ZerEnd

	Movei 1,HstLen		;Get length
	Movem 1,HstAdr		;Store in block
	Movei 1,.NDGNT		;Get node table function
	Movei 2,HstAdr		;Block addr
	NODE			;Load up table
	 ErJmp Awful
	Hlrz 1,HstAdr		;Get number returned
	Movem 1,Number

Main:	Setzm Waiting		;Top of main loop -- clear waiting count
	Movn x,Number		;Get number of known nodes
	Movsi x,(x)		;Make into Aobjn

Top:	Skipge 1,Nodjfn(x)	;Per node loop -- Error or done?
	 Jrst Bot		; yes
	Jumpn 1,GotJfn		;Have JFN?

	Hrroi 1,Strbuf		;Build connect name
	Hrroi 2,[Asciz 'DCN:']	;Decnet active device
	Setz 3,
	SOUT
	Move 2,HstAdr+.NDBK1(x)	;Get addr of node block
	Hrro 2,.NDNAM(2)	;Get addr of node name
	SOUT
	Hrroi 2,[Asciz '-29.']	;PHONE object
	SOUT
	Idpb 3,1		;Tie off

	Movsi 1,(GJ%SHT)	;Get network JFN
	Hrroi 2,Strbuf
	GTJFN
	 ErJmp IncWai		;Failed!! Bump waiting count
	Movem 1,NodJfn(x)	;Save JFN in table

GotJfn:	Skipe Until(x)		;Open?
	 Jrst ChkJfn		; yes
 	Move 2,[100000,,OF%RD+OF%WR] ;8 bit bytes
	OPENF			;Open
	 Trna			; Failed!
	  Jrst IsOpen		;  Success!!
	Caie 1,DCNX13		;Not accessible?
	 Jrst Incwai		; No, say we are waiting
	RLJFN			;Yes. Fail here and now
	Setom Nodjfn(x)
	Jrst Bot

IsOpen:	TIME			;Here with newly opened JFN
	Addi 1,15.*1000.	;Set up timeout
	Movem 1,Until(x)
	Move 1,NodJfn(x)

ChkJfn:	Movei 2,.MORLS		;Read link status
	MTOPR
	 ErJmp Bot		; not now
	Tlne 3,(MO%CON)		;Connected?
	 Jrst Gotcon		; YES!!
	Tlne 3,(MO%WFC)		;No, waiting for a connect?
	 Jrst DoWait		; Yes, check timeout tho
	Tlnn 3,(MO%ABT)		;No, connect been rejected?
	 Jrst DoWait		; No, cehck timeout
	Move 1,NodJfn(x)	;Yes.
	CLOSF			;Ask me if I care, just ask me...
	 Trn			; ...funny you should ask
	Setom NodJfn(x)		;Mark as done
	Jrst Bot

GotCon:	Setz n,			;Clear user count
	Movei 1,"[		;]
	PBOUT
	Move 1,HstAdr+.NDBK1(x)	;Get addr of node block
	Hrro 1,.NDNAM(1)	;Get addr of node name
	PSOUT			;[
	Movei 1,"]
	PBOUT
	Hrroi 1,CRLF
	PSOUT

GotLop:	Move 1,NodJfn(x)	;Send "DIRECTORY" Message
	Hrroi 2,Mess
	Movei 3,6.
	SOUTR
	 Erjmp Failed

	Hrroi 2,Strbuf		;Read string returned
	Movni 3,StrLen*5-1
	SINR
	 ErJmp Failed
	Addi 3,StrLen*5-1
	Jumpe 3,NoMore		;Zero length record means the end
 
	Movei 1,.PRIOU		;Output to user
	Hrroi 2,Strbuf
	SOUT

	Hrroi 1,CRLF
	PSOUT
	Aoja n,GotLop

Failed: Hrroi 1,[Asciz 'Link failed.']
	PSOUT
	Hrroi 1,CrLf
	PSOUT
Nomore:	Jumpn n,Some
	Hrroi 1,[Asciz 'No one.']
	PSOUT
	Hrroi 1,CrLf
	PSOUT
Some:	Hrroi 1,CrLf
	PSOUT

	Move 1,NodJfn(x)
	CLOSF
	 Trn
	Setom NodJfn(x)
	Jrst Bot

DoWait:	TIME
	Camg 1,Until(x)
	 Jrst IncWai
	Move 1,NodJfn(x)	;yes.
	CLOSF
	 Trn
	Setom NodJfn(x)
	Jrst Bot

IncWai:	Aos Waiting
Bot:	Aobjn x,Top

	Movei 1,500.
	DISMS
	Skipe Waiting
	 Jrst Main

Die:	HALTF
	Jrst .-1
 
Awful:	Hrroi 1,StrBuf
	Hrloi 2,.FHSLF
	Movsi 3,-StrLen*5-1
	ERSTR
	 Trna
	  Trn
	Hrroi 1,StrBuf
	ESOUT
	Jrst Die

CrLf:	.BYTE 7 ? ^M ? ^J ? .BYTE

Mess:	.BYTE 7			;7 bit byte mode
	15. ? "X ? ": ? ": ? "Y ? 0
	.BYTE
 
ZerBeg::
Waiting: Block 1
Number:	Block 1
Until:	Block Maxnod
NodJfn:	Block Maxnod
Strbuf:	block StrLen
Zerend==.-1

	End S
