int * table_to_mem(TABLE *t) { int len,long_count; int k,i,rows;int * buff; Triple Qin,*Qout;char tmp[9]; Code stmt; start_table(t,pop_operator); stmt = get_ready_stmt(); // peek at the header i = machine_step_fetch(&Qin,0); rows = Qin.pointer; buff = (int *) G_malloc(rows*sizeof(Triple)+8); long_count=3*rows; //three fixed longs per row Qout = (Triple *) (buff+2); for(i=0;i<rows;i++) { if(i) machine_step_fetch(&Qin,0); len = machine_key_len(stmt); k =1+ (len+3) /4; // count longs, word count and key keys have bytes countsdata if(k==0) printf("error\n"); // make this append (qin,qout); *Qout = Qin; Qout->key = (char *) G_malloc(4*k+1); long_count += k; Qout->key[len+4]=0; sprintf(Qout->key,"%4d",len); memcpy(Qout->key+4,Qin.key,len); DBG_printf("TM%s%c%3d\n",Qout->key,Qin.link,Qout->pointer); Qout++; } sprintf(tmp,"%8d",long_count); memcpy(buff,tmp,8); return buff; }
int mem_to_table(TABLE* table,int * buff,int mode) { int rows,total; int i;Triple *data; Triple * Qson; start_table((TABLE *) table,append_operator); data = &(table)->operators[append_data]; Qson = (Triple *) (buff+2); rows = Qson[0].pointer; total = 0; for(i=0;i<rows;i++) { *data = Qson[i]; DBG_printf("MT%s%c%3d\n",Qson[i].key,Qson[i].link,Qson[i].pointer); //len = (int) Qson.key; // for blob bind machine_reset(table->stmt); bind_code(&(table)->operators[append_operator],table->stmt); machine_step(table->stmt); } return rows; }
int table_to_Json(TABLE *t,IO_Structure * to) { // set some stuf up to restore Json brackets struct {int count;int total;} brk[8];int cur; char prev; int len; int i,rows; Triple Qin;char tmp[9]; Code stmt; cur = 0; start_table(t,select_operator); stmt = get_ready_stmt(); // peek at the header i = machine_step_fetch(&Qin,0); rows = Qin.pointer; prev=Qin.link; brk[cur].total = Qin.pointer; brk[cur].count = 0; for(i=0;i<rows;i++) { if(i) machine_step_fetch(&Qin,0); len = machine_key_len(stmt); // mke a stream json send_(Qin.key,len,to); send_(tmp,sprintf(tmp,"%c",Qin.link),to); // restor brckets brk[cur].count++; while(brk[cur].count == brk[cur].total) { cur--;brk[cur].count += brk[cur+1].count; send_("}",1,to); } if((Qin.pointer > 1) && (prev != '.') && ( prev != ',')) { cur++; brk[cur].count = 0;brk[cur].total = Qin.pointer; send_("{",1,to); } prev=Qin.link; } DBG_printf("TJ%s%c%3d\n",Qin.key,Qin.link,Qin.pointer); return 0; }
void *srv_thread_function(void *arg) { printf("server thread started!\n"); // hooray // initializing variables ptsrv_thread_args targs = (ptsrv_thread_args) arg; sim = targs->sim; int PORT = targs->port; int socketfd; /* Socket file descriptor */ struct sockaddr_in server_name; /* server socket's address */ struct sockaddr_in client_name; /* client socket's address */ fd_set active_fd_set, read_fd_set; /* Array of file descriptors */ int nbytes; char buffer[BUFFER_SIZE]; int socketdest; char* pacote; char str_aux[80]; FILE* motd; char name[150]; //robot name buffer table = start_table(); /* Create the socket. socket (NAMESPACE, STYLE, PROTOCOL) - namespace: local, other, internet: PF_INET || PF_INET6 - style: SOCK_STREAM, SOCK_DGRAM - protocol: 0 means 'auto' */ socketfd = socket (PF_INET, SOCK_STREAM, 0); if (socketfd < 0) fatal (); /* Give the socket a name. */ server_name.sin_family = AF_INET; /* Ineternet family */ server_name.sin_port = htons (PORT); /* set port */ server_name.sin_addr.s_addr = htonl (INADDR_ANY); /* Any internetface */ /* Bind the socket to the port. */ if (bind (socketfd, (struct sockaddr *) &server_name, sizeof (server_name)) < 0) fatal (); // 23 apr 2010 - Disabling Nagle algorithm in the protocol stack, so it sends packages // immeadiately, without waiting for more bytes to be available to send, which is more // appropriate for this real-time program, char flag = 1; setsockopt(socketfd, IPPROTO_TCP, 1, (char *) &flag, sizeof(int)); /* Tell the OS to start to listen to the socket. */ if (listen (socketfd, 1) < 0) fatal (); /* Initialize the set of active sockets. */ FD_ZERO (&active_fd_set); FD_SET (socketfd, &active_fd_set); for (;;) { int i,j; /* Block until there is activity in the socket. */ read_fd_set = active_fd_set; if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0) fatal (); /* Service all the sockets with input pending. */ for (i = 0; i < FD_SETSIZE; ++i) { if (FD_ISSET (i, &read_fd_set)) { if (i == socketfd) { /* Connection request on original socket. */ int new_socket; socklen_t size; /* Accept the the connection and associate it with a new socket. */ size = sizeof (client_name); new_socket = accept (socketfd, (struct sockaddr *) &client_name, &size); if (new_socket < 0) fatal (); fprintf (stderr, "Server: connect from host %s, port %hd.\n", inet_ntoa (client_name.sin_addr), ntohs (client_name.sin_port)); FD_SET (new_socket, &active_fd_set); /* Add the new socket to the set of active fd */ // adding new user to the table //add_user (table, inet_ntoa (client_name.sin_addr), new_socket); add_user (table, new_socket); } else { /* Data arriving on an already-connected socket. */ nbytes = read (i, buffer, BUFFER_SIZE); buffer[nbytes-1] = 0; if (nbytes ==0) { /* Cient ended the transmission */ printf ("Client disconnected.\n"); remove_user (table, i); print_table (table); close (i); FD_CLR (i, &active_fd_set); } else if (nbytes < 0) { /* Read error */ fprintf (stderr, "Read error, client disconnected.\n"); remove_user (table, i); print_table (table); close (i); FD_CLR (i, &active_fd_set); } else { // client message //debug //printf("%s\n", buffer); if (IsPackageValid(buffer, nbytes)) { if (NewRobot(buffer, name)) { //new robot connected, get name printf("New robot added: %s\n", name); set_id(table, i, name); //add the new name to the table strcpy(sim->robotnames[sim->num_available_clients], name); sim->num_available_clients++; print_table (table); sim->clientlist_update = true; //CreateUpdate(buffer, &nbytes, sim->GetRobot(get_id(table, i))); //write(i, buffer, nbytes); } else { //update settings for this robot if (GetUpdate(buffer, nbytes, sim->GetRobot(get_id(table, i))) ) { //printf("got update from %s\n", sim->GetRobot(get_id(table, i))->robot_name); CreateUpdate(buffer, &nbytes, sim->GetRobot(get_id(table, i))); write(i, buffer, nbytes); } else { //printf("pacote de ok\n"); CreateUpdate(buffer, &nbytes, sim->GetRobot(get_id(table, i))); write(i, buffer, nbytes); } } } //nbytes = write (i, buffer, strlen(buffer) + 1); /*switch (get_tipo(buffer)) { case 2: // mensagem privada; nome = get_dest(buffer); socketdest = busca_usuario(tabela, nome); if (socketdest) { nbytes = write (socketdest, buffer, strlen(buffer) + 1); nbytes = write (i, buffer, strlen(buffer) + 1); } free(nome); break; case 3: //mensagem de sauda��o de novo usuario nome = get_rem(buffer); adiciona_usuario(tabela, nome, i); imprime_tabela(tabela); free(nome); if ((motd = fopen("motd", "r")) != NULL) { // abre o Message Of The Day while (fgets(str_aux,79, motd)) { pacote = monta_pacote_sis(str_aux, nome); nbytes = write (i, pacote, pacote[0]); usleep(2000); } fclose(motd); } default: // mensagem comum (tambem repassa para todos os usuarios a mensagem de saudacao - case 3 nao tem break) for (j = 3; j < FD_SETSIZE; ++j) { if (j != socketfd) nbytes = write (j, buffer, strlen(buffer) + 1); } break; case 4: // mensagens do sistema switch (get_sis(buffer)) { case 'l': // cliente requisitando a listagem de usuarios conectados reset_iterator(tabela); nome = get_rem(buffer); strcpy(str_aux, "Usuarios conectados:\n"); pacote = monta_pacote_sis(str_aux, nome); nbytes = write (i, pacote, pacote[0]); free(pacote); do { usleep(2000); strcpy(str_aux, get_next_nome(tabela)); strcat(str_aux, "\n"); pacote = monta_pacote_sis(str_aux, nome); nbytes = write (i, pacote, pacote[0]); free(pacote); } while (! iterator_finished(tabela)); usleep(2000); strcpy(str_aux, "Fim da listagem.\n"); pacote = monta_pacote_sis(str_aux, nome); nbytes = write (i, pacote, pacote[0]); free(pacote); break; } break; }*/ } } } } } }