// File: /workspace/github_cpp_makefile_star2_O1/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 $0x498, %rsp            # imm = 0x498
movq %rdi, %rbx
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
je 0x5491
callq 0x4320
movl %eax, %r14d
leal 0x1(%r14), %eax
movl %eax, 0x1c(%rsp)
leaq 0x418(%rsp), %rdi
callq 0x4390
leaq 0xb4dd(%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 0x70(%rsp), %rsi
movq %rcx, (%rsi)
leal 0x3f(%r14), %eax
testl %r14d, %r14d
cmovnsl %r14d, %eax
movl %eax, %ecx
andl $-0x40, %ecx
subl %ecx, %r14d
movl $0x1, %r12d
movl %r14d, %ecx
shlq %cl, %r12
movq %rdx, 0x8(%rsi)
sarl $0x6, %eax
movslq %eax, %r14
leaq 0x28(%rbx), %rax
movq %rax, 0x48(%rsp)
leaq 0x20(%rsp), %r13
leaq 0x65e0(%rip), %rbp      # 0xb4b8
movq %r12, 0x10(%rsp)
movq %r14, 0x40(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xf0(%rsp)
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)
orq %r12, 0x80(%rsp,%r14,8)
movl 0x1c(%rsp), %edi
leaq 0x80(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x70(%rsp), %r8
leaq 0x418(%rsp), %r9
callq 0x42e0
testl %eax, %eax
js 0x50ad
testq %r12, 0x80(%rsp,%r14,8)
je 0x51b8
movq 0x8(%rbx), %rdi
callq 0x40e0
movq %rax, 0x108(%rsp)
leaq 0x110(%rsp), %rdi
movq %rax, %rsi
movq 0xb040(%rip), %rdx      # 0xffc8
callq 0x5f18
movq 0x108(%rsp), %rsi
callq 0x553c
movl %eax, %r12d
movl $0x2, %r14d
testb %al, %al
je 0x519d
movq 0x108(%rsp), %rdi
callq 0x4050
movq %rax, %r15
xorl %r14d, %r14d
testq %rax, %rax
je 0x519d
movq %r15, %rdi
leaq 0x6117(%rip), %rsi      # 0xb0e8
callq 0x42f0
testl %eax, %eax
je 0x50de
movq %r15, %rdi
leaq 0x6104(%rip), %rsi      # 0xb0ec
callq 0x42f0
testl %eax, %eax
je 0x50f3
leaq 0xb354(%rip), %rax      # 0x10350
cmpl $0x0, (%rax)
je 0x519d
movl $0x8, %edx
movq 0xaf9f(%rip), %rdi      # 0xffb0
leaq 0x60db(%rip), %rsi      # 0xb0f3
callq 0x4310
movq %r15, %rdi
callq 0x4130
movq 0xaf84(%rip), %rdi      # 0xffb0
movq %r15, %rsi
movq %rax, %rdx
callq 0x4310
movb $0xa, 0x310(%rsp)
movl $0x1, %edx
movq 0xaf65(%rip), %rdi      # 0xffb0
leaq 0x310(%rsp), %rsi
callq 0x4310
movb $0x20, 0x310(%rsp)
movl $0x1, %edx
movq 0xaf44(%rip), %rdi      # 0xffb0
leaq 0x310(%rsp), %rsi
callq 0x4310
movq %rax, 0x8(%rsp)
movq 0x108(%rsp), %rdi
callq 0x44e0
movq %rax, %r15
testq %rax, %rax
je 0x5108
movq %r15, %rdi
callq 0x4130
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x4310
jmp 0x5124
callq 0x4030
cmpl $0x4, (%rax)
jne 0x542b
movl $0x12, %edx
movq 0xaee9(%rip), %rdi      # 0xffb0
leaq 0x6007(%rip), %rsi      # 0xb0d5
callq 0x4310
movl $0x1, %r14d
jmp 0x540a
movq 0x108(%rsp), %rsi
movq %rbx, %rdi
callq 0x558c
jmp 0x519d
movq 0x108(%rsp), %rsi
movq %rbx, %rdi
callq 0x568c
jmp 0x519d
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rax
leaq (%rcx,%rax), %rdi
movl 0x20(%rcx,%rax), %esi
orl $0x1, %esi
callq 0x4510
movl $0x3, %edx
movq 0x8(%rsp), %rdi
leaq 0x5fc7(%rip), %rsi      # 0xb0fc
callq 0x4310
movq 0x108(%rsp), %rdi
callq 0x40b0
movq %rax, %r15
testq %rax, %rax
je 0x5169
movq %r15, %rdi
callq 0x4130
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x4310
jmp 0x5187
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rax
movq %rcx, %rdi
addq %rax, %rdi
movl 0x20(%rcx,%rax), %esi
orl $0x1, %esi
callq 0x4510
movl $0x2, %edx
movq 0x8(%rsp), %rdi
leaq 0x66c8(%rip), %rsi      # 0xb860
callq 0x4310
leaq 0x110(%rsp), %rdi
callq 0x5d12
testb %r12b, %r12b
movq 0x10(%rsp), %r12
je 0x540a
leaq 0xb199(%rip), %rax      # 0x10358
cmpb $0x1, (%rax)
leaq 0x108(%rsp), %r14
jne 0x53de
cmpl $0x0, 0x20(%rbx)
jle 0x53de
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x4300
movq %r12, %rax
shlq $0x5, %rax
movq 0x48(%rsp), %rcx
movq (%rcx,%rax), %rsi
movq %r14, %rdi
movl $0x8, %edx
callq 0x4180
movq 0x108(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4280
leaq 0x310(%rsp), %r15
movsbl %al, %ecx
movl $0xff, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x4600
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r15, %rdi
callq 0x4130
leaq (%rsp,%rax), %rdx
addq $0x310, %rdx            # imm = 0x310
movq %r13, %rdi
movq %r15, %rsi
callq 0x5e88
movl $0x1, %ecx
movq %r13, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x4500
movq %rax, %r15
movl $0x1, %ecx
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x4460
movl $0x8, %r14d
andq %rax, %r15
cmpq $-0x1, %r15
je 0x5356
movl $0x1, %ecx
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x4500
movq %rax, %r15
movl $0x1, %ecx
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x4460
decl %r14d
jne 0x528c
subq %r15, %rax
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %rax, %rcx
callq 0x42d0
movq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x44a0
movl %eax, %r15d
leaq 0x60(%rsp), %rax
cmpq %rax, %r14
je 0x5307
movq 0x60(%rsp), %rsi
incq %rsi
movq %r14, %rdi
callq 0x42c0
leaq 0xb042(%rip), %rax      # 0x10350
cmpl $0x0, (%rax)
je 0x5359
movl $0x1, %edx
movq 0xac91(%rip), %rdi      # 0xffb0
movq %rbp, %rsi
callq 0x4310
movq 0xac82(%rip), %rdi      # 0xffb0
movl %r12d, %esi
callq 0x4530
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x4310
movq %r14, %rdi
movl %r15d, %esi
callq 0x4530
jmp 0x5359
xorl %r15d, %r15d
movslq 0x174(%rbx,%r12,4), %r14
cmpb $0x1, 0x168(%rbx,%r14)
jne 0x53a2
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x53a2
movq (%rdi), %rax
movl $0x1, %esi
movq %r14, %rdx
movl $0x1, %ecx
callq *0x28(%rax)
xorl %ecx, %ecx
testl %r15d, %r15d
setg %cl
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
movl $0x2, %esi
movq %r14, %rdx
callq *0x28(%rax)
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x53be
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x42c0
leaq 0x108(%rsp), %r14
movq %r14, %rdi
callq 0x4060
incq %r12
movslq 0x20(%rbx), %rax
cmpq %rax, %r12
jl 0x51dd
xorl %r14d, %r14d
leaq 0xaf68(%rip), %rax      # 0x10350
cmpl $0x0, (%rax)
je 0x5405
movl $0x1, %edx
movq 0xabb7(%rip), %rdi      # 0xffb0
leaq 0x630b(%rip), %rsi      # 0xb70b
callq 0x4310
movq 0x10(%rsp), %r12
testb $0x1, %r14b
movq 0x40(%rsp), %r14
je 0x4ee2
addq $0x498, %rsp            # imm = 0x498
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movl $0x10, %edi
callq 0x4140
movq %rax, %r14
leaq 0x118(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x5c7f(%rip), %rsi      # 0xb0ce
leaq 0x5c7e(%rip), %rdx      # 0xb0d4
leaq 0x108(%rsp), %rdi
callq 0x5e88
movl (%r15), %edx
movb $0x1, %bpl
leaq 0x108(%rsp), %rsi
movq %r14, %rdi
callq 0x5be8
xorl %ebp, %ebp
leaq 0xa26e(%rip), %rsi      # 0xf6f0
movq 0xab17(%rip), %rdx      # 0xffa0
movq %r14, %rdi
callq 0x4520
leaq 0x5bf7(%rip), %rdi      # 0xb08f
leaq 0x5bfd(%rip), %rsi      # 0xb09c
leaq 0x5c0d(%rip), %rcx      # 0xb0b3
movl $0x5b, %edx
callq 0x4190
movq %rax, %rbx
movq 0x108(%rsp), %rdi
cmpq %r12, %rdi
je 0x54d8
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x42c0
jmp 0x54d8
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
je 0x5534
movq %r14, %rdi
callq 0x41e0
jmp 0x5534
jmp 0x54e9
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x5d12
jmp 0x5534
jmp 0x5503
jmp 0x5503
jmp 0x5524
jmp 0x5524
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x5527
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x42c0
jmp 0x5527
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x4060
movq %rbx, %rdi
callq 0x4550