void * pipes_function(void * pointer) { INTERF *interf = (INTERF *) pointer; int i; char cadena[10]; ini_pipes(); calculate_next(); copy_pipes(); for (i = 0; i < NUM_PIPES; i++) current_pipes[i]->position = 123 + 35 * i; listo1 = 1; while (!crash() && !fcrash) { for (i = 0; i < NUM_PIPES && !fcrash; i++) { usleep(25000); sprintf(cadena, "%d", score); write_interf(interf, 0, 13, 70, cadena); current_pipes[i]->position--; print_pipe(interf, current_pipes[i]); if (current_pipes[i]->position <= 0) { erase_first(interf); score++; calculate_next_u(i); copy_pipes_u(i); current_pipes[i]->position = 87 + 123; } } } pthread_exit(NULL); }
/* print/dump/visualize a command list */ void parser_print(cmds* cmd) { if (cmd==NULL) { printf("NULL\n"); return; } switch (cmd->kind) { case EXIT: printf("EXIT "); break; case CD: printf("CD %s ",cmd->cd.path); break; case PROG: print_prog(&cmd->prog); break; case PIPE: print_pipe(cmd); break; case ENV: if (cmd->env.value != NULL) { printf("SET %s=%s ",cmd->env.name,cmd->env.value); } else { printf("UNSET %s ",cmd->env.name); } break; case JOB: switch (cmd->job.kind) { case INFO: printf("JOBS "); break; case BG: printf("BG "); break; case FG: printf("FG "); } if (cmd->job.id!=-1) printf("%d ",cmd->job.id); break; } if (cmd->next!=NULL) { printf(";\n"); parser_print(cmd->next); } else { printf("\n"); } }
int main(int argc, char* argv[]) { struct pipe p; void* h; void* h0, *h1, *h2; void* b; const void* b0, *b1, *b2; int s0, s1, s2; // init printf("%d\n", init_pipe(&p, 3, 2, 0x1)); #if 0 //get and push h0 = get_buf(&p, &b); printf("%p %p\n", h0, b); printf("%d\n", push_buf(&p, h0, 1)); print_pipe(&p); h0 = pull_buf(&p, 0, &b0, &s0); h1 = pull_buf(&p, 1, &b1, &s1); h2 = pull_buf(&p, 2, &b2, &s2); put_buf(&p, h0); put_buf(&p, h1); put_buf(&p, h2); print_pipe(&p); h0 = get_buf(&p, &b); printf("%p %p\n", h0, b); printf("%d\n", push_buf(&p, h0, 1)); #endif // src push 0 1 2 h = get_buf(&p, &b); push_buf(&p, h, 1); // src push 0 1 2 h = get_buf(&p, &b); push_buf(&p, h, 2); // dst pull 1 2 h2 = pull_buf(&p, 2, &b2, &s2); put_buf(&p, h2); h1 = pull_buf(&p, 1, &b1, &s1); put_buf(&p, h1); //print_pipe(&p); exit(0); // src push 1 2 (0 is full) h = get_buf(&p, &b); push_buf(&p, h, 3); //print_pipe(&p); exit(0); // dst pull 1 2 h2 = pull_buf(&p, 2, &b2, &s2); put_buf(&p, h2); h1 = pull_buf(&p, 1, &b1, &s1); put_buf(&p, h1); //print_pipe(&p); exit(0); // dst pull 1 2 h2 = pull_buf(&p, 2, &b2, &s2); put_buf(&p, h2); h1 = pull_buf(&p, 1, &b1, &s1); put_buf(&p, h1); //print_pipe(&p); exit(0); // dst pull 0 h0 = pull_buf(&p, 0, &b0, &s0); put_buf(&p, h0); // dst pull 0 h0 = pull_buf(&p, 0, &b0, &s0); put_buf(&p, h0); //print_pipe(&p); exit(0); // src push 0 1 2 h = get_buf(&p, &b); push_buf(&p, h, 4); // src push 0 1 2 h = get_buf(&p, &b); push_buf(&p, h, 5); // src push 0 1 2 h = get_buf(&p, &b); push_buf(&p, h, 6); // src push 0 1 2 h = get_buf(&p, &b); push_buf(&p, h, 7); //print_pipe(&p); exit(0); // dst pull 0 h0 = pull_buf(&p, 0, &b0, &s0); put_buf(&p, h0); // dst pull 0 h0 = pull_buf(&p, 0, &b0, &s0); put_buf(&p, h0); h0 = pull_buf(&p, 0, &b0, &s0); printf("%p\n", h0); print_pipe(&p); exit(0); //should be src(0), dst0(0), dst1(2), dst2(2) #if 0 struct queue q; void* p; init_queue(&q, 5); printf("%d\n", enqueue(&q, (void*)1)); printf("%d\n", enqueue(&q, (void*)2)); printf("%d\n", enqueue(&q, (void*)3)); printf("%d\n", enqueue(&q, (void*)4)); printf("%d\n", enqueue(&q, (void*)5)); printf("%d\n", enqueue(&q, (void*)6)); printf("\n"); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); printf("\n\n"); init_queue(&q, 7); printf("%d\n", enqueue(&q, (void*)1)); printf("%d\n", enqueue(&q, (void*)2)); printf("%d\n", enqueue(&q, (void*)3)); printf("%d\n", enqueue(&q, (void*)4)); printf("%d\n", enqueue(&q, (void*)5)); printf("%d\n", enqueue(&q, (void*)6)); printf("%d\n", enqueue(&q, (void*)7)); printf("%d\n", enqueue(&q, (void*)8)); printf("\n"); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); printf("%d ", dequeue(&q, &p)); printf("%p\n", p); #endif return 0; }
// apply socket options related to ipfw commands static int apply_socket_options(int s, int option_id, void *option, socklen_t option_length) { // choose the action corresponding to the given command switch(option_id) { // add a rule to the firewall (the rule contains dummynet pipes); // option should be of type "struct ip_fw*" case IP_FW_ADD: #ifdef MESSAGE_DEBUG DEBUG("Add ipfw rule: "); print_rule((struct ip_fw*)option); #endif if(getsockopt(s, IPPROTO_IP, option_id, option, &option_length) < 0) { WARNING("Error getting socket options"); perror("getsockopt"); return ERROR; } break; // delete a firewall rule (the rule contains dummynet pipes); // option should be of type "u_int32_t *" case IP_FW_DEL: DEBUG("Delete ipfw rule #%d", (*(u_int32_t *)option)); if(setsockopt(s, IPPROTO_IP, option_id, option, option_length)<0) { WARNING("Error setting socket options"); perror("setsockopt"); return ERROR; } break; // configure a dummynet pipe; // option should be of type "dn_pipe *" case IP_DUMMYNET_CONFIGURE: #ifdef MESSAGE_DEBUG DEBUG("Configure ipfw dummynet pipe: "); print_pipe((struct dn_pipe*)option); #endif if(setsockopt(s, IPPROTO_IP, option_id, option, option_length)<0) { WARNING("Error setting socket options"); perror("setsockopt"); return ERROR; } break; /* FUTURE PLAN: DELETE _PIPES_ INSTEAD OF RULES case IP_DUMMYNET_DEL: printf("delete pipe: option_id=%d option=%d option_length=%d\n", option_id, (*(int16_t*)(option)), option_length); if(setsockopt(s, IPPROTO_IP, option_id, option, option_length)<0) { WARNING("Error setting socket options"); perror("setsockopt"); return ERROR; } break; */ default: WARNING("Unrecognized ipfw socket option: %d", option_id); return -1; } return 0; }