feat: finalize with step 5

This commit is contained in:
jonas committed 2026-09-03 23:39:25 +02:00
1 parent 129dd5d8b8
commit aab128b55c
10 files changed
+332 -37

No files matched your search

@@ -1,10 +1,21 @@
package westworldwithmessaging
import (
"fmt"
"sort"
"sync"
)
// SendMsgImmediately and NoAdditionalInfo are the Go versions of the
// book's "SEND_MSG_IMMEDIATELY" and "NO_ADDITIONAL_INFO" - both
// defined as 0 in the book's MessageDispatcher.h purely so call
// sites read clearly instead of showing a bare 0/NULL. NoAdditionalInfo
// is nil rather than 0, since Go's nil is what an empty "any" (our
// ExtraInfo field's type) actually looks like.
const SendMsgImmediately = 0.0
var NoAdditionalInfo any = nil
// messageDispatcher is the Go version of the singleton class
// MessageDispatcher. Same singleton pattern as everything else in
// this package: unexported type, one shared instance, reached only
@@ -109,6 +120,9 @@ func (d *messageDispatcher) DispatchMessage(delay float64, sender int, receiver
// if there is no delay, route the telegram immediately
if delay <= 0.0 {
fmt.Printf("\nInstant telegram dispatched at time: %.5f by %s for %s. Msg is %s",
Clock.GetCurrentTime(), GetNameOfEntity(sender), GetNameOfEntity(receiver), GetNameOfMessage(MessageType(msg)))
// send the telegram to the recipient
d.discharge(pReceiver, telegram)
} else {
@@ -118,6 +132,9 @@ func (d *messageDispatcher) DispatchMessage(delay float64, sender int, receiver
// and put it in the queue
d.insertIntoPriorityQueue(telegram)
fmt.Printf("\nDelayed telegram from %s recorded at time %.5f for %s. Msg is %s",
GetNameOfEntity(sender), currentTime, GetNameOfEntity(receiver), GetNameOfMessage(MessageType(msg)))
}
}
@@ -146,6 +163,9 @@ func (d *messageDispatcher) DispatchDelayedMessages() {
// read the telegram from the front of the queue
telegram := d.priorityQueue[0]
fmt.Printf("\nQueued telegram ready for dispatch: Sent to %s. Msg is %s",
GetNameOfEntity(telegram.Receiver), GetNameOfMessage(MessageType(telegram.Msg)))
// find the recipient
pReceiver := EntityMgr.GetEntityFromID(telegram.Receiver)