FOSSology  4.4.0
Open Source License Compliance by Open Source Software
host.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2010, 2011, 2012 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
11 /* local includes */
12 #include <host.h>
13 #include <logging.h>
14 #include <scheduler.h>
15 
16 /* ************************************************************************** */
17 /* **** Locals ************************************************************** */
18 /* ************************************************************************** */
19 
29 static int print_host_all(gchar* host_name, host_t* host, GOutputStream* ostr)
30 {
31  host_print(host, ostr);
32  return 0;
33 }
34 
35 /* ************************************************************************** */
36 /* **** Contructor Destructor *********************************************** */
37 /* ************************************************************************** */
38 
48 host_t* host_init(char* name, char* address, char* agent_dir, int max)
49 {
50  host_t* host = g_new0(host_t, 1);
51 
52  host->name = g_strdup(name);
53  host->address = g_strdup(address);
54  host->agent_dir = g_strdup(agent_dir);
55  host->max = max;
56  host->running = 0;
57 
58  return host;
59 }
60 
66 void host_destroy(host_t* host)
67 {
68  g_free(host->name);
69  g_free(host->address);
70  g_free(host->agent_dir);
71 
72  host->name = NULL;
73  host->address = NULL;
74  host->agent_dir = NULL;
75  host->max = 0;
76  host->running = 0;
77 
78  g_free(host);
79 }
80 
81 /* ************************************************************************** */
82 /* **** Functions and events ************************************************ */
83 /* ************************************************************************** */
84 
91 void host_insert(host_t* host, scheduler_t* scheduler)
92 {
93  g_tree_insert(scheduler->host_list, host->name, host);
94  scheduler->host_queue = g_list_append(scheduler->host_queue, host);
95 }
96 
103 {
104  host->running++;
105  V_HOST("HOST[%s] load increased to %d\n", host->name, host->running);
106 }
107 
114 {
115  host->running--;
116  V_HOST("HOST[%s] load decreased to %d\n", host->name, host->running);
117 }
118 
125 void host_print(host_t* host, GOutputStream* ostr)
126 {
127  char* buf;
128 
129  buf = g_strdup_printf("host:%s address:%s max:%d running:%d\n",
130  host->name, host->address, host->max, host->running);
131  g_output_stream_write(ostr, buf, strlen(buf), NULL, NULL);
132 
133  g_free(buf);
134 }
135 
144 host_t* get_host(GList** queue, uint8_t num)
145 {
146  GList* host_queue = *queue;
147  GList* curr = NULL;
148  host_t* ret = NULL;
149 
150  for(curr = host_queue; curr != NULL; curr = curr->next)
151  {
152  ret = curr->data;
153  if(ret->max - ret->running >= num)
154  break;
155  }
156 
157  if(curr == NULL)
158  return NULL;
159 
160  host_queue = g_list_remove(host_queue, ret);
161  host_queue = g_list_append(host_queue, ret);
162 
163  *queue = host_queue;
164  return ret;
165 }
166 
174 void print_host_load(GTree* host_list, GOutputStream* ostr)
175 {
176  g_tree_foreach(host_list, (GTraverseFunc)print_host_all, ostr);
177  g_output_stream_write(ostr, "\nend\n", 5, NULL, NULL);
178 }
void host_insert(host_t *host, scheduler_t *scheduler)
Inserts a new host into the scheduler structure.
Definition: host.c:91
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:48
void host_decrease_load(host_t *host)
Decrease the number of running agents on a host by 1.
Definition: host.c:113
void print_host_load(GTree *host_list, GOutputStream *ostr)
Prints the host information to ostr.
Definition: host.c:174
void host_destroy(host_t *host)
Frees and uninitializes any memory associated with the host struct.
Definition: host.c:66
host_t * get_host(GList **queue, uint8_t num)
Definition: host.c:144
void host_increase_load(host_t *host)
Increase the number of running agents on a host by 1.
Definition: host.c:102
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:29
void host_print(host_t *host, GOutputStream *ostr)
Prints the information about a host to the output stream.
Definition: host.c:125
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
Definition: libfossagent.c:295
Log related operations.
Header file for the scheduler.
Definition: host.h:26
char * agent_dir
The location on the host machine where the executables are.
Definition: host.h:29
int running
The number of agents currently running on this host.
Definition: host.h:31
char * address
The address of the host, used by ssh when starting a new agent.
Definition: host.h:28
char * name
The name of the host, used to store host internally to scheduler.
Definition: host.h:27
int max
The max number of agents that can run on this host.
Definition: host.h:30
GList * host_queue
Round-robin queue for choosing which host use next.
Definition: scheduler.h:161
GTree * host_list
List of all hosts available to the scheduler.
Definition: scheduler.h:160