job.c 4.45 KB
Newer Older
1
/*******************************************************************************
nikaeinn's avatar
nikaeinn committed
2 3
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom
4

nikaeinn's avatar
nikaeinn committed
5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9 10


nikaeinn's avatar
nikaeinn committed
11 12 13 14
    OpenAirInterface 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 General Public License for more details.
15

nikaeinn's avatar
nikaeinn committed
16 17 18 19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
    included in this distribution in the file called "COPYING". If not,
    see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
nikaeinn's avatar
nikaeinn committed
22 23 24
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr
25

nikaeinn's avatar
nikaeinn committed
26
  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.
27

nikaeinn's avatar
nikaeinn committed
28
*******************************************************************************/
29 30
/*! \file job.c
* \brief handle jobs for future nodes' update
31 32
* \author  S. Gashaw, M. Mahersi,  J. Harri, N. Nikaein,
* \date 2014
33 34 35 36 37 38 39 40 41 42 43 44 45
* \version 0.1
* \company Eurecom
* \email: 
* \note
* \warning
*/

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#include "omg.h"

46 47 48 49 50 51 52 53 54 55 56 57 58 59
job_list *
add_job (pair_struct * job, job_list * job_vector)
{

  job_list *tmp;
  job_list *entry = (job_list *) malloc (sizeof (struct job_list_struct));
  entry->pair = job;
  entry->next = NULL;

  if (job_vector != NULL)
    {

      tmp = job_vector;
      tmp->next = entry;
60
    }
61 62 63

  return entry;

64 65
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
//add_job2

job_list *
addjob (pair_struct * job, job_list * job_vector)
{

  job_list *tmp;
  job_list *entry = (job_list *) malloc (sizeof (struct job_list_struct));
  entry->pair = job;
  entry->next = NULL;

  if (job_vector == NULL)
    {
      return entry;
    }
  else
    {
      tmp = job_vector;
      while (tmp->next != NULL)
	{
	  tmp = tmp->next;
	}

      tmp->next = entry;
    }

  return job_vector;


}
96 97

// display list of jobs
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
void
display_job_list (double curr_t, job_list * job_vector)
{

  job_list *tmp = job_vector;
  if (tmp == NULL)
    {
      LOG_D (OMG, "Empty Job_list\n");
    }
  else
    {
      //LOG_D(OMG, "first_Job_time in Job_Vector %f\n", Job_Vector->pair->a);
      while (tmp != NULL)
	{
	  if ((tmp->pair != NULL) /*&& tmp->pair->b->id==0 */ )
	    //LOG_D(OMG, "%.2f %.2f \n",tmp->pair->b->x_pos, tmp->pair->b->y_pos);
	    LOG_D (OMG, "%.2f  %d %d %.2f %.2f  %.2f\n", curr_t, tmp->pair->b->id,tmp->pair->b->gid,
		   tmp->pair->b->x_pos, tmp->pair->b->y_pos,
		   tmp->pair->b->mob->speed);
117 118


119
	  tmp = tmp->next;
120

121
	}
122
    }
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
}

//average nodes speed for each mobility type
unsigned int
nodes_avgspeed (job_list * job_vector)
{
  job_list *tmp = job_vector;
  unsigned int avg = 0, cnt = 0;
  if (tmp == NULL)
    {
      LOG_D (OMG, "Empty Job_list\n");
      return 0;
    }
  else
    {
      while (tmp != NULL)
	{
	  if ((tmp->pair != NULL))
	    {
	      avg += tmp->pair->b->mob->speed;
	      cnt++;
	    }
	  tmp = tmp->next;
	}
    }
  return avg / cnt;
149 150 151
}

// quick sort of the linked list
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
job_list *
job_list_sort (job_list * list, job_list * end)
{

  job_list *pivot, *tmp, *next, *before, *after;
  if (list != end && list->next != end)
    {

      pivot = list;
      before = pivot;
      after = end;
      for (tmp = list->next; tmp != end; tmp = next)
	{
	  next = tmp->next;
	  if (tmp->pair->next_event_t > pivot->pair->next_event_t)
	    tmp->next = after, after = tmp;
	  else
	    tmp->next = before, before = tmp;
	}

      before = job_list_sort (before, pivot);
      after = job_list_sort (after, end);

      pivot->next = after;
      return before;
177
    }
178
  return list;
179 180
}

181 182
job_list *
quick_sort (job_list * list)
183
{
184
  return job_list_sort (list, NULL);
185 186 187
}


188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
job_list *
remove_job (job_list * list, int nid, int node_type)
{

  job_list *current, *previous;

  current = list;
  previous = NULL;
  while (current != NULL)
    {
      if (current->pair->b->id == nid && current->pair->b->type == node_type)
	{
	  if (current == list || previous == NULL)
	    list = current->next;
	  else
	    previous->next = current->next;

	  free (current);
	  break;

	}
      previous = current;
210 211
      current = current->next;
    }
212
  return list;
213

214
}