// PupStatServ.bcpl
// Copyright Xerox Corporation 1979
// Last modified September 22, 1979  4:49 PM by Boggs

get "Pup0.decl"
get "Pup1.decl"
get "PupStats.decl"

external
[
//outgoing procedures
InitStatServ

//incoming procedures
Allocate; Enqueue; Dequeue; InitializeContext; Block
OpenLevel1Socket; ReleasePBI; ExchangePorts; CompletePup
HLookup

//incoming statics
pupRT
]

static [ statSoc ]

//-----------------------------------------------------------------
let InitStatServ(zone,ctxQ) be
//-----------------------------------------------------------------
[
statSoc = Allocate(zone,lenPupSoc)
OpenLevel1Socket(statSoc,table [ 0; 0; socketStatistics ])
Enqueue(ctxQ,InitializeContext(Allocate(zone,75),75,StatServ))
]


//-----------------------------------------------------------------
and StatServ() be
//-----------------------------------------------------------------
[
Block() repeatwhile statSoc>>PupSoc.iQ.head eq 0
let pbi = Dequeue(lv statSoc>>PupSoc.iQ)
if pbi>>PBI.pup.type ne typeSendStats then
   [ ReleasePBI(pbi); loop ]
ExchangePorts(pbi)
let res = valof
   [
   let rte = HLookup(pupRT,pbi>>PBI.pup.words↑1)
   if rte ne 0 then
      if rte>>RTE.hops eq 0 then
         resultis rte>>RTE.ndb>>NDB.level0Stats(pbi,rte>>RTE.ndb)
   resultis false
   ]
test res
   ifso CompletePup(pbi,typeStatsAck)
   ifnot CompletePup(pbi,typeStatsNak,pupOvBytes)
] repeat