// File: /workspace/github_c_makefile_star2_O0/MAGI003769[P]EE450-Socket-Project/serverA.c
// Function: main
int main(void) {
    int sockfd;
    struct addrinfo hints, *servinfo, *p;
    int rv;
    int numbytes;
    struct sockaddr_storage their_addr;
    socklen_t addr_len;
    char s[INET6_ADDRSTRLEN];

    char buf[MAXBUFLEN];
    char data[MAXBUFLEN];
    char *args[3];

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_PASSIVE;

    // get information of backend server itself
    if((rv = getaddrinfo(IPADDRESS, MYPORT, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    // loop through all the results and make a socket
    for(p = servinfo; p != NULL; p = p->ai_next) {
        if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }

        if(bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sockfd);
            perror("server: bind");
            continue;
        }
        break;
    }

    if(p == NULL) {
        fprintf(stderr, "talker: failed to bind socket\n");
        return 2;
    }

    freeaddrinfo(servinfo);

    addr_len = sizeof their_addr;
    
    printf("The Server A is up and running using UDP on port %s\n", MYPORT);
    //fflush(stdout);

    while(1) {
        if((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1, 0,
            (struct sockaddr *)&their_addr, &addr_len)) == -1) {
            perror("recvfrom");
            exit(1);
        }
        buf[numbytes] = '\0';

        // split messages into map_id, source_vtx and target_vtx 
        split_args2(args, buf);

        // read file and search the map_id
        //memset(&buf, 0, sizeof buf);
        search_map(args[0], data);

        printf("The Server A has received input for finding graph of map %s\n", args[0]);

        numbytes = sendto(sockfd, data, strlen(data), 0,
            (struct sockaddr *)&their_addr, addr_len);
        
        if(numbytes == -1) {
            perror("listener: sendto");
            exit(1);
        }

        if(strcmp(data, MAPNOTFOUND) == 0) {
            printf("The Server A does not have the required graph id %s\n", args[0]);
            printf("The Server A has sent \"Graph not Found\" to AWS\n");
        } else {
            printf("The Server A has sent Graph to AWS\n");
        }
        
        //close(sockfd);
    }

    return 0;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2090, %rsp           # imm = 0x2090
movl $0x0, -0x4(%rbp)
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x1070
movl $0x0, -0x34(%rbp)
movl $0x2, -0x30(%rbp)
movl $0x1, -0x38(%rbp)
leaq 0xb7a(%rip), %rdi       # 0x2038
leaq 0xb7d(%rip), %rsi       # 0x2042
leaq -0x38(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq 0x1140
movl %eax, -0x4c(%rbp)
cmpl $0x0, %eax
je 0x1517
movq 0x2b17(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x2078(%rbp)
movl -0x4c(%rbp), %edi
callq 0x1060
movq -0x2078(%rbp), %rdi
movq %rax, %rdx
leaq 0xb44(%rip), %rsi       # 0x2048
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x4(%rbp)
jmp 0x172b
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x1598
movq -0x48(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x48(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x48(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x1170
movl %eax, -0x8(%rbp)
cmpl $-0x1, %eax
jne 0x1556
leaq 0xb0a(%rip), %rdi       # 0x2059
callq 0x10f0
jmp 0x158a
movl -0x8(%rbp), %edi
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x48(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x10d0
cmpl $-0x1, %eax
jne 0x1588
movl -0x8(%rbp), %edi
callq 0x1090
leaq 0xae7(%rip), %rdi       # 0x2068
callq 0x10f0
jmp 0x158a
jmp 0x1598
movq -0x48(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x151f
cmpq $0x0, -0x48(%rbp)
jne 0x15c3
movq 0x2a52(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xac5(%rip), %rsi       # 0x2075
movb $0x0, %al
callq 0x10b0
movl $0x2, -0x4(%rbp)
jmp 0x172b
movq -0x40(%rbp), %rdi
callq 0x1160
movl $0x80, -0xd4(%rbp)
leaq 0xab7(%rip), %rdi       # 0x2094
leaq 0xa5e(%rip), %rsi       # 0x2042
movb $0x0, %al
callq 0x1050
movl -0x8(%rbp), %edi
leaq -0x10b0(%rbp), %rsi
movl $0xf9f, %edx            # imm = 0xF9F
xorl %ecx, %ecx
leaq -0xd0(%rbp), %r8
leaq -0xd4(%rbp), %r9
callq 0x10c0
movl %eax, -0x50(%rbp)
cmpl $-0x1, %eax
jne 0x162d
leaq 0xaab(%rip), %rdi       # 0x20c9
callq 0x10f0
movl $0x1, %edi
callq 0x1130
movslq -0x50(%rbp), %rax
movb $0x0, -0x10b0(%rbp,%rax)
leaq -0x2070(%rbp), %rdi
leaq -0x10b0(%rbp), %rsi
callq 0x12c0
movq -0x2070(%rbp), %rdi
leaq -0x2050(%rbp), %rsi
callq 0x1330
movq -0x2070(%rbp), %rsi
leaq 0xa65(%rip), %rdi       # 0x20d2
movb $0x0, %al
callq 0x1050
movl -0x8(%rbp), %eax
movl %eax, -0x2084(%rbp)
leaq -0x2050(%rbp), %rax
movq %rax, -0x2080(%rbp)
leaq -0x2050(%rbp), %rdi
callq 0x1040
movl -0x2084(%rbp), %edi
movq -0x2080(%rbp), %rsi
movq %rax, %rdx
movl -0xd4(%rbp), %r9d
xorl %ecx, %ecx
leaq -0xd0(%rbp), %r8
callq 0x1080
movl %eax, -0x50(%rbp)
cmpl $-0x1, -0x50(%rbp)
jne 0x16db
leaq 0xa43(%rip), %rdi       # 0x210f
callq 0x10f0
movl $0x1, %edi
callq 0x1130
leaq -0x2050(%rbp), %rdi
leaq 0x941(%rip), %rsi       # 0x202a
callq 0x10a0
cmpl $0x0, %eax
jne 0x1718
movq -0x2070(%rbp), %rsi
leaq 0xa1f(%rip), %rdi       # 0x2120
movb $0x0, %al
callq 0x1050
leaq 0xa46(%rip), %rdi       # 0x2155
movb $0x0, %al
callq 0x1050
jmp 0x1726
leaq 0xa66(%rip), %rdi       # 0x2185
movb $0x0, %al
callq 0x1050
jmp 0x15eb
movl -0x4(%rbp), %eax
addq $0x2090, %rsp           # imm = 0x2090
popq %rbp
retq