// PupNetDirTest.bcpl
// Last modified September 25, 1982  10:29 AM by Taft

// Bldr PupNetDirTest PupNetDirLookup
// Pup1OpenClose Pup1b PupAl1a PupRoute PupAlEthb PupAlEtha
// Context ContextInit Interrupt AltoQueue AltoTimer
// CmdScan CmdScanEdit CmdScanAux CmdScanDisplay Keyword KeywordInit
// StringUtil AltoByteBlt Template
// Pup1Init PupDummyGate PupAlEthInit InterruptInit

get "Pup0.decl"
get "Pup1.decl"
get "PupNetDir.decl"

external
[
InitPupLevel1; EnumeratePupAddresses; GetPartner; PupAddressLookup
InitializeContext; CallContextList; Block
InitCmd; GetString; CreateKeywordTable; InsertKeyword; GetKeyword
AddToZone; Allocate; Free; Closes; Ws; Wss; PutTemplate; Enqueue; Idle
sysZone; dsp; lvUserFinishProc; lvIdle
]

static
[
dontCheckRT = false
ctxQ
savedUserFinishProc
]

let Test() be
[
let v = vec 20000
AddToZone(sysZone, v, 20000)
let v = vec 1; ctxQ = v; ctxQ!0 = 0

savedUserFinishProc = @lvUserFinishProc
@lvUserFinishProc = UserFinishProc
@lvIdle = Block

InitPupLevel1(sysZone, ctxQ, 10)

Enqueue(ctxQ, InitializeContext(Allocate(sysZone, 1000), 1000, Main))

CallContextList(ctxQ!0) repeat
]

and Main(ctx) be
[
let kt = CreateKeywordTable(10)
InsertKeyword(kt, "Enumerate")!0 = Enumerate
InsertKeyword(kt, "NameLookup")!0 = Name
InsertKeyword(kt, "AddressLookup")!0 = Address
InsertKeyword(kt, "Quit")!0 = Quit

   [ // repeat
   let cs = InitCmd(100, 10)
   if cs ne 0 then
      [
      Wss(cs, "*n> ")
      let proc = GetKeyword(cs, kt)!0
      Wss(cs, " ")
      proc(cs)
      Closes(cs)
      ]
   ] repeat
]

and Quit(cs) be finish

and Enumerate(cs) be
[
let name = GetString(cs)
Wss(cs, " ")
let ec = EnumeratePupAddresses(name, Filter, nil, dontCheckRT)
if ec ne 0 then DisplayError(ec)
Free(sysZone, name)
]

and Filter(port, arg) = valof
[
PutTemplate(dsp, " [$O#$O#$EUO]", port>>Port.net, port>>Port.host,
 lv port>>Port.socket)
resultis false
]

and Name(cs) be
[
let name = GetString(cs)
Wss(cs, " ")
let port = vec lenPort
if GetPartner(name, dsp, port) then
   PutTemplate(dsp, " [$O#$O#$EUO]", port>>Port.net, port>>Port.host,
    lv port>>Port.socket)
Free(sysZone, name)
]

and Address(cs) be
[
let name = GetString(cs)
Wss(cs, " ")
let port = vec lenPort
let ec = EnumeratePupAddresses(name, 0, port, true)
DisplayError(ec)
if ec eq 0 then
   [
   let string = PupAddressLookup(port, lv ec)
   test string ne 0
      ifso [ PutTemplate(dsp, " = $S", string); Free(sysZone, string) ]
      ifnot Ws(selecton ec into
         [
         case ecAddressNotFound: " -- address not found."
         case ecNoServerResponded: " -- no server responded."
         case ecCantGetThere: " -- can't get there from here."
         default: " -- error ???"
         ])
   ]
Free(sysZone, name)
]

and DisplayError(ec) be
[
if ec ne 0 then Ws(selecton ec into
   [
   case ecNameNotFound: " -- name not found."
   case ecNoServerResponded: " -- no server responded."
   case ecCantGetThere: " -- can't get there from here."
   default: " -- error ???"
   ])
]

and UserFinishProc() be
[
@lvIdle = Idle
@lvUserFinishProc = savedUserFinishProc
]