Non Blocking Channel Operations in ActionScript

Our first example demonstrates non-blocking channel operations. In ActionScript, we don’t have built-in channels or select statements, but we can simulate similar behavior using timers and event listeners.

package {
    import flash.display.Sprite;
    import flash.events.TimerEvent;
    import flash.utils.Timer;

    public class NonBlockingOperations extends Sprite {
        private var messagesTimer:Timer;
        private var signalsTimer:Timer;

        public function NonBlockingOperations() {
            messagesTimer = new Timer(1000);
            signalsTimer = new Timer(1500);

            // Simulate non-blocking receive
            checkMessages();

            // Simulate non-blocking send
            var msg:String = "hi";
            sendMessage(msg);

            // Simulate multi-way non-blocking select
            checkMultipleChannels();
        }

        private function checkMessages():void {
            if (Math.random() < 0.5) {
                trace("received message: some message");
            } else {
                trace("no message received");
            }
        }

        private function sendMessage(msg:String):void {
            if (Math.random() < 0.5) {
                trace("sent message", msg);
            } else {
                trace("no message sent");
            }
        }

        private function checkMultipleChannels():void {
            messagesTimer.addEventListener(TimerEvent.TIMER, onMessageReceived);
            signalsTimer.addEventListener(TimerEvent.TIMER, onSignalReceived);

            messagesTimer.start();
            signalsTimer.start();

            function onMessageReceived(event:TimerEvent):void {
                trace("received message: some message");
                cleanupTimers();
            }

            function onSignalReceived(event:TimerEvent):void {
                trace("received signal: some signal");
                cleanupTimers();
            }

            function cleanupTimers():void {
                messagesTimer.stop();
                signalsTimer.stop();
                messagesTimer.removeEventListener(TimerEvent.TIMER, onMessageReceived);
                signalsTimer.removeEventListener(TimerEvent.TIMER, onSignalReceived);
            }

            // Simulate the default case (no activity) after a short delay
            var defaultTimer:Timer = new Timer(500, 1);
            defaultTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onDefaultCase);
            defaultTimer.start();

            function onDefaultCase(event:TimerEvent):void {
                trace("no activity");
                cleanupTimers();
            }
        }
    }
}

In this ActionScript example, we simulate non-blocking channel operations using timers and random number generation:

  1. The checkMessages() function simulates a non-blocking receive operation. It randomly decides whether a message was received or not.

  2. The sendMessage() function simulates a non-blocking send operation. It randomly decides whether the message was sent successfully or not.

  3. The checkMultipleChannels() function simulates a multi-way non-blocking select operation. It sets up two timers (messagesTimer and signalsTimer) to represent different channels. The first timer to fire will trigger its corresponding event, simulating a received message or signal. A third timer represents the default case, which occurs if neither of the other timers fires within a short time frame.

To run this ActionScript code, you would typically compile it into a SWF file and run it in a Flash Player or AIR runtime environment. The output would be similar to:

no message received
no message sent
received message: some message

or

no message received
sent message hi
no activity

The actual output may vary due to the random nature of the simulated operations.

This example demonstrates how to achieve non-blocking behavior in ActionScript, even though the language doesn’t have built-in constructs for channels or select statements like some other languages do.