Replace multi-worker attempt with simple single worker
Get things working before scaling it up
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
#define STREQ(a, b) (strcmp((a), (b)) == 0)
|
||||
|
||||
void print_help(char *);
|
||||
enum guestfs_inpsect_command_const parse_command(char *);
|
||||
enum guestfs_inspect_command_const parse_command(char *);
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc == 1) {
|
||||
@@ -20,19 +20,26 @@ int main(int argc, char **argv) {
|
||||
zsock_t *daemon = zsock_new_req(ep);
|
||||
free(ep);
|
||||
|
||||
struct guestfs_inpsect_command *command = malloc(80);
|
||||
struct guestfs_inpsect_command *command = malloc(sizeof(struct guestfs_inpsect_command));
|
||||
command->name = calloc(strlen(argv[1]) + 1, sizeof(char));
|
||||
strcpy(command->name, argv[1]);
|
||||
/* command->command = parse_command(argv[2]); */
|
||||
/* command->args.ls = 5; */
|
||||
command->command = parse_command(argv[2]);
|
||||
command->args.ls.paths_length = 1;
|
||||
command->args.ls.paths = calloc(1, sizeof(char *));
|
||||
command->args.ls.paths[0] = calloc(strlen(argv[3] + 1), sizeof(char));
|
||||
strcpy(command->args.ls.paths[0], argv[3]);
|
||||
|
||||
zsock_send(daemon, "b", command, 80);
|
||||
zmsg_t *msg = command_to_zmsg(command);
|
||||
zmsg_send(&msg, daemon);
|
||||
|
||||
zmsg_t *msg = zmsg_recv(daemon);
|
||||
char *res = zmsg_popstr(msg);
|
||||
zmsg_t *rep = zmsg_recv(daemon);
|
||||
char *res = zmsg_popstr(rep);
|
||||
printf("Res:\n%s\n", res);
|
||||
zmsg_destroy(msg);
|
||||
|
||||
zmsg_destroy(&msg);
|
||||
free(res);
|
||||
|
||||
zsock_destroy(&daemon);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -42,3 +49,11 @@ void print_help(char *name) {
|
||||
printf(" ls <path>\n");
|
||||
printf(" cat <path>\n");
|
||||
}
|
||||
|
||||
enum guestfs_inspect_command_const parse_command(char *input) {
|
||||
if (STREQ(input, "ls")) {
|
||||
return GUESTFS_COMMAND_LS;
|
||||
}
|
||||
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -85,6 +85,8 @@ static void init_guestfs(guestfs_h *g, char *disk_path) {
|
||||
free(root);
|
||||
}
|
||||
free(roots);
|
||||
|
||||
printf("Finished initializing guestfs\n");
|
||||
}
|
||||
|
||||
static void *worker_task(zsock_t *pipe, char *disk_path) {
|
||||
@@ -105,23 +107,27 @@ static void *worker_task(zsock_t *pipe, char *disk_path) {
|
||||
printf("Received a message in the worker\n");
|
||||
|
||||
printf("Size: %zu\tContent size: %zu\n", zmsg_size(msg), zmsg_content_size(msg));
|
||||
struct guestfs_inpsect_command *command = malloc(80);
|
||||
memcpy(command, zmsg_last(msg), 80);
|
||||
struct guestfs_inpsect_command *command = zmsg_to_command(msg);
|
||||
printf("Name: %s\n", command->name);
|
||||
printf("paths: %zu\n", command->args.ls.paths_length);
|
||||
printf("path: %s\n", command->args.ls.paths[0]);
|
||||
|
||||
free(command);
|
||||
|
||||
// Sending reply
|
||||
/* zmsg_send(&msg, pipe); */
|
||||
/* zstr_send(pipe, ); */
|
||||
|
||||
zmsg_t *reply = zmsg_new();
|
||||
if (!reply) {
|
||||
printf("wuddahec\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
zmsg_pushstr(reply, "From worker!");
|
||||
|
||||
zmsg_send(&reply, pipe);
|
||||
|
||||
zmsg_destroy(&msg);
|
||||
|
||||
zmsg_destroy(&reply);
|
||||
}
|
||||
|
||||
guestfs_close(g);
|
||||
@@ -134,54 +140,17 @@ int main(int argc, char **argv) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
const int worker_count = argc - 1;
|
||||
|
||||
// One worker per disk image.
|
||||
struct {
|
||||
char *name;
|
||||
zactor_t *worker;
|
||||
} worker_map[worker_count];
|
||||
|
||||
for (int i = 0; i < worker_count; i++) {
|
||||
char *path = strtok(argv[i+1], ":");
|
||||
worker_map[i].name = strtok(NULL, ":");
|
||||
printf("name: %s\n", worker_map[i].name);
|
||||
worker_map[i].worker = zactor_new(worker_task, path);
|
||||
}
|
||||
char *path = strtok(argv[1], ":");
|
||||
char *name = strtok(NULL, ":");
|
||||
printf("name: '%s'\n", name);
|
||||
|
||||
char *ep = endpoint();
|
||||
printf("ep: %s\n", ep);
|
||||
zsock_t *frontend = zsock_new_router(ep);
|
||||
zsock_t *worker = zsock_new_rep(ep);
|
||||
free(ep);
|
||||
|
||||
while (true) {
|
||||
zmsg_t *msg = zmsg_recv(frontend);
|
||||
printf("Received a message in the router\n");
|
||||
if (!msg) break;
|
||||
worker_task(worker, path);
|
||||
|
||||
// Find the worker with the given name.
|
||||
zactor_t *worker = NULL;
|
||||
struct guestfs_inpsect_command *cmd = (struct guestfs_inpsect_command *) zmsg_last(msg);
|
||||
for (int i = 0; i < worker_count; i++) {
|
||||
if (STREQ(cmd->name, worker_map[i].name)) {
|
||||
worker = worker_map[i].name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: REMOVE
|
||||
if (!worker) {
|
||||
worker = worker_map[0].worker;
|
||||
}
|
||||
// TODO: REMOVE
|
||||
|
||||
if (worker) {
|
||||
zmsg_send(&msg, zactor_sock(worker));
|
||||
} else {
|
||||
// The name specified does not exist.
|
||||
printf("There is no drive with the name %s\n.", cmd->name);
|
||||
}
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <czmq.h>
|
||||
|
||||
enum guestfs_inspect_command_const {
|
||||
GUESTFS_COMMAND_LS,
|
||||
/* GUESTFS_COMMAND_TOUCH, */
|
||||
@@ -6,19 +8,18 @@ enum guestfs_inspect_command_const {
|
||||
};
|
||||
|
||||
struct guestfs_ls_args {
|
||||
size_t path_length;
|
||||
char path[];
|
||||
size_t paths_length;
|
||||
char **paths;
|
||||
};
|
||||
struct guestfs_cat_args {};
|
||||
|
||||
struct guestfs_inpsect_command {
|
||||
char *name;
|
||||
enum guestfs_inspect_command_const command;
|
||||
union {
|
||||
struct guestfs_ls_args ls;
|
||||
struct guestfs_cat_args cat;
|
||||
} args;
|
||||
size_t name_length;
|
||||
char name[];
|
||||
};
|
||||
|
||||
static char *endpoint() {
|
||||
@@ -29,3 +30,45 @@ static char *endpoint() {
|
||||
return res;
|
||||
}
|
||||
|
||||
static zmsg_t *command_to_zmsg(struct guestfs_inpsect_command *command) {
|
||||
zmsg_t *res = zmsg_new();
|
||||
|
||||
zmsg_addstr(res, command->name);
|
||||
zmsg_addmem(res, &(command->command), sizeof(command->command));
|
||||
|
||||
switch (command->command) {
|
||||
case GUESTFS_COMMAND_LS:
|
||||
zmsg_addmem(res, &(command->args.ls.paths_length), sizeof(command->args.ls.paths_length));
|
||||
|
||||
for (size_t i = 0; i < (command->args.ls.paths_length); i++) {
|
||||
zmsg_addstr(res, command->args.ls.paths[i]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static struct guestfs_inpsect_command *zmsg_to_command(zmsg_t *msg) {
|
||||
struct guestfs_inpsect_command *res = malloc(sizeof(struct guestfs_inpsect_command));
|
||||
|
||||
res->name = zmsg_popstr(msg);
|
||||
zframe_t *next = zmsg_pop(msg);
|
||||
res->command = *(zframe_data(next));
|
||||
free(next);
|
||||
|
||||
switch (res->command) {
|
||||
case GUESTFS_COMMAND_LS:
|
||||
next = zmsg_pop(msg);
|
||||
res->args.ls.paths_length = *(zframe_data(next));
|
||||
free(next);
|
||||
|
||||
res->args.ls.paths = calloc(res->args.ls.paths_length, sizeof(char *));
|
||||
|
||||
for (size_t i = 0; i < res->args.ls.paths_length; i++) {
|
||||
res->args.ls.paths[i] = zmsg_popstr(msg);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user