// File: /workspace/github_c_makefile_star2_O2/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
leaq 0x10(%rsp), %r13
andq $0x0, (%r13)
leaq 0x1b19(%rip), %rdi      # 0x39dc
callq 0x1070
pushq $0x1
popq %r14
leaq 0x47b5(%rip), %rbp      # 0x6688
leaq 0x430e(%rip), %rbx      # 0x61e8
leaq 0x8(%rsp), %r15
xorl %r12d, %r12d
cmpq $0xe, %r14
ja 0x2059
cmpl $0x0, (%rbx)
je 0x2059
movl %r14d, %edi
movq %r15, %rsi
movq %r13, %rdx
callq 0x1a82
testl %eax, %eax
jne 0x2049
movq %r13, %r15
movq 0x8(%rsp), %r13
movq %r13, %rdi
leaq 0x14d4(%rip), %rsi      # 0x33f1
pushq $0x7
popq %rdx
callq 0x1170
testl %eax, %eax
je 0x1f76
testl %r12d, %r12d
jne 0x2005
movq -0x10(%rbx), %rax
testq %rax, %rax
je 0x1f45
movl %r14d, %edi
callq *%rax
movq 0x8(%rsp), %r13
movl 0x10(%rsp), %esi
movl -0x40(%rbx), %ecx
xorl %r12d, %r12d
leaq 0x16c9(%rip), %rdi      # 0x361f
movq %r13, %rdx
xorl %eax, %eax
callq 0x10c0
movq 0x8(%rsp), %rsi
movl 0x10(%rsp), %edx
movl %r14d, %edi
callq 0x2532
jmp 0x2031
movq 0x10(%rsp), %rsi
incq %rsi
movq %r13, %rdi
callq 0x1180
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq %rax, %rdi
leaq 0x10fd(%rip), %r12      # 0x309b
movq %r12, %rsi
callq 0x11d0
xorl %edi, %edi
movq %r12, %rsi
callq 0x11d0
movq %rax, %rdi
callq 0x1200
movl %eax, (%rbp)
xorl %edi, %edi
movq %r12, %rsi
callq 0x11d0
movq %rax, %r13
movq %rax, %rdi
callq 0x10b0
movq %rax, %rdi
callq 0x1160
movq %rax, %r12
movq %rax, -0x8(%rbp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x1040
movl (%rbp), %ecx
leaq 0x15e3(%rip), %rdi      # 0x35d7
movl %r14d, %esi
movq %r12, %rdx
xorl %eax, %eax
callq 0x10c0
pushq $0x1
popq %r12
movq -0x18(%rbx), %rax
testq %rax, %rax
je 0x2017
movq -0x8(%rbx), %rsi
movl %r14d, %edi
callq *%rax
movl 0x10(%rsp), %esi
movq 0x8(%rsp), %rdx
movl -0x40(%rbx), %ecx
leaq 0x15f5(%rip), %rdi      # 0x361f
xorl %eax, %eax
callq 0x10c0
movq %r15, %r13
andq $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rdi
callq 0x1030
leaq 0x8(%rsp), %r15
incq %r14
addq $0x10, %rbp
addq $0x58, %rbx
jmp 0x1ee2
leaq -0x1bc(%rip), %rsi      # 0x1ea4
pushq $0xa
popq %rdi
callq 0x1140
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq