function dispatcher()

in Clients/AmbrosiaJS/PTI-Node/App/src/PublisherFramework.g.ts [80:255]


function dispatcher(message: Messages.DispatchedMessage): void
{
    const loggingPrefix: string = "Dispatcher";

    try
    {
        switch (message.type)
        {
            case Messages.DispatchedMessageType.RPC:
                let rpc: Messages.IncomingRPC = message as Messages.IncomingRPC;

                switch (rpc.methodID)
                {
                    case IC.POST_METHOD_ID:
                        try
                        {
                            let methodName: string = IC.getPostMethodName(rpc);
                            let methodVersion: number = IC.getPostMethodVersion(rpc); // Use this to do version-specific method behavior
                    
                            switch (methodName)
                            {
                                case "incrementValue":
                                    {
                                        const value: number = IC.getPostMethodArg(rpc, "value");
                                        IC.postResult<number>(rpc, PTM.ServerAPI.incrementValue(value));
                                    }
                                    break;
                                
                                default:
                                    {
                                        let errorMsg: string = `Post method '${methodName}' is not implemented`;
                                        Utils.log(`(${errorMsg})`, loggingPrefix)
                                        IC.postError(rpc, new Error(errorMsg));
                                    }
                                    break;
                            }
                        }
                        catch (error: unknown)
                        {
                            const err: Error = Utils.makeError(error);
                            Utils.log(err);
                            IC.postError(rpc, err);
                        }
                        break;

                    case 1:
                        {
                            const rawParams: Uint8Array = rpc.getRawParams();
                            PTM.ServerAPI.doWork(rawParams);
                        }
                        break;
                    
                    case 2:
                        {
                            const isFinalState: boolean = rpc.getJsonParam("isFinalState");
                            PTM.ServerAPI.reportState(isFinalState);
                        }
                        break;
                    
                    case 3:
                        {
                            const currentTime: number = rpc.getJsonParam("currentTime");
                            PTM.ServerAPI.checkHealth(currentTime);
                        }
                        break;
                    
                    case 4:
                        {
                            const numRPCBytes: number = rpc.getJsonParam("numRPCBytes");
                            const iterationWithinRound: number = rpc.getJsonParam("iterationWithinRound");
                            const startTimeOfRound: number = rpc.getJsonParam("startTimeOfRound");
                            PTM.ClientAPI.continueSendingMessages(numRPCBytes, iterationWithinRound, startTimeOfRound);
                        }
                        break;
                    
                    case 5:
                        {
                            const rawParams: Uint8Array = rpc.getRawParams();
                            PTM.ClientAPI.doWorkEcho(rawParams);
                        }
                        break;
                    
                    default:
                        Utils.log(`Error: Method dispatch failed (reason: No method is associated with methodID ${rpc.methodID})`);
                        break;
                }
                break;

            case Messages.DispatchedMessageType.AppEvent:
                let appEvent: Messages.AppEvent = message as Messages.AppEvent;
                
                switch (appEvent.eventType)
                {
                    case Messages.AppEventType.ICStarting:
                        // TODO: Add an exported [non-async] function 'onICStarting(): void' to ./PTI.ts, then (after the next code-gen) a call to it will be generated here
                        break;

                    case Messages.AppEventType.ICStarted:
                        // TODO: Add an exported [non-async] function 'onICStarted(): void' to ./PTI.ts, then (after the next code-gen) a call to it will be generated here
                        break;

                    case Messages.AppEventType.ICConnected:
                        // Note: Types and methods are published in this handler so that they're available regardless of the 'icHostingMode'
                        // Code-gen: Published types will go here
                        Meta.publishMethod(1, "doWork", ["rawParams: Uint8Array"]);
                        Meta.publishMethod(2, "reportState", ["isFinalState: boolean"]);
                        Meta.publishMethod(3, "checkHealth", ["currentTime: number"]);
                        Meta.publishPostMethod("incrementValue", 1, ["value: number"], "number");
                        Meta.publishMethod(4, "continueSendingMessages", ["numRPCBytes: number", "iterationWithinRound: number", "startTimeOfRound: number"]);
                        Meta.publishMethod(5, "doWorkEcho", ["rawParams: Uint8Array"]);
                        PTM.EventHandlers.onICConnected();
                        break;

                    case Messages.AppEventType.ICStopped:
                        // TODO: Add an exported [non-async] function 'onICStopped(exitCode: number): void' to ./PTI.ts, then (after the next code-gen) a call to it will be generated here
                        break;

                    case Messages.AppEventType.ICReadyForSelfCallRpc:
                        // TODO: Add an exported [non-async] function 'onICReadyForSelfCallRpc(): void' to ./PTI.ts, then (after the next code-gen) a call to it will be generated here
                        break;
    
                    case Messages.AppEventType.RecoveryComplete:
                        PTM.EventHandlers.onRecoveryComplete();
                        break;

                    case Messages.AppEventType.UpgradeState:
                        {
                            const upgradeMode: Messages.AppUpgradeMode = appEvent.args[0];
                            PTM.EventHandlers.onUpgradeState(upgradeMode);
                        }
                        break;

                    case Messages.AppEventType.UpgradeCode:
                        {
                            const upgradeMode: Messages.AppUpgradeMode = appEvent.args[0];
                            PTM.EventHandlers.onUpgradeCode(upgradeMode);
                        }
                        break;

                    case Messages.AppEventType.IncomingCheckpointStreamSize:
                        // TODO: Add an exported [non-async] function 'onIncomingCheckpointStreamSize(): void' to ./PTI.ts, then (after the next code-gen) a call to it will be generated here
                        break;
                    
                    case Messages.AppEventType.FirstStart:
                        PTM.EventHandlers.onFirstStart();
                        break;

                    case Messages.AppEventType.BecomingPrimary:
                        PTM.EventHandlers.onBecomingPrimary();
                        break;
                    
                    case Messages.AppEventType.CheckpointLoaded:
                        {
                            const checkpointSizeInBytes: number = appEvent.args[0];
                            PTM.EventHandlers.onCheckpointLoaded(checkpointSizeInBytes);
                        }
                        break;

                    case Messages.AppEventType.CheckpointSaved:
                        PTM.EventHandlers.onCheckpointSaved();
                        break;

                    case Messages.AppEventType.UpgradeComplete:
                        PTM.EventHandlers.onUpgradeComplete();
                        break;
                }
                break;
        }
    }
    catch (error: unknown)
    {
        let messageName: string = (message.type === Messages.DispatchedMessageType.AppEvent) ? `AppEvent:${Messages.AppEventType[(message as Messages.AppEvent).eventType]}` : Messages.DispatchedMessageType[message.type];
        Utils.log(`Error: Failed to process ${messageName} message`);
        Utils.log(Utils.makeError(error));
    }
}