home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / Newton Platform Info / Newton 2.0 Sample Code / Endpoints / Comms FSM-1 / Comms FSM.text < prev    next >
Encoding:
Text File  |  1996-03-27  |  48.4 KB  |  1,717 lines  |  [TEXT/MPS ]

  1. // Text of project Comms FSM written on 3/27/96 at 2:46 PM
  2. // Beginning of file protoEvent
  3.  
  4. // Before Script for "_userproto000"
  5. //    Newton Developer Technical Support Sample Code
  6. //    protoEvent - An NTK Finite State Machine User Proto
  7. //    by Jim Schram, Newton Developer Technical Support
  8. //    Copyright ©1996 Apple Computer, Inc.  All rights reserved.
  9. //    
  10. //    You may incorporate this sample code into your applications without
  11. //    restriction.  This sample code has been provided "AS IS" and the
  12. //    responsibility for its operation is 100% yours.  You are not
  13. //    permitted to modify and redistribute the source as "DTS Sample Code."
  14. //    If you are going to re-distribute the source, we require that you
  15. //    make it clear in the source that the code was descended from
  16. //    Apple-provided sample code, but that you've made changes.
  17.  
  18.  
  19. _userproto000 :=
  20.     {viewBounds: {left: 8, top: 16, right: 96, bottom: 32}, _proto: @218};
  21.  
  22.  
  23. constant |layout_protoEvent| := _userproto000;
  24. // End of file protoEvent
  25. // Beginning of file protoState
  26.  
  27. // Before Script for "_userproto001"
  28. //    Newton Developer Technical Support Sample Code
  29. //    protoState - An NTK Finite State Machine User Proto
  30. //    by Jim Schram, Newton Developer Technical Support
  31. //    Copyright ©1996 Apple Computer, Inc.  All rights reserved.
  32. //    
  33. //    You may incorporate this sample code into your applications without
  34. //    restriction.  This sample code has been provided "AS IS" and the
  35. //    responsibility for its operation is 100% yours.  You are not
  36. //    permitted to modify and redistribute the source as "DTS Sample Code."
  37. //    If you are going to re-distribute the source, we require that you
  38. //    make it clear in the source that the code was descended from
  39. //    Apple-provided sample code, but that you've made changes.
  40.  
  41.  
  42. _userproto001 :=
  43.     {viewBounds: {left: 8, top: 16, right: 112, bottom: 56}, _proto: @473};
  44.  
  45.  
  46. constant |layout_protoState| := _userproto001;
  47. // End of file protoState
  48. // Beginning of file protoFSM
  49.  
  50. // Before Script for "_userproto002"
  51. //    Newton Developer Technical Support Sample Code
  52. //    protoFSM - An NTK Finite State Machine User Proto
  53. //    by Jim Schram, Newton Developer Technical Support
  54. //    Copyright ©1996 Apple Computer, Inc.  All rights reserved.
  55. //    
  56. //    You may incorporate this sample code into your applications without
  57. //    restriction.  This sample code has been provided "AS IS" and the
  58. //    responsibility for its operation is 100% yours.  You are not
  59. //    permitted to modify and redistribute the source as "DTS Sample Code."
  60. //    If you are going to re-distribute the source, we require that you
  61. //    make it clear in the source that the code was descended from
  62. //    Apple-provided sample code, but that you've made changes.
  63.  
  64. kFSMCleanUpFunc := func(fsmFrame)
  65. begin
  66.     local fsmSymbol, stateSymbol, eventSymbol, hasGenesisState;
  67.     
  68.     RemoveSlot(fsmFrame._proto, '_proto);
  69.     RemoveSlot(fsmFrame._proto, 'viewBounds);
  70.     RemoveSlot(fsmFrame, 'viewBounds);
  71.     
  72.     fsmFrame.fsm_private_states := {    };
  73.     
  74.     if not fsmSymbol := GetSlot(fsmFrame, 'declareSelf) then
  75.         begin
  76.             fsmSymbol := 'unknown;
  77.             print("A protoFSM implementation is unnamed (you forgot the declareSelf slot).");
  78.         end;
  79.     
  80.     if fsmFrame.stepChildren then
  81.         foreach stateFrame in fsmFrame.stepChildren do
  82.             begin
  83.                 RemoveSlot(stateFrame, '_proto);
  84.                 RemoveSlot(stateFrame, 'viewBounds);
  85.                 
  86.                 if not stateSymbol := GetSlot(stateFrame, 'declareSelf) then
  87.                     begin
  88.                         stateSymbol := 'unknown;
  89.                         print("A protoState in the '|" & fsmSymbol & "| protoFSM implementation is unnamed (you forgot the declareSelf slot).");
  90.                     end;
  91.                 if fsmFrame.fsm_private_states.(stateSymbol) then
  92.                     print("The '|" & stateSymbol & "| protoState in the '|" & fsmSymbol & "| protoFSM implementation already exists (duplicate declareSelf slot value).");
  93.                 else
  94.                     fsmFrame.fsm_private_states.(stateSymbol) := stateFrame;
  95.                 
  96.                 if stateFrame.stepChildren then
  97.                     foreach eventFrame in stateFrame.stepChildren do
  98.                         begin
  99.                             RemoveSlot(eventFrame, '_proto);
  100.                             RemoveSlot(eventFrame, 'viewBounds);
  101.                             
  102.                             if not eventSymbol := GetSlot(eventFrame, 'declareSelf) then
  103.                                 begin
  104.                                     eventSymbol := 'unknown;
  105.                                     print("A protoEvent in the '|" & stateSymbol & "| state of the '|" & fsmSymbol & "| protoFSM implementation is unnamed (you forgot the declareSelf slot).");
  106.                                 end;
  107.                             if stateFrame.(eventSymbol) then
  108.                                 print("The '|" & eventSymbol & "| protoEvent in the '|" & stateSymbol & "| protoState in the '|" & fsmSymbol & "| protoFSM implementation already exists (duplicate declareSelf slot value).");
  109.                             else
  110.                                 stateFrame.(eventSymbol) := eventFrame;
  111.                             
  112.                             RemoveSlot(eventFrame, 'declareSelf);
  113.                         end;
  114.                 
  115.                 RemoveSlot(stateFrame, 'declareSelf);
  116.                 RemoveSlot(stateFrame, 'stepChildren);
  117.                 
  118.                 hasGenesisState := hasGenesisState or stateSymbol = 'Genesis;
  119.             end;
  120.     
  121.     if not hasGenesisState then
  122.         print("The '|" & fsmSymbol & "| protoFSM implementation is missing the required '|Genesis| state.");
  123.     
  124.     if not kDebugOn then        // GoToState is a debug-only function!
  125.         begin
  126.             RemoveSlot(fsmFrame._proto, 'GoToState);
  127.             RemoveSlot(fsmFrame, 'GoToState);
  128.         end;
  129.     RemoveSlot(fsmFrame, 'stepChildren);
  130. end
  131.  
  132. _userproto002 :=
  133.     {viewBounds: {left: 8, top: 8, right: 128, bottom: 72},
  134.      DoEvent:
  135.        func(eventSymbol, paramArray)    // SELF can be anything that inherits to the finite state machine instance frame
  136.        begin
  137.            local x := fsm_private_context;
  138.            if not x then        // this catches the situation where the FSM is disposed before a pending delayed action/call/send executes
  139.                return;
  140.            
  141.            if kDebugOn then
  142.                if paramArray and PrimClassOf(paramArray) <> 'Array then
  143.                    Throw('|evt.ex.msg|, "protoFSM:DoEvent 2nd argument must be Nil or Array");
  144.            
  145.            x.pendingEventQueue:EnQueue(eventSymbol);
  146.            x.pendingParamsQueue:EnQueue(paramArray);
  147.            
  148.            if not x.busy then
  149.                begin
  150.                    x.busy := true;
  151.                    AddDelayedSend(x.fsm, 'DoEvent_Loop, nil, x.turtle);
  152.                end;
  153.            
  154.            nil;
  155.        end,
  156.      instantiate:
  157.        //    SELF is the finite state machine template frame, e.g.:
  158.        //
  159.        //    local fsm := GetLayout("myFSM"):Instantiate();
  160.        //
  161.        //    "myFSM" is assumed to be a layout based on protoFSM,
  162.        
  163.        func()
  164.        begin
  165.            local obj := {    _proto:                            self,
  166.                                fsm:                                nil,
  167.                                currentStateFrame:        nil,
  168.                                currentEventFrame:        nil,
  169.                                fsm_private_context:    {    fsm:                                nil,
  170.                                                                        turtle:                            1,
  171.                                                                        level:                            0,
  172.                                                                        busy:                            nil,
  173.                                                                        waitView:                        nil,
  174.                                                                        waitAborted:                nil,
  175.                                                                        pendingState:                'Genesis,
  176.                                                                        pendingEventQueue:        QueueTemplate:Instantiate(),
  177.                                                                        pendingParamsQueue:    QueueTemplate:Instantiate(),
  178.                                                                        currentState:                nil,
  179.                                                                        currentEvent:                nil,
  180.                                                                        currentParams:            nil,    },    };
  181.            obj.currentStateFrame := {    _proto:        obj.fsm_private_states.Genesis,
  182.                                                        _parent:    obj,    };
  183.            obj.fsm := obj.fsm_private_context.fsm := obj;
  184.        end,
  185.      dispose:
  186.        func()    // SELF is the finite state machine instance frame
  187.        begin
  188.            fsm_private_context.pendingEventQueue:Reset();
  189.            fsm_private_context.pendingParamsQueue:Reset();
  190.            foreach slot in fsm_private_context do fsm_private_context.slot := nil;
  191.            currentStateFrame := currentEventFrame := fsm_private_context := nil;        // guaranteed to return nil so that the caller can conveniently nil out the FSM container variable
  192.        end,
  193.      DoEvent_Loop:
  194.        func()    // SELF is the finite state machine instance frame
  195.        begin
  196.            local x := fsm_private_context;
  197.            if not x then        // this catches the situation where the FSM is disposed before a pending delayed action/call/send executes
  198.                return;
  199.            
  200.            local ok;
  201.            local pendingStateFrame;
  202.            
  203.            if x.pendingState then
  204.                if fsm_private_states.(x.pendingState) then
  205.                    if fsm_private_states.(x.pendingState).(x.pendingEventQueue:Peek()) then
  206.                        ok := true;
  207.                    else
  208.                        begin
  209.                            if kDebugOn then :?DebugFSM('UnknownEvent, x.pendingState, x.pendingEventQueue:Peek(), x.pendingParamsQueue:Peek());            // ignore if event not programmed
  210.                        end;
  211.                else
  212.                    begin
  213.                        if kDebugOn then :?DebugFSM('UnknownState, x.pendingState, x.pendingEventQueue:Peek(), x.pendingParamsQueue:Peek());            // error --> remain in current state
  214.                    end;
  215.            else
  216.                begin
  217.                    if kDebugOn then :?DebugFSM('NilState, x.pendingState, x.pendingEventQueue:Peek(), x.pendingParamsQueue:Peek());                    // machine halted
  218.                end;
  219.            
  220.            if not ok then
  221.                begin
  222.                    currentStateFrame := nil;
  223.                    currentEventFrame := nil;
  224.                    
  225.                    x.pendingEventQueue:DeQueue();        // there is a problem with this state or event
  226.                    x.pendingParamsQueue:DeQueue();        // so remove the offending pending queue elements
  227.                end;
  228.            else
  229.                begin
  230.                    x.currentState := x.pendingState;
  231.                    x.currentEvent := x.pendingEventQueue:DeQueue();
  232.                    x.currentParams := x.pendingParamsQueue:DeQueue();
  233.                    
  234.                    currentStateFrame :=    {    _proto:        fsm_private_states.(x.currentState),
  235.                                                            _parent:    self,    };
  236.                    
  237.                    currentEventFrame :=    {    _proto:        fsm_private_states.(x.currentState).(x.currentEvent),
  238.                                                            _parent:    currentStateFrame,    };
  239.                    
  240.                    if currentEventFrame.Action then
  241.                        begin
  242.                            if kDebugOn then :?TraceFSM('PreAction, x.currentState, x.currentEvent, x.currentParams);
  243.                            
  244.                            x.level := x.level + 1;
  245.                            try
  246.                                Perform(currentEventFrame, 'Action, x.currentParams);
  247.                            onexception |evt.ex| do
  248.                                begin
  249.                                    try
  250.                                        :?ExceptionHandler(CurrentException());
  251.                                    onexception |evt.ex| do
  252.                                        nil;
  253.                                end;
  254.                            x.level := x.level - 1;
  255.                            
  256.                            if kDebugOn then :?TraceFSM('PostAction, x.currentState, x.currentEvent, x.currentParams);
  257.                        end;
  258.                    
  259.                    if currentEventFrame.nextState exists then
  260.                        if not x.pendingState := currentEventFrame.nextState then
  261.                            if kDebugOn then :?DebugFSM('NilNextState, x.currentState, x.currentEvent, x.currentParams);        // machine halted
  262.                    
  263.                    if kDebugOn then :?TraceFSM('NextState, x.pendingState, x.pendingEventQueue:Peek(), x.pendingParamsQueue:Peek());
  264.                end;
  265.            
  266.            if x.waitView                                                // check for terminal state & exit waitView if necessary
  267.            and x.pendingState
  268.            and pendingStateFrame := fsm_private_states.(x.pendingState) then
  269.                if pendingStateFrame.terminal then
  270.                    begin
  271.                        x.pendingEventQueue:Reset();
  272.                        x.pendingParamsQueue:Reset();
  273.                        AddDelayedCall(    func()
  274.                                                    if x.waitView then
  275.                                                        x.waitView:Close(), nil, 1    );
  276.                    end;
  277.        
  278.            if x.pendingEventQueue:IsEmpty() then
  279.                x.busy := nil;
  280.            else
  281.                AddDelayedSend(self, 'DoEvent_Loop, nil, x.turtle);
  282.            
  283.            nil;
  284.        end,
  285.      SetSpeed:
  286.        func(newSpeed)        // SELF is the finite state machine instance frame
  287.        begin
  288.            fsm_private_context.turtle := newSpeed;
  289.        end,
  290.      IsBusy:
  291.        func()    // SELF is the finite state machine instance frame
  292.        begin
  293.            fsm_private_context.busy;
  294.        end,
  295.      GetSpeed:
  296.        func()    // SELF is the finite state machine instance frame
  297.        begin
  298.            fsm_private_context.turtle;
  299.        end,
  300.      GoToState:
  301.        // SELF is the finite state machine instance frame
  302.        // This function is for DEBUGGING USE ONLY ! ! !
  303.        // It is STRIPPED from the resulting package when kDebugOn = nil
  304.        
  305.        func(newState)
  306.        begin
  307.            local x := fsm_private_context;
  308.            
  309.            x.pendingState := newState;
  310.            x.pendingEventQueue:Reset();
  311.            x.pendingParamsQueue:Reset();
  312.            
  313.            nil;
  314.        end,
  315.      QueueTemplate:
  316.        {
  317.            Instantiate:            func()                                    // This is a very simple First-In-First-Out queue
  318.                                        {    _proto:        self,
  319.                                            queue:        [],    },
  320.            
  321.            Reset:                    func() SetLength(queue, 0),
  322.            
  323.            Peek:                    func() if Length(queue) > 0 then queue[0],        // else nil
  324.            
  325.            DeQueue:                func()
  326.                                        if Length(queue) > 0 then        // else nil
  327.                                            begin
  328.                                                local data := queue[0];
  329.                                                RemoveSlot(queue, 0);
  330.                                                data;
  331.                                            end,
  332.            
  333.            EnQueue:                func(data)
  334.                                        begin
  335.                                            AddArraySlot(queue, data);
  336.                                            nil;
  337.                                        end,
  338.            
  339.            GetQueueSize:        func() Length(queue),
  340.            
  341.            IsEmpty:                func() Length(queue) = 0,
  342.        },
  343.      ProtoClone:
  344.        func(object)
  345.        begin
  346.            local f := 
  347.                func native(obj)
  348.                begin
  349.                    if not IsFrame(obj) or IsFunction(obj) then
  350.                        Throw('|evt.ex.msg|, "ProtoClone only works with frames.");
  351.                    
  352.                    local new := {_proto: obj};
  353.                    foreach slot, value in obj do
  354.                        if IsFrame(value) and not IsFunction(value) then
  355.                            new.(slot) := call f with (value);
  356.                    new;
  357.                end;
  358.            
  359.            call f with (object);
  360.        end,
  361.      WaitForTerminal:
  362.        func(options)
  363.        begin
  364.            local x := fsm_private_context;
  365.            
  366.            if x.waitView
  367.            or x.level <> 0
  368.            or x.pendingEventQueue:IsEmpty() then
  369.                return;
  370.            
  371.            x.waitView := BuildContext(waitViewTemplate);
  372.            x.waitView:SetOwnerContext(x, options);
  373.            x.waitView:ModalDialog();
  374.            x.waitAborted;        // return the value of waitAborted (true = user aborted via the status slip, nil = FSM terminal state was reached normally)
  375.        end,
  376.      waitViewTemplate:
  377.        {    viewClass:                    clView,
  378.            viewFlags:                    vVisible,
  379.            viewFormat:                    vfNone,
  380.            viewBounds:                    {    left:            0,
  381.                                                    top:            0,
  382.                                                    right:        0,
  383.                                                    bottom:        0,    },
  384.            
  385.            statusView:                    nil,
  386.            statusViewOptions:        nil,
  387.             fsmContext:                    nil,
  388.             aborted:                        nil,
  389.             
  390.            SetOwnerContext:            func(owner, options)
  391.                                                begin
  392.                                                    self.statusView := nil;
  393.                                                    self.statusViewOptions := options;        // frame of options the caller of WaitForTerminal is passing us (e.g.  progress messages, etc.)
  394.                                                    self.fsmContext := owner;                // the fsm_private_context slot of the FSM
  395.                                                    self.aborted := nil;
  396.                                                end,
  397.            
  398.            viewIdleScript:                func()
  399.                                                begin
  400.                                                    statusView := BuildContext(statusViewTemplate);
  401.                                                    statusView:SetOwnerContext(self, statusViewOptions);
  402.                                                    statusView:ModalDialog();
  403.                                                    nil;
  404.                                                end,
  405.            
  406.            viewSetupDoneScript:    func()
  407.                                                begin
  408.                                                    inherited:?ViewSetupDoneScript();
  409.                                                    if not statusViewOptions then
  410.                                                        :SetUpIdle(2000);
  411.                                                    else if statusViewOptions.delayUntilStatusVisible then
  412.                                                        :SetUpIdle(if statusViewOptions.delayUntilStatusVisible <= 0 then 1 else statusViewOptions.delayUntilStatusVisible);
  413.                                                end,
  414.            
  415.            viewQuitScript:            func()
  416.                                                begin
  417.                                                    if statusView then
  418.                                                        statusView:Close();
  419.                                                    fsmContext.waitAborted := aborted;
  420.                                                    fsmContext.waitView := nil;
  421.                                                end,
  422.            
  423.            statusViewTemplate:        {    _proto:                                protoStatusTemplate,
  424.                                                    initialSetup:                        nil,
  425.                                                    waitView:                            nil,
  426.                                                    delayUntilAbortTimer:        nil,
  427.                                                    delayUntilAbortVisible:        nil,
  428.                                                    abortButtonText:                nil,
  429.                                                    
  430.                                                    viewIdleScript:                    func()
  431.                                                                                            begin
  432.                                                                                                inherited:?viewIdleScript();
  433.                                                                                                local contents :=    {    name:        'vBarber,
  434.                                                                                                                                values:        {    barber:        true,    },    };
  435.                                                                                                
  436.                                                                                                if delayUntilAbortVisible then
  437.                                                                                                    begin
  438.                                                                                                        delayUntilAbortTimer := delayUntilAbortTimer + 300;
  439.                                                                                                        if delayUntilAbortTimer > delayUntilAbortVisible then
  440.                                                                                                            begin
  441.                                                                                                                delayUntilAbortVisible := nil;
  442.                                                                                                                contents.values.primary := {    text:            abortButtonText,
  443.                                                                                                                                                            script:        func()
  444.                                                                                                                                                                            begin
  445.                                                                                                                                                                                waitView.aborted := true;
  446.                                                                                                                                                                                waitView:Close();
  447.                                                                                                                                                                            end,    };
  448.                                                                                                                base:ViewSet(contents);
  449.                                                                                                                return 300;
  450.                                                                                                            end;
  451.                                                                                                    end;
  452.                                                                                                
  453.                                                                                                base:UpdateIndicator(contents);
  454.                                                                                                300;
  455.                                                                                            end,
  456.                                                    
  457.                                                    viewSetupDoneScript:        func()
  458.                                                                                            begin
  459.                                                                                                inherited:?ViewSetupDoneScript();
  460.                                                                                                :SetUpIdle(100);
  461.                                                                                                self.delayUntilAbortTimer := 0;
  462.                                                                                            end,
  463.                                                    
  464.                                                    SetOwnerContext:                func(owner, options)
  465.                                                                                            begin
  466.                                                                                                self.waitView := owner;
  467.                                                                                                self.delayUntilAbortVisible := if options then options.delayUntilAbortVisible else 8000;
  468.                                                                                                self.abortButtonText := if options and options.abortButtonText then options.abortButtonText else "Abort";
  469.                                                                                                 self.initialSetup := {    name:        'vBarber, 
  470.                                                                                                                                values:        {    icon:                    ROM_routeUpdateBitmap,
  471.                                                                                                                                                    statusText:        if options then options.statusText else "Please wait...",
  472.                                                                                                                                                    titleText:            if options then options.titleText else nil,
  473.                                                                                                                                                    barber:                true,
  474.                                                                                                                                                    primary:            nil,
  475.                                                                                                                                                    closeBox:            nil,    },    };
  476.                                                                                             end,
  477.                                                },
  478.        },
  479.      ExceptionHandler:
  480.        func(exception)
  481.        begin
  482.            local x := fsm_private_context;
  483.            local message :=    if x then
  484.                                            "The following exception occured in event ("
  485.                                            & x.currentEvent
  486.                                            & ") of state ("
  487.                                            & x.currentState
  488.                                            & ") of finite state machine ("
  489.                                            & x.fsm.declareSelf
  490.                                            & "):  "
  491.                                        else
  492.                                            "The following exception occured:  ";
  493.            
  494.            local exceptionStr := "<unable to create string representation of exception frame>";
  495.            try
  496.                exceptionStr := :ObjectToString(exception);
  497.            onexception |evt.ex| do
  498.                nil;
  499.            
  500.            GetRoot():Notify(kNotifyAlert, kAppName, message & exceptionStr);
  501.            
  502.            print(message);
  503.            print(exception);
  504.            if GetGlobalVar('BreakOnThrows) then
  505.                BreakLoop();
  506.            
  507.            nil;
  508.        end,
  509.      ObjectToString:
  510.        // Converts almost any NewtonScript data type into a string representation.
  511.        // Does NOT handle recursive/self-referencing frames.
  512.        // Does NOT follow _proto & _parent pointers.
  513.        // Does NOT check for out-of-memory conditions, bad object refs, et cetera.
  514.        
  515.        func(obj)
  516.        begin
  517.            local separator := ", ";
  518.            local separatorLen := StrLen(separator);
  519.            
  520.            local p :=
  521.                func(s)
  522.                if EndsWith(s, separator) then
  523.                    StrMunger(s, StrLen(s) - separatorLen, nil, nil, 0, nil)
  524.                else
  525.                    s;
  526.            
  527.            local f :=
  528.                func(obj)
  529.                begin
  530.                    (    if IsFunction(obj) then
  531.                            "func(" & NumberStr(GetFunctionArgCount(obj)) & (if GetFunctionArgCount(obj) = 1 then " arg)" else " args)")
  532.                        
  533.                        else if IsFrame(obj) then
  534.                            begin
  535.                                local s := "{";
  536.                                foreach slot, item in obj do
  537.                                    s := s & SPrintObject(slot) & ": " & 
  538.                                        if slot = '_parent or slot = '_proto then
  539.                                            "<ignored>" & separator
  540.                                        else
  541.                                            call f with (item);
  542.                                call p with (s) & "}";
  543.                            end
  544.                        
  545.                        else if IsArray(obj) then
  546.                            begin
  547.                                local s := "[";
  548.                                foreach item in obj do
  549.                                    s := s & call f with (item);
  550.                                call p with (s) & "]";
  551.                            end
  552.                        
  553.                        else if IsString(obj) then
  554.                            $" & obj & $"
  555.                        
  556.                        else if IsSymbol(obj) then
  557.                            $' & obj
  558.                        
  559.                        else if IsNumber(obj) or IsInteger(obj) then
  560.                            NumberStr(obj)
  561.                        
  562.                        else if IsImmediate(obj) then
  563.                            if not obj then
  564.                                "nil"
  565.                            else if obj = true then
  566.                                "true"
  567.                            else
  568.                                SPrintObject(obj)
  569.                        
  570.                        else
  571.                            SPrintObject(obj)
  572.                    
  573.                    ) & separator;
  574.                end;
  575.            
  576.            call p with (call f with (obj));
  577.        end,
  578.      _proto: @218
  579.     };
  580.  
  581.  
  582. constant |layout_protoFSM| := _userproto002;
  583. // End of file protoFSM
  584. // Beginning of file CommsFSM
  585.  
  586. // Before Script for "Comms FSM"
  587. //    Newton Developer Technical Support Sample Code
  588. //    CommsFSM - An NTK Finite State Machine Implementation
  589. //    by Jim Schram, Newton Developer Technical Support
  590. //    Copyright ©1996 Apple Computer, Inc.  All rights reserved.
  591. //    
  592. //    You may incorporate this sample code into your applications without
  593. //    restriction.  This sample code has been provided "AS IS" and the
  594. //    responsibility for its operation is 100% yours.  You are not
  595. //    permitted to modify and redistribute the source as "DTS Sample Code."
  596. //    If you are going to re-distribute the source, we require that you
  597. //    make it clear in the source that the code was descended from
  598. //    Apple-provided sample code, but that you've made changes.
  599.  
  600.  
  601. Comms FSM :=
  602.     {viewBounds: {left: 0, top: 0, right: 496, bottom: 496},
  603.      DebugFSM:
  604.        func(reason, state, event, params)
  605.        begin
  606.            local s :=    "Reason = " & :ObjectToString(reason)
  607.                            & "\nState = " & :ObjectToString(state)
  608.                            & "\nEvent = " & :ObjectToString(event)
  609.                            & "\nParams = " & :ObjectToString(params);
  610.            
  611.            :MTrace(s);
  612.            print(SubstituteChars(s, "\n", "\t"));
  613.        end,
  614.      TraceFSM:
  615.        func(when, state, event, params)
  616.        begin
  617.            local s :=    "When = " & :ObjectToString(when)
  618.                            & "\nState = " & :ObjectToString(state)
  619.                            & "\nEvent = " & :ObjectToString(event)
  620.                            & "\nParams = " & :ObjectToString(params);
  621.            
  622.            :MTrace(s);
  623.            print(SubstituteChars(s, "\n", "\t"));
  624.        end,
  625.      MNotifyError:
  626.        // where = string describing location of error
  627.        // error = NewtonScript object (e.g. integer, real number, exception frame, etc.) describing the error
  628.        
  629.        func(where, error)
  630.        begin
  631.            if not error
  632.            or error = -16005
  633.            or fQuiet then
  634.                return;
  635.            
  636.            :MNotify("An error occured in " & where & ".  Error = " & :ObjectToString(error));
  637.        end,
  638.      MNotify:
  639.        func(message)
  640.        begin
  641.            GetRoot():Notify(kNotifyAlert, kAppName, message);
  642.        end,
  643.      MTrace:
  644.        func(s)
  645.        begin
  646.            SetValue(GetRoot().(kAppSymbol).vTraceBox, 'text, s);
  647.            RefreshViews();
  648.        end,
  649.      fState: nil,
  650.      fConnectAction: nil,
  651.      fEndpoint: nil,
  652.      fPowerOffState: nil,
  653.      declareSelf: 'CommsFSM,
  654.      fQuiet: nil,
  655.      debug: "Comms FSM",
  656.      _proto: _userproto002
  657.     };
  658.  
  659. Genesis :=
  660.     {viewBounds: {left: 8, top: 16, right: 112, bottom: 56},
  661.      terminal: true,
  662.      declareSelf: 'Genesis,
  663.      debug: "Genesis",
  664.      _proto: _userproto001
  665.     };
  666. AddStepForm(Comms FSM, Genesis);
  667.  
  668. Create :=
  669.     {viewBounds: {left: 8, top: 16, right: 96, bottom: 32},
  670.      Action:
  671.        func()
  672.        begin
  673.            fsm.fConnectAction := 'Connect;
  674.            fsm.fState := 'Connecting;
  675.            fsm.fPowerOffState := nil;
  676.            fsm.fQuiet := nil;
  677.            fsm.fEndpoint := {    _proto:        protoBasicEndpoint,
  678.                                        _parent:    fsm,    };
  679.            
  680.            RegPowerOff(    kAppSymbol,
  681.                                    func(what, why)            // we create the closure here so as to set up SELF as the event frame in the closure
  682.                                    begin
  683.                                        if what = 'okToPowerOff then
  684.                                            begin
  685.                                                if why <> 'idle                                                        // keep the unit awake whenever we're connected
  686.                                                or fsm.fState = 'Disconnected then                        // unless the user or an application explicitly
  687.                                                    return true;                                                        // wants it to sleep
  688.                                            end;
  689.                                        
  690.                                        else if what = 'powerOff then
  691.                                            begin
  692.                                                if why <> 'idle                                                        // if we simply must go to sleep but we're still
  693.                                                or fsm.fState <> 'Disconnected then                        // connected then begin the disconnect process
  694.                                                    begin
  695.                                                        fsm.fPowerOffState := 'holdYourHorses;        // set a flag to indicate we're powering down
  696.                                                        :DoEvent('PowerOff, nil);
  697.                                                        return 'holdYourHorses;
  698.                                                    end;
  699.                                            end;
  700.                                        
  701.                                        nil;    // ALWAYS return nil here!
  702.                                    end    );
  703.            
  704.            :DoEvent('Instantiate, nil);
  705.        end,
  706.      nextState: 'Instantiate,
  707.      declareSelf: 'Create,
  708.      debug: "Create",
  709.      _proto: _userproto000
  710.     };
  711. AddStepForm(Genesis, Create);
  712.  
  713.  
  714.  
  715.  
  716.  
  717. Instantiate :=
  718.     {viewBounds: {left: 120, top: 16, right: 224, bottom: 88},
  719.      declareSelf: 'Instantiate,
  720.      debug: "Instantiate",
  721.      _proto: _userproto001
  722.     };
  723. AddStepForm(Comms FSM, Instantiate);
  724.  
  725. Instantiate :=
  726.     {viewBounds: {left: 8, top: 16, right: 96, bottom: 32},
  727.      Action:
  728.        func()
  729.        begin
  730.            try
  731.                fEndPoint:Instantiate(fEndPoint, :MBuildConfigOptions());
  732.            onexception |evt.ex| do
  733.                return :DoEvent('InstantiateFailure, [CurrentException()]);
  734.            
  735.            :DoEvent('InstantiateSuccess, nil);
  736.        end,
  737.      declareSelf: 'Instantiate,
  738.      MBuildConfigOptions:
  739.        func()
  740.        begin
  741.            [
  742.                {    label:        kCMSAsyncSerial,
  743.                    type:        'service,
  744.                    opCode:        opSetRequired,
  745.                    result:        nil,    },
  746.                
  747.                {    label:        kCMOSerialIOParms,
  748.                    type:        'option,
  749.                    opCode:        opSetRequired,
  750.                    result:        nil,
  751.                    form:        'template,
  752.                    data:    {
  753.                        arglist:    [
  754.                            k1StopBits,                // 1 stop bit
  755.                            kNoParity,                // no parity bit
  756.                            k8DataBits,                // 8 data bits
  757.                            k19200bps,    ],            // date rate in bps
  758.                        typelist:    ['struct,
  759.                            'long,                        // stop bits
  760.                            'long,                        // parity
  761.                            'long,                        // data bits
  762.                            'long,    ],    },    },            // bps
  763.                
  764.                {    label:        kCMOInputFlowControlParms,
  765.                    type:        'option,
  766.                    opCode:        opSetRequired,
  767.                    result:        nil,
  768.                    form:        'template,
  769.                    data:    {
  770.                        arglist:    [
  771.                            unicodeDC1,             // xonChar    
  772.                            unicodeDC3,             // xoffChar    
  773.                            nil,                         // useSoftFlowControl    
  774.                            nil,                         // useHardFlowControl    
  775.                            0,                         // not needed; returned    
  776.                            0,    ],                         // not needed; returned    
  777.                        typelist:    ['struct,
  778.                            'char,                    // XON character
  779.                            'char,                    // XOFF character
  780.                            'boolean,                // software flow control
  781.                            'boolean,                // hardware flow control
  782.                            'boolean,                // hardware flow blocked
  783.                            'boolean,    ],    },    },    // software flow blocked
  784.                
  785.                {    label:        kCMOOutputFlowControlParms,
  786.                    type:        'option,
  787.                    opCode:        opSetRequired,
  788.                    result:        nil,
  789.                    form:        'template,
  790.                    data:    {
  791.                        arglist:    [
  792.                            unicodeDC1,             // xonChar    
  793.                            unicodeDC3,             // xoffChar    
  794.                            nil,                         // useSoftFlowControl    
  795.                            nil,                         // useHardFlowControl    
  796.                            0,                         // not needed; returned    
  797.                            0,    ],                         // not needed; returned    
  798.                        typelist:    ['struct,
  799.                            'char,                    // XON character
  800.                            'char,                    // XOFF character
  801.                            'boolean,                // software flow control
  802.                            'boolean,                // hardware flow control
  803.                            'boolean,                // hardware flow blocked
  804.                            'boolean,    ],    },    },    // software flow blocked
  805.            ];
  806.        end,
  807.      debug: "Instantiate",
  808.      _proto: _userproto000
  809.     };
  810. AddStepForm(Instantiate, Instantiate);
  811.  
  812.  
  813.  
  814. Instantiate Success :=
  815.     {viewBounds: {left: 8, top: 32, right: 96, bottom: 48},
  816.      Action:
  817.        func()
  818.        begin
  819.            :DoEvent('Bind, nil);
  820.        end,
  821.      declareSelf: 'InstantiateSuccess,
  822.      nextState: 'Bind,
  823.      debug: "Instantiate Success",
  824.      _proto: _userproto000
  825.     };
  826. AddStepForm(Instantiate, Instantiate Success);
  827.  
  828.  
  829.  
  830. Instantiate Failure :=
  831.     {viewBounds: {left: 8, top: 48, right: 96, bottom: 64},
  832.      Action:
  833.        func(error)
  834.        begin
  835.            :MNotifyError("Endpoint Instantiate", error);
  836.            :DoEvent('CleanUp, nil);
  837.        end,
  838.      declareSelf: 'InstantiateFailure,
  839.      nextState: 'CleanUp,
  840.      debug: "Instantiate Failure",
  841.      _proto: _userproto000
  842.     };
  843. AddStepForm(Instantiate, Instantiate Failure);
  844.  
  845.  
  846.  
  847.  
  848.  
  849. Bind :=
  850.     {viewBounds: {left: 120, top: 96, right: 224, bottom: 200},
  851.      declareSelf: 'Bind,
  852.      debug: "Bind",
  853.      _proto: _userproto001
  854.     };
  855. AddStepForm(Comms FSM, Bind);
  856.  
  857. Bind :=
  858.     {viewBounds: {left: 8, top: 16, right: 96, bottom: 32},
  859.      declareSelf: 'Bind,
  860.      Action:
  861.        func()
  862.        begin
  863.            fEndPoint:Bind(:MBuildConfigOptions(), fCompletionSpec);
  864.        end,
  865.      fCompletionSpec:
  866.        {    async:                    true,
  867.            reqTimeout:            kNoTimeout,
  868.            completionScript:    func(ep, options, result)
  869.                                        begin
  870.                                            if result then
  871.                                                ep:DoEvent('BindFailure, [result]);
  872.                                            else
  873.                                                ep:DoEvent('BindSuccess, nil);
  874.                                        end,    },
  875.      MBuildConfigOptions:
  876.        func()
  877.        begin
  878.            nil;
  879.        end,
  880.      debug: "Bind",
  881.      _proto: _userproto000
  882.     };
  883. AddStepForm(Bind, Bind);
  884.  
  885.  
  886.  
  887. Bind Success :=
  888.     {viewBounds: {left: 8, top: 32, right: 96, bottom: 48},
  889.      declareSelf: 'BindSuccess,
  890.      Action:
  891.        func()
  892.        begin
  893.            :DoEvent('Connect, nil);
  894.        end,
  895.      nextState: 'Connect,
  896.      debug: "Bind Success",
  897.      _proto: _userproto000
  898.     };
  899. AddStepForm(Bind, Bind Success);
  900.  
  901.  
  902.  
  903. Bind Failure :=
  904.     {viewBounds: {left: 8, top: 48, right: 96, bottom: 64},
  905.      declareSelf: 'BindFailure,
  906.      Action:
  907.        func(error)
  908.        begin
  909.            :MNotifyError("Endpoint Bind", error);
  910.            :DoEvent('Dispose, nil);
  911.        end,
  912.      nextState: 'Dispose,
  913.      debug: "Bind Failure",
  914.      _proto: _userproto000
  915.     };
  916. AddStepForm(Bind, Bind Failure);
  917.  
  918.  
  919.  
  920. Cancel :=
  921.     {viewBounds: {left: 8, top: 64, right: 96, bottom: 80},
  922.      declareSelf: 'Cancel,
  923.      Action:
  924.        func()
  925.        begin
  926.            fQuiet := true;
  927.            
  928.            try
  929.                fEndPoint:Cancel(nil);
  930.            onexception |evt.ex.comm| do
  931.                nil;
  932.        end,
  933.      debug: "Cancel",
  934.      _proto: _userproto000
  935.     };
  936. AddStepForm(Bind, Cancel);
  937.  
  938.  
  939.  
  940. Power Off :=
  941.     {viewBounds: {left: 8, top: 80, right: 96, bottom: 96},
  942.      declareSelf: 'PowerOff,
  943.      Action:
  944.        func()
  945.        begin
  946.            :DoEvent('Cancel, nil);
  947.        end,
  948.      debug: "Power Off",
  949.      _proto: _userproto000
  950.     };
  951. AddStepForm(Bind, Power Off);
  952.  
  953.  
  954.  
  955.  
  956.  
  957. Connect :=
  958.     {viewBounds: {left: 120, top: 208, right: 224, bottom: 344},
  959.      declareSelf: 'Connect,
  960.      debug: "Connect",
  961.      _proto: _userproto001
  962.     };
  963. AddStepForm(Comms FSM, Connect);
  964.  
  965. Connect :=
  966.     {viewBounds: {left: 8, top: 16, right: 96, bottom: 32},
  967.      declareSelf: 'Connect,
  968.      Action:
  969.        func()
  970.        begin
  971.            if fConnectAction = 'Connect then
  972.                fEndPoint:Connect(:MBuildConnectConfigOptions(), fCompletionSpec_Connect);
  973.            else
  974.                fEndPoint:Listen(:MBuildListenConfigOptions(), fCompletionSpec_Listen);
  975.        end,
  976.      fCompletionSpec_Connect:
  977.        {    async:                    true,
  978.            reqTimeout:            kNoTimeout,
  979.            completionScript:    func(ep, options, result)
  980.                                        begin
  981.                                            if result then
  982.                                                ep:DoEvent('ConnectFailure, [result]);
  983.                                            else
  984.                                                ep:DoEvent('ConnectSuccess, nil);
  985.                                        end,    },
  986.      fCompletionSpec_Listen:
  987.        {    async:                    true,
  988.            reqTimeout:            kNoTimeout,
  989.            completionScript:    func(ep, options, result)
  990.                                        begin
  991.                                            if result then
  992.                                                ep:DoEvent('ListenFailure, [result]);
  993.                                            else
  994.                                                ep:DoEvent('ListenSuccess, nil);
  995.                                        end,    },
  996.      MBuildListenConfigOptions:
  997.        func()
  998.        begin
  999.            nil;
  1000.        end,
  1001.      MBuildConnectConfigOptions:
  1002.        func()
  1003.        begin
  1004.            nil;
  1005.        end,
  1006.      debug: "Connect",
  1007.      _proto: _userproto000
  1008.     };
  1009. AddStepForm(Connect, Connect);
  1010.  
  1011.  
  1012.  
  1013. Connect Success :=
  1014.     {viewBounds: {left: 8, top: 32, right: 96, bottom: 48},
  1015.      Action:
  1016.        func()
  1017.        begin
  1018.            fState := 'Connected;
  1019.        end,
  1020.      declareSelf: 'ConnectSuccess,
  1021.      nextState: 'Connected,
  1022.      debug: "Connect Success",
  1023.      _proto: _userproto000
  1024.     };
  1025. AddStepForm(Connect, Connect Success);
  1026.  
  1027.  
  1028.  
  1029. Connect Failure :=
  1030.     {viewBounds: {left: 8, top: 48, right: 96, bottom: 64},
  1031.      Action:
  1032.        func(error)
  1033.        begin
  1034.            :MNotifyError("Endpoint Connect", error);
  1035.            
  1036.            :DoEvent('UnBind, nil);
  1037.        end,
  1038.      declareSelf: 'ConnectFailure,
  1039.      nextState: 'UnBind,
  1040.      debug: "Connect Failure",
  1041.      _proto: _userproto000
  1042.     };
  1043. AddStepForm(Connect, Connect Failure);
  1044.  
  1045.  
  1046.  
  1047. Listen Success :=
  1048.     {viewBounds: {left: 8, top: 64, right: 96, bottom: 80},
  1049.      Action:
  1050.        func()
  1051.        begin
  1052.            :DoEvent('Accept, nil);
  1053.        end,
  1054.      declareSelf: 'ListenSuccess,
  1055.      nextState: 'Accept,
  1056.      debug: "Listen Success",
  1057.      _proto: _userproto000
  1058.     };
  1059. AddStepForm(Connect, Listen Success);
  1060.  
  1061.  
  1062.  
  1063. Listen Failure :=
  1064.     {viewBounds: {left: 8, top: 80, right: 96, bottom: 96},
  1065.      Action:
  1066.        func(error)
  1067.        begin
  1068.            :MNotifyError("Endpoint Listen", error);
  1069.        
  1070.            :DoEvent('UnBind, nil);
  1071.        end,
  1072.      declareSelf: 'ListenFailure,
  1073.      nextState: 'UnBind,
  1074.      debug: "Listen Failure",
  1075.      _proto: _userproto000
  1076.     };
  1077. AddStepForm(Connect, Listen Failure);
  1078.  
  1079.  
  1080.  
  1081. Cancel :=
  1082.     {viewBounds: {left: 8, top: 96, right: 96, bottom: 112},
  1083.      declareSelf: 'Cancel,
  1084.      Action:
  1085.        func()
  1086.        begin
  1087.            fQuiet := true;
  1088.            
  1089.            try
  1090.                fEndPoint:Cancel(nil);
  1091.            onexception |evt.ex.comm| do
  1092.                nil;
  1093.        end,
  1094.      debug: "Cancel",
  1095.      _proto: _userproto000
  1096.     };
  1097. AddStepForm(Connect, Cancel);
  1098.  
  1099.  
  1100.  
  1101. Power Off :=
  1102.     {viewBounds: {left: 8, top: 112, right: 96, bottom: 128},
  1103.      declareSelf: 'PowerOff,
  1104.      Action:
  1105.        func()
  1106.        begin
  1107.            :DoEvent('Cancel, nil);
  1108.        end,
  1109.      debug: "Power Off",
  1110.      _proto: _userproto000
  1111.     };
  1112. AddStepForm(Connect, Power Off);
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118. Accept :=
  1119.     {viewBounds: {left: 120, top: 352, right: 224, bottom: 456},
  1120.      declareSelf: 'Accepting,
  1121.      debug: "Accept",
  1122.      _proto: _userproto001
  1123.     };
  1124. AddStepForm(Comms FSM, Accept);
  1125.  
  1126. Accept :=
  1127.     {viewBounds: {left: 8, top: 16, right: 96, bottom: 32},
  1128.      declareSelf: 'Accept,
  1129.      Action:
  1130.        func()
  1131.        begin
  1132.            fEndPoint:Accept(nil, fCompletionSpec);
  1133.        end,
  1134.      fCompletionSpec:
  1135.        {    async:                    true,
  1136.            reqTimeout:            kNoTimeout,
  1137.            completionScript:    func(ep, options, result)
  1138.                                        begin
  1139.                                            if result then
  1140.                                                ep:DoEvent('AcceptFailure, [result]);
  1141.                                            else
  1142.                                                ep:DoEvent('AcceptSuccess, nil);
  1143.                                        end,    },
  1144.      debug: "Accept",
  1145.      _proto: _userproto000
  1146.     };
  1147. AddStepForm(Accept, Accept);
  1148.  
  1149.  
  1150.  
  1151. Accept Success :=
  1152.     {viewBounds: {left: 8, top: 32, right: 96, bottom: 48},
  1153.      Action:
  1154.        func()
  1155.        begin
  1156.            fState := 'Connected;
  1157.        end,
  1158.      declareSelf: 'AcceptSuccess,
  1159.      nextState: 'Connected,
  1160.      debug: "Accept Success",
  1161.      _proto: _userproto000
  1162.     };
  1163. AddStepForm(Accept, Accept Success);
  1164.  
  1165.  
  1166.  
  1167. Accept Failure :=
  1168.     {viewBounds: {left: 8, top: 48, right: 96, bottom: 64},
  1169.      Action:
  1170.        func(error)
  1171.        begin
  1172.            :MNotifyError("Endpoint Accept", error);
  1173.            
  1174.            :DoEvent('UnBind, nil);
  1175.        end,
  1176.      declareSelf: 'AcceptFailure,
  1177.      nextState: 'UnBind,
  1178.      debug: "Accept Failure",
  1179.      _proto: _userproto000
  1180.     };
  1181. AddStepForm(Accept, Accept Failure);
  1182.  
  1183.  
  1184.  
  1185. Cancel :=
  1186.     {viewBounds: {left: 8, top: 64, right: 96, bottom: 80},
  1187.      declareSelf: 'Cancel,
  1188.      Action:
  1189.        func()
  1190.        begin
  1191.            fQuiet := true;
  1192.            
  1193.            try
  1194.                fEndPoint:Cancel(nil);
  1195.            onexception |evt.ex.comm| do
  1196.                nil;
  1197.        end,
  1198.      debug: "Cancel",
  1199.      _proto: _userproto000
  1200.     };
  1201. AddStepForm(Accept, Cancel);
  1202.  
  1203.  
  1204.  
  1205. Power Off :=
  1206.     {viewBounds: {left: 8, top: 80, right: 96, bottom: 96},
  1207.      declareSelf: 'PowerOff,
  1208.      Action:
  1209.        func()
  1210.        begin
  1211.            :DoEvent('Cancel, nil);
  1212.        end,
  1213.      debug: "Power Off",
  1214.      _proto: _userproto000
  1215.     };
  1216. AddStepForm(Accept, Power Off);
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222. Connected :=
  1223.     {viewBounds: {left: 232, top: 16, right: 336, bottom: 88},
  1224.      declareSelf: 'Connected,
  1225.      debug: "Connected",
  1226.      _proto: _userproto001
  1227.     };
  1228. AddStepForm(Comms FSM, Connected);
  1229.  
  1230. Disconnect :=
  1231.     {viewBounds: {left: 8, top: 16, right: 96, bottom: 32},
  1232.      Action:
  1233.        func()
  1234.        begin
  1235.            fState := 'Disconnecting;
  1236.            fQuiet := true;
  1237.            
  1238.            try
  1239.                fEndPoint:Cancel(nil);
  1240.            onexception |evt.ex.comm| do
  1241.                nil;
  1242.            
  1243.            :DoEvent('Disconnect, nil);
  1244.        end,
  1245.      declareSelf: 'Disconnect,
  1246.      nextState: 'Disconnect,
  1247.      debug: "Disconnect",
  1248.      _proto: _userproto000
  1249.     };
  1250. AddStepForm(Connected, Disconnect);
  1251.  
  1252.  
  1253.  
  1254. Cancel :=
  1255.     {viewBounds: {left: 8, top: 32, right: 96, bottom: 48},
  1256.      declareSelf: 'Cancel,
  1257.      Action:
  1258.        func()
  1259.        begin
  1260.            try
  1261.                fEndPoint:Cancel(nil);
  1262.            onexception |evt.ex.comm| do
  1263.                nil;
  1264.        end,
  1265.      debug: "Cancel",
  1266.      _proto: _userproto000
  1267.     };
  1268. AddStepForm(Connected, Cancel);
  1269.  
  1270.  
  1271.  
  1272. Power Off :=
  1273.     {viewBounds: {left: 8, top: 48, right: 96, bottom: 64},
  1274.      declareSelf: 'PowerOff,
  1275.      Action:
  1276.        func()
  1277.        begin
  1278.            :DoEvent('Disconnect, nil);
  1279.        end,
  1280.      debug: "Power Off",
  1281.      _proto: _userproto000
  1282.     };
  1283. AddStepForm(Connected, Power Off);
  1284.  
  1285.  
  1286.  
  1287.  
  1288.  
  1289. Disconnect :=
  1290.     {viewBounds: {left: 232, top: 96, right: 336, bottom: 200},
  1291.      declareSelf: 'Disconnect,
  1292.      debug: "Disconnect",
  1293.      _proto: _userproto001
  1294.     };
  1295. AddStepForm(Comms FSM, Disconnect);
  1296.  
  1297. Disconnect :=
  1298.     {viewBounds: {left: 8, top: 16, right: 96, bottom: 32},
  1299.      declareSelf: 'Disconnect,
  1300.      Action:
  1301.        func()
  1302.        begin
  1303.            try
  1304.                fEndPoint:Disconnect(true, fCompletionSpec);
  1305.            onexception |evt.ex| do
  1306.                :DoEvent('DisconnectFailure, [CurrentException()]);
  1307.        end,
  1308.      fCompletionSpec:
  1309.        {    async:                    true,
  1310.            reqTimeout:            kNoTimeout,
  1311.            completionScript:    func(ep, options, result)
  1312.                                        begin
  1313.                                            if result then
  1314.                                                ep:DoEvent('DisconnectFailure, [result]);
  1315.                                            else
  1316.                                                ep:DoEvent('DisconnectSuccess, nil);
  1317.                                        end,    },
  1318.      debug: "Disconnect",
  1319.      _proto: _userproto000
  1320.     };
  1321. AddStepForm(Disconnect, Disconnect);
  1322.  
  1323.  
  1324.  
  1325. Disconnect Success :=
  1326.     {viewBounds: {left: 8, top: 32, right: 96, bottom: 48},
  1327.      Action:
  1328.        func()
  1329.        begin
  1330.            :DoEvent('UnBind, nil);
  1331.        end,
  1332.      declareSelf: 'DisconnectSuccess,
  1333.      nextState: 'UnBind,
  1334.      debug: "Disconnect Success",
  1335.      _proto: _userproto000
  1336.     };
  1337. AddStepForm(Disconnect, Disconnect Success);
  1338.  
  1339.  
  1340.  
  1341. Disconnect Failure :=
  1342.     {viewBounds: {left: 8, top: 48, right: 96, bottom: 64},
  1343.      Action:
  1344.        func(error)
  1345.        begin
  1346.            :MNotifyError("Endpoint Disconnect", error);
  1347.        
  1348.            :DoEvent('UnBind, nil);
  1349.        end,
  1350.      declareSelf: 'DisconnectFailure,
  1351.      nextState: 'UnBind,
  1352.      fCompletionSpec:
  1353.        {    async:                    true,
  1354.            reqTimeout:            kNoTimeout,
  1355.            completionScript:    func(ep, options, result)
  1356.                                        begin
  1357.                                            if result then
  1358.                                                ep:DoEvent('UnBindFailure, [result]);
  1359.                                            else
  1360.                                                ep:DoEvent('UnBindSuccess, nil);
  1361.                                        end,    },
  1362.      debug: "Disconnect Failure",
  1363.      _proto: _userproto000
  1364.     };
  1365. AddStepForm(Disconnect, Disconnect Failure);
  1366.  
  1367.  
  1368.  
  1369. Cancel :=
  1370.     {viewBounds: {left: 8, top: 64, right: 96, bottom: 80},
  1371.      declareSelf: 'Cancel,
  1372.      Action:
  1373.        func()
  1374.        begin
  1375.            fQuiet := true;
  1376.            
  1377.            try
  1378.                fEndPoint:Cancel(nil);
  1379.            onexception |evt.ex.comm| do
  1380.                nil;
  1381.        end,
  1382.      debug: "Cancel",
  1383.      _proto: _userproto000
  1384.     };
  1385. AddStepForm(Disconnect, Cancel);
  1386.  
  1387.  
  1388.  
  1389. Power Off :=
  1390.     {viewBounds: {left: 8, top: 80, right: 96, bottom: 96},
  1391.      declareSelf: 'PowerOff,
  1392.      debug: "Power Off",
  1393.      _proto: _userproto000
  1394.     };
  1395. AddStepForm(Disconnect, Power Off);
  1396.  
  1397.  
  1398.  
  1399.  
  1400.  
  1401. UnBind :=
  1402.     {viewBounds: {left: 232, top: 208, right: 336, bottom: 312},
  1403.      declareSelf: 'UnBind,
  1404.      debug: "UnBind",
  1405.      _proto: _userproto001
  1406.     };
  1407. AddStepForm(Comms FSM, UnBind);
  1408.  
  1409. UnBind :=
  1410.     {viewBounds: {left: 8, top: 16, right: 96, bottom: 32},
  1411.      declareSelf: 'UnBind,
  1412.      Action:
  1413.        func()
  1414.        begin
  1415.            try
  1416.                fEndPoint:UnBind(fCompletionSpec);
  1417.            onexception |evt.ex| do
  1418.                :DoEvent('UnBindFailure, [CurrentException()]);
  1419.        end,
  1420.      fCompletionSpec:
  1421.        {    async:                    true,
  1422.            reqTimeout:            kNoTimeout,
  1423.            completionScript:    func(ep, options, result)
  1424.                                        begin
  1425.                                            if result then
  1426.                                                ep:DoEvent('UnBindFailure, [result]);
  1427.                                            else
  1428.                                                ep:DoEvent('UnBindSuccess, nil);
  1429.                                        end,    },
  1430.      debug: "UnBind",
  1431.      _proto: _userproto000
  1432.     };
  1433. AddStepForm(UnBind, UnBind);
  1434.  
  1435.  
  1436.  
  1437. UnBind Success :=
  1438.     {viewBounds: {left: 8, top: 32, right: 96, bottom: 48},
  1439.      Action:
  1440.        func()
  1441.        begin
  1442.            :DoEvent('Dispose, nil);
  1443.        end,
  1444.      declareSelf: 'UnBindSuccess,
  1445.      nextState: 'Dispose,
  1446.      debug: "UnBind Success",
  1447.      _proto: _userproto000
  1448.     };
  1449. AddStepForm(UnBind, UnBind Success);
  1450.  
  1451.  
  1452.  
  1453. UnBind Failure :=
  1454.     {viewBounds: {left: 8, top: 48, right: 96, bottom: 64},
  1455.      Action:
  1456.        func(exception)
  1457.        begin
  1458.            :MNotifyError("Endpoint UnBind", exception);
  1459.            
  1460.            :DoEvent('Dispose, nil);
  1461.        end,
  1462.      declareSelf: 'UnBindFailure,
  1463.      nextState: 'Dispose,
  1464.      debug: "UnBind Failure",
  1465.      _proto: _userproto000
  1466.     };
  1467. AddStepForm(UnBind, UnBind Failure);
  1468.  
  1469.  
  1470.  
  1471. Cancel :=
  1472.     {viewBounds: {left: 8, top: 64, right: 96, bottom: 80},
  1473.      declareSelf: 'Cancel,
  1474.      Action:
  1475.        func()
  1476.        begin
  1477.            fQuiet := true;
  1478.            
  1479.            try
  1480.                fEndPoint:Cancel(nil);
  1481.            onexception |evt.ex.comm| do
  1482.                nil;
  1483.        end,
  1484.      debug: "Cancel",
  1485.      _proto: _userproto000
  1486.     };
  1487. AddStepForm(UnBind, Cancel);
  1488.  
  1489.  
  1490.  
  1491. Power Off :=
  1492.     {viewBounds: {left: 8, top: 80, right: 96, bottom: 96},
  1493.      declareSelf: 'PowerOff,
  1494.      debug: "Power Off",
  1495.      _proto: _userproto000
  1496.     };
  1497. AddStepForm(UnBind, Power Off);
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503. Dispose :=
  1504.     {viewBounds: {left: 232, top: 320, right: 336, bottom: 392},
  1505.      declareSelf: 'Dispose,
  1506.      debug: "Dispose",
  1507.      _proto: _userproto001
  1508.     };
  1509. AddStepForm(Comms FSM, Dispose);
  1510.  
  1511. Dispose :=
  1512.     {viewBounds: {left: 8, top: 16, right: 96, bottom: 32},
  1513.      Action:
  1514.        func()
  1515.        begin
  1516.            try
  1517.                fEndPoint:Dispose();
  1518.            onexception |evt.ex| do
  1519.                return :DoEvent('DisposeFailure, [CurrentException()]);
  1520.            
  1521.            :DoEvent('DisposeSuccess, nil);
  1522.        end,
  1523.      declareSelf: 'Dispose,
  1524.      debug: "Dispose",
  1525.      _proto: _userproto000
  1526.     };
  1527. AddStepForm(Dispose, Dispose);
  1528.  
  1529.  
  1530.  
  1531. Dispose Success :=
  1532.     {viewBounds: {left: 8, top: 32, right: 96, bottom: 48},
  1533.      Action:
  1534.        func()
  1535.        begin
  1536.            :DoEvent('CleanUp, nil);
  1537.        end,
  1538.      declareSelf: 'DisposeSuccess,
  1539.      nextState: 'CleanUp,
  1540.      debug: "Dispose Success",
  1541.      _proto: _userproto000
  1542.     };
  1543. AddStepForm(Dispose, Dispose Success);
  1544.  
  1545.  
  1546.  
  1547. Dispose Failure :=
  1548.     {viewBounds: {left: 8, top: 48, right: 96, bottom: 64},
  1549.      Action:
  1550.        func(error)
  1551.        begin
  1552.            :MNotifyError("Endpoint Dispose", error);
  1553.            
  1554.            :DoEvent('CleanUp, nil);
  1555.        end,
  1556.      declareSelf: 'DisposeFailure,
  1557.      nextState: 'CleanUp,
  1558.      debug: "Dispose Failure",
  1559.      _proto: _userproto000
  1560.     };
  1561. AddStepForm(Dispose, Dispose Failure);
  1562.  
  1563.  
  1564.  
  1565.  
  1566.  
  1567. Clean Up :=
  1568.     {viewBounds: {left: 344, top: 16, right: 448, bottom: 56},
  1569.      declareSelf: 'CleanUp,
  1570.      debug: "Clean Up",
  1571.      _proto: _userproto001
  1572.     };
  1573. AddStepForm(Comms FSM, Clean Up);
  1574.  
  1575. Clean Up :=
  1576.     {viewBounds: {left: 8, top: 16, right: 96, bottom: 32},
  1577.      Action:
  1578.        func()
  1579.        begin
  1580.            if fPowerOffState then
  1581.                begin
  1582.                    fPowerOffState := nil;
  1583.                    PowerOffResume(kAppSymbol);
  1584.                end;
  1585.            UnRegPowerOff(kAppSymbol);
  1586.            
  1587.            fEndpoint := nil;
  1588.            fQuiet := nil;
  1589.            fState := 'Disconnected;
  1590.            fConnectAction := nil;
  1591.        end,
  1592.      declareSelf: 'CleanUp,
  1593.      nextState: 'Genesis,
  1594.      debug: "Clean Up",
  1595.      _proto: _userproto000
  1596.     };
  1597. AddStepForm(Clean Up, Clean Up);
  1598.  
  1599.  
  1600.  
  1601.  
  1602.  
  1603. // After Script for "Comms FSM"
  1604. thisView := Comms FSM;
  1605. call kFSMCleanUpFunc with (thisView);
  1606.  
  1607.  
  1608. constant |layout_CommsFSM| := Comms FSM;
  1609. // End of file CommsFSM
  1610. // Beginning of file Main.t
  1611. vMain :=
  1612.     {title: kAppName,
  1613.      viewBounds: {left: -4, top: 2, right: 224, bottom: 310},
  1614.      viewFormat: 83951953,
  1615.      FSM: nil,
  1616.      viewQuitScript:
  1617.        // must return the value of inherited:?viewQuitScript();
  1618.        func()
  1619.        begin
  1620.            FSM:DoEvent('Cancel, nil);
  1621.            FSM:DoEvent('Disconnect, nil);
  1622.            FSM:WaitForTerminal(    // this function returns NIL if FSM terminal state reached normally, TRUE if user aborted
  1623.                                                {    statusText:                        "Please wait...",                                            // message at top of status dialog
  1624.                                                    titleText:                            "FSM is executing toward terminal state.",    // message at bottom of status dialog
  1625.                                                    delayUntilStatusVisible:    2000,                        // show status slip 2 seconds after entering WaitForTerminal
  1626.                                                    delayUntilAbortVisible:        8000,                        // show abort button 8 seconds after status dialog opens
  1627.                                                    abortButtonText:                "Go Away!",    }    );        // text inside abort button
  1628.            FSM := FSM:Dispose();
  1629.            inherited:?viewQuitScript();        // this method is defined internally
  1630.        end,
  1631.      viewSetupFormScript:
  1632.        func()
  1633.        begin
  1634.            // resize to fit on all "small" newtons.
  1635.            constant kMaxWidth := 240;
  1636.            constant kMaxHeight := 336;
  1637.            
  1638.            local b := GetAppParams();
  1639.            self.viewBounds := RelBounds(b.appAreaLeft, b.appAreaTop,
  1640.                                                  MIN(b.appAreaWidth, kMaxWidth),
  1641.                                                  MIN(b.appAreaHeight, kMaxHeight));
  1642.            
  1643.            FSM := GetLayout("CommsFSM"):Instantiate();
  1644.        end,
  1645.      reorienttoscreen: ROM_DefRotateFunc,
  1646.      debug: "vMain",
  1647.      _proto: @157
  1648.     };
  1649.  
  1650. Connect :=
  1651.     {
  1652.      buttonClickScript:
  1653.        func()
  1654.        begin
  1655.            FSM:DoEvent('Create, nil);
  1656.        end,
  1657.      text: "Connect",
  1658.      viewBounds: {left: 46, top: 34, right: 114, bottom: 54},
  1659.      debug: "Connect",
  1660.      _proto: @226
  1661.     };
  1662. AddStepForm(vMain, Connect);
  1663.  
  1664.  
  1665.  
  1666. Disconnect :=
  1667.     {
  1668.      buttonClickScript:
  1669.        func()
  1670.        begin
  1671.            FSM:DoEvent('Disconnect, nil);
  1672.        end,
  1673.      text: "Disconnect",
  1674.      viewBounds: {left: 126, top: 34, right: 194, bottom: 54},
  1675.      debug: "Disconnect",
  1676.      _proto: @226
  1677.     };
  1678. AddStepForm(vMain, Disconnect);
  1679.  
  1680.  
  1681.  
  1682. Cancel :=
  1683.     {
  1684.      buttonClickScript:
  1685.        func()
  1686.        begin
  1687.            FSM:DoEvent('Cancel, nil);
  1688.        end,
  1689.      text: "Cancel",
  1690.      viewBounds: {left: 86, top: 66, right: 154, bottom: 86},
  1691.      debug: "Cancel",
  1692.      _proto: @226
  1693.     };
  1694. AddStepForm(vMain, Cancel);
  1695.  
  1696.  
  1697.  
  1698. vTraceBox :=
  1699.     {text: "",
  1700.      viewBounds: {left: 8, top: 108, right: 224, bottom: 180},
  1701.      viewJustify: 0,
  1702.      viewFont: simpleFont9,
  1703.      debug: "vTraceBox",
  1704.      _proto: @218
  1705.     };
  1706. AddStepForm(vMain, vTraceBox);
  1707. StepDeclare(vMain, vTraceBox, 'vTraceBox);
  1708.  
  1709.  
  1710.  
  1711.  
  1712. constant |layout_Main.t| := vMain;
  1713. // End of file Main.t
  1714.  
  1715.  
  1716.  
  1717.