FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
host.c
Go to the documentation of this file.
1 /* **************************************************************
2 Copyright (C) 2010, 2011, 2012 Hewlett-Packard Development Company, L.P.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 
17 ************************************************************** */
23 /* local includes */
24 #include <host.h>
25 #include <logging.h>
26 #include <scheduler.h>
27 
28 /* ************************************************************************** */
29 /* **** Locals ************************************************************** */
30 /* ************************************************************************** */
31 
41 static int print_host_all(gchar* host_name, host_t* host, GOutputStream* ostr)
42 {
43  host_print(host, ostr);
44  return 0;
45 }
46 
47 /* ************************************************************************** */
48 /* **** Contructor Destructor *********************************************** */
49 /* ************************************************************************** */
50 
60 host_t* host_init(char* name, char* address, char* agent_dir, int max)
61 {
62  host_t* host = g_new0(host_t, 1);
63 
64  host->name = g_strdup(name);
65  host->address = g_strdup(address);
66  host->agent_dir = g_strdup(agent_dir);
67  host->max = max;
68  host->running = 0;
69 
70  return host;
71 }
72 
78 void host_destroy(host_t* host)
79 {
80  g_free(host->name);
81  g_free(host->address);
82  g_free(host->agent_dir);
83 
84  host->name = NULL;
85  host->address = NULL;
86  host->agent_dir = NULL;
87  host->max = 0;
88  host->running = 0;
89 
90  g_free(host);
91 }
92 
93 /* ************************************************************************** */
94 /* **** Functions and events ************************************************ */
95 /* ************************************************************************** */
96 
103 void host_insert(host_t* host, scheduler_t* scheduler)
104 {
105  g_tree_insert(scheduler->host_list, host->name, host);
106  scheduler->host_queue = g_list_append(scheduler->host_queue, host);
107 }
108 
115 {
116  host->running++;
117  V_HOST("HOST[%s] load increased to %d\n", host->name, host->running);
118 }
119 
126 {
127  host->running--;
128  V_HOST("HOST[%s] load decreased to %d\n", host->name, host->running);
129 }
130 
137 void host_print(host_t* host, GOutputStream* ostr)
138 {
139  char* buf;
140 
141  buf = g_strdup_printf("host:%s address:%s max:%d running:%d\n",
142  host->name, host->address, host->max, host->running);
143  g_output_stream_write(ostr, buf, strlen(buf), NULL, NULL);
144 
145  g_free(buf);
146 }
147 
156 host_t* get_host(GList** queue, uint8_t num)
157 {
158  GList* host_queue = *queue;
159  GList* curr = NULL;
160  host_t* ret = NULL;
161 
162  for(curr = host_queue; curr != NULL; curr = curr->next)
163  {
164  ret = curr->data;
165  if(ret->max - ret->running >= num)
166  break;
167  }
168 
169  if(curr == NULL)
170  return NULL;
171 
172  host_queue = g_list_remove(host_queue, ret);
173  host_queue = g_list_append(host_queue, ret);
174 
175  *queue = host_queue;
176  return ret;
177 }
178 
186 void print_host_load(GTree* host_list, GOutputStream* ostr)
187 {
188  g_tree_foreach(host_list, (GTraverseFunc)print_host_all, ostr);
189  g_output_stream_write(ostr, "\nend\n", 5, NULL, NULL);
190 }
static int print_host_all(gchar *host_name, host_t *host, GOutputStream *ostr)
GTraversFunction that allows the information for all hosts to be printed.
Definition: host.c:41
GTree * host_list
List of all hosts available to the scheduler.
Definition: scheduler.h:171
void host_print(host_t *host, GOutputStream *ostr)
Prints the information about a host to the output stream.
Definition: host.c:137
void host_destroy(host_t *host)
Frees and uninitializes any memory associated with the host struct.
Definition: host.c:78
GList * host_queue
Round-robin queue for choosing which host use next.
Definition: scheduler.h:172
int max
The max number of agents that can run on this host.
Definition: host.h:42
host_t * host_init(char *name, char *address, char *agent_dir, int max)
Creates a new host, and adds it to the host list.
Definition: host.c:60
Log related operations.
char * address
The address of the host, used by ssh when starting a new agent.
Definition: host.h:40
char * name
The name of the host, used to store host internally to scheduler.
Definition: host.h:39
void host_insert(host_t *host, scheduler_t *scheduler)
Inserts a new host into the scheduler structure.
Definition: host.c:103
void host_increase_load(host_t *host)
Increase the number of running agents on a host by 1.
Definition: host.c:114
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
Definition: libfossagent.c:309
void print_host_load(GTree *host_list, GOutputStream *ostr)
Prints the host information to ostr.
Definition: host.c:186
host_t * get_host(GList **queue, uint8_t num)
Definition: host.c:156
Definition: host.h:38
char * agent_dir
The location on the host machine where the executables are.
Definition: host.h:41
Header file for the scheduler.
void host_decrease_load(host_t *host)
Decrease the number of running agents on a host by 1.
Definition: host.c:125
int running
The number of agents currently running on this host.
Definition: host.h:43