DIRECTORY
Commander USING [ CommandProc, Register ],
IO,
Lark
-- USING [
-- CommandEvents, ConnectionSpec, Event, LarkModel, Passel, StatusEvents ]--,
LarkSmartsMonitorImpl,
MBQueue USING [ Create, QueueClientAction ],
Nice,
RefID USING [ ID, Reseal, Unseal ],
ThParty USING [ Alert, ConversationInfo, CreateConversation, DescribeParty, GetConversationInfo, GetKeyTable, GetParty, SmartsInterfaceRecord ],
ThPartyPrivate USING [ RegisterLocal ],
Thrush
USING [
ConversationID, ConvEvent, Credentials, PartyID, NB, nullConvID, nullID, ROPE, SHHH, SmartsID, StateInConv ],
ThSmartsPrivate
USING [
AssessDamage, ChangeState, ComputeConnection, ConvDesc, Deregister, EnterLarkState, ForgetConv, GetConv, GetConvDesc, GetSIC, GetSmartsInfo, LarkFailed, LarkInfo, LarkState, OpenConversations, SmartsInfo, SmartsInfoBody, TrunkBundle ],
ThSmartsRpcControl,
Triples USING [Make ],
TU,
VoiceUtils USING [ Problem, ProblemFR, Report ]
;
Types and Constants
ConvDesc: TYPE = ThSmartsPrivate.ConvDesc;
Reseal: PROC[r: REF] RETURNS[RefID.ID] = INLINE {RETURN[RefID.Reseal[r]]; };
ROPE: TYPE = Thrush.ROPE;
SHHH: TYPE = Lark.SHHH; -- Encrypts conv. if first arg to RPC PROC
PartyID: TYPE = Thrush.PartyID;
nullID: RefID.ID = Thrush.nullID;
nullConvID: Thrush.ConversationID = Thrush.nullConvID;
SmartsInfo: TYPE = ThSmartsPrivate.SmartsInfo;
StateInConv: TYPE = Thrush.StateInConv;
PD:
TYPE =
RECORD [
timeoutNoAction: INT,
doReports: BOOL←FALSE,
doNice: BOOL←FALSE
];
pd: REF PD ← NEW[PD←[]];
Report:
PROC[what:
ROPE, info: SmartsInfo] = {
IF NOT pd.doReports THEN RETURN;
VoiceUtils.Report[what, $Lark, info.larkInfo];
};
Events
RecordTrunkEvent handles events from the EtherPhone Trunk connection.
The only interesting events at present are "ring" (place trunk->station call) and "tones F" (station->trunk call being "answered")
The upstream code produces a single "enabled" event when the line starts ringing, and a single "disabled" event when it appears that ringing has stopped. This "disabled" event must be anticipated and ignored if the station answers (calling trunk can't hang up on completed call!)
EventSpec: TYPE = REF EventSpecBody;
EventSpecBody:
TYPE =
RECORD [
smartsInfo: SmartsInfo,
sEvent: Lark.StatusEvent
];
LarkTrunkParseEvent:
PROC[
smartsInfo: SmartsInfo, sEvent: Lark.StatusEvent] = {
smartsInfo.requests.QueueClientAction[
QdLarkTrunkParseEvent, NEW[EventSpecBody ← [smartsInfo, sEvent]]];
};
QdLarkTrunkParseEvent:
ENTRY
PROC[r:
REF] = {
-- r is the EventSpec
eventSpec: EventSpec = NARROW[r];
smartsInfo: SmartsInfo = eventSpec.smartsInfo; {
ENABLE {
UNWIND=>NULL;
ThSmartsPrivate.LarkFailed => { VoiceUtils.ProblemFR["%g: Lark Failed", $Smarts, smartsInfo, TU.RefAddr[smartsInfo]]; GOTO Failed; };
};
sEvent: Lark.StatusEvent ← eventSpec.sEvent;
SELECT sEvent.device
FROM
ringDetect =>
SELECT sEvent.event
FROM
Lark.enabled => CmdRinging[smartsInfo];
Lark.disabled => CmdStopRinging[smartsInfo];
ENDCASE => ERROR;
ENDCASE;
EXITS
Failed =>
ThSmartsPrivate.Deregister[smartsInfo.smartsID];
}; };
CmdRinging:
INTERNAL
PROC[info: SmartsInfo] = {
partyID: PartyID; -- self
calledPartyRname: ROPE;
calledPartyID: PartyID;
cDesc: ConvDesc;
convEvent: Thrush.ConvEvent;
nb: Thrush.NB; {
SELECT ThSmartsPrivate.GetSIC[info]
FROM
$idle => NULL;
ENDCASE => RETURN; -- not a valid time for this call. Ignore. Host system will handle.
cDesc ← ThSmartsPrivate.GetConv[info, nullConvID, TRUE];
partyID ← cDesc.situation.self.partyID; -- self
[nb, calledPartyRname] ←
ThParty.DescribeParty[partyID: partyID, nameReq: $owner];
IF nb # $success
OR calledPartyRname=
NIL
THEN
GOTO Failed;
-- Enter error state? Forget Conv?
[nb, calledPartyID] ← ThParty.GetParty[partyID: partyID, rName: calledPartyRname];
SELECT nb
FROM
$success => NULL;
$noSuchParty2 => GOTO Failed; -- Enter error state? Forget Conv?
ENDCASE => GOTO Failed; -- Enter error state? Forget Conv?
[nb, convEvent] ← ThParty.CreateConversation[
credentials: cDesc.situation.self,
state: $initiating
];
IF nb # $success
THEN {
ThSmartsPrivate.AssessDamage[nb, cDesc, convEvent];
GOTO Failed;
No idea what to do! This is pretty catastrophic, since no one else know about conv
}
ELSE NoteNewTkState[cDesc, convEvent];
nb←ThParty.Alert[credentials: cDesc.situation.self, calledPartyID: calledPartyID];
IF nb # $success THEN GOTO Failed; -- enter error state?
EXITS
Failed => ThSmartsPrivate.ForgetConv[cDesc];
}};
CmdStopRinging:
INTERNAL
PROC[info: SmartsInfo] = {
nb: Thrush.NB;
cDesc: ConvDesc=ThSmartsPrivate.GetConvDesc[info];
IF cDesc=NIL THEN RETURN;
SELECT cDesc.situation.self.state
FROM
$initiating, $ringback => NULL; -- ringing stopped before called party answered.
ENDCASE => RETURN; -- already active or something; ignore end of ringing
nb ← ThSmartsPrivate.ChangeState[cDesc, $idle, $terminating];
Complain about nb # $success here?
};
LarkTrunkProgress:
PUBLIC
ENTRY
PROC[
interface: ThSmartsRpcControl.InterfaceRecord,
shh: Thrush.SHHH,
convEvent: Thrush.ConvEvent
] = {
ENABLE UNWIND => NULL; -- RestoreInvariant;
Some party has changed state in a conversation we know about.
info: SmartsInfo = ThSmartsPrivate.GetSmartsInfo[smartsID: convEvent.self.smartsID];
IF info=NIL THEN { Report["No info at Progress", NIL]; RETURN; };
info.requests.QueueClientAction[QdLarkTrunkProgress, convEvent];
};
QdLarkTrunkProgress:
ENTRY
PROC[r:
REF] = {
ENABLE UNWIND => NULL; -- RestoreInvariant;
convEvent: Thrush.ConvEvent = NARROW[r];
info: SmartsInfo = ThSmartsPrivate.GetSmartsInfo[smartsID: convEvent.self.smartsID];
cDesc: ConvDesc;
convID: Thrush.ConversationID = convEvent.self.convID;
whatNeedsDoing: WhatNeedsDoing;
IF info=NIL THEN { Report["Smarts Info Missing at Progress", NIL]; RETURN; };
cDesc ← ThSmartsPrivate.GetConv[ info, convID, TRUE ];
IF convEvent.self.partyID = convEvent.other.partyID
THEN {
-- own state changed
NoteNewTkState[cDesc, convEvent];
SELECT info.currentConvID
FROM
convID, nullConvID =>
NULL;
(new conversation and we're idle) or (this is the one we like)
ENDCASE => DoRejectCall[cDesc, convEvent];
We have to reject this, since we're already dealing with another conv.
We are (still) willing to seriously consider only one conversation at a time.
RETURN;
};
IF convID#info.currentConvID
THEN {
Situation is that someone else reported a change of theirs concerning a conversation we've expressed no interest in. I don't believe this should happen.
Report["Strange progress report", cDesc.info];
ThSmartsPrivate.ForgetConv[cDesc];
RETURN;
};
Someone else's state changed in a conv. we're interested in; see if it means anything to us!
whatNeedsDoing ← whatNeedsDoingIf[cDesc.situation.self.state][convEvent.other.state];
SELECT whatNeedsDoing
FROM
$noop, $ntiy => NULL; -- No action is needed, or we don't know what one to take.
$imp => ERROR; -- This is supposed to be impossible!
$xrep => Report["Didn't expect state change report", info];
$frgt => ThSmartsPrivate.ForgetConv[cDesc];
$invl => {
Report["Invalid state transition", info];
[]←ThSmartsPrivate.ChangeState[cDesc: cDesc, state: $idle,
reason: $error, comment: "System Error: Invalid state transition"];
};
$idle => {
nb2: Thrush.NB; cInfo: ThParty.ConversationInfo;
[nb2, cInfo] ← ThParty.GetConversationInfo[convID: convEvent.self.convID];
IF nb2 # $success
OR (cInfo.numParties-cInfo.numIdle) <= 1
THEN
[]←ThSmartsPrivate.ChangeState[cDesc, $idle,
IF nb2#$success THEN $error ELSE $terminating];
};
$actv => [] ← ThSmartsPrivate.ChangeState[cDesc, $active];
$reac, $deac => NULL;
ENDCASE => ERROR;
};
QdNotification:
ENTRY
PROC [r:
REF] ~ {
ENABLE UNWIND => NULL; -- RestoreInvariant;
cDesc: ConvDesc = NARROW[r];
IF cDesc.situation.self.state = $notified
THEN
[] ← ThSmartsPrivate.ChangeState[cDesc, $active];
};
DoRejectCall:
INTERNAL
PROC[cDesc: ConvDesc, convEvent: Thrush.ConvEvent] = {
[]←ThSmartsPrivate.ChangeState[cDesc, $idle, $busy];
};
NoteNewTkState:
INTERNAL
PROC[cDesc: ConvDesc, convEvent: Thrush.ConvEvent] ~ {
OPEN now: cDesc.situation.self;
nb: Thrush.NB;
larkStateData: REF←NIL;
state: StateInConv ← convEvent.self.state;
info: ThSmartsPrivate.SmartsInfo = cDesc.info;
larkInfo: ThSmartsPrivate.LarkInfo = info.larkInfo;
cInfo: ThParty.ConversationInfo;
larkState: ThSmartsPrivate.LarkState ← $none;
savedCurrentConvID: Thrush.ConversationID;
cDesc.situation ← convEvent^;
IF info.currentConvID=nullConvID THEN info.currentConvID ← cDesc.situation.self.convID;
savedCurrentConvID ← info.currentConvID;
SELECT state
FROM
$notified => info.requests.QueueClientAction[QdNotification, cDesc];
$idle => {
IF larkInfo.larkState=trunkForwarding THEN larkState ← $idle;
ThSmartsPrivate.ForgetConv[cDesc];
};
$neverWas => ThSmartsPrivate.ForgetConv[cDesc];
$active => {
phoneNumber: ROPE;
[nb, phoneNumber] ← ThParty.DescribeParty[partyID: now.partyID, nameReq: $address];
IF nb = $success THEN [nb, cInfo] ← ThParty.GetConversationInfo[convID: now.convID];
larkState ←
IF nb # $success OR phoneNumber=NIL OR cInfo.originator=now.partyID THEN trunkTalking ELSE trunkSignalling;
larkStateData ←
NEW[ThSmartsPrivate.TrunkBundle←[ThSmartsPrivate.ComputeConnection[cDesc], phoneNumber]];
};
ENDCASE;
IF state > $idle
AND cDesc.keyTable=
NIL
THEN {
[nb, cDesc.keyTable] ← ThParty.GetKeyTable[credentials: now];
IF nb # $success THEN cDesc.keyTable←NIL;
};
IF state>$idle
AND cDesc.keyTable#info.larkInfo.keyTable
AND larkStateData=
NIL
THEN {
larkStateData ← cDesc.keyTable;
larkState ← larkInfo.larkState;
};
IF now.convID = savedCurrentConvID
AND larkState # $none
THEN
ThSmartsPrivate.EnterLarkState[larkInfo, larkState, larkStateData];
};
WhatNeedsDoing:
TYPE =
ATOM;
-- {
Just codes to dispatch on in Supervisor; explained there
$noop, $idle, $actv, $frgt, $reac, $deac, -- cases explained in code
$invl, -- considered an invalid request
$xrep, -- we got a report we feel we shouldn't have got
$ntiy, -- not implemented yet
$imp -- this situation should not arise even in the face of invalid requests
};
whatNeedsDoingIf:
ARRAY StateInConv
OF
ARRAY StateInConv
OF WhatNeedsDoing ← [
If we're in the state identified by the row, and someone else in the conversation reports a transition onto the state identified by the column, what should we do?
never idle failed resrv pars init notif rback ring canAc activ inact -- ← (other)
[ $imp, $frgt, $frgt, $frgt, $frgt, $frgt, $frgt, $frgt, $frgt, $frgt, $frgt, $frgt],
--neverWas
(Clip this table to view without these comments.)
This situation arises when we've forgotten about the conversation that somebody else is still reporting on.
[ $imp, $noop, $frgt, $noop, $noop, $noop, $noop, $noop, $noop, $noop, $noop, $ntiy ], -- idle
[ $imp, $imp, $frgt, $imp, $imp, $imp, $imp, $imp, $imp, $imp, $imp, $imp ], -- failed
[ $imp, $imp, $frgt, $imp, $imp, $imp, $imp, $imp, $imp, $imp, $imp, $imp ],
-- reserved
The actions of other parties are not of interest to us yet, since they're not in this conv.
[ $imp, $imp, $frgt, $imp, $imp, $imp, $imp, $imp, $imp, $imp, $imp, $imp ],
-- parsing
Ditto.
[ $imp,
$idle,$frgt, $invl, $invl, $invl, $xrep, $xrep,
$xrep
,
$actv,
$actv,
$actv ],
-- initiating
They can either enter ringing to indicate interest, or go active without ringing
[ $imp,
$idle,$frgt, $invl, $invl, $invl, $xrep, $noop, $noop, $ntiy, $noop, $ntiy ],
-- notified
We don't expect to hear from others while we're deciding whether to play
[ $imp,
$idle,$frgt, $invl, $invl, $invl, $invl, $invl, $invl, $invl, $invl, $invl ],
-- ringback
They have earlier expressed interest noopringing), and are now joining the fray
[ $imp,
$idle,$frgt, $invl, $invl, $invl, $invl, $invl, $invl, $invl, $invl, $invl ],
-- ringing
The only thing that interests us here is everybody else quitting.
[ $imp, $idle,$frgt, $invl, $invl, $invl, $xrep, $xrep, $noop, $ntiy, $noop, $ntiy ], -- canActivate
[ $imp, $idle,$frgt, $invl, $invl, $invl, $xrep, $xrep, $noop, $ntiy, $reac, $deac ], -- active
[ $imp, $idle,$frgt, $invl, $invl, $invl, $xrep, $xrep, $noop, $ntiy, $ntiy, $ntiy ] -- inactive (current ^)
];
}.