Exemplo n.º 1
0
void circuit()
{
     int r, c;
     int do_cut;
     int radius;
     int centerx, centery;


     clear_invert_map();

     grid = (char **) new_grid(map.num_row, map.num_col, sizeof(char));
     count = (int *) new_array(map.num_row, sizeof(int));

     radius = (map.sec_width > map.sec_height ? map.sec_width : map.sec_height)/4.0;
     radius = (radius == 0 ? 1 : radius );

     line_size = (map.sec_width > map.sec_height ? map.sec_width : map.sec_height)/4.0;
     line_size = (line_size == 0 ? 1 : line_size);

     for( r = 0; r < map.num_row; r++ )
     {
          count[r] = 0;
          for( c = 0; c < map.num_col; c++ )
          {
               if( (r == 0 || r == map.num_row-1)
               && (c == 0 || c == map.num_col/2 || c == map.num_col-1) )
                    do_cut = 1;
               else
                    do_cut = rand()%4;

               if( do_cut != 1 )
               {
                    grid[r][c] = 0;
                    continue;
               }

               section_center(&centerx, &centery, r, c);
               circlefill(map.map, centerx, centery, radius, 255);

               grid[r][c] = 1;
               count[r]++;
          }
     }


     connect_rows();
     connect_front();
     connect_back();
     connect_mid();

     delete_grid((void **)grid, map.num_row);
     delete_array(count);

     /* redraw outline */
     /* TODO: once in a while if cuts off the edge.. */
     rect(map.map, 0, 0, map.width-1, map.height-1, 0);

return;
}
Exemplo n.º 2
0
PROCESS_THREAD(tftpd_process, ev, data)
{
    static struct etimer t;
    static tftp_header *h;
    static int len, block, ack;
    static int tries;
    static int fd = -1;
#if WITH_EXEC
    static char *elf_err;
#endif

    PROCESS_BEGIN();

    etimer_set(&t, CLOCK_CONF_SECOND*3);
    PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);

    setup_server();
#if WITH_EXEC
    elfloader_init();
#endif

	print_local_addresses();

    while(1) {
        /* connection from client */
        RECV_PACKET(h);
        len = 0;
        init_config();

        if(h->op == uip_htons(TFTP_RRQ)) {
            connect_back();
            PRINTF("< rrq for %s\n", h->filename);
            len += strlen(h->filename)+1;

            if(strcmp("octet", h->filename+len)) {
                send_error(EUNDEF, "only octet mode supported");
                goto close_connection;
            }
            len += strlen(h->filename+len)+1; /* skip mode */

            parse_opts(h->options+len, uip_datalen()-len-2);

            if(config.to_ack & OACK_ERROR) {
                send_error(EOPTNEG, "");
                goto close_connection;
            } 

            fd = cfs_open(h->filename, CFS_READ);
            if(fd<0) {
                send_error(ENOTFOUND, "");
                goto close_connection;
            }

            block = 0; ack = 0;
            tries = TFTP_MAXTRIES;

            PRINTF("starting transfer...\n");

            for(;;) {
                if(send_oack())
                    len = config.blksize; /* XXX hack to prevent loop exit*/
                else
                    len = send_data(fd, block+1);

                if(len<0) {
                    send_error(EUNDEF, "read failed");
                    goto close_file;
                }

                RECV_PACKET_TIMEOUT(h,t);

                if(ev == PROCESS_EVENT_TIMER) {
                    PRINTF("ack timed out, tries left: %d\n", tries);
                    if(--tries<=0) goto close_file;
                    continue;
                }

                if(h->op != uip_htons(TFTP_ACK)) {
                    send_error(EBADOP, "");
                    goto close_file;
                }

                config.to_ack = 0;
                tries = TFTP_MAXTRIES;
                ack = uip_htons(h->block_nr);
                if(ack == block+1) block++;

                if(len < config.blksize && ack == block) goto done;
            }
        }
        
        else if(h->op == uip_htons(TFTP_WRQ)) {
            connect_back();
            PRINTF("< wrq for %s\n", h->filename);
            len += strlen(h->filename)+1;
            strncpy(config.filename, h->filename, sizeof(config.filename)-1);

            if(strcmp("octet", h->filename+strlen(h->filename)+1)) {
                send_error(EUNDEF, "only octet mode supported");
                goto close_connection;
            }
            len += strlen(h->filename+len)+1; /* skip mode */

            parse_opts(h->options+len, uip_datalen()-len-2);

            if(config.to_ack & OACK_ERROR) {
                send_error(EOPTNEG, "");
                goto close_connection;
            } 

            cfs_remove(h->filename);
            fd = cfs_open(h->filename, CFS_WRITE);
            if(fd<0) {
                send_error(EACCESS, "");
                goto close_connection;
            }

            block = 0; ack = 0;
            tries = TFTP_MAXTRIES;

            PRINTF("starting transfer...\n");

            if(!send_oack())
                send_ack(block);

            for(;;) {
                RECV_PACKET_TIMEOUT(h,t);

                if(ev == PROCESS_EVENT_TIMER) {
                    PRINTF("data timed out, tries left: %d\n", tries);
                    if(--tries<=0) goto close_file;
                    len = config.blksize; /* XXX hack to prevent loop exit*/
                    goto resend_ack;
                }

                if(h->op != uip_htons(TFTP_DATA)) {
                    send_error(EBADOP, "");
                    goto close_file;
                }

                config.to_ack = 0;
                tries = TFTP_MAXTRIES;
                ack = uip_htons(h->block_nr);
                if(ack != block+1) continue;
                /* else */ block++;

                len = recv_data(fd, block);
                if(len<0) {
                    send_error(EUNDEF, "write failed");
                    goto close_file;
                }

#if WITH_EXEC
                if(len < config.blksize) {
                    if(config.exec) {
                        if(exec_file(config.filename, &elf_err) != 0) {
                            send_error(EUNDEF, elf_err);
                            goto close_file;
                        }
                    }
                }
#endif

resend_ack:

                if(!send_oack())
                    send_ack(block);

                if(len < config.blksize) goto done;
            }
        }

done:
            PRINTF("done.\n");

close_file:
        if(fd>=0)
            cfs_close(fd);
        fd = -1;

close_connection:
        if(client_conn)
            uip_udp_remove(client_conn);
        client_conn = 0;

        PRINTF("connection closed.\n");
    } 
    PROCESS_END();
}