// File: /workspace/github_c_makefile_star2_O3/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 0x1aba(%rip), %rdi      # 0x39dc
callq 0x1070
cmpl $0x0, 0x42ba(%rip)      # 0x61e8
je 0x20c4
movl $0x1, %ebx
leaq 0x4748(%rip), %r12      # 0x6688
leaq 0x4261(%rip), %r14      # 0x61a8
leaq 0x10(%rsp), %r13
leaq 0x8(%rsp), %r15
xorl %ebp, %ebp
movl %ebx, %edi
movq %r13, %rsi
movq %r15, %rdx
callq 0x1ace
testl %eax, %eax
jne 0x20a5
movq 0x10(%rsp), %rdx
movl (%rdx), %eax
movl $0x5f512a2a, %ecx       # imm = 0x5F512A2A
xorl %ecx, %eax
movl 0x3(%rdx), %ecx
movl $0x2a2a515f, %esi       # imm = 0x2A2A515F
xorl %esi, %ecx
orl %eax, %ecx
je 0x1fca
testl %ebp, %ebp
jne 0x2067
movq 0x30(%r14), %rax
testq %rax, %rax
je 0x1f9e
movl %ebx, %edi
callq *%rax
movq 0x10(%rsp), %rdx
movl 0x8(%rsp), %esi
movl (%r14), %ecx
xorl %ebp, %ebp
leaq 0x1671(%rip), %rdi      # 0x361f
xorl %eax, %eax
callq 0x10c0
movq 0x10(%rsp), %rsi
movl 0x8(%rsp), %edx
movl %ebx, %edi
callq 0x25a9
jmp 0x2092
movq 0x8(%rsp), %rsi
incq %rsi
movq %rdx, %rdi
callq 0x1170
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq %rax, %rdi
leaq 0x10a9(%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 0x1581(%rip), %rdi      # 0x35d7
movl %ebx, %esi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x10c0
movl $0x1, %ebp
movq 0x28(%r14), %rax
testq %rax, %rax
je 0x2078
movq 0x38(%r14), %rsi
movl %ebx, %edi
callq *%rax
movl 0x8(%rsp), %esi
movq 0x10(%rsp), %rdx
movl (%r14), %ecx
leaq 0x1594(%rip), %rdi      # 0x361f
xorl %eax, %eax
callq 0x10c0
movq $0x0, 0x8(%rsp)
movq 0x10(%rsp), %rdi
callq 0x1030
cmpq $0xd, %rbx
ja 0x20c4
incq %rbx
addq $0x10, %r12
cmpl $0x0, 0x98(%r14)
leaq 0x58(%r14), %r14
jne 0x1f53
leaq -0x1c7(%rip), %rsi      # 0x1f04
movl $0xa, %edi
callq 0x1140
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq