Ejemplo n.º 1
0
int main()
{
    int result = 0;

    signal(SIGINT, ctrlc);

    if (setup_destination()) {
        printf("Error initializing destination.\n");
        result = 1;
        goto done;
    }

    if (setup_source()) {
        printf("Done initializing source.\n");
        result = 1;
        goto done;
    }

    wait_local_devices();

    loop();

  done:
    cleanup_destination();
    cleanup_source();
    return result;
}
Ejemplo n.º 2
0
Archivo: writer.hpp Proyecto: psiha/gil
    libpng_writer( void * const p_target_object, png_rw_ptr const write_data_fn, png_flush_ptr const output_flush_fn )
        :
        libpng_image( ::png_create_write_struct_2( PNG_LIBPNG_VER_STRING, NULL, &detail::png_error_function, &detail::png_warning_function, NULL, NULL, NULL ) )
    {
        if ( !successful_creation() )
            cleanup_and_throw_libpng_error();

        setup_destination( p_target_object, write_data_fn, output_flush_fn );
    }
Ejemplo n.º 3
0
Archivo: Shark.cpp Proyecto: ghiroe/PA4
bool Shark::update() {
    bool output = false;
    //output will be changed to true if the state changes
    switch (state) {
        case 'x':
            break;
        case 's':
            break;
        case 'e':
            //Checks to see if reef is empty
            if (this->reef->is_empty() && prefeast_size * 2 > size) {
                state = 'f';
                cout << display_code << reef->get_id() << ": This reef has no more food for me." << endl;
                output = true;
            }
            //Eats from reef if it is not empty
            else {
                //Heads home if it's twice its original size
                if (prefeast_size * 2 <= size) {
                    setup_destination(home->get_location());
                    cout << display_code << get_id() << ": I'm full. I'm heading home." << endl;
                    state = 'z';
                    output = true;
                }
                //Keeps eating if it is not full yet
                else {
                    size += this->reef->provide_food()/2;
                    cout << display_code << get_id() << ": Grew to size " << size << endl;
                }
            }
            break;
        case 'a':
            if (fabs((target->get_location()).x - (this->get_location()).x) <= range &&
                fabs((target->get_location()).y - (this->get_location()).y) <= range) {
                if (target->get_energy() <= 0) {
                    cout << display_code << get_id() << ": I triumph!" << endl;
                    state = 's';
                    output = true;
                }
                else {
                    cout << display_code << get_id() << ": Chomp!" << endl;
                    target->get_bitten(attack_strength);
                }
            }
            else {
                this->target = NULL;
                cout << display_code << get_id() << ": Darn! It escaped." << endl;
                output = true;
            }
            break;
    }
    return output;
}
Ejemplo n.º 4
0
void Miner::start_mining(Gold_Mine* inputMine, Town_Hall* inputHome) {
    if (Miner::state == 'x') {
        cout<<Miner::display_code<<Miner::id_num<<": I am dead. Are you kidding me? Ask a zombie to work!?!?"<<endl;
    }
    else {
        mine=inputMine;//set up location
        home=inputHome;
        setup_destination(mine->get_location()); //set up new destination
        setstate('o'); //set new state
        cout<< "Miner " << get_id() << " is mining at Gold_Mine " << inputMine->get_id() << " and depositing at Town_Hall " << inputHome->get_id() << "." << endl;
        cout << display_code << get_id() << ": Yes, my lord."<< endl;
    }
}
Ejemplo n.º 5
0
int main()
{
    int result = 0;

    if (setup_destination()) {
        printf("Error initializing destination.\n");
        result = 1;
        goto done;
    }

    if (setup_source()) {
        printf("Done initializing source.\n");
        result = 1;
        goto done;
    }

    wait_ready();

    if (setup_map()) {
        printf("Error initializing map.\n");
        result = 1;
        goto done;
    }

    printf("SENDING UDP\n");
    loop();

    set_map_protocol(MAPPER_PROTO_TCP);
    printf("SENDING TCP\n");
    loop();

    set_map_protocol(MAPPER_PROTO_UDP);
    printf("SENDING UDP AGAIN\n");
    loop();

    if (sent != received) {
        printf("Not all sent messages were received.\n");
        printf("Updated value %d time%s, but received %d of them.\n",
               sent, sent == 1 ? "" : "s", received);
        result = 1;
    }

done:
    cleanup_destination();
    cleanup_source();
    printf("Test %s.\n", result ? "FAILED" : "PASSED");
    return result;
}
Ejemplo n.º 6
0
int main()
{
    int result = 0;

    value = (float)rand();

    signal(SIGINT, ctrlc);

    if (setup_destination()) {
        printf("Error initializing destination.\n");
        result = 1;
        goto done;
    }

    if (setup_source()) {
        printf("Done initializing source.\n");
        result = 1;
        goto done;
    }

    wait_local_devices();

    if (automate)
        connect_signals();

    // start things off
    printf("STARTING TEST...\n");
    times[0] = get_current_time();
    msig_update_instance(sendsig, counter++, &value, 0, MAPPER_TIMETAG_NOW);
    while (!done) {
        mdev_poll(destination, 0);
        mdev_poll(source, 0);
    }
    goto done;

  done:
    cleanup_destination();
    cleanup_source();
    print_results();
    return result;
}
Ejemplo n.º 7
0
int main()
{
    int result = 0;

    if (setup_destination()) {
        printf("Error initializing destination.\n");
        result = 1;
        goto done;
    }

    if (setup_source()) {
        printf("Done initializing source.\n");
        result = 1;
        goto done;
    }

    wait_ready();

    if (setup_router()) {
        printf("Error initializing router.\n");
        result = 1;
        goto done;
    }

    loop();

    if (sent != received) {
        printf("Not all sent messages were received.\n");
        printf("Updated value %d time%s, but received %d of them.\n",
               sent, sent == 1 ? "" : "s", received);
        result = 1;
    }

  done:
    cleanup_destination();
    cleanup_source();
    printf("Test %s.\n", result ? "FAILED" : "PASSED");
    return result;
}
Ejemplo n.º 8
0
void Person::start_moving(Cart_Point dest)
//Tells the Person to start moving
{
  //Check if the Person is alive, and if not, ignore the command and print a message
  if (is_alive())
    {
      //Calls the setup_destination function to change the object's destination
      setup_destination(dest);

      //Sets the state to "moving"
      state = 'm';

      //Prints message "Moving (id) to (destination)."
      cout << "Moving " << Game_Object::get_id() << " to " << destination << endl;

      //Prints message "(display_code)(id): On my way."
      cout << display_code << Game_Object::get_id() << ": On my way.\n";
    }
  else 
    {
      cout << display_code << get_id() << ": I am dead. I may move but you just cannot see me.\n";
    }
}
Ejemplo n.º 9
0
void Inspector::start_inspecting(list <Gold_Mine*> minelist) {
    subscript=0;
    minelist2=minelist;
    Cart_Vector distance;
    double truedistance;
    vector<Cart_Point> locations;
    vector<double> dist;
    vector<double> dist2;
    vector<int> order;

    for (vector<double>::iterator i=dist.begin(); i!=dist.end(); i++)
        i=dist.erase(i);
    for (vector<double>::iterator i=dist2.begin(); i!=dist2.end(); i++)
        i=dist2.erase(i);
    for (vector<int>::iterator i=order.begin(); i!=order.end(); i++)
        i=order.erase(i);
    for (vector<Cart_Point>::iterator i=locations.begin(); i!=locations.end(); i++)
        i=locations.erase(i);
    for (int i=0; i<5; i++)
        goldout[i]=0;
    for (vector<Cart_Point>::iterator i=destinations.begin(); i!=destinations.end(); i++)
        i=destinations.erase(i);

    for (list<Gold_Mine*>::iterator i=minelist.begin(); i!=minelist.end(); i++) {
        Cart_Point a((*i)->get_location());
        locations.push_back(a);
        Cart_Vector distance(this->get_location()-a);
        truedistance=sqrt((distance.x*distance.x)+(distance.y*distance.y));
        dist.push_back(truedistance);
    }

    for (int i=0; i<dist.size(); i++) {
        dist2.push_back(dist[i]);
    }
    sort(dist.begin(), dist.end());

    for (int i=0; i<dist.size(); i++) {
        for (int j=0; j<dist.size(); j++) {
            if (dist[i]==dist2[j]) {
                order.push_back(j);
            }
        }
    }


    int next=order.front();
    Cart_Point one=locations[next];
    destinations.push_back(one);
    for (int i=0; i<locations.size(); i++) {
        if (locations[i].x==destinations.back().x && locations[i].y==destinations.back().y) {
            locations.erase(locations.begin()+i);
        }
    }
    int count=-1;

    while (locations.size()>1) {
        count=count+1;
        dist.clear();
        dist2.clear();
        order.clear();
        for (int i=0; i<locations.size(); i++) {
            Cart_Point distance(destinations[count].x-locations[i].x, destinations[count].y-locations[i].y);
            truedistance=sqrt((distance.x*distance.x)+(distance.y*distance.y));
            dist.push_back(truedistance);
        }
        for (int i=0; i<dist.size(); i++) {
            dist2.push_back(dist[i]);

        }
        sort(dist.begin(), dist.end());
        for (int i=0; i<dist.size(); i++) {
            for (int j=0; j<dist.size(); j++) {
                if (dist[i]==dist2[j])
                    order.push_back(j);
            }
        }

        int next=order.front();
        Cart_Point one=locations[next];
        destinations.push_back(one);
        for (int i=0; i<locations.size(); i++) {
            if (locations[i].x==destinations.back().x && locations[i].y==destinations.back().y) {
                locations.erase(locations.begin()+i);
            }
        }
    }
    if (locations.size()==1)
        destinations.push_back(locations[0]);

    setup_destination(destinations[0]);
    setstate('k');
    cout << display_code << get_id() << " starting to inspect!" << endl;



}
Ejemplo n.º 10
0
bool Inspector::update() {
    switch (state) {
    case 'k': { //outbound
        bool ans=update_location();
        if (ans==true) {
            state='c';
            return true;
        }
        else
            return false;
        break;
    }
    case 'c': { //counting gold at mine outbound
        Gold_Mine* ptr1;
        for (list<Gold_Mine*>::iterator i=minelist2.begin(); i!=minelist2.end(); i++) {
            if ((*i)->location.x == this->location.x && (*i)->location.y == this->location.y )
                ptr1= *i;
        }
        goldout[subscript]=(ptr1->amount);
        cout << "This mine contains " << ptr1->amount << " Gold!" <<endl;
        subscript=subscript+1;
        int truesize=getdestinationssize();
        if (subscript<truesize) {
            setup_destination(destinations[subscript]);
            state='k';
        }
        else {//setup new destination
            subscript=(subscript-2);
            setup_destination(destinations[subscript]);
            state='j';
            cout << "Heading Back!"<<endl;
        }
        return false;
        break;
    }

    case 'j': { //inbound journey
        bool ans=update_location();
        if (ans==true) {
            state='l';
            return true;
        }
        else
            return false;
        break;
    }
    case 'l': { //counting gold at mine inbound
        Gold_Mine* ptr1;
        for (list<Gold_Mine*>::iterator i=minelist2.begin(); i!=minelist2.end(); i++) {
            if ((*i)->location.x == this->location.x && (*i)->location.y == this->location.y )
                ptr1= *i;
        }

        cout << "Amount of gold removed from the mine is " << goldout[subscript]-(ptr1->amount) << " gold!" <<endl;
        if (subscript>0) {
            subscript=subscript-1;
            setup_destination(destinations[subscript]);
            state='j';
        }
        else {
            cout <<"Inspection trip completed!" <<endl;
            state='s';
        }
        return false;
        break;
    }
    case 's': {
        state='s';
        return false;
        break;
    }
    case 'x': {
        return false;
        break;
    }
    }
    return true;
}
Ejemplo n.º 11
0
int main(int argc, char **argv)
{
    int i, j, result = 0;

    // process flags for -v verbose, -t terminate, -h help
    for (i = 1; i < argc; i++) {
        if (argv[i] && argv[i][0] == '-') {
            int len = strlen(argv[i]);
            for (j = 1; j < len; j++) {
                switch (argv[i][j]) {
                    case 'h':
                        printf("testexpression.c: possible arguments "
                               "-q quiet (suppress output), "
                               "-t terminate automatically, "
                               "-h help\n");
                        return 1;
                        break;
                    case 'q':
                        verbose = 0;
                        break;
                    case 't':
                        terminate = 1;
                        break;
                    default:
                        break;
                }
            }
        }
    }

    signal(SIGINT, ctrlc);

    if (setup_destination()) {
        eprintf("Error initializing destination.\n");
        result = 1;
        goto done;
    }

    if (setup_source()) {
        eprintf("Done initializing source.\n");
        result = 1;
        goto done;
    }

    wait_ready();

    if (autoconnect && setup_maps()) {
        eprintf("Error setting map.\n");
        result = 1;
        goto done;
    }

    loop();

    if (sent != received) {
        eprintf("Not all sent messages were received.\n");
        eprintf("Updated value %d time%s, but received %d of them.\n",
                sent, sent == 1 ? "" : "s", received);
        result = 1;
    }

  done:
    cleanup_destination();
    cleanup_source();
    printf("Test %s.\n", result ? "FAILED" : "PASSED");
    return result;
}
Ejemplo n.º 12
0
int main(int argc, char **argv)
{
    int i, j, result = 0;

    // process flags for -v verbose, -t terminate, -h help
    for (i = 1; i < argc; i++) {
        if (argv[i] && argv[i][0] == '-') {
            int len = strlen(argv[i]);
            for (j = 1; j < len; j++) {
                switch (argv[i][j]) {
                    case 'h':
                        eprintf("testselect.c: possible arguments"
                                "-q quiet (suppress output), "
                                "-t terminate automatically, "
                                "-h help\n");
                        return 1;
                        break;
                    case 'q':
                        verbose = 0;
                        break;
                    case 't':
                        terminate = 1;
                        break;
                    default:
                        break;
                }
            }
        }
    }

    signal(SIGINT, ctrlc);

    if (setup_destination()) {
        eprintf("Error initializing destination.\n");
        result = 1;
        goto done;
    }

    if (setup_source()) {
        eprintf("Error initializing source.\n");
        result = 1;
        goto done;
    }

    wait_local_devices();

    if (autoconnect && setup_connection()) {
        eprintf("Error initializing connection.\n");
        result = 1;
        goto done;
    }

    loop();

    if (sent != received) {
        result = 1;
        eprintf("Error: sent %i messages but received %i messages.\n",
                sent, received);
    }

  done:
    cleanup_destination();
    cleanup_source();
    printf("Test %s.\n", result ? "FAILED" : "PASSED");
    return result;
}
Ejemplo n.º 13
0
int main()
{
    int result = 0;
    int stats[6], i;

    signal(SIGINT, ctrlc);

    if (setup_destination()) {
        printf("Error initializing destination.\n");
        result = 1;
        goto done;
    }

    if (setup_source()) {
        printf("Done initializing source.\n");
        result = 1;
        goto done;
    }

    wait_local_devices();

    if (automate)
        connect_signals();

    printf("\n**********************************************\n");
    printf("************ NO INSTANCE STEALING ************\n");
    loop(100);

    stats[0] = sent;
    stats[1] = received;

    for (i=0; i<10; i++)
        msig_release_instance(sendsig, sendinst[i], MAPPER_TIMETAG_NOW);
    sent = received = 0;

    msig_set_instance_allocation_mode(recvsig, IN_STEAL_OLDEST);
    printf("\n**********************************************\n");
    printf("*************** IN_STEAL_OLDEST **************\n");
    loop(100);

    stats[2] = sent;
    stats[3] = received;

    sent = received = 0;

    msig_set_instance_allocation_mode(recvsig, IN_UNDEFINED);
    msig_set_instance_management_callback(recvsig, overflow_handler);
    printf("\n**********************************************\n");
    printf("*********** CALLBACK > ADD INSTANCE **********\n");
    loop(100);

    stats[4] = sent;
    stats[5] = received;

    printf("NO STEALING: sent %i updates, received %i updates (mismatch is OK).\n", stats[0], stats[1]);
    printf("STEAL OLDEST: sent %i updates, received %i updates (mismatch is OK).\n", stats[2], stats[3]);
    printf("ADD INSTANCE: sent %i updates, received %i updates.\n", stats[4], stats[5]);

    result = (stats[4] != stats[5]);

  done:
    cleanup_destination();
    cleanup_source();
    printf("Test %s.\n", result ? "FAILED" : "PASSED");
    return result;
}
Ejemplo n.º 14
0
bool Miner::update() {
    switch (state) {
    case 's': {
        state='s';
        return false;
        break;
    }
    case 'm': {
        bool ans=update_location();
        if (ans==true) {
            state='s';
            return true;
        }
        else {
            state='m';
            return false;
        }
        break;
    }
    case 'o': {
        bool ans=update_location();
        if (ans==true) {
            state='g';
            return true;
        }
        else {
            state='o';
            return false;
        }
        break;
    }
    case 'g': {
        amount=mine->dig_gold();
        cout << display_code<< get_id()<< ": Got " << amount << " gold." << endl;
        setup_destination(home->get_location());
        state='i';
        return true;
        break;
    }
    case 'i': {
        bool ans=update_location();
        if (ans==true) {
            state='d';
            return true;
        }
        else {
            state='i';
            return false;
        }
        break;
    }
    case 'd': {
        cout << display_code << get_id() << ": Deposit " << amount << " of gold." << endl;
        home->deposit_gold(amount);
        amount=0;//print out deposit amount

        if (mine->is_empty()==true) {
            state='s';
            cout<< display_code << get_id() << ": More work?" << endl;
            return true;//stop miner and ask for more work
        }
        else {
            setup_destination(mine->get_location());//setup new destination
            state='o';
            cout << display_code<< get_id() << ": Going back for more." << endl;
            return true;
        }
        break;
    }
    case 'x': {
        return false;
    }
    default:
        return false;
    }
}
Ejemplo n.º 15
0
int main(int argc, char **argv)
{
    int i, j, result = 0, stats[6];

    // process flags for -v verbose, -t terminate, -h help
    for (i = 1; i < argc; i++) {
        if (argv[i] && argv[i][0] == '-') {
            int len = strlen(argv[i]);
            for (j = 1; j < len; j++) {
                switch (argv[i][j]) {
                    case 'h':
                        printf("testinstance.c: possible arguments "
                               "-q quiet (suppress output), "
                               "-h help\n");
                        return 1;
                        break;
                    case 'q':
                        verbose = 0;
                        break;
                    default:
                        break;
                }
            }
        }
    }

    signal(SIGINT, ctrlc);

    if (setup_destination()) {
        eprintf("Error initializing destination.\n");
        result = 1;
        goto done;
    }

    if (setup_source()) {
        eprintf("Done initializing source.\n");
        result = 1;
        goto done;
    }

    wait_local_devices();

    if (automate)
        map_signals();

    eprintf("\n**********************************************\n");
    eprintf("************ NO INSTANCE STEALING ************\n");
    loop();

    stats[0] = sent;
    stats[1] = received;

    for (i=0; i<10; i++)
        mapper_signal_instance_release(sendsig, i, MAPPER_NOW);
    sent = received = 0;

    mapper_signal_set_instance_stealing_mode(recvsig, MAPPER_STEAL_OLDEST);
    eprintf("\n**********************************************\n");
    eprintf("************ STEAL OLDEST INSTANCE ***********\n");
    if (!verbose)
        printf("\n");
    loop();

    stats[2] = sent;
    stats[3] = received;
    sent = received = 0;

    for (i=0; i<10; i++)
        mapper_signal_instance_release(sendsig, i, MAPPER_NOW);
    sent = received = 0;

    mapper_signal_set_instance_event_callback(recvsig, more_handler,
                                              MAPPER_INSTANCE_OVERFLOW
                                              | MAPPER_UPSTREAM_RELEASE);
    eprintf("\n**********************************************\n");
    eprintf("*********** CALLBACK -> ADD INSTANCE *********\n");
    if (!verbose)
        printf("\n");
    loop();

    stats[4] = sent;
    stats[5] = received;

    eprintf("NO STEALING: sent %i updates, received %i updates (mismatch is OK).\n",
            stats[0], stats[1]);
    eprintf("STEAL OLDEST: sent %i updates, received %i updates (mismatch is OK).\n",
            stats[2], stats[3]);
    eprintf("ADD INSTANCE: sent %i updates, received %i updates.\n",
            stats[4], stats[5]);

    result = (stats[4] != stats[5]);

  done:
    cleanup_destination();
    cleanup_source();
    printf("Test %s.\n", result ? "FAILED" : "PASSED");
    return result;
}