This code is to study how buffering and the proposed acknowledgment and invitation protocols bears on circuit thruput. This has all to do with the resources devoted to a circuit thru a sequence of nodes and links. In retrospect this OCaml code could well have been written in C. It developed in a different direction than I anticipated.
An event is a record—a message to the future: what, when and where:
what
Reason for message:
ACK
upstream acknowledgment of receipt of packet so that it may be freed there,
INV
upstream indication that sender has room for new packet,
NP s
downstream message of arrival of packet.
whn
the time the event happens,
where
which node.

The three routines below access a simple priority queue which accepts new events and doles them out in the order that they have been predicted to occur:

newevent
Accepts a new event,
nextevent
Recovers the next event,
nmt
Returns whether there are more events.
Note the chincy list abstraction; I like it. We had to build our own notion of list (ml, mutable list) since the native lists cannot be spliced.

Life cycle of an allocated buffer in a node:

Coming
Reserved for invited packet—perhaps arriving;
This state is entered as we send an invitation upstream.
Waiting
Buffering packet due to busy out link;
This state is entered when a packet has finished arriving.
Going
How many blocks have been sent but unacknowledged on out link.
This state is entered when we begin to transmit a packet; it lasts until an acknowledgment.
State of node and its downstream link:
bw
Bandwidth in b/s, (immutable, per link)
delay
link latency in sec, (immutable, per link)
quota
buffer limit established by customer, (immutable in this simulation, per link, per circuit)
clear
how many unsent packets we have been invited to send down stream, (mutable, per link, per circuit)
coming
how may unreceived packets we have invited the upstream node to send, (mutable, per link, per circuit)
waiting
how many packets are waiting for a downstream invitation, (mutable, per link, per circuit)
sent
how many unacknowledged packets we have sent (mutable, per link, per circuit) .
‘link’ in the above is one way traffic on that link. Presumably clear and waiting are never positive at the same time, at least between events.
coming+waiting+sent ≤ quota.