// File: XuhuiSun95[P]Ticket-Booking-Service/server/Server.cpp
// Function: Server::ProcessTicket(int const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void Server::ProcessTicket(const int &sock, const std::string &type) {

    std::string msg = mServer->GetMessage(sock);
    std::cout << "Message Received: " << msg << std::endl;

    if(msg.find(type)!=std::string::npos) {

        std::size_t ws = msg.find(" ");
        int ticket = std::stoi(msg.substr(ws+1));
        
        mtx.lock();
        std::string reply = HandleRequest(ticket);
        mtx.unlock();
        std::cout << "Ticket left: " << mTickets << std::endl;

        mServer->SendMessage(sock, reply);
    } else {

        //  Create a new forward socket to handle the forward operation
        ServerSocket* pair = new ServerSocket();
        pair->Init(mPairInfo.first, mPairInfo.second, false);

        std::cout << "Forward Message!" << std::endl;
        std::string reply = pair->ForwardTicket(msg);
        std::cout << "Receive pair reply: " << reply << std::endl;

        delete pair;

        std::cout << "Forward reply!" << std::endl;
        mServer->SendMessage(sock, reply);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq 0x8(%rax), %rsi
movq -0x10(%rbp), %rdx
leaq -0x38(%rbp), %rdi
callq 0x54d0
movq 0x77be(%rip), %rdi      # 0x9fc8
leaq 0x37f3(%rip), %rsi      # 0x6004
callq 0x21f0
movq %rax, -0xc8(%rbp)
jmp 0x281f
movq -0xc8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x21c0
movq %rax, -0xd8(%rbp)
jmp 0x2838
movq -0xd8(%rbp), %rdi
movq 0x776a(%rip), %rsi      # 0x9fb0
callq 0x2220
jmp 0x284d
movq -0x18(%rbp), %rsi
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
callq 0x23b0
cmpq $-0x1, %rax
je 0x29d5
leaq 0x3a6c(%rip), %rsi      # 0x62db
xorl %eax, %eax
movl %eax, %edx
leaq -0x38(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x2390
movq -0xe0(%rbp), %rsi
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdx
incq %rdx
leaq -0x78(%rbp), %rdi
movq $-0x1, %rcx
callq 0x2210
jmp 0x28a7
xorl %eax, %eax
movl %eax, %esi
leaq -0x78(%rbp), %rdi
movl $0xa, %edx
callq 0x2ed0
movl %eax, -0xe4(%rbp)
jmp 0x28c1
leaq -0x78(%rbp), %rdi
callq 0x2150
movl -0xe4(%rbp), %eax
movq -0xd0(%rbp), %rdi
movl %eax, -0x54(%rbp)
addq $0x60, %rdi
callq 0x2f10
jmp 0x28e5
movq -0xd0(%rbp), %rsi
leaq -0x98(%rbp), %rdi
leaq -0x54(%rbp), %rdx
callq 0x2ba0
jmp 0x28fe
movq -0xd0(%rbp), %rdi
addq $0x60, %rdi
callq 0x2f40
jmp 0x2910
movq 0x76b1(%rip), %rdi      # 0x9fc8
leaq 0x36f9(%rip), %rsi      # 0x6017
callq 0x21f0
movq %rax, -0xf0(%rbp)
jmp 0x292c
movq -0xf0(%rbp), %rdi
movq -0xd0(%rbp), %rax
movl (%rax), %esi
callq 0x2330
movq %rax, -0xf8(%rbp)
jmp 0x294a
movq -0xf8(%rbp), %rdi
movq 0x7658(%rip), %rsi      # 0x9fb0
callq 0x2220
jmp 0x295f
movq -0xd0(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rsi
leaq -0x98(%rbp), %rdx
callq 0x55a0
jmp 0x297c
leaq -0x98(%rbp), %rdi
callq 0x2150
jmp 0x2b79
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2b8b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x2150
jmp 0x2b8b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x2150
jmp 0x2b8b
movl $0x41c, %edi            # imm = 0x41C
callq 0x2200
movq %rax, -0x100(%rbp)
jmp 0x29e8
movq -0x100(%rbp), %rdi
callq 0x5200
jmp 0x29f6
movq -0xd0(%rbp), %rdx
movq -0x100(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
movq %rdx, %rsi
addq $0x38, %rsi
addq $0x58, %rdx
xorl %ecx, %ecx
callq 0x5270
jmp 0x2a26
movq 0x759b(%rip), %rdi      # 0x9fc8
leaq 0x35f1(%rip), %rsi      # 0x6025
callq 0x21f0
movq %rax, -0x108(%rbp)
jmp 0x2a42
movq -0x108(%rbp), %rdi
movq 0x7560(%rip), %rsi      # 0x9fb0
callq 0x2220
jmp 0x2a57
movq -0xa0(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
leaq -0x38(%rbp), %rdx
callq 0x5620
jmp 0x2a70
movq 0x7551(%rip), %rdi      # 0x9fc8
leaq 0x35b8(%rip), %rsi      # 0x6036
callq 0x21f0
movq %rax, -0x110(%rbp)
jmp 0x2a8c
movq -0x110(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x21c0
movq %rax, -0x118(%rbp)
jmp 0x2aa8
movq -0x118(%rbp), %rdi
movq 0x74fa(%rip), %rsi      # 0x9fb0
callq 0x2220
jmp 0x2abd
movq -0xa0(%rbp), %rax
movq %rax, -0x120(%rbp)
cmpq $0x0, %rax
je 0x2ae9
movq -0x120(%rbp), %rdi
callq 0x5240
movq -0x120(%rbp), %rdi
callq 0x21e0
movq 0x74d8(%rip), %rdi      # 0x9fc8
leaq 0x3554(%rip), %rsi      # 0x604b
callq 0x21f0
movq %rax, -0x128(%rbp)
jmp 0x2b05
movq -0x128(%rbp), %rdi
movq 0x749d(%rip), %rsi      # 0x9fb0
callq 0x2220
jmp 0x2b1a
movq -0xd0(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
callq 0x55a0
jmp 0x2b37
leaq -0xc0(%rbp), %rdi
callq 0x2150
jmp 0x2b79
movq -0x100(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x21e0
jmp 0x2b8b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x2150
jmp 0x2b8b
leaq -0x38(%rbp), %rdi
callq 0x2150
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x2150
movq -0x40(%rbp), %rdi
callq 0x2350
nopl (%rax)