Changeset - 846a54c8854c
[Not reviewed]
default
0 1 0
LordOfWar - 16 years ago 2009-09-26 01:54:32

added structures for testing our pogram just above the main function
1 file changed with 47 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/server/distrend.c
Show inline comments
 
@@ -258,266 +258,313 @@ void change_job_priority(struct distrenj
 

	
 
  if(job->frameset[0].status == FRAMESETSTATUS_UNASSIGNED)
 
    /* if job was not yet started */
 
    distrenjob_enqueue(head, job);
 
  else{ // if job has already been started then place it before the jobs with the same priority
 
    // iterate through linked list of jobs
 
    for(current_job = head; current_job != NULL; current_job = current_job->next){
 
      if(current_job == NULL){ // if it has reached the end of the list, add job there
 
        current_job = job;
 
        break;
 
      }
 
      else if(job->priority <= current_job->priority){ // if job's priority is less than or equal to current_job's priority, insert job
 
        prev_job->next = job;                        // keep in mind 1 is the highest priority given to jobs, head has a
 
        job->next = current_job;                     // priority of zero so it will always be before other jobs
 
        break;
 
      }
 

	
 
      prev_job = current_job;
 
    }
 
  }
 
}
 

	
 
/**
 
  Frame Finder: matches your computer up with a lovely frame to render
 
  TODO: Major issue here, the client needs to know the frame number, AND the job number!
 
  Notice that this function starts by looking at the oldest job first
 

	
 
	TODO: Link this up with the main() function to check if there are frames available or not and provide jobnum/framenum to the client
 

	
 
  @return 0 success, other: error
 
*/
 
int find_jobframe(struct distrenjob *head, struct distrenjob **job, struct frameset **frame)
 
{
 
  if(general_info.hibernate)
 
    return 1;
 

	
 
  unsigned int frame_counter;
 
  short int your_job_type;
 
  unsigned short int found;
 

	
 
  struct distrenjob *distrenjob_ptr;
 

	
 
  your_job_type = 0;
 
  found = 0;
 
  /* iterate through jobs from first to last */
 
  for(distrenjob_ptr = head->next; (!found && !distrenjob_ptr) && !distrenjob_ptr->hibernate; distrenjob_ptr = distrenjob_ptr->next)
 
    for(frame_counter = (distrenjob_ptr->prev_frame_index + 1); !found && frame_counter < distrenjob_ptr->total_frames; frame_counter ++)
 
      if(distrenjob_ptr->frameset[frame_counter].status == FRAMESETSTATUS_UNASSIGNED) // jobframe found
 
	{
 
	  found = 1;
 
	  your_job_type = distrenjob_ptr->type;
 
	  distrenjob_ptr->frameset[frame_counter].status = FRAMESETSTATUS_ASSIGNED;
 
	  distrenjob_ptr->frameset[frame_counter].start_time = clock();
 
	  distrenjob_ptr->assigned_frames++;
 
	  distrenjob_ptr->prev_frame_index = frame_counter;
 
	}
 

	
 
  if(!found)
 
    {
 
      fprintf(stderr, "No more jobs to render\n");
 
      sleep(1); /*< @todo eliminate the need for this line*/
 
      return 1;
 
    }
 

	
 
  *job = distrenjob_ptr;
 
  *frame = &distrenjob_ptr->frameset[frame_counter];
 

	
 
  return 0;
 
}
 

	
 
/** Checks for dead, laggy, or stale slaves */
 
void frame_watchdog(struct distrenjob *distrenjob_head)
 
{
 
  struct distrenjob *distrenjob_ptr;
 
  unsigned int counter;
 

	
 
  /*watchdog_forgiveness = seconds of forgiveness before frame is re-assigned */
 
  distrenjob_ptr = distrenjob_head;
 

	
 
  for(distrenjob_ptr = distrenjob_head; distrenjob_ptr; distrenjob_ptr = distrenjob_ptr->next)
 
  /* iterate through jobs */
 

	
 
    /* if the job has been started, checks by seeing if either to first or second frame has been started */
 
    if(distrenjob_ptr->frameset[0].status != FRAMESETSTATUS_UNASSIGNED || distrenjob_ptr->frameset[1].status != FRAMESETSTATUS_UNASSIGNED)
 
      for(counter = 0; counter < distrenjob_ptr->total_frames; counter ++)
 
      /* iterate through all frames for this job*/
 

	
 
        if((distrenjob_ptr->frameset[counter].start_time + distrenjob_ptr->watchdog_forgiveness) < clock())
 
        /*
 
        If frame is not completed within the number of seconds specified by watchdog_forgiveness
 
        Then change the frame status to unassigned
 
        */
 
          distrenjob_ptr->frameset[counter].status = FRAMESETSTATUS_UNASSIGNED;
 
}
 

	
 
/**
 
   Finds a distrenjob struct based on the jobnum
 
   @arg jobnum job number to search for
 
   @return NULL on job doesn't exist
 
 */
 
struct distrenjob *distrenjob_get(struct distrenjob *head, jobnum_t jobnum)
 
{
 
  struct distrenjob *distrenjob_ptr;
 

	
 
  /*
 
    The conditions of the for loop will leave distrenjob_ptr at NULL if the end of the list is reached. It will leave it pointing to the correct job if it is found.
 
   */
 
  for(distrenjob_ptr = head;
 
      distrenjob_ptr
 
        && distrenjob_ptr->jobnum != jobnum;
 
      distrenjob_ptr = distrenjob_ptr->next);
 

	
 
  return distrenjob_ptr;
 
}
 

	
 

	
 
/**
 
   Removes a distrenjob from the distrenjob linked list. It does not free the distrenjob, however. You should do that with distrenjob_free() from distrenjob.h
 

	
 
   @arg head pointer to the head of the linkedlist of distrenjobs
 
 */
 
void distrenjob_remove(struct distrenjob *head, struct distrenjob *bj)
 
{
 
  struct distrenjob *previous_distrenjob;
 

	
 
  for(previous_distrenjob = head;
 
      previous_distrenjob
 
	&& previous_distrenjob->next != bj; /*< stop on the distrenjob that comes before bj */
 
      previous_distrenjob = previous_distrenjob->next)
 
    /* all of the action is in the definition of the for loop itself */;
 

	
 
  /*
 
    This removes references to bj from the linked list. I.E., we now skip bj when iterating through the list
 
  */
 
  previous_distrenjob->next = bj->next;
 

	
 
  general_info.jobs_in_queue--;
 
}
 

	
 

	
 
/* Grabs config info from confs */
 
int distrend_do_config(int argc, char *argv[], struct distrend_config **config)
 
{
 
  cfg_opt_t myopts_listen[] =
 
    {
 
      CFG_SIMPLE_STR("type", NULL),
 
      CFG_SIMPLE_STR("path", NULL),
 
      CFG_SIMPLE_INT("port", NULL),
 
      CFG_END()
 
    };
 
  cfg_opt_t myopts[] =
 
    {
 
      CFG_SEC("listen",  /* this must be imported into struct listens (which must still be declared) */
 
          myopts_listen,
 
          CFGF_MULTI),
 
      CFG_SIMPLE_STR("datadir", NULL),
 
      CFG_END()
 
    };
 

	
 
  struct distrenjob *distrenjob;
 

	
 
  int tmp;
 

	
 
  xmlinit();
 
  /*
 
   * test xml2distrenjob()
 
   */
 
  tmp = xml2distrenjob(&distrenjob, "distrenjob.xml.example");
 
  if(tmp)
 
    fprintf(stderr, "xml2distrenjob() returned %d. Try to cd to distren/doc if you want to test out the xml2distrenjob() function. (This will only fix this error if the error is due to an inability of the xml library to access distrenjob.xml.example)\n", tmp);
 
  else
 
    fprintf(stderr, "using email ``%s'' for user ``%s'' -- reading in XML files and pulling data from them using libxml2+XPath works!!!\n", distrenjob->email, distrenjob->submitter);
 

	
 
  fprintf(stderr, "%s:%d running config\n", __FILE__, __LINE__);
 

	
 
  *config = malloc(sizeof(struct distrend_config));
 
  myopts[1].simple_value = &(*config)->datadir;
 

	
 
  options_init(argc, argv, &(*config)->mycfg, myopts, "server", &(*config)->options);
 

	
 
  fprintf(stderr, "using %s as datadir\n", (*config)->datadir);
 

	
 
  xmlcleanup();
 
  return 0;
 
}
 
int distrend_config_free(struct distrend_config *config)
 
{
 
  options_free(config->options);
 
  free(config);
 

	
 
  return 0;
 
}
 
/* ************************** Test Functions ************************* */
 
void printFrameInfo(struct distrenjob *head, struct distrenjob *job, int frame_num)
 
{
 
  char *status = "";
 
  frame_num = frame_num - job->frameset[0].num;
 

	
 
  switch(job->frameset[frame_num].status)
 
  {
 
    case FRAMESETSTATUS_UNASSIGNED:
 
      _distren_asprintf(&status, "%s", "unassigned");
 
      break;
 
    case FRAMESETSTATUS_ASSIGNED:
 
      _distren_asprintf(&status, "%s", "assigned");
 
      break;
 
    case FRAMESETSTATUS_DONE:
 
      _distren_asprintf(&status, "%s", "completed");
 
      break;
 
    case FRAMESETSTATUS_CANCELED:
 
      _distren_asprintf(&status, "%s", "canceled");
 
  }
 

	
 
  printf("frame #: %d --> %s", frame_num, status);
 
}
 

	
 
void printJob(struct distrenjob *head, struct distrenjob *job)
 
{
 
  int counter;
 
  printf("frame_num: status");
 
  for(counter = 0; counter < job->total_frames; counter++)
 
    {
 
      printFrameInfo(head, job, job->frameset[counter].num);
 
    }
 
}
 

	
 
void printJobInfo(struct distrenjob *head, struct distrenjob *job)
 
{
 
  printf("type: %d", job->type);
 
  printf("name: %s", job->email);
 
  printf("submitter: %s", job->submitter);
 
  printf("e-mail: %s", job->email);
 
  printf("priority, %d", job->priority);
 
  printf("completed: %d", job->completed_frames);
 
  printf("assigned: %d", job->assigned_frames);
 
  printf("total: %d", job->total_frames);
 
  printf("watchdog: %d", job->watchdog_forgiveness);
 
  printf("hibernate: %d", job->hibernate);
 
  printf("prev_frame: %d", job->prev_frame_index);
 
}
 

	
 
/* ************************** Main ************************* */
 

	
 
int main(int argc, char *argv[])
 
{
 

	
 
  /* @TODO: Put some arg-grabbing code here */
 

	
 
  struct distrenjob head;
 
  head.priority = 0; // make head have the highest priority
 

	
 
  int cont;
 
  struct distrend_listenset *listenset;
 
  struct distrend_config *config;
 

	
 
  enum clientstatus
 
  {
 
    CLIENTSTATUS_UNINITIALIZED = 0,
 
    CLIENTSTATUS_BUSY = 1,
 
    CLIENTSTATUS_IDLE = 2
 
  } clientstatus;
 

	
 
  cont = 1;
 
  memset(&head, '\0', sizeof(struct distrenjob));
 

	
 
  start_data(); // Starts fresh or loads data from xml dump. Should we grab the return?
 

	
 
  distrend_do_config(argc, argv, &config);
 

	
 
  distrend_listen(&listenset, config);
 
  /* This is called the "main loop" */
 
  while(cont)
 
    {
 
      struct distren_action *action;
 
      int clientsays = 0; /*< temporary example variable, will be replaced when we can handle messages */
 

	
 
      distrend_accept(&action);
 
      cont = distrend_do(action);
 

	
 
      /* Make the following code more event-driven */
 
      frame_watchdog(&head);
 

	
 

	
 
      struct frameset *frame;
 
      struct distrenjob *job;
 

	
 
      /* If the client is idle, must be modified for climbing through linked list of clients (client->clientnum) */
 
      if(clientstatus == CLIENTSTATUS_IDLE)
 
	{
 
	  int returnnum = find_jobframe(&head, &job, &frame); // Finds a frame to render
 
	  if(returnnum)
 
	    {
 
	      fprintf(stderr,"No frames are available to render at this time. Idling...\n");
 
	      sleep(10);
 
	    }
 
	  else
 
	    /* returnnum == 0 */
 
	    remotio_send_to_client(frame->num, job->jobnum); // Pseudo-sends data to client
 
	}
 
      /* If the client states that they finished the frame */
 
      	if(clientsays == DISTREN_REQUEST_DONEFRAME){
 
      	  clientstatus = CLIENTSTATUS_IDLE; // Sets the client back to idle
 
      	  finish_frame(&head, job, frame->num); // @TODO: Check that finish_frame really gets the jobnum somehow (Matt: can we give it the pointer to the job?, if not we can use distrenjob_get(&head, jobnum))
 
      	}
 

	
 
      distrend_action_free(action);
 
    }
 

	
 
  distrend_unlisten(listenset);
 
  distrend_config_free(config);
 

	
 
  return 0;
 
}
0 comments (0 inline, 0 general)