VoiceRopeImpl.mesa
Copyright © 1986 by Xerox Corporation. All rights reserved.
Doug Terry, October 14, 1986 3:59:55 pm PDT
Client code for interacting with the Voice Rope Service.
DIRECTORY
Atom USING [GetPName],
FinchSmarts USING [ConvDesc, LookupServiceInterface, ReportSystemStateProc, ReportConversationStateProc, ReportRequestStateProc, RegisterForReports, VoiceConnect],
Process USING [SecondsToTicks, SetTimeout, Ticks],
PupSocket USING [GetUniqueID],
RefID USING [ID, nullID],
Rope USING [ROPE, Concat],
RPC USING [CallFailed, ImportFailed],
Thrush USING [ActionType, ConversationID, InterfaceSpec, NB, none, nullConvID, SHHH],
VoiceRopeServer,
VoiceRopeServerRpcControl USING [ImportInterface],
VoiceUtils USING [Problem],
VoiceRope;
VoiceRopeImpl: CEDAR MONITOR
IMPORTS Atom, FinchSmarts, Process, PupSocket, Rope, RPC, VoiceRopeServer, VoiceRopeServerRpcControl, VoiceUtils
EXPORTS VoiceRope
~
BEGIN
ROPE: TYPE ~ Rope.ROPE;
recordingService: ROPE = "recording";
recordingServiceInterface: ROPE = "VoiceRopeServer";
RequestID: TYPE = CARD;
PendingRequest: TYPE = REF PendingRequestBody;
PendingRequestBody:
TYPE =
RECORD [
id: RequestID,
state: Thrush.ActionType ← $unknown
];
PendingRequests: TYPE = LIST OF PendingRequest;
VoiceRopeInfo: TYPE = REF VoiceRopeInfoBody;
VoiceRopeInfoBody:
TYPE =
RECORD [
shhh: Thrush.SHHH ← Thrush.none,
convID: Thrush.ConversationID ← Thrush.nullConvID, -- conv hint (see below)
serviceID: RefID.ID ← RefID.nullID,
imported: BOOLEAN ← FALSE,
pendingRequests: PendingRequests ← NIL,
reportArrived: CONDITION
];
info: VoiceRopeInfo ← NEW[VoiceRopeInfoBody];
convID is a hint of a conversation that used to work. It may well be gone by the time we can use it for anything, but it won't hurt anything either. If the conversation is still active the next time we call record/play/stop, we'd better use it! Eventually we should provide for the possibility that several conversations could exist at once.
VoiceRope client interface
Handle: TYPE = VoiceRope.Handle; -- handles are no longer used for anything
Open:
PUBLIC
PROC [voiceRopeDBName:
ROPE ←
NIL, localName:
ROPE ←
NIL,
Complain:
PROC[complaint:
ROPE] ←
NIL]
RETURNS [handle: Handle] ~ {
If voiceRopeDBName or localName is omitted, a default based on the user profile choice of Thrush Server is invented. If Complain is omitted, Log.Problem[...$Finch] is used.
RETURN[NIL]; -- this routine is no longer needed!
};
Record:
PUBLIC
PROC [handle: Handle←
NIL]
RETURNS [voiceRope: VoiceRope.VoiceRope] ~ {
Records a voice rope, registers it , and returns its ID. A NIL return value indicates that something went wrong.
nb: Thrush.NB;
cDesc: FinchSmarts.ConvDesc;
intID: RequestID;
cDesc ← GetConversation[];
IF cDesc = NIL OR NOT RecordingServiceInterface[cDesc] THEN RETURN;
intID ← NewID[];
AddRequest[intID];
[nb
, voiceRope] ← VoiceRopeServer
.Record[info.shhh, cDesc.situation.self, info.serviceID
, intID,
TRUE
! RPC.CallFailed => {nb ← $callFailed; CONTINUE}];
IF nb#$success
THEN
{
VoiceUtils.Problem[Rope.Concat["Attempt to record failed - ",Atom.GetPName[nb]], $VoiceRope];
RETURN;
};
wait for report that says it finished (or was abandoned)
WaitForRequestState[state: $finished, id: intID];
[nb, voiceRope.length] ← VoiceRopeServer.Length[
info.shhh, voiceRope
! RPC.CallFailed => {nb ← $callFailed; CONTINUE}];
IF nb#$success
THEN
{
VoiceUtils.Problem[Rope.Concat["Attempt to get voicerope length failed - ",Atom.GetPName[nb]], $VoiceRope];
RETURN;
};
};
Play:
PUBLIC
PROC [handle: Handle←
NIL, voiceRope: VoiceRope.VoiceRope, queueIt:
BOOL←
TRUE, failOK:
BOOL←
FALSE, wait:
BOOL←
FALSE]
RETURNS [] ~ {
Play a specified voice rope. The boolean arguments are interpreted as follows:
queueIt => play after all other record/playback requests are satisfied.
failOK => playing is optional; leave connection open if tune doesn't exist.
wait => wait until things appear to be started properly, or have failed.
nb: Thrush.NB;
cDesc: FinchSmarts.ConvDesc;
intID: RequestID;
cDesc ← GetConversation[];
IF cDesc = NIL OR NOT RecordingServiceInterface[cDesc] THEN RETURN;
intID ← NewID[];
AddRequest[intID];
nb ← VoiceRopeServer
.Play[info.shhh, voiceRope
, cDesc.situation.self, info.serviceID
, intID
, queueIt
! RPC.CallFailed => {nb ← $callFailed; CONTINUE}];
IF nb#$success
THEN
{
VoiceUtils.Problem[Rope.Concat["Attempt to play voicerope failed - ",Atom.GetPName[nb]], $VoiceRope];
RETURN;
};
IF wait
THEN {
WaitForRequestState[state: $started, id: intID];
};
};
Stop:
PUBLIC
PROC [handle: Handle ←
NIL]
RETURNS [] ~ {
Stops any recording or playback in progress.
ENABLE UNWIND => NULL;
nb: Thrush.NB;
cDesc: FinchSmarts.ConvDesc;
cDesc ← GetConversation[];
IF cDesc =
NIL
OR cDesc.situation.self.state#$active
OR
NOT RecordingServiceInterface[cDesc] THEN RETURN;
nb ← VoiceRopeServer
.Stop[info.shhh, cDesc.situation.self, info.serviceID
! RPC.CallFailed => {nb ← $callFailed; CONTINUE}];
IF nb#$success
THEN
{
VoiceUtils.Problem[Rope.Concat["Attempt to stop failed - ",Atom.GetPName[nb]], $VoiceRope];
RETURN;
};
};
Retain:
PUBLIC
PROC [handle: Handle ←
NIL, vr: VoiceRope.VoiceRope, class: VoiceRope.InterestClass, refID:
ROPE, other:
ROPE ←
NIL]
RETURNS [] ~ {
Registers a new interest in the voice rope. The voice rope will be retained until either a corresponding Forget is done or the class' garbage collection process determines that the voice rope is no longer referenced, e.g. refID no longer exists. Taken together, the vr, class, and refID must be unique. Repeated calls of Retain with the same parameters (ignoring other) will only register a single interest.
nb: Thrush.NB;
IF NOT RecordingServiceInterface[] THEN RETURN;
nb ← VoiceRopeServer.Retain[info.shhh, vr, class, refID, other ! RPC.CallFailed => {nb ← $callFailed; CONTINUE}];
IF nb#$success
THEN
{
VoiceUtils.Problem[Rope.Concat["Attempt to retain voicerope failed - ",Atom.GetPName[nb]], $VoiceRope];
RETURN;
};
};
Forget:
PUBLIC
PROC [handle: Handle ←
NIL, vr: VoiceRope.VoiceRope, class: VoiceRope.InterestClass, refID:
ROPE]
RETURNS [] ~ {
The specified refID of the specified class drops its interest in the voice rope. The voice rope is not necessarily deleted, however, since other interests in the same voice rope may exist.
nb: Thrush.NB;
IF NOT RecordingServiceInterface[] THEN RETURN;
nb ← VoiceRopeServer.Forget[info.shhh, vr, class, refID ! RPC.CallFailed => {nb ← $callFailed; CONTINUE}];
IF nb#$success
THEN
{
VoiceUtils.Problem[Rope.Concat["Attempt to forget voicerope failed - ",Atom.GetPName[nb]], $VoiceRope];
RETURN;
};
};
GetByInterest:
PUBLIC
PROC [handle: Handle ←
NIL, class: VoiceRope.InterestClass, refID:
ROPE]
RETURNS [voiceRope: VoiceRope.VoiceRope] ~ {
Returns any voice rope that is of interest to the given class and refID; returns NIL if no such voice rope exists.
nb: Thrush.NB;
IF NOT RecordingServiceInterface[] THEN RETURN;
[nb, voiceRope] ← VoiceRopeServer.GetByInterest[info.shhh, class, refID ! RPC.CallFailed => {nb ← $callFailed; CONTINUE}];
IF nb#$success
THEN
{
VoiceUtils.Problem[Rope.Concat["Attempt to lookup voicerope by interest failed - ",Atom.GetPName[nb]], $VoiceRope];
RETURN;
};
};
Cat:
PUBLIC
PROC [handle: Handle ←
NIL, vr1, vr2, vr3, vr4, vr5: VoiceRope.VoiceRope ←
NIL]
RETURNS [new: VoiceRope.VoiceRope] ~ {
Concatenates together the non-NIL voice ropes to produce a new voice rope.
nb: Thrush.NB;
IF NOT RecordingServiceInterface[] THEN RETURN;
[nb, new] ← VoiceRopeServer.Cat[info.shhh, vr1, vr2, vr3, vr4, vr5 ! RPC.CallFailed => {nb ← $callFailed; CONTINUE}];
IF nb#$success
THEN
{
VoiceUtils.Problem[Rope.Concat["Attempt to cat voiceropes failed - ",Atom.GetPName[nb]], $VoiceRope];
RETURN;
};
};
Substr:
PUBLIC
PROC [handle: Handle ←
NIL, vr: VoiceRope.VoiceRope, start:
INT ← 0, len:
INT ←
LAST[
INT]]
RETURNS [new: VoiceRope.VoiceRope] ~ {
Creates a new voice rope that is a substring of an existing voice rope.
nb: Thrush.NB;
IF NOT RecordingServiceInterface[] THEN RETURN;
[nb, new] ← VoiceRopeServer.Substr[info.shhh, vr, start, len ! RPC.CallFailed => {nb ← $callFailed; CONTINUE}];
IF nb#$success
THEN
{
VoiceUtils.Problem[Rope.Concat["Attempt to substring voicerope failed - ",Atom.GetPName[nb]], $VoiceRope];
RETURN;
};
};
Replace:
PUBLIC
PROC [handle: Handle ←
NIL, vr: VoiceRope.VoiceRope, start:
INT ← 0, len:
INT ←
LAST[
INT], with: VoiceRope.VoiceRope ←
NIL]
RETURNS [new: VoiceRope.VoiceRope] ~ {
Creates a new voice rope in which the given interval of the voice rope "vr" is replaced by the voice rope "with".
nb: Thrush.NB;
IF NOT RecordingServiceInterface[] THEN RETURN;
[nb, new] ← VoiceRopeServer.Replace[info.shhh, vr, start, len, with ! RPC.CallFailed => {nb ← $callFailed; CONTINUE}];
IF nb#$success
THEN
{
VoiceUtils.Problem[Rope.Concat["Attempt to replace part of a voicerope failed - ",Atom.GetPName[nb]], $VoiceRope];
RETURN;
};
};
Length:
PUBLIC
PROC [handle: Handle ←
NIL, vr: VoiceRope.VoiceRope]
RETURNS [len:
INT] ~ {
Returns the actual length of the voice rope. This operation ignores the start and length values specified in the voice rope. Thus, vr.start ← 0; vr.length ← Length[handle, vr] will restore a voice rope to its full contents.
nb: Thrush.NB;
IF NOT RecordingServiceInterface[] THEN RETURN;
[nb, len] ← VoiceRopeServer.Length[info.shhh, vr ! RPC.CallFailed => {nb ← $callFailed; CONTINUE}];
IF nb#$success
THEN
{
VoiceUtils.Problem[Rope.Concat["Attempt to get voicerope length failed - ",Atom.GetPName[nb]], $VoiceRope];
RETURN;
};
};
DescribeRope:
PUBLIC
PROC [handle: Handle ←
NIL, vr: VoiceRope.VoiceRope, minSilence:
INT ← -1]
RETURNS [noise: VoiceRope.IntervalSpecs] ~ {
nb: Thrush.NB;
len: INT;
IF NOT RecordingServiceInterface[] THEN RETURN;
[nb, len, noise] ← VoiceRopeServer.DescribeRope[info.shhh, vr, minSilence ! RPC.CallFailed => {nb ← $callFailed; CONTINUE}];
IF nb#$success
THEN
{
VoiceUtils.Problem[Rope.Concat["Attempt to describe voicerope failed - ",Atom.GetPName[nb]], $VoiceRope];
RETURN;
};
};
Report handling
Action reports are generated by the voice rope server when a request for $recording/$playback is $scheduled, $started, $finished, or $flushed. Reports are also available from FinchSmarts concerning the state of a conversation or Finch in general.
A list of pending requests is maintained as part of VoiceRopeInfo. This list is sorted so that the most recent request is at the head of the list. Upon recent of a $finished or $flushed report, the associated request (and all previous requests) are removed from the pending request queue. All pending requests are flushed if the established conversation goes idle (i.e. the user hangs up). The condition, reportArrived, is raised whenever a new report is received. Procedures that wish to wait for a given action report may wait on this condition; they should then check the state of the pending request queue and take appropriate action (such as waiting again).
SystemReport: FinchSmarts.ReportSystemStateProc ~ {
Don't care about changes in system state at the moment.
NULL;
};
ConversationReport: FinchSmarts.ReportConversationStateProc ~ {
[ nb: NB, cDesc: ConvDesc, remark: ROPE←NIL ]
IF cDesc = NIL OR cDesc.situation.self.convID # info.convID THEN RETURN;
SELECT cDesc.situation.self.state
FROM
$active => NULL;
$idle, $neverWas, $failed => {
-- throw away pending requests
RemoveRequests[NIL];
NotifyWaiters[];
};
ENDCASE;
};
RequestReport: FinchSmarts.ReportRequestStateProc ~ {
[ cDesc: ConvDesc, actionReport: Thrush.ActionReport, actionRequest: REF ] RETURNS [betterActionRequest: REF]
request: PendingRequest;
IF actionReport.self.convID # info.convID THEN RETURN[NIL];
SELECT actionReport.actionClass
FROM
$recording, $playback => {
request ← GetRequestByID[actionReport.actionID];
IF request = NIL THEN RETURN[NIL];
request.state ← actionReport.actionType;
SELECT request.state
FROM
$scheduled, $started => NULL;
Throw away pending requests that are complete. This includes requests preceding the one being reported on.
$finished, $flushed =>
RemoveRequests[request];
ENDCASE;
NotifyWaiters[];
};
ENDCASE=> request ← request; -- a place to stand during debugging
RETURN[NIL]; -- leave actionRequest alone
};
The following routines manage the queue of pending requests. (They are the only monitor entry procedures in this module.)
GetRequestByID:
ENTRY PROC [id: RequestID]
RETURNS [request: PendingRequest ←
NIL] ~ {
FOR q: PendingRequests ← info.pendingRequests, q.rest
WHILE q #
NIL
DO
IF q.first.id = id THEN RETURN[q.first];
ENDLOOP;
};
AddRequest:
ENTRY PROC [id: RequestID]
RETURNS [] ~ {
request: PendingRequest ← NEW[PendingRequestBody ← [id]];
info.pendingRequests ← CONS[request, info.pendingRequests];
};
RemoveRequests:
ENTRY PROC [request: PendingRequest]
RETURNS [] ~ {
IF request =
NIL
OR info.pendingRequests =
NIL
OR info.pendingRequests.first = request
THEN
info.pendingRequests ← NIL
ELSE
FOR q: PendingRequests ← info.pendingRequests, q.rest
WHILE q.rest #
NIL
DO
IF q.rest.first = request
THEN {
q.rest ← NIL; -- truncate queue to remove this and all previous requests
EXIT;
};
ENDLOOP;
};
NotifyWaiters:
ENTRY PROC []
RETURNS [] ~ {
BROADCAST info.reportArrived;
};
Wait:
ENTRY PROC []
RETURNS [] ~ {
WAIT info.reportArrived;
};
WaitForRequestState:
PROC [state: Thrush.ActionType, id: RequestID]
RETURNS [] ~ {
request: PendingRequest;
timeout: Process.Ticks ← IF state = $started THEN Process.SecondsToTicks[2] ELSE Process.SecondsToTicks[10];
TRUSTED { Process.SetTimeout[@info.reportArrived, timeout]; };
DO
request ← GetRequestByID[id];
IF request = NIL THEN EXIT; -- completed or conv went idle
SELECT state
FROM
$scheduled =>
SELECT request.state
FROM
$scheduled, $started, $finished, $flushed => EXIT;
ENDCASE => NULL; -- unknown?
$started =>
SELECT request.state
FROM
$started, $finished, $flushed => EXIT;
ENDCASE => NULL; -- probably $scheduled
$finished =>
SELECT request.state
FROM
$finished, $flushed => EXIT;
ENDCASE => NULL; -- probably $scheduled or $started
ENDCASE => EXIT;
Wait[];
ENDLOOP;
};
Establishing conversations and RPC connections
GetConversation:
PROC []
RETURNS [cDesc: FinchSmarts.ConvDesc ←
NIL] ~ {
nb: Thrush.NB;
[nb, cDesc] ← FinchSmarts.VoiceConnect[recordingService, info.convID];
IF nb#$success
THEN {
VoiceUtils.Problem[Rope.Concat["Can't establish conversation - ",Atom.GetPName[nb]], $VoiceRope];
RETURN[NIL];
};
info.convID ← cDesc.situation.self.convID; -- save for future use
};
RecordingServiceInterface:
PROC[cDesc: FinchSmarts.ConvDesc ←
NIL]
RETURNS [imported:
BOOLEAN ←
TRUE] = {
nb: Thrush.NB;
interfaceSpec: Thrush.InterfaceSpec;
IF info.imported THEN RETURN;
[nb, info.shhh, interfaceSpec] ← FinchSmarts.LookupServiceInterface[recordingService, recordingServiceInterface, cDesc];
IF nb#$success THEN RETURN[FALSE];
info.serviceID ← interfaceSpec.serviceID;
info.imported ← TRUE;
VoiceRopeServerRpcControl.ImportInterface[interfaceName: interfaceSpec.interfaceName, hostHint: interfaceSpec.hostHint
!
RPC.ImportFailed => {
info.imported ← FALSE;
VoiceUtils.Problem["Can't import recording service.", $VoiceRope];
CONTINUE}];
RETURN[info.imported];
};
NewID: PROC RETURNS[LONG CARDINAL] ={RETURN[LOOPHOLE[PupSocket.GetUniqueID[]]]};
Initializations
FinchSmarts.RegisterForReports[c: ConversationReport, r: RequestReport];
END.
Doug Terry, August 14, 1986 9:40:42 am PDT
changes to: DIRECTORY, VoiceRopeClientImpl, EXPORTS, ~, Open, Record, Play, Stop, Retain, Forget, GetByInterest, Cat, Substr, Replace, Length, DescribeRope, Proc, Proc
Doug Terry, August 14, 1986 9:58:55 am PDT
changes to: DIRECTORY, VoiceRopeClientImpl, EXPORTS, ~, Open, Record, Play, Stop, Retain, Forget, GetByInterest, Cat, Substr, Replace, Length, DescribeRope, VoiceRopeInterface, Proc, Proc, END
Doug Terry, August 27, 1986 11:43:29 am PDT
changes to: DIRECTORY, VoiceRopeImpl, ~, Open, Play, Stop, VoiceRopeInterface, DescribeRope
Doug Terry, August 29, 1986 4:13:15 pm PDT
changes to: ~, Play, VoiceRopeImpl, Record, Stop, NewID, Proc, Retain, Forget, GetByInterest, Cat, Substr, Replace, Length, DescribeRope, RecordingServiceInterface
Doug Terry, September 5, 1986 4:28:37 pm PDT
changes to: DIRECTORY, IMPORTS, EXPORTS, ~, Handle, Open, Proc
Doug Terry, September 25, 1986 2:59:31 pm PDT
changes to: DIRECTORY, SystemReport, ConversationReport, RequestReport, FinchSmarts, Record
Doug Terry, September 29, 1986 5:56:59 pm PDT
changes to: SystemReport, Record, ConversationReport, RequestReport, ~
Doug Terry, October 3, 1986 3:51:47 pm PDT
changes to: ~, GetRequestByID, GetRequestByRef, NewRequest, RemoveRequests, NewID, Record, Play, ConversationReport, RequestReport, WaitForRequestState, DIRECTORY, IMPORTS, Retain, Forget, GetByInterest, Cat, Substr, Replace, Length, DescribeRope, RecordingServiceInterface, Stop, WaitForRequestState, Handle, Open
Doug Terry, October 3, 1986 4:45:36 pm PDT
changes to: RequestReport
Doug Terry, October 6, 1986 1:44:44 pm PDT
changes to: DIRECTORY, ~, Record, Play, Stop, Retain, Forget, GetByInterest, Cat, Substr, Replace, Length, DescribeRope, RecordingServiceInterface, IMPORTS
Doug Terry, October 9, 1986 8:00:18 pm PDT
changes to: Record, Stop, Retain, Forget, GetByInterest, Cat, Substr, Replace, Length, DescribeRope
Doug Terry, October 9, 1986 8:45:25 pm PDT
changes to: Wait, WaitForRequestState
Doug Terry, October 14, 1986 3:59:55 pm PDT
changes to: ConversationReport