// File: cybozu[P]yrmcds/src/server.cpp
// Function: yrmcds::server::serve()
void server::serve() {
    auto res = cybozu::signal_setup({SIGHUP, SIGQUIT, SIGTERM, SIGINT, SIGUSR1});
    res->set_handler([this](const struct signalfd_siginfo& si,
                            cybozu::reactor& r) {
                         switch(si.ssi_signo) {
                         case SIGUSR1:
                             for( auto& handler: m_handlers )
                                 handler->dump_stats();
                             break;
                         case SIGHUP:
                             cybozu::logger::instance().reopen();
                             cybozu::logger::info() << "got SIGHUP.";
                             break;
                         case SIGQUIT:
                         case SIGTERM:
                         case SIGINT:
                             cybozu::logger::info() << "got signal (signo=" << si.ssi_signo << ").";
                             m_signaled = true;
                             r.quit();
                             break;
                         }
                     });
    m_reactor.add_resource(std::move(res), cybozu::reactor::EVENT_IN);

    for( auto& handler: m_handlers )
        handler->on_start();

    if( is_master() )
        goto MASTER_ENTRY;
    while( true ) {
        serve_slave();
        if( m_signaled ) return;

        // disconnected from the master
        cybozu::logger::info() << "Try to promote to master...";
        for( int i = 0; i < MASTER_CHECKS; ++i ) {
            if( is_master() )
                goto MASTER_ENTRY;
            std::this_thread::sleep_for( std::chrono::milliseconds(100) );
        }
        cybozu::logger::warning() << "Could not promote to master. Join the cluster again as a slave.";
    }

  MASTER_ENTRY:
    serve_master();
    std::quick_exit(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movups 0x5ad87(%rip), %xmm0    # 0x7a978
movaps %xmm0, -0x40(%rbp)
movl $0xa, -0x30(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0x20(%rbp)
movq $0x5, -0x18(%rbp)
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x10(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x2a770
movq -0xf8(%rbp), %rdi
callq 0x221b0
movq %rax, %rcx
movq -0xf0(%rbp), %rax
movq %rcx, -0xe8(%rbp)
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
leaq -0x68(%rbp), %rsi
callq 0x1ff20
movq -0xe8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x221d0
jmp 0x1fc6e
leaq -0x60(%rbp), %rdi
callq 0x22200
movq -0xf0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x108(%rbp)
leaq -0x80(%rbp), %rdi
movq %rdi, -0x100(%rbp)
leaq -0x10(%rbp), %rsi
callq 0x22220
movq -0x108(%rbp), %rdi
movq -0x100(%rbp), %rsi
movl $0x1, %edx
callq 0xea00
jmp 0x1fcb7
leaq -0x80(%rbp), %rdi
callq 0x11290
movq -0xf0(%rbp), %rax
addq $0x120, %rax            # imm = 0x120
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x22270
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
callq 0x222a0
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x222d0
testb $0x1, %al
jne 0x1fd16
jmp 0x1fd98
leaq -0x90(%rbp), %rdi
callq 0x22310
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x22330
movq %rax, %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
jmp 0x1fd43
jmp 0x1fd45
leaq -0x90(%rbp), %rdi
callq 0x22350
jmp 0x1fcfa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x22200
jmp 0x1ff09
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x11290
jmp 0x1ff09
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x1ff09
callq 0x22370
movb %al, -0x109(%rbp)
jmp 0x1fda5
movb -0x109(%rbp), %al
testb $0x1, %al
jne 0x1fdb1
jmp 0x1fdb6
jmp 0x1fef4
jmp 0x1fdb8
jmp 0x1fdba
movq -0xf0(%rbp), %rdi
callq 0x1ffb0
jmp 0x1fdc8
movq -0xf0(%rbp), %rax
testb $0x1, (%rax)
je 0x1fde6
leaq -0x10(%rbp), %rdi
callq 0x22530
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
leaq -0xb0(%rbp), %rdi
callq 0x22390
jmp 0x1fdf4
leaq 0x5ac69(%rip), %rsi     # 0x7aa64
leaq -0xb0(%rbp), %rdi
callq 0x223e0
jmp 0x1fe09
leaq -0xb0(%rbp), %rdi
callq 0xa770
movl $0x0, -0xb4(%rbp)
cmpl $0x32, -0xb4(%rbp)
jge 0x1fea6
callq 0x22370
movb %al, -0x10a(%rbp)
jmp 0x1fe35
movb -0x10a(%rbp), %al
testb $0x1, %al
jne 0x1fe41
jmp 0x1fe63
jmp 0x1fef4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xa770
jmp 0x1ff09
movl $0x64, -0xc4(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xc4(%rbp), %rsi
callq 0x224d0
jmp 0x1fe82
leaq -0xc0(%rbp), %rdi
callq 0x22420
jmp 0x1fe90
jmp 0x1fe92
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x1fe1f
leaq -0xd8(%rbp), %rdi
callq 0x1b6e0
jmp 0x1feb4
leaq 0x5abc5(%rip), %rsi     # 0x7aa80
leaq -0xd8(%rbp), %rdi
callq 0x224f0
jmp 0x1fec9
leaq -0xd8(%rbp), %rdi
callq 0xa770
jmp 0x1fdb8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0xa770
jmp 0x1ff09
movq -0xf0(%rbp), %rdi
callq 0x20210
jmp 0x1ff02
xorl %edi, %edi
callq 0x8390
leaq -0x10(%rbp), %rdi
callq 0x22530
movq -0x70(%rbp), %rdi
callq 0x8bc0
nopl (%rax,%rax)