Commit 9a4e0d40 authored by Johannes Kanig's avatar Johannes Kanig Committed by Sylvain Dailler

R531-011 declare global queue in request module

This allows sharing of some code between unix and windows server

* request.c
(queue): move global variable here
(init_request_queue): initialize global queue
(remove_from_queue): moved here
* server-unix.c
(queue): remove global variable
(server_init_listening): call init_request_queue
(remove_from_queue): moved from here
(handle_msg): change call to remove_from_queue, now without queue arg
* server-win.c
(queue): remove global variable
(handle_msg): now call remove_from_queue
(init): call init_request_queue

Change-Id: I741b8f958a0678a7acdc0c40a3b6fa72f7fb7a2c
(cherry picked from commit 77ff3c64d289cb8d3c845f52708fed9155ca49e1)
parent 810a2588
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
#include "options.h" #include "options.h"
#include "logging.h" #include "logging.h"
// Global pointers are initialized with NULL by C semantics
pqueue queue;
//count the semicolons in <buf>, up to <len> //count the semicolons in <buf>, up to <len>
int count_semicolons(char* buf, int len); int count_semicolons(char* buf, int len);
...@@ -144,3 +147,21 @@ void free_request(prequest r) { ...@@ -144,3 +147,21 @@ void free_request(prequest r) {
free(r->args); free(r->args);
free(r); free(r);
} }
void init_request_queue () {
queue = init_queue(100);
}
void remove_from_queue(char *id) {
// inefficient, but what else?
pqueue tmp = init_queue(queue->capacity);
while (!queue_is_empty(queue)) {
prequest r = queue_pop(queue);
if (strcmp(r->id,id)) queue_push(tmp, r);
}
while (!queue_is_empty(tmp)) {
prequest r = queue_pop(tmp);
queue_push(queue, r);
}
free_queue(tmp);
}
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#ifndef REQUEST_H #ifndef REQUEST_H
#define REQUEST_H #define REQUEST_H
#include "queue.h"
#include <stdbool.h> #include <stdbool.h>
typedef enum { REQ_RUN, REQ_INTERRUPT } request_type; typedef enum { REQ_RUN, REQ_INTERRUPT } request_type;
...@@ -39,4 +40,13 @@ void print_request(prequest r); ...@@ -39,4 +40,13 @@ void print_request(prequest r);
//does *not* free the id of the request //does *not* free the id of the request
void free_request(prequest r); void free_request(prequest r);
// Global queue to contain queued requests
extern pqueue queue;
// initialize the global request queue
void init_request_queue();
// remove the entries from the queue whose id field is equal to <id>
void remove_from_queue(char *id);
#endif #endif
...@@ -70,7 +70,6 @@ int poll_len = 0; ...@@ -70,7 +70,6 @@ int poll_len = 0;
plist processes; plist processes;
plist clients; plist clients;
char *current_dir; char *current_dir;
pqueue queue;
static int cpipe[2]; static int cpipe[2];
...@@ -235,7 +234,7 @@ void server_init_listening(char* socketname, int parallel) { ...@@ -235,7 +234,7 @@ void server_init_listening(char* socketname, int parallel) {
} }
init_logging(); init_logging();
queue = init_queue(100); init_request_queue();
clients = init_list(parallel); clients = init_list(parallel);
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
poll_len = 2 + parallel; poll_len = 2 + parallel;
...@@ -502,20 +501,6 @@ void run_request (prequest r) { ...@@ -502,20 +501,6 @@ void run_request (prequest r) {
send_started_msg_to_client(client, r->id); send_started_msg_to_client(client, r->id);
} }
void remove_from_queue(pqueue p, char *id) {
// inefficient, but what else?
pqueue tmp = init_queue(p->capacity);
while (!queue_is_empty(p)) {
prequest r = queue_pop(p);
if (strcmp(r->id,id)) queue_push(tmp, r);
}
while (!queue_is_empty(tmp)) {
prequest r = queue_pop(tmp);
queue_push(p, r);
}
free_queue(tmp);
}
void handle_msg(pclient client, int key) { void handle_msg(pclient client, int key) {
prequest r; prequest r;
char* buf; char* buf;
...@@ -546,7 +531,7 @@ void handle_msg(pclient client, int key) { ...@@ -546,7 +531,7 @@ void handle_msg(pclient client, int key) {
break; break;
case REQ_INTERRUPT: case REQ_INTERRUPT:
// removes all occurrences of r->id from the queue // removes all occurrences of r->id from the queue
remove_from_queue(queue, r->id); remove_from_queue(r->id);
// kill all processes whose id is r->id; // kill all processes whose id is r->id;
// TODO kill_processes(processes, r->id); // TODO kill_processes(processes, r->id);
free_request(r); free_request(r);
......
...@@ -154,8 +154,6 @@ void add_to_completion_port(HANDLE h, ULONG_PTR key) { ...@@ -154,8 +154,6 @@ void add_to_completion_port(HANDLE h, ULONG_PTR key) {
} }
} }
pqueue queue;
void init_connect_data(t_conn_data* data, int kind) { void init_connect_data(t_conn_data* data, int kind) {
ZeroMemory(data, sizeof(t_conn_data)); ZeroMemory(data, sizeof(t_conn_data));
data->overlap.hEvent = CreateEvent(NULL, FALSE, TRUE, NULL); data->overlap.hEvent = CreateEvent(NULL, FALSE, TRUE, NULL);
...@@ -470,6 +468,7 @@ void handle_msg(pclient client, int key) { ...@@ -470,6 +468,7 @@ void handle_msg(pclient client, int key) {
} }
break; break;
case REQ_INTERRUPT: case REQ_INTERRUPT:
remove_from_queue(r->id);
// TODO: remove r from the queue if still there, or kill the process r->id; // TODO: remove r from the queue if still there, or kill the process r->id;
free_request(r); free_request(r);
break; break;
...@@ -647,7 +646,7 @@ void init() { ...@@ -647,7 +646,7 @@ void init() {
strcpy(pipe_name, TEXT("\\\\.\\pipe\\")); strcpy(pipe_name, TEXT("\\\\.\\pipe\\"));
strcat(pipe_name, my_pipe_name); strcat(pipe_name, my_pipe_name);
queue = init_queue(100); init_request_queue();
clients = init_list(16); clients = init_list(16);
processes = init_list(16); processes = init_list(16);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment