This class contains methods that address two issues that relates to this example. The first is the initialization of the structures needed for holding the logged messages together with the methods that both archive the messages submitted to the log and their subsequent display.
The second set of methods address the generation of a numbered sequence of names to identify producers and consumers. This is not strictly needed because the InterSystems IRIS process ids provided by the $JOB command also does this, but it is easier to use more human-readable labels.
/// Utility class
Class Semaphore.Util Extends %RegisteredObject [ ProcedureBlock ]
{
/// The name of the shared semaphore
Parameter ME = "Util";
/// initialize output log
ClassMethod InitLog()
{
// initialize the logging global
Kill ^SemaphoreLog
Set ^SemaphoreLog = 0
Quit
}
/// Enter messages as received into a global
/// for logging purposes
ClassMethod Logger(sender As %String, msg As %String)
{
Set inx = $INCREMENT(^SemaphoreLog)
Set ^SemaphoreLog(inx, 0) = $JOB
Set ^SemaphoreLog(inx, 1) = sender
Set ^SemaphoreLog(inx, 2) = msg
Write "(", ^SemaphoreLog, ") ", msg, !
Quit
}
/// display the messages in the log
ClassMethod ShowLog()
{
Set msgcnt = $GET(^SemaphoreLog, 0)
Write "Message Log: Entries = ", msgcnt, !, !
Write "#", ?5, "$JOB", ?12, "Sender", ?25, "Message", !
For i = 1 : 1 : msgcnt {
Set job = ^SemaphoreLog(i, 0)
Set sender = ^SemaphoreLog(i, 1)
Set msg = ^SemaphoreLog(i, 2)
Write i, ")", ?5, job, ?12, sender, ":", ?25, msg, !
}
Quit
}
/// initialize the name index
ClassMethod InitIndex()
{
// initialize the logging global
Kill ^SemaphoreNames
Quit
}
/// initialize the name index
ClassMethod IndexName(name As %String) As %String
{
If ($DATA(^SemaphoreNames(name)) = 0) {
Set ^SemaphoreNames(name) = 0
}
Set index = $INCREMENT(^SemaphoreNames(name))
Quit (name _ "." _ index)
}
}