// File: /workspace/github_c_makefile_star2_O1/umbrant[P]Non-blocking-Buffers/nbb.c
// Function: nbb_recv_data
void nbb_recv_data(int signum)
{
  int i;
  char* recv;
  size_t recv_len = 0;
  int retval = -1;
  // char* reply_msg;
  int is_new_conn_msg = 0;

  // Attempt to debug Qt. XXX: Remove when done.
  printf("***NBB***: Inside signal handler\n");

  // Since i = 0 is already reserved for nameserver
  for(i = 1;channel_list[i].in_use && i < SERVICE_MAX_CHANNELS;i++) {
    retval = nbb_read_item(i, (void**)&recv, &recv_len);

    if(retval == OK) {
      if (memcmp(recv, NEW_CONN_NOTIFY_MSG, NEW_CONN_NOTIFY_MSG_LEN) == 0) {       
        recv = (char*)realloc(recv, recv_len + 1);
        recv[recv_len] = '\0';

        char* tmp = NULL;

/*
				char* msg_start = (char *) recv + NEW_CONN_NOTIFY_MSG_LEN + 1;
				int name_len = 0;

				connected_nodes[i].pid = (int) strtol(msg_start, &tmp, 10);
				name_len = recv_len - 1 - NEW_CONN_NOTIFY_MSG_LEN - ((int) (tmp - recv));
				connected_nodes[i].name = (char *) malloc(sizeof(char) * (name_len+1));
				assert(connected_nodes[i].name != NULL);
				strncpy(connected_nodes[i].name, tmp + 1, name_len);
				connected_nodes[i].name[name_len] = '\0';
*/

        strtok(recv, " ");
        tmp = strtok(NULL, " ");
        connected_nodes[i].pid = atoi(tmp);
        tmp = strtok(NULL, " ");
        connected_nodes[i].name = (char*)malloc(strlen(tmp) * sizeof(char));
        strcpy(connected_nodes[i].name, tmp);


        printf("***NBB***: New connection on slot %d from client_name: %s with pid: %d\n", i, connected_nodes[i].name, connected_nodes[i].pid);

        is_new_conn_msg = 1;
      }

      // Notify of new connection on slot i
      if (is_new_conn_msg && channel_list[i].new_conn != NULL) {
        channel_list[i].new_conn(i, channel_list[i].arg);
      }

      // We only have new data when the msg is "real data" (not our initial msg).
      // Notify event of new available data on slot i
      if (!is_new_conn_msg && channel_list[i].new_data != NULL) {
        channel_list[i].new_data(i);
      }

      printf("** Received '%.*s' from shm id %d\n",
             (int) recv_len, recv, channel_list[i].read_id);

      if (!is_new_conn_msg) {
        nbb_flush_shm(i, recv, recv_len);
      }

      // XXX: This is for debugging. Remove before production.
      // Reply message
      /* reply_msg = (char*)calloc(recv_len, sizeof(char));
      memcpy(reply_msg, recv, recv_len);
      nbb_insert_item(i, reply_msg, recv_len);
      free(reply_msg); */

      recv_len = 0; 
      free(recv);
    }

  }

  signal(SIGUSR1, nbb_recv_data);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq $0x0, 0x8(%rsp)
leaq 0x1ab8(%rip), %rdi      # 0x39dc
callq 0x1070
cmpl $0x0, 0x42b8(%rip)      # 0x61e8
je 0x20c2
movl $0x1, %ebx
leaq 0x4746(%rip), %r12      # 0x6688
leaq 0x425f(%rip), %r14      # 0x61a8
leaq 0x10(%rsp), %r13
leaq 0x8(%rsp), %r15
xorl %ebp, %ebp
movl %ebx, %edi
movq %r13, %rsi
movq %r15, %rdx
callq 0x1ac4
testl %eax, %eax
jne 0x20a3
movq 0x10(%rsp), %rdi
movl (%rdi), %eax
movl $0x5f512a2a, %ecx       # imm = 0x5F512A2A
xorl %ecx, %eax
movl 0x3(%rdi), %ecx
movl $0x2a2a515f, %edx       # imm = 0x2A2A515F
xorl %edx, %ecx
orl %eax, %ecx
jne 0x2024
movq 0x8(%rsp), %rsi
incq %rsi
callq 0x1170
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq %rax, %rdi
leaq 0x10ec(%rip), %r15      # 0x309b
movq %r15, %rsi
callq 0x11c0
xorl %edi, %edi
movq %r15, %rsi
callq 0x11c0
movq %rax, %rdi
callq 0x11f0
movl %eax, (%r12)
xorl %edi, %edi
movq %r15, %rsi
callq 0x11c0
movq %r13, %r15
movq %rax, %r13
movq %rax, %rdi
callq 0x10b0
movq %rax, %rdi
callq 0x1160
movq %rax, %rbp
movq %rax, -0x8(%r12)
movq %rax, %rdi
movq %r13, %rsi
movq %r15, %r13
leaq 0x8(%rsp), %r15
callq 0x1040
movl (%r12), %ecx
leaq 0x15c4(%rip), %rdi      # 0x35d7
movl %ebx, %esi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x10c0
movl $0x1, %ebp
testl %ebp, %ebp
je 0x2055
movq 0x28(%r14), %rax
testq %rax, %rax
je 0x2039
movq 0x38(%r14), %rsi
movl %ebx, %edi
callq *%rax
movl 0x8(%rsp), %esi
movq 0x10(%rsp), %rdx
movl (%r14), %ecx
leaq 0x15d3(%rip), %rdi      # 0x361f
xorl %eax, %eax
callq 0x10c0
jmp 0x2090
movq 0x30(%r14), %rax
testq %rax, %rax
je 0x2062
movl %ebx, %edi
callq *%rax
movl 0x8(%rsp), %esi
movq 0x10(%rsp), %rdx
movl (%r14), %ecx
leaq 0x15aa(%rip), %rdi      # 0x361f
xorl %eax, %eax
callq 0x10c0
testl %ebp, %ebp
jne 0x2090
movq 0x10(%rsp), %rsi
movl 0x8(%rsp), %edx
movl %ebx, %edi
callq 0x25b8
movq $0x0, 0x8(%rsp)
movq 0x10(%rsp), %rdi
callq 0x1030
cmpq $0xd, %rbx
ja 0x20c2
incq %rbx
addq $0x10, %r12
cmpl $0x0, 0x98(%r14)
leaq 0x58(%r14), %r14
jne 0x1f55
leaq -0x1c3(%rip), %rsi      # 0x1f06
movl $0xa, %edi
callq 0x1140
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq