// File: /workspace/github_cpp_makefile_star2_O3/merelin[P]mediasmartserverd/src/device_monitor.cpp
// Function: DeviceMonitor::Main()
void DeviceMonitor::Main( ) {
	assert( dev_monitor_ );
	
	const int fd_mon = udev_monitor_get_fd( dev_monitor_ );
	const int nfds = fd_mon + 1;
        int queue_tok = 8;
	
	sigset_t sigempty;
	sigemptyset( &sigempty );

        struct timespec timeout;

        if( activity )
        {
            timeout.tv_sec = 0;
            //timeout.tv_sec = 1;
            timeout.tv_nsec = 100000000;
            //timeout.tv_nsec = 0;
        }
        else
        {
            timeout.tv_sec = 999;
            timeout.tv_nsec = 0;
        }

	while ( true ) {
		fd_set fds_read;
		FD_ZERO( &fds_read );
		FD_SET( fd_mon, &fds_read );
		
		// block for something interesting to happen
		int res = pselect( nfds, &fds_read, 0, 0, &timeout, &sigempty );
		if ( res < 0 ) {
			if ( EINTR != errno ) throw ErrnoException( "select" );
			std::cout << "Exiting on signal\n";
			return; // signalled
		}
		
		// udev monitor notification?
		if ( FD_ISSET( fd_mon, &fds_read ) ) {
			std::tr1::shared_ptr< udev_device > device( udev_monitor_receive_device( dev_monitor_ ), &udev_device_unref );
                        // make sure this is a device we want to monitor
			if (!acceptDevice_(device.get())) continue;
	
			const char* str = udev_device_get_action( device.get() );
			if ( !str ) {
			} else if ( 0 == strcasecmp( str, "add" ) ) {
				deviceAdded_( device.get() );
			} else if ( 0 == strcasecmp( str, "remove" ) ) {
				deviceRemove_( device.get() );
			} else {
				if ( debug ) {
					std::cout << "action: " << str << '\n';
					std::cout << ' ' << udev_device_get_syspath(device.get()) << "' (" << udev_device_get_subsystem(device.get()) << ")\n";
				}
			}
		}

                if( activity )
                {
                    for( int i = 0; i < num_disks_; ++i )
                    {
                        std::ifstream stats;
                        stats.open( statsFile(i).c_str() );

                        char buf[256];
                        stats.getline( &(buf[0]), 255 );
                        std::string s(&(buf[0]));

                        // tokenize the string
                        std::string::size_type last = s.find_first_not_of( " ", 0 );
                        std::string::size_type pos = s.find_first_of( " ", last );

                        int tok = 0;
                        int queue_length = 0;
                        while( std::string::npos != pos || std::string::npos != last )
                        {
                            last = s.find_first_not_of(" ", pos );
                            pos = s.find_first_of( " ", last );
                            ++tok;

                            if( tok == queue_tok )
                            {
                                queue_length = atoi(s.substr( last, pos - last ).c_str());
                                if( debug )
                                    std::cout << " " << i << " " << queue_length;
                                break;
                            }
                        }

                        int led_idx = ledIndex(i);

                        if( led_enabled_[led_idx] && leds_ )
                        {
                            if( queue_length > 0 )
                            {
                                leds_->Set( LED_BLUE, led_idx, true );
                                leds_->Set( LED_RED, led_idx, true );
                            }
                            else
                            {
                                leds_->Set( LED_BLUE, led_idx, true );
                                leds_->Set( LED_RED, led_idx, false );
                            }
                        }
                    }
                }
                if( debug )
                    std::cout << "\n";
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
movq %rdi, %rbx
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
je 0x542d
callq 0x4320
movl %eax, %r15d
leal 0x1(%r15), %ebp
leaq 0x408(%rsp), %r14
movq %r14, %rdi
callq 0x4390
leaq 0xb4e2(%rip), %rcx      # 0x10358
xorl %eax, %eax
cmpb $0x0, (%rcx)
movl $0x3e7, %ecx            # imm = 0x3E7
cmovneq %rax, %rcx
movl $0x5f5e100, %edx        # imm = 0x5F5E100
cmoveq %rax, %rdx
leaq 0x60(%rsp), %r8
movq %rcx, (%r8)
leal 0x3f(%r15), %eax
testl %r15d, %r15d
cmovnsl %r15d, %eax
movl %eax, %ecx
andl $-0x40, %ecx
subl %ecx, %r15d
movl %r15d, %ecx
movl $0x1, %r15d
shlq %cl, %r15
movq %rdx, 0x8(%r8)
sarl $0x6, %eax
movslq %eax, %r12
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rsi
movaps %xmm0, 0x70(%rsi)
movaps %xmm0, 0x60(%rsi)
movaps %xmm0, 0x50(%rsi)
movaps %xmm0, 0x40(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
orq %r15, 0x70(%rsp,%r12,8)
movl %ebp, 0x4(%rsp)
movl %ebp, %edi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r14, %r9
callq 0x42e0
testl %eax, %eax
js 0x53f9
leaq 0x28(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq 0x10(%rsp), %r13
leaq 0x659f(%rip), %r14      # 0xb4b8
movq %r15, 0x8(%rsp)
movq %r12, 0x30(%rsp)
testq %r15, 0x70(%rsp,%r12,8)
je 0x513b
movq 0x8(%rbx), %rdi
callq 0x40e0
movq %rax, 0xf8(%rsp)
leaq 0x100(%rsp), %rdi
movq %rax, %rsi
movq 0xb077(%rip), %rdx      # 0xffc8
callq 0x5f26
movq 0xf8(%rsp), %rsi
callq 0x553a
testb %al, %al
je 0x5066
movq 0xf8(%rsp), %rdi
callq 0x4050
movq %rax, %r12
testq %rax, %rax
je 0x5129
movq %r12, %rdi
leaq 0x615a(%rip), %rsi      # 0xb0e8
callq 0x42f0
testl %eax, %eax
je 0x5078
movq %r12, %rdi
leaq 0x6147(%rip), %rsi      # 0xb0ec
callq 0x42f0
testl %eax, %eax
je 0x508d
leaq 0xb397(%rip), %rax      # 0x10350
cmpl $0x0, (%rax)
je 0x5129
movl $0x8, %edx
movq 0xafe2(%rip), %rdi      # 0xffb0
leaq 0x611e(%rip), %rsi      # 0xb0f3
callq 0x4310
movq %r12, %rdi
callq 0x4130
movq 0xafc7(%rip), %rdi      # 0xffb0
movq %r12, %rsi
movq %rax, %rdx
callq 0x4310
movb $0xa, 0x300(%rsp)
movl $0x1, %edx
movq 0xafa8(%rip), %rdi      # 0xffb0
leaq 0x300(%rsp), %rsi
callq 0x4310
movb $0x20, 0x300(%rsp)
movl $0x1, %edx
movq 0xaf87(%rip), %rdi      # 0xffb0
leaq 0x300(%rsp), %rsi
callq 0x4310
movq %rax, %rbp
movq 0xf8(%rsp), %rdi
callq 0x44e0
movq %rax, %r15
testq %rax, %rax
je 0x50a2
movq %r15, %rdi
callq 0x4130
movq %rbp, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x4310
jmp 0x50ba
leaq 0x100(%rsp), %rdi
callq 0x5d10
jmp 0x5388
movq 0xf8(%rsp), %rsi
movq %rbx, %rdi
callq 0x558a
jmp 0x5129
movq 0xf8(%rsp), %rsi
movq %rbx, %rdi
callq 0x568a
jmp 0x5129
movq (%rbp), %rax
movq -0x18(%rax), %rax
leaq (%rax,%rbp), %rdi
movl 0x20(%rbp,%rax), %esi
orl $0x1, %esi
callq 0x4510
movl $0x3, %edx
movq %rbp, %rdi
leaq 0x6033(%rip), %rsi      # 0xb0fc
callq 0x4310
movq 0xf8(%rsp), %rdi
callq 0x40b0
movq %rax, %r15
testq %rax, %rax
je 0x50fb
movq %r15, %rdi
callq 0x4130
movq %rbp, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x4310
jmp 0x5115
movq (%rbp), %rax
movq -0x18(%rax), %rax
movq %rbp, %rdi
addq %rax, %rdi
movl 0x20(%rbp,%rax), %esi
orl $0x1, %esi
callq 0x4510
movl $0x2, %edx
movq %rbp, %rdi
leaq 0x673c(%rip), %rsi      # 0xb860
callq 0x4310
leaq 0x100(%rsp), %rdi
callq 0x5d10
movq 0x8(%rsp), %r15
leaq 0xb216(%rip), %rax      # 0x10358
cmpb $0x1, (%rax)
leaq 0xf8(%rsp), %r12
jne 0x5364
cmpl $0x0, 0x20(%rbx)
jle 0x5364
xorl %ebp, %ebp
movq %r12, %rdi
callq 0x4300
movq %rbp, %rax
shlq $0x5, %rax
movq 0x38(%rsp), %rcx
movq (%rcx,%rax), %rsi
movq %r12, %rdi
movl $0x8, %edx
callq 0x4180
movq 0xf8(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x4280
leaq 0x300(%rsp), %r15
movsbl %al, %ecx
movl $0xff, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x4600
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r15, %rdi
callq 0x4130
leaq (%rsp,%rax), %rdx
addq $0x300, %rdx            # imm = 0x300
movq %r13, %rdi
movq %r15, %rsi
callq 0x5e90
movl $0x1, %ecx
movq %r13, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x4500
movq %rax, %r12
movl $0x1, %ecx
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4460
movl $0x8, %r15d
andq %rax, %r12
cmpq $-0x1, %r12
je 0x52d7
movl $0x1, %ecx
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4500
movq %rax, %r12
movl $0x1, %ecx
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4460
decl %r15d
jne 0x520e
subq %r12, %rax
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %rax, %rcx
callq 0x42d0
movq 0x40(%rsp), %r15
movq %r15, %rdi
callq 0x44a0
movl %eax, %r12d
leaq 0x50(%rsp), %rax
cmpq %rax, %r15
je 0x5289
movq 0x50(%rsp), %rsi
incq %rsi
movq %r15, %rdi
callq 0x42c0
leaq 0xb0c0(%rip), %rax      # 0x10350
cmpl $0x0, (%rax)
je 0x52da
movl $0x1, %edx
movq 0xad0f(%rip), %rdi      # 0xffb0
movq %r14, %rsi
callq 0x4310
movq 0xad00(%rip), %rdi      # 0xffb0
movl %ebp, %esi
callq 0x4530
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x4310
movq %r15, %rdi
movl %r12d, %esi
callq 0x4530
jmp 0x52da
xorl %r12d, %r12d
movslq 0x174(%rbx,%rbp,4), %r15
cmpb $0x1, 0x168(%rbx,%r15)
jne 0x5323
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x5323
movq (%rdi), %rax
movl $0x1, %esi
movq %r15, %rdx
movl $0x1, %ecx
callq *0x28(%rax)
xorl %ecx, %ecx
testl %r12d, %r12d
setg %cl
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
movl $0x2, %esi
movq %r15, %rdx
callq *0x28(%rax)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x533f
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42c0
leaq 0xf8(%rsp), %r12
movq %r12, %rdi
callq 0x4060
incq %rbp
movslq 0x20(%rbx), %rax
cmpq %rax, %rbp
movq 0x8(%rsp), %r15
jl 0x515f
leaq 0xafe5(%rip), %rax      # 0x10350
cmpl $0x0, (%rax)
je 0x5388
movl $0x1, %edx
movq 0xac34(%rip), %rdi      # 0xffb0
leaq 0x6388(%rip), %rsi      # 0xb70b
callq 0x4310
xorps %xmm0, %xmm0
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movq 0x30(%rsp), %r12
orq %r15, 0x70(%rsp,%r12,8)
movl 0x4(%rsp), %edi
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x60(%rsp), %r8
leaq 0x408(%rsp), %r9
callq 0x42e0
testl %eax, %eax
jns 0x4f23
callq 0x4030
cmpl $0x4, (%rax)
jne 0x544c
movq 0xaba6(%rip), %rdi      # 0xffb0
leaq 0x5cc4(%rip), %rsi      # 0xb0d5
movl $0x12, %edx
callq 0x4310
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5c5b(%rip), %rdi      # 0xb08f
leaq 0x5c61(%rip), %rsi      # 0xb09c
leaq 0x5c71(%rip), %rcx      # 0xb0b3
movl $0x5b, %edx
callq 0x4190
movq %rax, %rbx
movl $0x10, %edi
callq 0x4140
movq %rax, %r14
leaq 0x108(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x5c5f(%rip), %rsi      # 0xb0ce
leaq 0x5c5e(%rip), %rdx      # 0xb0d4
leaq 0xf8(%rsp), %rdi
callq 0x5e90
movl (%rbx), %edx
movb $0x1, %bpl
leaq 0xf8(%rsp), %rsi
movq %r14, %rdi
callq 0x5be6
xorl %ebp, %ebp
leaq 0xa2df(%rip), %rsi      # 0xf780
movq 0xaaf8(%rip), %rdx      # 0xffa0
movq %r14, %rdi
callq 0x4520
movq %rax, %rbx
movq 0xf8(%rsp), %rdi
cmpq %r15, %rdi
je 0x54d0
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x42c0
testb %bpl, %bpl
jne 0x54da
jmp 0x5531
movq %rax, %rbx
movq %r14, %rdi
callq 0x41e0
jmp 0x5531
jmp 0x54e6
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x5d10
jmp 0x5531
jmp 0x5500
jmp 0x5500
jmp 0x5521
jmp 0x5521
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x5524
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42c0
jmp 0x5524
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x4060
movq %rbx, %rdi
callq 0x4550
nop