static int verify_existence(struct starsystem_info *ss, int should_already_exist) { unsigned char pwdhash[20]; unsigned char buffer[200]; struct packed_buffer *pb; int i, rc; unsigned char pass; unsigned char printable_hash[100]; int send_update = 0; fprintf(stderr, "snis_multiverse: verify_existence 1: should %salready exist\n", should_already_exist ? "" : "not "); rc = read_and_unpack_fixed_size_buffer(ss, buffer, 20, "r", pwdhash, (uint16_t) 20); if (rc != 0) return rc; snis_format_sha1_hash(pwdhash, printable_hash, 100); printable_hash[40] = '\0'; pthread_mutex_lock(&data_mutex); fprintf(stderr, "snis_multiverse: lookup hash %s\n", printable_hash); i = lookup_ship_by_hash(pwdhash); fprintf(stderr, "snis_multiverse: verify existence pwdhash lookup returns %d\n", i); if (i < 0) { if (should_already_exist) { /* It doesn't exist, but it should */ fprintf(stderr, "snis_multiverse: hash %s does not exist, but should.\n", printable_hash); pass = SNISMV_VERIFICATION_RESPONSE_FAIL; } else { /* doesn't exist, but we asked to create it, so that is expected */ fprintf(stderr, "snis_multiverse: hash %s does not exist, as expected.\n", printable_hash); pass = SNISMV_VERIFICATION_RESPONSE_PASS; rc = create_new_ship(pwdhash); if (rc) /* We failed to create it. */ pass = SNISMV_VERIFICATION_RESPONSE_TOO_MANY_BRIDGES; } } else { /* It exists, pass if it should exist, fail otherwise */ if (should_already_exist) { fprintf(stderr, "snis_multiverse: hash %s exists, as expected.\n", printable_hash); pass = SNISMV_VERIFICATION_RESPONSE_PASS; send_update = 1; } else { fprintf(stderr, "snis_multiverse: hash %s exists, but should not.\n", printable_hash); pass = SNISMV_VERIFICATION_RESPONSE_FAIL; } } print_hash("checking hash ", pwdhash); fprintf(stderr, "snis_multiverse: verify existence pass=%d\n", pass); pthread_mutex_unlock(&data_mutex); pb = packed_buffer_allocate(22); packed_buffer_append(pb, "bbr", SNISMV_OPCODE_VERIFICATION_RESPONSE, pass, pwdhash, 20); packed_buffer_queue_add(&ss->write_queue, pb, &ss->write_queue_mutex); if (send_update) send_bridge_update_to_snis_server(ss, pwdhash); return 0; }
int main (int argc, char *argv[]) { Ship* ship = NULL; ALLEGRO_TIMER *main_timer = NULL; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_COLOR cyan = al_map_rgb(0,255,255); ALLEGRO_BITMAP *space_background = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; bool redraw = true; ALLEGRO_EVENT ev; if(!al_init()) { fprintf(stderr, "Error initializing Allegro"); exit(EXIT_FAILURE); } if(!al_init_image_addon()) { fprintf(stderr, "Error initializing Allegro"); exit(EXIT_FAILURE); } if(!al_install_keyboard()) { fprintf(stderr, "Error initializing keyboard\n"); exit(EXIT_FAILURE); } display=al_create_display(W, H); if(display == NULL) { fprintf(stderr, "Error creating main display"); exit(EXIT_FAILURE); } space_background = al_load_bitmap(BACKGROUND_FILE); if(space_background == NULL) { fprintf(stderr, "Error loading background\n"); exit(EXIT_FAILURE); } ship=create_new_ship("../data/spaceship.png"); ship->current_dir = DOWN; al_set_target_backbuffer(display); al_draw_bitmap(space_background, 0, 0, 0); print_ship_on_screen(ship); al_flip_display(); main_timer=al_create_timer(1.0/FPS); if(main_timer == NULL) { fprintf(stderr, "Error creating main timer\n"); exit(EXIT_FAILURE); } event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_timer_event_source(main_timer)); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_start_timer(main_timer); while(1) { al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else { switch (ev.type) { case ALLEGRO_EVENT_TIMER: redraw = true; break; case ALLEGRO_EVENT_KEY_DOWN: switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: ship->is_moving = 1; ship->current_dir = UP; break; case ALLEGRO_KEY_DOWN: ship->is_moving = 1; ship->current_dir = DOWN; break; case ALLEGRO_KEY_LEFT: ship->is_moving = 1; ship->current_dir = LEFT; break; case ALLEGRO_KEY_RIGHT: ship->is_moving = 1; ship->current_dir = RIGHT; break; default: break; } break; case ALLEGRO_EVENT_KEY_UP: ship->is_moving = 0; break; default: break; } } if(redraw && al_event_queue_is_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap(space_background, 0, 0, 0); move_ship(ship); animate_ship(ship); print_ship_on_screen(ship); al_flip_display(); } } al_destroy_timer(main_timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }