// File: 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
movq %rsp, %rbp
subq $0x620, %rsp            # imm = 0x620
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x520(%rbp)
cmpq $0x0, 0x8(%rax)
je 0x6493
jmp 0x64b2
leaq 0xbbf5(%rip), %rdi      # 0x1208f
leaq 0xbbfb(%rip), %rsi      # 0x1209c
movl $0x5b, %edx
leaq 0xbc06(%rip), %rcx      # 0x120b3
callq 0x5250
movq -0x520(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x5460
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
movl $0x8, -0x14(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x54f0
leaq 0x11f38(%rip), %rax     # 0x18420
testb $0x1, (%rax)
je 0x6505
movq $0x0, -0xa8(%rbp)
movq $0x5f5e100, -0xa0(%rbp) # imm = 0x5F5E100
jmp 0x651b
movq $0x3e7, -0xa8(%rbp) # imm = 0x3E7
movq $0x0, -0xa0(%rbp)
jmp 0x651d
jmp 0x651f
leaq -0x128(%rbp), %rax
movq %rax, -0x138(%rbp)
movl $0x0, -0x12c(%rbp)
movl -0x12c(%rbp), %eax
cmpq $0x10, %rax
jae 0x6569
movq -0x138(%rbp), %rax
movl -0x12c(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x12c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12c(%rbp)
jmp 0x6537
jmp 0x656b
movl -0xc(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0xc(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x128(%rbp,%rax,8), %rcx
movq %rcx, -0x128(%rbp,%rax,8)
movl -0x10(%rbp), %edi
leaq -0x128(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0xa8(%rbp), %r8
leaq -0x98(%rbp), %r9
movq %rcx, %rdx
callq 0x5410
movl %eax, -0x13c(%rbp)
cmpl $0x0, -0x13c(%rbp)
jge 0x66ed
callq 0x5030
movq %rax, %rcx
movl $0x4, %eax
cmpl (%rcx), %eax
je 0x66d1
movb $0x1, -0x175(%rbp)
movl $0x10, %edi
callq 0x51b0
movq %rax, -0x530(%rbp)
leaq -0x161(%rbp), %rdi
movq %rdi, -0x528(%rbp)
callq 0x56c0
movq -0x528(%rbp), %rdx
leaq 0xbaa7(%rip), %rsi      # 0x120ce
leaq -0x160(%rbp), %rdi
callq 0x7600
jmp 0x6635
callq 0x5030
movq -0x530(%rbp), %rdi
movl (%rax), %edx
leaq -0x160(%rbp), %rsi
callq 0x76b0
jmp 0x6651
movq -0x530(%rbp), %rdi
movb $0x0, -0x175(%rbp)
leaq 0x10f9a(%rip), %rsi     # 0x17600
leaq 0x1113(%rip), %rdx      # 0x7780
callq 0x5680
jmp 0x6ec4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x66a9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x5230
leaq -0x161(%rbp), %rdi
callq 0x53f0
testb $0x1, -0x175(%rbp)
jne 0x66c0
jmp 0x66cc
movq -0x530(%rbp), %rdi
callq 0x52a0
jmp 0x6eb8
movq 0x118d8(%rip), %rdi     # 0x17fb0
leaq 0xb9f6(%rip), %rsi      # 0x120d5
callq 0x5380
addq $0x620, %rsp            # imm = 0x620
popq %rbp
retq
movl -0xc(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x128(%rbp,%rax,8), %rax
movq %rax, -0x538(%rbp)
movl -0xc(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x538(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x6a0e
movq -0x520(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x5120
movq %rax, %rsi
movq 0x11879(%rip), %rdx     # 0x17fc8
leaq -0x188(%rbp), %rdi
movq %rdi, -0x548(%rbp)
callq 0x77a0
movq -0x548(%rbp), %rdi
callq 0x77d0
movq %rax, -0x540(%rbp)
jmp 0x6777
movq -0x540(%rbp), %rsi
movq -0x520(%rbp), %rdi
callq 0x6ed0
movb %al, -0x549(%rbp)
jmp 0x6792
movb -0x549(%rbp), %al
testb $0x1, %al
jne 0x67d0
jmp 0x679e
movl $0x2, -0x18c(%rbp)
jmp 0x69ef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x77f0
jmp 0x6eb8
leaq -0x188(%rbp), %rdi
callq 0x77d0
movq %rax, -0x558(%rbp)
jmp 0x67e5
movq -0x558(%rbp), %rdi
callq 0x5060
movq %rax, -0x560(%rbp)
jmp 0x67fa
movq -0x560(%rbp), %rax
movq %rax, -0x198(%rbp)
cmpq $0x0, -0x198(%rbp)
jne 0x6817
jmp 0x69e5
movq -0x198(%rbp), %rdi
leaq 0xb8c3(%rip), %rsi      # 0x120e8
callq 0x5420
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x6861
leaq -0x188(%rbp), %rdi
callq 0x77d0
movq %rax, -0x568(%rbp)
jmp 0x6847
movq -0x568(%rbp), %rsi
movq -0x520(%rbp), %rdi
callq 0x6f40
jmp 0x685c
jmp 0x69e3
movq -0x198(%rbp), %rdi
leaq 0xb87d(%rip), %rsi      # 0x120ec
callq 0x5420
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x68ab
leaq -0x188(%rbp), %rdi
callq 0x77d0
movq %rax, -0x570(%rbp)
jmp 0x6891
movq -0x570(%rbp), %rsi
movq -0x520(%rbp), %rdi
callq 0x6fe0
jmp 0x68a6
jmp 0x69e1
leaq 0x11b66(%rip), %rax     # 0x18418
cmpl $0x0, (%rax)
je 0x69df
movq 0x116ee(%rip), %rdi     # 0x17fb0
leaq 0xb82a(%rip), %rsi      # 0x120f3
callq 0x5380
movq %rax, -0x578(%rbp)
jmp 0x68d7
movq -0x578(%rbp), %rdi
movq -0x198(%rbp), %rsi
callq 0x5380
movq %rax, -0x580(%rbp)
jmp 0x68f3
movq -0x580(%rbp), %rdi
movl $0xa, %esi
callq 0x5470
jmp 0x6906
movq 0x116a3(%rip), %rdi     # 0x17fb0
movl $0x20, %esi
callq 0x5470
movq %rax, -0x588(%rbp)
jmp 0x6920
leaq -0x188(%rbp), %rdi
callq 0x77d0
movq %rax, -0x590(%rbp)
jmp 0x6935
movq -0x590(%rbp), %rdi
callq 0x5650
movq %rax, -0x598(%rbp)
jmp 0x694a
movq -0x598(%rbp), %rsi
movq -0x588(%rbp), %rdi
callq 0x5380
movq %rax, -0x5a0(%rbp)
jmp 0x6966
movq -0x5a0(%rbp), %rdi
leaq 0xb788(%rip), %rsi      # 0x120fc
callq 0x5380
movq %rax, -0x5a8(%rbp)
jmp 0x6982
leaq -0x188(%rbp), %rdi
callq 0x77d0
movq %rax, -0x5b0(%rbp)
jmp 0x6997
movq -0x5b0(%rbp), %rdi
callq 0x50f0
movq %rax, -0x5b8(%rbp)
jmp 0x69ac
movq -0x5b8(%rbp), %rsi
movq -0x5a8(%rbp), %rdi
callq 0x5380
movq %rax, -0x5c0(%rbp)
jmp 0x69c8
movq -0x5c0(%rbp), %rdi
leaq 0xbf4e(%rip), %rsi      # 0x12924
callq 0x5380
jmp 0x69dd
jmp 0x69df
jmp 0x69e1
jmp 0x69e3
jmp 0x69e5
movl $0x0, -0x18c(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x77f0
movl -0x18c(%rbp), %eax
testl %eax, %eax
je 0x6a0c
jmp 0x6a07
jmp 0x651d
jmp 0x6a0e
leaq 0x11a0b(%rip), %rax     # 0x18420
testb $0x1, (%rax)
je 0x6e94
movl $0x0, -0x19c(%rbp)
movq -0x520(%rbp), %rcx
movl -0x19c(%rbp), %eax
cmpl 0x20(%rcx), %eax
jge 0x6e92
leaq -0x3a8(%rbp), %rdi
callq 0x5450
movq -0x520(%rbp), %rdi
movl -0x19c(%rbp), %esi
callq 0x7810
movq %rax, -0x5c8(%rbp)
jmp 0x6a65
movq -0x5c8(%rbp), %rdi
callq 0x5170
movq %rax, %rsi
leaq -0x3a8(%rbp), %rdi
movl $0x8, %edx
callq 0x5240
jmp 0x6a87
leaq -0x3a8(%rbp), %rdi
leaq -0x4b0(%rbp), %rsi
movl $0xff, %edx
callq 0x50d0
jmp 0x6aa1
leaq -0x4d1(%rbp), %rdi
movq %rdi, -0x5d0(%rbp)
callq 0x56c0
movq -0x5d0(%rbp), %rdx
leaq -0x4d0(%rbp), %rdi
leaq -0x4b0(%rbp), %rsi
callq 0x7600
jmp 0x6ad0
leaq -0x4d1(%rbp), %rdi
callq 0x53f0
leaq -0x4d0(%rbp), %rdi
leaq 0xb9b5(%rip), %rsi      # 0x1249f
xorl %eax, %eax
movl %eax, %edx
callq 0x51f0
movq %rax, -0x4e0(%rbp)
movq -0x4e0(%rbp), %rdx
leaq -0x4d0(%rbp), %rdi
leaq 0xb990(%rip), %rsi      # 0x1249f
callq 0x51d0
movq %rax, -0x4e8(%rbp)
movl $0x0, -0x4ec(%rbp)
movl $0x0, -0x4f0(%rbp)
movb $0x1, %al
movq $-0x1, %rcx
cmpq -0x4e8(%rbp), %rcx
movb %al, -0x5d1(%rbp)
jne 0x6b5e
movq $-0x1, %rax
cmpq -0x4e0(%rbp), %rax
setne %al
movb %al, -0x5d1(%rbp)
movb -0x5d1(%rbp), %al
testb $0x1, %al
jne 0x6b6d
jmp 0x6cf6
movq -0x4e8(%rbp), %rdx
leaq -0x4d0(%rbp), %rdi
leaq 0xb91d(%rip), %rsi      # 0x1249f
callq 0x51f0
movq %rax, -0x4e0(%rbp)
movq -0x4e0(%rbp), %rdx
leaq -0x4d0(%rbp), %rdi
leaq 0xb8fc(%rip), %rsi      # 0x1249f
callq 0x51d0
movq %rax, -0x4e8(%rbp)
movl -0x4ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4ec(%rbp)
movl -0x4ec(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x6cf1
movq -0x4e0(%rbp), %rdx
movq -0x4e8(%rbp), %rcx
subq %rdx, %rcx
leaq -0x510(%rbp), %rdi
leaq -0x4d0(%rbp), %rsi
callq 0x53c0
jmp 0x6bf3
leaq -0x510(%rbp), %rdi
callq 0x5170
movq %rax, %rdi
callq 0x5610
movl %eax, -0x4f0(%rbp)
leaq -0x510(%rbp), %rdi
callq 0x5230
leaq 0x117f8(%rip), %rax     # 0x18418
cmpl $0x0, (%rax)
je 0x6cef
movq 0x11380(%rip), %rdi     # 0x17fb0
leaq 0xb868(%rip), %rsi      # 0x1249f
callq 0x5380
movq %rax, -0x5e0(%rbp)
jmp 0x6c45
movq -0x5e0(%rbp), %rdi
movl -0x19c(%rbp), %esi
callq 0x5690
movq %rax, -0x5e8(%rbp)
jmp 0x6c60
movq -0x5e8(%rbp), %rdi
leaq 0xb831(%rip), %rsi      # 0x1249f
callq 0x5380
movq %rax, -0x5f0(%rbp)
jmp 0x6c7c
movq -0x5f0(%rbp), %rdi
movl -0x4f0(%rbp), %esi
callq 0x5690
jmp 0x6c90
jmp 0x6cef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x6e84
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x4d1(%rbp), %rdi
callq 0x53f0
jmp 0x6e84
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x4d0(%rbp), %rdi
callq 0x5230
jmp 0x6e84
jmp 0x6cf6
jmp 0x6b2f
movq -0x520(%rbp), %rdi
movl -0x19c(%rbp), %esi
callq 0x7830
movl %eax, -0x5f4(%rbp)
jmp 0x6d10
movq -0x520(%rbp), %rax
movl -0x5f4(%rbp), %ecx
movl %ecx, -0x514(%rbp)
movslq -0x514(%rbp), %rcx
testb $0x1, 0x168(%rax,%rcx)
je 0x6e58
movq -0x520(%rbp), %rdi
addq $0x10, %rdi
callq 0x7850
movq %rax, -0x600(%rbp)
jmp 0x6d51
movq -0x600(%rbp), %rax
cmpq $-0x1, %rax
je 0x6e58
cmpl $0x0, -0x4f0(%rbp)
jle 0x6de2
movq -0x520(%rbp), %rdi
addq $0x10, %rdi
callq 0x7880
movq %rax, -0x608(%rbp)
jmp 0x6d84
movq -0x608(%rbp), %rdi
movslq -0x514(%rbp), %rdx
movq (%rdi), %rax
movq 0x28(%rax), %rax
movl $0x1, %ecx
movl %ecx, %esi
callq *%rax
jmp 0x6da4
movq -0x520(%rbp), %rdi
addq $0x10, %rdi
callq 0x7880
movq %rax, -0x610(%rbp)
jmp 0x6dbd
movq -0x610(%rbp), %rdi
movslq -0x514(%rbp), %rdx
movq (%rdi), %rax
movq 0x28(%rax), %rax
movl $0x2, %esi
movl $0x1, %ecx
callq *%rax
jmp 0x6de0
jmp 0x6e56
movq -0x520(%rbp), %rdi
addq $0x10, %rdi
callq 0x7880
movq %rax, -0x618(%rbp)
jmp 0x6dfb
movq -0x618(%rbp), %rdi
movslq -0x514(%rbp), %rdx
movq (%rdi), %rax
movq 0x28(%rax), %rax
movl $0x1, %ecx
movl %ecx, %esi
callq *%rax
jmp 0x6e1b
movq -0x520(%rbp), %rdi
addq $0x10, %rdi
callq 0x7880
movq %rax, -0x620(%rbp)
jmp 0x6e34
movq -0x620(%rbp), %rdi
movslq -0x514(%rbp), %rdx
movq (%rdi), %rax
movq 0x28(%rax), %rax
movl $0x2, %esi
xorl %ecx, %ecx
callq *%rax
jmp 0x6e54
jmp 0x6e56
jmp 0x6e58
leaq -0x4d0(%rbp), %rdi
callq 0x5230
leaq -0x3a8(%rbp), %rdi
callq 0x5090
movl -0x19c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x19c(%rbp)
jmp 0x6a28
leaq -0x3a8(%rbp), %rdi
callq 0x5090
jmp 0x6eb8
jmp 0x6e94
leaq 0x1157d(%rip), %rax     # 0x18418
cmpl $0x0, (%rax)
je 0x6eb3
movq 0x11109(%rip), %rdi     # 0x17fb0
leaq 0xb921(%rip), %rsi      # 0x127cf
callq 0x5380
jmp 0x651d
movq -0x170(%rbp), %rdi
callq 0x56b0
nopw %cs:(%rax,%rax)