// File: LiGaCu[P]Distributed_System_to_Compute_the_Shortest_Path/serverC.cpp
// Function: main
int main(void)
{
    int selffd;
    struct addrinfo hints, *servinfo;
    int rv;

    char buf[MAXDATASIZE];int numbytes;
    std::string stringbuff;
    int srcVtxid, dstVtxid, filesize; char mapid;
    float Pspeed; int Tspeed;
    float Tt,Tp;
    int path[11]; float mindistan = inf_dis;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
    hints.ai_socktype = SOCK_DGRAM;
    if ((rv = getaddrinfo("127.0.0.1", CUDPPORT, &hints, &servinfo)) != 0) 
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }
    selffd = getUDPsockandbind(servinfo);
    freeaddrinfo(servinfo);

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
    hints.ai_socktype = SOCK_DGRAM;
    if ((rv = getaddrinfo("127.0.0.1", AWSUDPPORT, &hints, &servinfo)) != 0) 
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    std::cout << "The server C is up and running using UDP on port " << CUDPPORT << "\n" << std::endl;

    while(1)
    {
        if ((numbytes = recvfrom(selffd, buf, MAXDATASIZE-1 , 0, (struct sockaddr *) NULL, NULL)) == -1) 
        {
            perror("recvfrom");
            exit(1);
        }
        memcpy(&srcVtxid, buf, sizeof(int));
        memcpy(&dstVtxid, buf+sizeof(int), sizeof(int));
        memcpy(&filesize, buf+2*sizeof(int), sizeof(int));
        memcpy(&mapid, buf+3*sizeof(int), sizeof(char));

        stringbuff.assign(buf + 3*sizeof(int) + sizeof(char), numbytes - 3*sizeof(int) - sizeof(char));

        std::istringstream tokenStream (stringbuff);
        std::string Pspeedbuf, Tspeedbuf;
        std::getline(tokenStream,Pspeedbuf);
        std::getline(tokenStream,Tspeedbuf);
        Pspeed = atof(Pspeedbuf.c_str());
        Tspeed = atoi(Tspeedbuf.c_str());

        std::cout << "The Server C has received data for calculation:\n" << "* Propagation Speed: " << Pspeed << " km/s;\n* Transmission Speed: " << Tspeed 
        << " KB/s;\n* Map ID: " << mapid << ";\n* Source ID: " << srcVtxid << ";\n* Destination ID: " << dstVtxid << ";\n" << std::endl;

        // find the shortest path and its length and store them in path and mindistan respectively.
        searchpath(tokenStream, srcVtxid, dstVtxid, path, &mindistan); 

        Tt = (float) filesize/ (float) Tspeed;
        Tp = mindistan/Pspeed;

        std::cout << "The Server C has finished the calculation:\n" <<  "Shortest path: ";
        for (int i =0 ; i< 11; i++)
        {
            if(path[i] == 2333)
                break;
            
            if (i==0)
                std::cout << path[i];
            else
                std::cout << " -- " << path[i];

        }
        std::cout << "\nShortest Distance: " << mindistan << " km\nTransmission Delay: " << Tt << " s\nPropagation Delay: " << Tp << " s\n" << std::endl;

        memcpy(buf                  , &mindistan, sizeof(float));
        memcpy(buf + sizeof(float)  , &Tt       , sizeof(float));
        memcpy(buf + sizeof(float)*2, &Tp       , sizeof(float));
        memcpy(buf + sizeof(float)*3, path      , sizeof(path));
        if ((numbytes = sendto(selffd, buf, sizeof(float)*3 + sizeof(path), 0, servinfo->ai_addr, servinfo->ai_addrlen)) == -1) 
        {
            perror("talker: sendto");
            exit(1);
        }
        std::cout << "The Server C has finished sending the output to AWS\n" << std::endl ;
    }

    close(selffd);
    freeaddrinfo(servinfo);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x7a0, %rsp            # imm = 0x7A0
movl $0x0, -0x4(%rbp)
leaq -0x468(%rbp), %rdi
callq 0x2180
movl $0x4cbebc20, -0x4c4(%rbp) # imm = 0x4CBEBC20
xorps %xmm0, %xmm0
movaps %xmm0, -0x20(%rbp)
movaps %xmm0, -0x30(%rbp)
movaps %xmm0, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x2, -0x38(%rbp)
leaq 0x1c4c(%rip), %rdi      # 0x4008
leaq 0x1c4f(%rip), %rsi      # 0x4012
leaq -0x40(%rbp), %rdx
leaq -0x48(%rbp), %rcx
callq 0x2140
movl %eax, -0x6a0(%rbp)
jmp 0x23d8
movl -0x6a0(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, %eax
je 0x2444
movq 0x3beb(%rip), %rax      # 0x5fd8
movq (%rax), %rax
movq %rax, -0x6a8(%rbp)
movl -0x4c(%rbp), %edi
callq 0x2190
movq -0x6a8(%rbp), %rdi
movq %rax, %rdx
leaq 0x1c08(%rip), %rsi      # 0x4018
movb $0x0, %al
callq 0x21a0
movl $0x1, -0x4(%rbp)
movl $0x1, -0x4d8(%rbp)
jmp 0x2b54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4d0(%rbp)
movl %eax, -0x4d4(%rbp)
jmp 0x2b6c
movq -0x48(%rbp), %rdi
callq 0x2b90
movl %eax, -0x6ac(%rbp)
jmp 0x2455
movl -0x6ac(%rbp), %eax
movl %eax, -0x8(%rbp)
movq -0x48(%rbp), %rdi
callq 0x2160
xorps %xmm0, %xmm0
movaps %xmm0, -0x20(%rbp)
movaps %xmm0, -0x30(%rbp)
movaps %xmm0, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x2, -0x38(%rbp)
leaq 0x1b7d(%rip), %rdi      # 0x4008
leaq 0x1b97(%rip), %rsi      # 0x4029
leaq -0x40(%rbp), %rdx
leaq -0x48(%rbp), %rcx
callq 0x2140
movl %eax, -0x6b0(%rbp)
jmp 0x24a7
movl -0x6b0(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, %eax
je 0x24fc
movq 0x3b1c(%rip), %rax      # 0x5fd8
movq (%rax), %rax
movq %rax, -0x6b8(%rbp)
movl -0x4c(%rbp), %edi
callq 0x2190
movq -0x6b8(%rbp), %rdi
movq %rax, %rdx
leaq 0x1b39(%rip), %rsi      # 0x4018
movb $0x0, %al
callq 0x21a0
movl $0x1, -0x4(%rbp)
movl $0x1, -0x4d8(%rbp)
jmp 0x2b54
movq 0x3acd(%rip), %rdi      # 0x5fd0
leaq 0x1b25(%rip), %rsi      # 0x402f
callq 0x20e0
movq %rax, -0x6c0(%rbp)
jmp 0x2518
movq -0x6c0(%rbp), %rdi
leaq 0x1aec(%rip), %rsi      # 0x4012
callq 0x20e0
movq %rax, -0x6c8(%rbp)
jmp 0x2534
movq -0x6c8(%rbp), %rdi
leaq 0x1bf1(%rip), %rsi      # 0x4133
callq 0x20e0
movq %rax, -0x6d0(%rbp)
jmp 0x2550
movq -0x6d0(%rbp), %rdi
movq 0x3a62(%rip), %rsi      # 0x5fc0
callq 0x2100
jmp 0x2565
jmp 0x2567
jmp 0x2569
movl -0x8(%rbp), %edi
xorl %ecx, %ecx
movl %ecx, %r9d
leaq -0x440(%rbp), %rsi
movl $0x3e7, %edx            # imm = 0x3E7
movq %r9, %r8
callq 0x20b0
movq %rax, -0x6d8(%rbp)
jmp 0x258e
movq -0x6d8(%rbp), %rax
movl %eax, -0x444(%rbp)
cmpl $-0x1, %eax
jne 0x25b8
leaq 0x1aba(%rip), %rdi      # 0x4061
callq 0x20c0
jmp 0x25ae
movl $0x1, %edi
callq 0x2120
movl -0x440(%rbp), %eax
movl %eax, -0x46c(%rbp)
movl -0x43c(%rbp), %eax
movl %eax, -0x470(%rbp)
movl -0x438(%rbp), %eax
movl %eax, -0x474(%rbp)
movb -0x434(%rbp), %al
movb %al, -0x475(%rbp)
leaq -0x433(%rbp), %rsi
movslq -0x444(%rbp), %rdx
addq $-0xd, %rdx
leaq -0x468(%rbp), %rdi
callq 0x2170
jmp 0x2608
leaq -0x658(%rbp), %rdi
leaq -0x468(%rbp), %rsi
movl $0x8, %edx
callq 0x21d0
jmp 0x2622
leaq -0x678(%rbp), %rdi
movq %rdi, -0x6e0(%rbp)
callq 0x2180
leaq -0x698(%rbp), %rdi
callq 0x2180
movq -0x6e0(%rbp), %rsi
leaq -0x658(%rbp), %rdi
callq 0x2050
jmp 0x2656
leaq -0x658(%rbp), %rdi
leaq -0x698(%rbp), %rsi
callq 0x2050
jmp 0x266b
leaq -0x678(%rbp), %rdi
callq 0x2070
movq %rax, %rdi
callq 0x2130
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x47c(%rbp)
leaq -0x698(%rbp), %rdi
callq 0x2070
movq %rax, %rdi
callq 0x21c0
movl %eax, -0x480(%rbp)
movq 0x3924(%rip), %rdi      # 0x5fd0
leaq 0x19b7(%rip), %rsi      # 0x406a
callq 0x20e0
movq %rax, -0x6e8(%rbp)
jmp 0x26c1
movq -0x6e8(%rbp), %rdi
leaq 0x19cc(%rip), %rsi      # 0x409b
callq 0x20e0
movq %rax, -0x6f0(%rbp)
jmp 0x26dd
movq -0x6f0(%rbp), %rdi
movss -0x47c(%rbp), %xmm0
callq 0x20a0
movq %rax, -0x6f8(%rbp)
jmp 0x26fa
movq -0x6f8(%rbp), %rdi
leaq 0x19a9(%rip), %rsi      # 0x40b1
callq 0x20e0
movq %rax, -0x700(%rbp)
jmp 0x2716
movq -0x700(%rbp), %rdi
movl -0x480(%rbp), %esi
callq 0x21e0
movq %rax, -0x708(%rbp)
jmp 0x2731
movq -0x708(%rbp), %rdi
leaq 0x1990(%rip), %rsi      # 0x40cf
callq 0x20e0
movq %rax, -0x710(%rbp)
jmp 0x274d
movq -0x710(%rbp), %rdi
movsbl -0x475(%rbp), %esi
callq 0x2110
movq %rax, -0x718(%rbp)
jmp 0x2769
movq -0x718(%rbp), %rdi
leaq 0x196a(%rip), %rsi      # 0x40e1
callq 0x20e0
movq %rax, -0x720(%rbp)
jmp 0x2785
movq -0x720(%rbp), %rdi
movl -0x46c(%rbp), %esi
callq 0x21e0
movq %rax, -0x728(%rbp)
jmp 0x27a0
movq -0x728(%rbp), %rdi
leaq 0x1943(%rip), %rsi      # 0x40f1
callq 0x20e0
movq %rax, -0x730(%rbp)
jmp 0x27bc
movq -0x730(%rbp), %rdi
movl -0x470(%rbp), %esi
callq 0x21e0
movq %rax, -0x738(%rbp)
jmp 0x27d7
movq -0x738(%rbp), %rdi
leaq 0x1921(%rip), %rsi      # 0x4106
callq 0x20e0
movq %rax, -0x740(%rbp)
jmp 0x27f3
movq -0x740(%rbp), %rdi
movq 0x37bf(%rip), %rsi      # 0x5fc0
callq 0x2100
jmp 0x2808
movl -0x46c(%rbp), %esi
movl -0x470(%rbp), %edx
leaq -0x658(%rbp), %rdi
leaq -0x4c0(%rbp), %rcx
leaq -0x4c4(%rbp), %r8
callq 0x2ca0
jmp 0x2830
cvtsi2ssl -0x474(%rbp), %xmm0
cvtsi2ssl -0x480(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x484(%rbp)
movss -0x4c4(%rbp), %xmm0
movss -0x47c(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x488(%rbp)
movq 0x3761(%rip), %rdi      # 0x5fd0
leaq 0x1893(%rip), %rsi      # 0x4109
callq 0x20e0
movq %rax, -0x748(%rbp)
jmp 0x2884
movq -0x748(%rbp), %rdi
leaq 0x18a3(%rip), %rsi      # 0x4135
callq 0x20e0
jmp 0x2899
movl $0x0, -0x69c(%rbp)
cmpl $0xb, -0x69c(%rbp)
jge 0x297b
movslq -0x69c(%rbp), %rax
cmpl $0x91d, -0x4c0(%rbp,%rax,4) # imm = 0x91D
jne 0x2904
jmp 0x297b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4d0(%rbp)
movl %eax, -0x4d4(%rbp)
leaq -0x698(%rbp), %rdi
callq 0x2090
leaq -0x678(%rbp), %rdi
callq 0x2090
leaq -0x658(%rbp), %rdi
callq 0x2080
jmp 0x2b6c
cmpl $0x0, -0x69c(%rbp)
jne 0x292b
movslq -0x69c(%rbp), %rax
movl -0x4c0(%rbp,%rax,4), %esi
movq 0x36ae(%rip), %rdi      # 0x5fd0
callq 0x21e0
jmp 0x2929
jmp 0x2965
movq 0x369e(%rip), %rdi      # 0x5fd0
leaq 0x180c(%rip), %rsi      # 0x4145
callq 0x20e0
movq %rax, -0x750(%rbp)
jmp 0x2947
movq -0x750(%rbp), %rdi
movslq -0x69c(%rbp), %rax
movl -0x4c0(%rbp,%rax,4), %esi
callq 0x21e0
jmp 0x2963
jmp 0x2965
jmp 0x2967
movl -0x69c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x69c(%rbp)
jmp 0x28a3
movq 0x364e(%rip), %rdi      # 0x5fd0
leaq 0x17c1(%rip), %rsi      # 0x414a
callq 0x20e0
movq %rax, -0x758(%rbp)
jmp 0x2997
movq -0x758(%rbp), %rdi
movss -0x4c4(%rbp), %xmm0
callq 0x20a0
movq %rax, -0x760(%rbp)
jmp 0x29b4
movq -0x760(%rbp), %rdi
leaq 0x179d(%rip), %rsi      # 0x415f
callq 0x20e0
movq %rax, -0x768(%rbp)
jmp 0x29d0
movq -0x768(%rbp), %rdi
movss -0x484(%rbp), %xmm0
callq 0x20a0
movq %rax, -0x770(%rbp)
jmp 0x29ed
movq -0x770(%rbp), %rdi
leaq 0x177d(%rip), %rsi      # 0x4178
callq 0x20e0
movq %rax, -0x778(%rbp)
jmp 0x2a09
movq -0x778(%rbp), %rdi
movss -0x488(%rbp), %xmm0
callq 0x20a0
movq %rax, -0x780(%rbp)
jmp 0x2a26
movq -0x780(%rbp), %rdi
leaq 0x175b(%rip), %rsi      # 0x418f
callq 0x20e0
movq %rax, -0x788(%rbp)
jmp 0x2a42
movq -0x788(%rbp), %rdi
movq 0x3570(%rip), %rsi      # 0x5fc0
callq 0x2100
jmp 0x2a57
movl -0x4c4(%rbp), %eax
movl %eax, -0x440(%rbp)
movl -0x484(%rbp), %eax
movl %eax, -0x43c(%rbp)
movl -0x488(%rbp), %eax
movl %eax, -0x438(%rbp)
movups -0x4a4(%rbp), %xmm0
movups %xmm0, -0x418(%rbp)
movaps -0x4c0(%rbp), %xmm0
movaps -0x4b0(%rbp), %xmm1
movups %xmm1, -0x424(%rbp)
movups %xmm0, -0x434(%rbp)
movl -0x8(%rbp), %edi
movq -0x48(%rbp), %rax
movq 0x18(%rax), %r8
movl 0x10(%rax), %r9d
leaq -0x440(%rbp), %rsi
movl $0x38, %edx
xorl %ecx, %ecx
callq 0x2030
movq %rax, -0x790(%rbp)
jmp 0x2ad0
movq -0x790(%rbp), %rax
movl %eax, -0x444(%rbp)
cmpl $-0x1, %eax
jne 0x2afa
leaq 0x16aa(%rip), %rdi      # 0x4193
callq 0x20c0
jmp 0x2af0
movl $0x1, %edi
callq 0x2120
movq 0x34cf(%rip), %rdi      # 0x5fd0
leaq 0x169a(%rip), %rsi      # 0x41a2
callq 0x20e0
movq %rax, -0x798(%rbp)
jmp 0x2b16
movq -0x798(%rbp), %rdi
movq 0x349c(%rip), %rsi      # 0x5fc0
callq 0x2100
jmp 0x2b2b
leaq -0x698(%rbp), %rdi
callq 0x2090
leaq -0x678(%rbp), %rdi
callq 0x2090
leaq -0x658(%rbp), %rdi
callq 0x2080
jmp 0x2567
leaq -0x468(%rbp), %rdi
callq 0x2090
movl -0x4(%rbp), %eax
addq $0x7a0, %rsp            # imm = 0x7A0
popq %rbp
retq
leaq -0x468(%rbp), %rdi
callq 0x2090
movq -0x4d0(%rbp), %rdi
callq 0x21f0
nopw %cs:(%rax,%rax)