// File: /workspace/github_c_makefile_star2_O1/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 0x125d7(%rip), %rsi     # 0x1b1a4
leaq 0x1266d(%rip), %rdx     # 0x1b241
leaq 0x2173d(%rip), %rcx     # 0x2a318
movl $0x2, %edi
xorl %eax, %eax
callq 0x1a227
cmpl $0x0, 0x22f32(%rip)     # 0x2bb20
je 0x8c07
leaq 0x125ad(%rip), %rsi     # 0x1b1a4
leaq 0x12655(%rip), %rdx     # 0x1b253
xorl %edi, %edi
xorl %eax, %eax
callq 0x1a227
leaq 0x12596(%rip), %rsi     # 0x1b1a4
leaq 0x1267b(%rip), %rdx     # 0x1b290
movl $0x2, %edi
xorl %eax, %eax
callq 0x1a227
xorl %edi, %edi
callq 0x82a0
cmpl $0x0, 0x212d1(%rip)     # 0x29f00
jne 0x8e20
movq %rax, %rbp
movl $0x3e9, %r15d           # imm = 0x3E9
leaq 0x1255f(%rip), %rbx     # 0x1b1a4
movq %rsp, %r12
leaq 0x126fc(%rip), %r13     # 0x1b34b
xorl %r14d, %r14d
cmpb $0x1, 0x230d7(%rip)     # 0x2bd30
jne 0x8c68
movb $0x0, 0x230ce(%rip)     # 0x2bd30
movl $0xbb9, %r15d           # imm = 0xBB9
cmpl $0xfa0, %r15d           # imm = 0xFA0
jg 0x8cbf
cmpl $0x3e9, %r15d           # imm = 0x3E9
je 0x8d02
cmpl $0xbb9, %r15d           # imm = 0xBB9
jne 0x8de9
movl $0x2, %edi
movq %rbx, %rsi
leaq 0x12690(%rip), %rdx     # 0x1b32a
xorl %eax, %eax
callq 0x1a227
leaq 0x12699(%rip), %rdi     # 0x1b341
callq 0xd1bc
movq %r14, %rdi
callq 0xf2d1
callq 0x8a09
jmp 0x8de0
cmpl $0xfa1, %r15d           # imm = 0xFA1
je 0x8d76
cmpl $0xfa2, %r15d           # imm = 0xFA2
jne 0x8de9
movq %r14, %rdi
movq %r12, %rsi
movl $0x1000, %edx           # imm = 0x1000
xorl %ecx, %ecx
callq 0xf2f6
testl %eax, %eax
je 0x8da2
movl $0x2710, %edi           # imm = 0x2710
callq 0x83f0
jmp 0x8dc3
movl $0x2, %edi
movq %rbx, %rsi
leaq 0x1257f(%rip), %rdx     # 0x1b290
xorl %eax, %eax
callq 0x1a227
testq %r14, %r14
jne 0x8d36
movl $0x1, %edi
callq 0x83a0
leaq 0x221ee(%rip), %rdi     # 0x2af1c
callq 0xf098
movq %rax, %r14
xorl %edi, %edi
movq %rbx, %rsi
testq %r14, %r14
je 0x8dcb
leaq 0x12576(%rip), %rdx     # 0x1b2c1
leaq 0x221ca(%rip), %rcx     # 0x2af1c
xorl %eax, %eax
callq 0x1a227
movq %r14, %rdi
movq %r12, %rsi
movl $0x1000, %edx           # imm = 0x1000
movl $0x1, %ecx
callq 0xf2f6
movl $0xfa1, %r15d           # imm = 0xFA1
jmp 0x8de9
movl $0x2, %edi
movq %rbx, %rsi
leaq 0x1257c(%rip), %rdx     # 0x1b301
xorl %eax, %eax
callq 0x1a227
leaq 0x12590(%rip), %rdi     # 0x1b323
callq 0xd1bc
movq %r14, %rdi
callq 0xf1c6
jmp 0x8dc3
movl $0x4, %edi
movq %rbx, %rsi
leaq 0x12f2d(%rip), %rdx     # 0x1bcde
movq %r12, %rcx
xorl %eax, %eax
callq 0x1a227
movq %r12, %rdi
callq 0xd1bc
movl $0xfa2, %r15d           # imm = 0xFA2
jmp 0x8de9
leaq 0x1250c(%rip), %rdx     # 0x1b2de
leaq 0x22143(%rip), %rcx     # 0x2af1c
xorl %eax, %eax
callq 0x1a227
movl $0x3e9, %r15d           # imm = 0x3E9
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x82a0
cmpq %rax, %rbp
je 0x8e13
callq 0xf9a4
movq %r13, %rdi
callq 0xd1bc
xorl %edi, %edi
callq 0x82a0
movl %eax, %ebp
movq %r14, %rdi
callq 0xf1c6
cmpl $0x0, 0x210e6(%rip)     # 0x29f00
je 0x8c52
leaq 0x1237d(%rip), %rsi     # 0x1b1a4
leaq 0x12475(%rip), %rdx     # 0x1b2a3
xorl %edi, %edi
xorl %eax, %eax
callq 0x1a227
leaq 0x12518(%rip), %rdi     # 0x1b356
callq 0xd1bc
xorl %eax, %eax
addq $0x1008, %rsp           # imm = 0x1008
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq