// File: /workspace/github_c_makefile_star2_O2/schroeder-lvb[P]sissm/src/sissm.c
// Function: sissmMainLoop
int sissmMainLoop( void )
{
    int errCode = 0;
    int masterState = SM_STARTUP;
    ftrackObj *fPtr = NULL;
    char strBuffer[4096];
    unsigned long timePrev;          // for tracking periodic 1.0 Hz call


    // Log some general info
    //
    logPrintf(LOG_LEVEL_INFO, "sissm", "Server ID: ::%s::", sissmConfig.serverName );
    if ( sissmConfig.gracefulExit ) 
        logPrintf(LOG_LEVEL_CRITICAL, "sissm", "** Warning: Console '^C' may take several seconds to process");
    logPrintf(LOG_LEVEL_INFO, "sissm", "State - SM_STARTUP" );

    timePrev = (unsigned long) time( NULL );

    for ( ;; ) {

        //  0. Synchronized SIGTERM exit process
        //
        if ( gracefulKill ) {
            logPrintf( LOG_LEVEL_CRITICAL, "sissm", "######## SIGTERM Exit #######" );
            break;
        }

	//  1. Server restart supercedure
	//
        if (sissmServerRestartPending()) masterState = SM_SYS_RESTART;     // restart supercedure

        //  2. Logfile tracking state machine
        //   
        switch ( masterState ) {
        case SM_STARTUP:
            logPrintf(LOG_LEVEL_INFO, "sissm", "State - SM_STARTUP");
            if ( fPtr == NULL ) {
                sleep( 1 );
                fPtr = ftrackOpen( sissmConfig.gameLogFile );
            }
            if (fPtr != NULL)  {
                logPrintf( LOG_LEVEL_CRITICAL, "sissm", "Tracking game logfile ::%s::", 
                    sissmConfig.gameLogFile );
                ftrackTailOfFile( fPtr, strBuffer, sizeof( strBuffer ), 1 );       // seek to end
                masterState = SM_POLLING_INIT;
            }
            else {
                logPrintf( LOG_LEVEL_CRITICAL, "sissm", "Trying to open game logfile ::%s::", 
                    sissmConfig.gameLogFile );
            }
            break;
        case SM_POLLING_INIT:
            logPrintf(LOG_LEVEL_INFO, "sissm", "State - SM_POLLING_INIT->TRACKING");
            eventsDispatch( "~INIT~" );
            ftrackResync( fPtr );      //      check for log file rotate and follow
            masterState = SM_POLLING_TRACKING;
            break;
        case SM_POLLING_TRACKING:
            if ( 0 == ftrackTailOfFile( fPtr, strBuffer, sizeof( strBuffer ), 0 ) ) {
                logPrintf(LOG_LEVEL_RAWDUMP, "sissm", "::%s::", strBuffer);
                eventsDispatch( strBuffer );
            }
            else { 
                usleep( SISSM_POLLING_INTERVAL_MICROSEC );
            }
            break;
        case SM_SYS_RESTART:
            logPrintf(LOG_LEVEL_INFO, "sissm", "State - SM_SYS_RESTART");
            eventsDispatch( "~RESTART~" );
            ftrackClose( fPtr );
            fPtr = NULL;
            sissmRestartServer();
            masterState = SM_STARTUP;
            break;
        default:
            ;;
        }

        // 3. periodic callbacks and alarms processing
	// currently at 1.0Hz polling rate
        //   
        if ( timePrev != (unsigned long int) time( NULL ) ) {
	    alarmDispatch();
            eventsDispatch( "~PERIODIC~" );
            timePrev = (unsigned int) time( NULL );
            ftrackResync( fPtr );                          // check for log file rotate and follow
        }
    }

    // 4. graceful exit
    // This only happens if "sissm.gracefulExit" flag is set in the .cfg file.
    // Before SISSM shuts down, generate a synthetic event to all plugins so that 
    // they can take actions to leave the server in a playable state without SISSM.
    //
    eventsDispatch( "~SIGTERM~" );

    return errCode;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1008, %rsp           # imm = 0x1008
leaq 0x115e0(%rip), %rsi     # 0x1a1a4
leaq 0x11676(%rip), %rdx     # 0x1a241
leaq 0x21746(%rip), %rcx     # 0x2a318
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x1920b
cmpl $0x0, 0x22f3d(%rip)     # 0x2bb20
je 0x8bfc
leaq 0x115b8(%rip), %rsi     # 0x1a1a4
leaq 0x11660(%rip), %rdx     # 0x1a253
xorl %edi, %edi
xorl %eax, %eax
callq 0x1920b
leaq 0x115a1(%rip), %rbx     # 0x1a1a4
leaq 0x11686(%rip), %rdx     # 0x1a290
pushq $0x2
popq %rbp
movl %ebp, %edi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1920b
xorl %edi, %edi
callq 0x82a0
movq %rax, %r14
movl $0x3e9, %r15d           # imm = 0x3E9
leaq 0x11711(%rip), %r12     # 0x1a341
xorl %r13d, %r13d
cmpl $0x0, 0x212c6(%rip)     # 0x29f00
jne 0x8de9
cmpb $0x1, 0x230e9(%rip)     # 0x2bd30
jne 0x8c86
movb $0x0, 0x230e0(%rip)     # 0x2bd30
movl %ebp, %edi
movq %rbx, %rsi
leaq 0x116ce(%rip), %rdx     # 0x1a32a
xorl %eax, %eax
callq 0x1920b
movq %r12, %rdi
callq 0xcc89
movq %r13, %rdi
callq 0xebee
callq 0x8a11
movl $0x3e9, %r15d           # imm = 0x3E9
xorl %r13d, %r13d
jmp 0x8d92
cmpl $0xfa2, %r15d           # imm = 0xFA2
je 0x8d48
cmpl $0xbb9, %r15d           # imm = 0xBB9
je 0x8c50
cmpl $0xfa1, %r15d           # imm = 0xFA1
je 0x8d1f
cmpl $0x3e9, %r15d           # imm = 0x3E9
jne 0x8d92
movl %ebp, %edi
movq %rbx, %rsi
leaq 0x115d2(%rip), %rdx     # 0x1a290
xorl %eax, %eax
callq 0x1920b
testq %r13, %r13
jne 0x8cea
pushq $0x1
popq %rdi
callq 0x83b0
leaq 0x22243(%rip), %rdi     # 0x2af1c
callq 0xe9c8
movq %rax, %r13
testq %rax, %rax
je 0x8dca
xorl %edi, %edi
movq %rbx, %rsi
leaq 0x115cb(%rip), %rdx     # 0x1a2c1
leaq 0x2221f(%rip), %rcx     # 0x2af1c
xorl %eax, %eax
callq 0x1920b
movq %r13, %rdi
movq %rsp, %rsi
movl $0x1000, %edx           # imm = 0x1000
pushq $0x1
popq %rcx
callq 0xec0d
movl $0xfa1, %r15d           # imm = 0xFA1
jmp 0x8d92
movl %ebp, %edi
movq %rbx, %rsi
leaq 0x115d6(%rip), %rdx     # 0x1a301
xorl %eax, %eax
callq 0x1920b
leaq 0x115ea(%rip), %rdi     # 0x1a323
callq 0xcc89
movq %r13, %rdi
callq 0xeaef
jmp 0x8d8c
movq %r13, %rdi
movq %rsp, %r15
movq %r15, %rsi
movl $0x1000, %edx           # imm = 0x1000
xorl %ecx, %ecx
callq 0xec0d
testl %eax, %eax
je 0x8d6d
movl $0x2710, %edi           # imm = 0x2710
callq 0x8400
jmp 0x8d8c
pushq $0x4
popq %rdi
movq %rbx, %rsi
leaq 0x11f5c(%rip), %rdx     # 0x1acd6
movq %r15, %rcx
xorl %eax, %eax
callq 0x1920b
movq %r15, %rdi
callq 0xcc89
movl $0xfa2, %r15d           # imm = 0xFA2
xorl %edi, %edi
callq 0x82a0
cmpq %rax, %r14
je 0x8c33
callq 0xf1ff
leaq 0x1159d(%rip), %rdi     # 0x1a34b
callq 0xcc89
xorl %edi, %edi
callq 0x82a0
movl %eax, %r14d
movq %r13, %rdi
callq 0xeaef
jmp 0x8c33
xorl %edi, %edi
movq %rbx, %rsi
leaq 0x11508(%rip), %rdx     # 0x1a2de
leaq 0x2213f(%rip), %rcx     # 0x2af1c
xorl %eax, %eax
callq 0x1920b
jmp 0x8c78
leaq 0x113b4(%rip), %rsi     # 0x1a1a4
leaq 0x114ac(%rip), %rdx     # 0x1a2a3
xorl %edi, %edi
xorl %eax, %eax
callq 0x1920b
leaq 0x1154f(%rip), %rdi     # 0x1a356
callq 0xcc89
xorl %eax, %eax
addq $0x1008, %rsp           # imm = 0x1008
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq