robotObject.c 2.25 KB
Newer Older
Timothy Stack's avatar
Timothy Stack committed
1 2
/*
 * Copyright (c) 2005 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
Timothy Stack's avatar
Timothy Stack committed
22
 */
23 24 25

#include "config.h"

26 27
#include <assert.h>

28 29
#include "robotObject.h"

30 31 32 33 34 35 36 37 38 39 40 41 42 43
struct robot_data ro_data;

char *ro_status_names[ROS_MAX] = {
    "",

    "Known",
    "Unknown",
    "Started Wiggling",
    "Wiggle Queue",
    "Wiggling",
    "Lost",
};

void roInit(void)
44
{
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    int lpc;

    for (lpc = 0; lpc < ROS_MAX; lpc++) {
	lnNewList(&ro_data.rd_lists[lpc]);
    }
}

void roMoveRobot(struct robot_object *ro, ro_status_t new_status)
{
    assert(ro != NULL);
    assert(new_status > ROS_MIN);
    assert(new_status < ROS_MAX);
    
    lnRemove(&ro->ro_link);
    lnAddTail(&ro_data.rd_lists[new_status], &ro->ro_link);
    ro->ro_status = new_status;

    switch (new_status) {
    case ROS_LOST:
	gettimeofday(&ro->ro_lost_timestamp, NULL);
	break;
    default:
	break;
    }
}
70

71 72 73 74 75 76 77 78 79
struct robot_object *roDequeueRobot(ro_status_t old_status,
				    ro_status_t new_status)
{
    struct robot_object *retval = NULL;
    
    assert(old_status > ROS_MIN);
    assert(old_status < ROS_MAX);
    assert(new_status > ROS_MIN);
    assert(new_status < ROS_MAX);
80

81 82 83
    if (!lnEmptyList(&ro_data.rd_lists[old_status])) {
	retval = (struct robot_object *)ro_data.rd_lists[old_status].lh_Head;
	roMoveRobot(retval, new_status);
84 85 86 87
    }
    
    return retval;
}
88 89 90 91 92 93 94 95 96 97 98 99

struct robot_object *roFindRobot(int id)
{
    struct robot_object *retval = NULL;

    for (retval = ro_data.rd_all;
	 (retval != NULL) && (retval->ro_id != id);
	 retval = retval->ro_next) {
    }

    return retval;
}