User guide

3.7. COROUTINE EXAMPLES 93
writef("*nCosim entered*n*n")
writef("Network nodes: %n*n", nodes)
writef("Stop time: %n*n", stoptime)
writef("Max processing time: %n*n", ptmax)
writef("Random number seed: %n*n", seed)
newline()
UNLESS initrnd(seed) DO
{ writef("Can’t initialise the random number generator*n")
RESULTIS 0
}
stopco := 0
wkqv, priq, cov := getvec(nodes), getvec(nodes+1), getvec(nodes)
UNLESS wkqv & priq & cov DO
{ writef("Can’t allocate space for the node work queues*n")
GOTO ret
}
FOR i = 1 TO nodes DO wkqv!i, cov!i := 0, 0
priqn := 0 // Number of events in the priority queue
count := 0 // Count of message processed
simtime := 0 // Simulated time
IF tracing DO writef("%i8: Starting simulation*n", simtime)
// Create and start the stop coroutine
stopco := createco(stopcofn, 200)
IF stopco DO callco(stopco)
// Create and start the message coroutines
FOR i = 1 TO nodes DO
{ LET co = createco(messcofn, 200)
IF co DO callco(co, i)
cov!i := co
}
// Run the event loop
{ LET event = getevent() // Get the earliest event
UNLESS event BREAK
simtime := event!0 // Set the simulated time
IF simtime > stoptime BREAK
callco(event!1)
} REPEAT
IF tracing DO writef("*nSimulation stopped*n*n")
writef("Messages processed: %n*n", count)
ret:
FOR i = nodes TO 1 BY -1 IF cov!i DO deleteco(cov!i)
IF cov DO freevec(cov)
IF wkqv DO freevec(wkqv)
IF priq DO freevec(priq)
IF stopco DO deleteco(stopco)
closernd()
RESULTIS 0
fail:
writef("Unable to initialise the simulator*n")
GOTO ret
}