15 #include <interface.h>
30 #include <sys/types.h>
37 #define FIELD_WIDTH 10
38 #define BUFFER_SIZE 1024
40 #define netw g_output_stream_write
42 #define PROXY_PROTOCOL "socks5"
43 #define PROXY_DEFAULT_PORT 1080
72 GSocketConnection* conn, GThreadPool* threads)
77 inter->
istr = g_io_stream_get_input_stream((GIOStream*)inter->
conn);
78 inter->
ostr = g_io_stream_get_output_stream((GIOStream*)inter->
conn);
79 g_thread_pool_push(threads, inter, NULL);
94 g_object_unref(inter->
conn);
128 GMatchInfo* regex_match;
132 char* arg1, * arg2, * arg3;
141 V_INTERFACE(
"INTERFACE: received \"%s\"\n",
buffer);
144 for(cmd =
buffer; *cmd; cmd++)
145 *cmd = g_ascii_tolower(*cmd);
147 cmd = g_match_info_fetch(regex_match, 1);
151 g_output_stream_write(conn->
ostr,
"Invalid command: \"", 18, NULL, NULL);
153 g_output_stream_write(conn->
ostr,
"\"\n", 2, NULL, NULL);
154 g_match_info_free(regex_match);
155 WARNING(
"INTERFACE: invalid command: \"%s\"",
buffer);
160 V_INTERFACE(
"INTERFACE: send \"received\"\n");
161 g_output_stream_write(conn->
ostr,
"received\n", 9, NULL, NULL);
168 if(strcmp(cmd,
"close") == 0)
170 g_output_stream_write(conn->
ostr,
"CLOSE\n", 6, NULL, NULL);
171 V_INTERFACE(
"INTERFACE: closing connection to user interface\n");
173 g_match_info_free(regex_match);
184 else if(strcmp(cmd,
"stop") == 0)
186 g_output_stream_write(conn->
ostr,
"CLOSE\n", 6, NULL, NULL);
187 V_INTERFACE(
"INTERFACE: shutting down scheduler gracefully\n");
190 g_match_info_free(regex_match);
201 else if(strcmp(cmd,
"die") == 0)
203 g_output_stream_write(conn->
ostr,
"CLOSE\n", 6, NULL, NULL);
204 V_INTERFACE(
"INTERFACE: killing the scheduler\n");
207 g_match_info_free(regex_match);
218 else if(strcmp(cmd,
"load") == 0)
232 else if(strcmp(cmd,
"kill") == 0)
234 arg1 = g_match_info_fetch(regex_match, 3);
235 arg2 = g_match_info_fetch(regex_match, 8);
239 if(arg1 == NULL || arg2 == NULL || strlen(arg1) == 0 || strlen(arg2) == 0)
242 cmd = g_strdup_printf(
"Invalid kill command: \"%s\"\n",
buffer);
243 g_output_stream_write(conn->
ostr, cmd, strlen(cmd), NULL, NULL);
245 else if((job = g_tree_lookup(scheduler->
job_list, &i)) == NULL)
254 job->
message = strdup(((arg2 == NULL) ?
"no message" : arg2));
268 else if(strcmp(cmd,
"pause") == 0)
270 arg1 = g_match_info_fetch(regex_match, 3);
272 if(arg1 == NULL || strlen(arg1) == 0)
274 arg1 = g_strdup_printf(
"Invalid pause command: \"%s\"\n",
buffer);
275 WARNING(
"received invalid pause command: %s",
buffer);
276 g_output_stream_write(conn->
ostr, arg1, strlen(arg1), NULL, NULL);
282 params->second = atoi(arg1);
283 params->first = g_tree_lookup(scheduler->
job_list, ¶ms->second);
295 else if(strcmp(cmd,
"reload") == 0)
305 else if(strcmp(cmd,
"agents") == 0)
320 else if(strcmp(cmd,
"status") == 0)
322 arg1 = g_match_info_fetch(regex_match, 3);
325 params->first = conn->
ostr;
326 params->second = (arg1 == NULL) ? 0 : atoi(arg1);
338 else if(strcmp(cmd,
"restart") == 0)
340 arg1 = g_match_info_fetch(regex_match, 3);
345 WARNING(
"received invalid restart command: %s",
buffer);
347 "ERROR: Invalid restart command: %s\n", arg1);
354 params->second = atoi(arg1);
355 params->first = g_tree_lookup(scheduler->
job_list, ¶ms->second);
372 else if(strcmp(cmd,
"verbose") == 0)
374 arg1 = g_match_info_fetch(regex_match, 3);
375 arg2 = g_match_info_fetch(regex_match, 5);
385 strcpy(
buffer,
"mask: h d i e s a j\nmask: ");
386 for(i = 1; i < 0x10000; i <<= 1)
392 else if(arg2 == NULL)
400 if((job = g_tree_lookup(scheduler->
job_list, &i)) == NULL)
403 cmd = g_strdup_printf(
"Invalid verbose command: \"%s\"\n",
buffer);
404 g_output_stream_write(conn->
ostr, cmd, strlen(cmd), NULL, NULL);
423 else if(strcmp(cmd,
"priority") == 0)
425 arg1 = g_match_info_fetch(regex_match, 3);
426 arg2 = g_match_info_fetch(regex_match, 5);
428 if(arg1 != NULL && arg2 != NULL)
433 params->first = g_tree_lookup(scheduler->
job_list, &i);
434 params->second = atoi(arg2);
441 if(arg1) g_free(arg1);
442 if(arg2) g_free(arg2);
445 WARNING(
"Invalid priority command: %s\n",
buffer);
447 "ERROR: Invalid priority command: %s\n", arg1);
458 else if(strcmp(cmd,
"database") == 0)
470 g_output_stream_write(conn->
ostr,
"Invalid command: \"", 18, NULL, NULL);
472 g_output_stream_write(conn->
ostr,
"\"\n", 2, NULL, NULL);
473 con_printf(
main_log,
"ERROR %s.%d: Interface received invalid command: %s\n", __FILE__, __LINE__, cmd);
476 g_match_info_free(regex_match);
496 GSocketListener* server_socket;
497 GSocketConnection* new_connection;
498 GError* error = NULL;
503 ERROR(
"Could not create server socket thread\n");
508 server_socket = g_socket_listener_new();
509 if(server_socket == NULL)
510 FATAL(
"could not create the server socket");
512 g_socket_listener_add_inet_port(server_socket, scheduler->
i_port, NULL, &error);
514 FATAL(
"[port:%d]: %s", scheduler->
i_port, error->message);
515 scheduler->
cancel = g_cancellable_new();
517 V_INTERFACE(
"INTERFACE: listening port is %d\n", scheduler->
i_port);
522 new_connection = g_socket_listener_accept(server_socket, NULL,
523 scheduler->
cancel, &error);
527 V_INTERFACE(
"INTERFACE: new interface connection\n");
529 FATAL(
"INTERFACE closing for %s", error->message);
534 V_INTERFACE(
"INTERFACE: socket listening thread closing\n");
535 g_socket_listener_close(server_socket);
536 g_object_unref(server_socket);
563 scheduler, CONF_interface_nthreads, FALSE, NULL);
565 #if GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 32
566 scheduler->
server = g_thread_new(
"interface",
570 scheduler, TRUE, NULL);
573 while(scheduler->
cancel == NULL)
578 WARNING(
"Multiple attempts made to initialize the interface");
596 g_cancellable_cancel(scheduler->
cancel);
597 g_thread_join(scheduler->
server);
598 g_thread_pool_free(scheduler->
workers, FALSE, TRUE);
606 WARNING(
"Attempt to destroy the interface without initializing it");
void list_agents_event(scheduler_t *scheduler, GOutputStream *ostr)
Receive agent on interface.
Header file with agent related operations.
Event handling operations.
int verbose
The verbose flag for the cli.
#define BUFFER_SIZE
Maximum buffer length.
void print_host_load(GTree *host_list, GOutputStream *ostr)
Prints the host information to ostr.
static void interface_conn_destroy(interface_connection *inter)
Free the memory associated with an interface connection.
static interface_connection * interface_conn_init(GSocketConnection *conn, GThreadPool *threads)
void interface_thread(interface_connection *conn, scheduler_t *scheduler)
Function that will run the thread associated with a particular interface instance.
struct interface_connection interface_connection
void interface_destroy(scheduler_t *scheduler)
Closes the server socket and thread pool that service UI connections.
void * interface_listen_thread(scheduler_t *scheduler)
Function that will listen for new connections to the server sockets.
void interface_init(scheduler_t *scheduler)
Create the interface thread and thread pool that handle UI connections.
void job_fail_event(scheduler_t *scheduler, job_t *job)
Events that causes a job to be marked a failed.
void job_restart_event(scheduler_t *scheduler, arg_int *params)
void job_verbose_event(scheduler_t *scheduler, job_t *job)
void job_pause_event(scheduler_t *scheduler, arg_int *params)
Event to pause a job.
void job_status_event(scheduler_t *scheduler, arg_int *params)
Event to get the status of the scheduler or a specific job.
void job_priority_event(scheduler_t *scheduler, arg_int *params)
char buffer[2048]
The last thing received from the scheduler.
void database_update_event(scheduler_t *scheduler, void *unused)
Checks the job queue for any new entries.
void database_exec_event(scheduler_t *scheduler, char *sql)
void scheduler_config_event(scheduler_t *scheduler, void *unused)
Load both the fossology configuration and all the agent configurations.
void scheduler_close_event(scheduler_t *scheduler, void *killed)
Sets the closing flag and possibly kills all currently running agents.
Header file for the scheduler.
const char * jobsql_failed
GOutputStream * ostr
Stream to write to the interface.
GInputStream * istr
Stream to read from the interface.
GSocketConnection * conn
The socket that is our connection.
gchar * message
Message that will be sent with job notification email.
int32_t verbose
The verbose level for all of the agents in this job.
GThread * server
Thread that is listening to the server socket.
GTree * job_list
List of jobs that have been created.
GRegex * parse_interface_cmd
Parses the commands received by the interface.
GTree * host_list
List of all hosts available to the scheduler.
GThreadPool * workers
Threads to handle incoming network communication.
gboolean i_terminate
Has the interface been terminated.
GCancellable * cancel
Used to stop the listening thread when it is running.
gboolean i_created
Has the interface been created.
uint16_t i_port
The port that the scheduler is listening on.