// File: /workspace/github_c_makefile_star2_O3/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 0x125c3(%rip), %rsi     # 0x1b1a4
leaq 0x12659(%rip), %rdx     # 0x1b241
leaq 0x21729(%rip), %rcx     # 0x2a318
movl $0x2, %edi
xorl %eax, %eax
callq 0x1a3e3
cmpl $0x0, 0x22f1e(%rip)     # 0x2bb20
je 0x8c1b
leaq 0x12599(%rip), %rsi     # 0x1b1a4
leaq 0x12641(%rip), %rdx     # 0x1b253
xorl %edi, %edi
xorl %eax, %eax
callq 0x1a3e3
leaq 0x12582(%rip), %rsi     # 0x1b1a4
leaq 0x12667(%rip), %rdx     # 0x1b290
movl $0x2, %edi
xorl %eax, %eax
callq 0x1a3e3
xorl %edi, %edi
callq 0x82a0
cmpl $0x0, 0x212bd(%rip)     # 0x29f00
je 0x8c7c
leaq 0x12558(%rip), %rsi     # 0x1b1a4
leaq 0x12650(%rip), %rdx     # 0x1b2a3
xorl %edi, %edi
xorl %eax, %eax
callq 0x1a3e3
leaq 0x126f3(%rip), %rdi     # 0x1b356
callq 0xd4f0
xorl %eax, %eax
addq $0x1008, %rsp           # imm = 0x1008
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbp
movl $0x3e9, %r12d           # imm = 0x3E9
leaq 0x12518(%rip), %rbx     # 0x1b1a4
leaq 0x12697(%rip), %r14     # 0x1b32a
leaq 0x126a7(%rip), %r15     # 0x1b341
xorl %r13d, %r13d
cmpb $0x1, 0x2308c(%rip)     # 0x2bd30
jne 0x8ce2
movb $0x0, 0x23083(%rip)     # 0x2bd30
movl $0x2, %edi
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1a3e3
movq %r15, %rdi
callq 0xd4f0
movq %r13, %rdi
callq 0xf611
callq 0x8a10
movl $0x3e9, %r12d           # imm = 0x3E9
xorl %r13d, %r13d
jmp 0x8e0f
cmpl $0xfa0, %r12d           # imm = 0xFA0
jg 0x8d02
cmpl $0x3e9, %r12d           # imm = 0x3E9
je 0x8d48
cmpl $0xbb9, %r12d           # imm = 0xBB9
je 0x8cad
jmp 0x8e0f
cmpl $0xfa1, %r12d           # imm = 0xFA1
je 0x8dbc
cmpl $0xfa2, %r12d           # imm = 0xFA2
jne 0x8e0f
movq %r13, %rdi
movq %rsp, %r12
movq %r12, %rsi
movl $0x1000, %edx           # imm = 0x1000
xorl %ecx, %ecx
callq 0xf630
testl %eax, %eax
je 0x8de8
movl $0x2710, %edi           # imm = 0x2710
callq 0x83f0
jmp 0x8e09
movl $0x2, %edi
movq %rbx, %rsi
leaq 0x12539(%rip), %rdx     # 0x1b290
xorl %eax, %eax
callq 0x1a3e3
testq %r13, %r13
jne 0x8d85
movl $0x1, %edi
callq 0x83a0
leaq 0x221a8(%rip), %rdi     # 0x2af1c
callq 0xf3d0
movq %rax, %r13
testq %rax, %rax
je 0x8e4f
xorl %edi, %edi
movq %rbx, %rsi
leaq 0x12530(%rip), %rdx     # 0x1b2c1
leaq 0x22184(%rip), %rcx     # 0x2af1c
xorl %eax, %eax
callq 0x1a3e3
movq %r13, %rdi
movq %rsp, %rsi
movl $0x1000, %edx           # imm = 0x1000
movl $0x1, %ecx
callq 0xf630
movl $0xfa1, %r12d           # imm = 0xFA1
jmp 0x8e0f
movl $0x2, %edi
movq %rbx, %rsi
leaq 0x12536(%rip), %rdx     # 0x1b301
xorl %eax, %eax
callq 0x1a3e3
leaq 0x1254a(%rip), %rdi     # 0x1b323
callq 0xd4f0
movq %r13, %rdi
callq 0xf4fe
jmp 0x8e09
movl $0x4, %edi
movq %rbx, %rsi
leaq 0x12edf(%rip), %rdx     # 0x1bcd6
movq %r12, %rcx
xorl %eax, %eax
callq 0x1a3e3
movq %r12, %rdi
callq 0xd4f0
movl $0xfa2, %r12d           # imm = 0xFA2
xorl %edi, %edi
callq 0x82a0
cmpq %rax, %rbp
je 0x8e3d
callq 0xfce9
leaq 0x12524(%rip), %rdi     # 0x1b34b
callq 0xd4f0
xorl %edi, %edi
callq 0x82a0
movl %eax, %ebp
movq %r13, %rdi
callq 0xf4fe
cmpl $0x0, 0x210bc(%rip)     # 0x29f00
je 0x8c9d
jmp 0x8c45
xorl %edi, %edi
movq %rbx, %rsi
leaq 0x12483(%rip), %rdx     # 0x1b2de
leaq 0x220ba(%rip), %rcx     # 0x2af1c
xorl %eax, %eax
callq 0x1a3e3
jmp 0x8cd4