Skip to content

Commit

Permalink
71 add --yaml client option (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis authored Mar 21, 2023
1 parent 0f5d9d7 commit 3da0c77
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 108 deletions.
2 changes: 1 addition & 1 deletion examples/example_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
void execute(enum IpcRequestOperation op, char *request) {
int fd;

if ((fd = create_fd_ipc_client()) == -1) {
if ((fd = create_socket_client()) == -1) {
exit(1);
}

Expand Down
2 changes: 1 addition & 1 deletion inc/fds.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdbool.h>

extern int fd_signal;
extern int fd_ipc;
extern int fd_socket_server;
extern int fd_cfg_dir;

extern nfds_t npfds;
Expand Down
15 changes: 9 additions & 6 deletions inc/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,28 @@ enum IpcRequestOperation {
struct IpcRequest {
enum IpcRequestOperation op;
struct Cfg *cfg;
int fd;
int socket_client;
bool bad;
bool raw;
};

struct IpcResponse {
bool done;
int rc;
int fd;
int socket_client;
bool messages;
bool state;
};

int ipc_request_send(struct IpcRequest *request);
void ipc_send_request(struct IpcRequest *request);

void ipc_response_send(struct IpcResponse *response);
void ipc_send_response(struct IpcResponse *response);

struct IpcRequest *ipc_request_receive(int fd_sock);
char *ipc_receive_raw_client(int socket_client);

struct IpcResponse *ipc_response_receive(int fd);
struct IpcRequest *ipc_receive_request_server(int socket_server);

struct IpcResponse *ipc_receive_response_client(int socket_client);

void ipc_request_free(struct IpcRequest *request);

Expand Down
10 changes: 5 additions & 5 deletions inc/sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

void socket_path(struct sockaddr_un *addr);

int create_fd_ipc_server(void);
int create_socket_server(void);

int create_fd_ipc_client(void);
int create_socket_client(void);

int socket_accept(int fd_sock);
int socket_accept(int socket_server);

char *socket_read(int fd);
char *socket_read(int socket_client);

ssize_t socket_write(int fd, char *data, size_t len);
ssize_t socket_write(int socket_client, char *data, size_t len);

#endif // SOCKETS_H

26 changes: 20 additions & 6 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void usage(FILE *stream) {
"OPTIONS\n"
" -L, --l[og-threshold] <debug|info|warning|error>\n"
" -c, --c[onfig] <path>\n"
" -y, --y[aml] YAML client output\n"
"COMMANDS\n"
" -h, --h[elp] show this message\n"
" -v, --v[ersion] display version information\n"
Expand Down Expand Up @@ -134,6 +135,9 @@ struct Cfg *parse_element(enum IpcRequestOperation op, enum CfgElement element,
bp += snprintf(bp, sizeof(buf) - (bp - buf), " %s", argv[i]);
}
log_error("invalid %s%s", cfg_element_name(element), buf);
if (cfg) {
cfg_free(cfg);
}
wd_exit(EXIT_FAILURE);
return NULL;
}
Expand Down Expand Up @@ -261,9 +265,12 @@ void parse_args(int argc, char **argv, struct IpcRequest **ipc_request, char **c
{ "set", required_argument, 0, 's' },
{ "version", no_argument, 0, 'v' },
{ "write", no_argument, 0, 'w' },
{ "yaml", no_argument, 0, 'y' },
{ 0, 0, 0, 0 }
};
static char *short_options = "c:d:ghL:s:vw";
static char *short_options = "c:d:ghL:s:vwy";

bool raw = false;

int c;
while (1) {
Expand All @@ -288,25 +295,32 @@ void parse_args(int argc, char **argv, struct IpcRequest **ipc_request, char **c
case 'v':
log_info("way-displays version %s", VERSION);
wd_exit(EXIT_SUCCESS);
return;
break;
case 'y':
raw = true;
break;
case 'g':
*ipc_request = parse_get(argc, argv);
return;
break;
case 's':
*ipc_request = parse_set(argc, argv);
return;
break;
case 'd':
*ipc_request = parse_del(argc, argv);
return;
break;
case 'w':
*ipc_request = parse_write(argc, argv);
return;
break;
case '?':
default:
usage(stderr);
wd_exit(EXIT_FAILURE);
return;
}
}

if (*ipc_request) {
(*ipc_request)->raw = raw;
}
}

64 changes: 49 additions & 15 deletions src/client.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

Expand All @@ -10,12 +11,52 @@
#include "log.h"
#include "process.h"

int handle_raw(int socket_client) {
int rc = EXIT_SUCCESS;

char *yaml = ipc_receive_raw_client(socket_client);
while (yaml) {
fprintf(stdout, "%s", yaml);
free(yaml);
yaml = ipc_receive_raw_client(socket_client);
}

return rc;
}

int handle_human(int socket_client) {
int rc = EXIT_SUCCESS;

struct IpcResponse *response;
bool done = false;

while (!done) {
response = ipc_receive_response_client(socket_client);
if (response) {
rc = response->rc;
done = response->done;
} else {
rc = IPC_RC_BAD_RESPONSE;
done = true;
}
}

if (response) {
ipc_response_free(response);
}

return rc;
}

int client(struct IpcRequest *ipc_request) {
if (!ipc_request) {
return EXIT_FAILURE;
}

if (ipc_request->raw) {
log_set_threshold(ERROR, true);
}

log_set_times(false);

int rc = EXIT_SUCCESS;
Expand All @@ -29,27 +70,20 @@ int client(struct IpcRequest *ipc_request) {
log_info("\nClient sending request: %s", ipc_request_op_friendly(ipc_request->op));
print_cfg(INFO, ipc_request->cfg, ipc_request->op == CFG_DEL);

int fd = ipc_request_send(ipc_request);
if (fd == -1) {
ipc_send_request(ipc_request);

if (ipc_request->socket_client == -1) {
rc = EXIT_FAILURE;
goto end;
}

struct IpcResponse *ipc_response;
bool done = false;
while (!done) {
ipc_response = ipc_response_receive(fd);
if (ipc_response) {
rc = ipc_response->rc;
done = ipc_response->done;
ipc_response_free(ipc_response);
} else {
rc = IPC_RC_BAD_RESPONSE;
done = true;
}
if (ipc_request->raw) {
rc = handle_raw(ipc_request->socket_client);
} else {
rc = handle_human(ipc_request->socket_client);
}

close(fd);
close(ipc_request->socket_client);

end:
ipc_request_free(ipc_request);
Expand Down
10 changes: 5 additions & 5 deletions src/fds.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define PFDS_SIZE 5

int fd_signal = -1;
int fd_ipc = -1;
int fd_socket_server = -1;
int fd_cfg_dir = -1;
bool fds_created = false;

Expand Down Expand Up @@ -61,7 +61,7 @@ int create_fd_cfg_dir(void) {

void create_fds(void) {
fd_signal = create_fd_signal();
fd_ipc = create_fd_ipc_server();
fd_socket_server = create_socket_server();
fd_cfg_dir = create_fd_cfg_dir();

fds_created = true;
Expand All @@ -75,7 +75,7 @@ void init_pfds(void) {
npfds = 2;
if (lid)
npfds++;
if (fd_ipc != -1)
if (fd_socket_server != -1)
npfds++;
if (fd_cfg_dir != -1)
npfds++;
Expand All @@ -90,9 +90,9 @@ void init_pfds(void) {
pfd_wayland->fd = wl_display_get_fd(displ->display);
pfd_wayland->events = POLLIN;

if (fd_ipc != -1) {
if (fd_socket_server != -1) {
pfd_ipc = &pfds[i++];
pfd_ipc->fd = fd_ipc;
pfd_ipc->fd = fd_socket_server;
pfd_ipc->events = POLLIN;
}

Expand Down
48 changes: 25 additions & 23 deletions src/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#include "marshalling.h"
#include "sockets.h"

int ipc_request_send(struct IpcRequest *request) {
int fd = -1;
void ipc_send_request(struct IpcRequest *request) {

char *yaml = marshal_ipc_request(request);
if (!yaml) {
Expand All @@ -20,24 +19,22 @@ int ipc_request_send(struct IpcRequest *request) {

log_debug_nocap("========sending server request==========\n%s\n----------------------------------------", yaml);

if ((fd = create_fd_ipc_client()) == -1) {
if ((request->socket_client = create_socket_client()) == -1) {
goto end;
}

if (socket_write(fd, yaml, strlen(yaml)) == -1) {
fd = -1;
if (socket_write(request->socket_client, yaml, strlen(yaml)) == -1) {
request->socket_client = -1;
goto end;
}

end:
if (yaml) {
free(yaml);
}

return fd;
}

void ipc_response_send(struct IpcResponse *response) {
void ipc_send_response(struct IpcResponse *response) {
char *yaml = marshal_ipc_response(response);

if (!yaml) {
Expand All @@ -47,24 +44,34 @@ void ipc_response_send(struct IpcResponse *response) {

log_debug_nocap("========sending client response==========\n%s----------------------------------------", yaml);

if (socket_write(response->fd, yaml, strlen(yaml)) == -1) {
if (socket_write(response->socket_client, yaml, strlen(yaml)) == -1) {
response->done = true;
}

free(yaml);
}

struct IpcRequest *ipc_request_receive(int fd_sock) {
char *ipc_receive_raw_client(int socket_client) {
char *yaml = NULL;

if (!(yaml = socket_read(socket_client))) {
close(socket_client);
return NULL;
}

return yaml;
}

struct IpcRequest *ipc_receive_request_server(int socket_server) {
struct IpcRequest *request = NULL;
int socket_client = -1;
char *yaml = NULL;
int fd = -1;

if ((fd = socket_accept(fd_sock)) == -1) {
if ((socket_client = socket_accept(socket_server)) == -1) {
return NULL;
}

if (!(yaml = socket_read(fd))) {
close(fd);
if (!(yaml = ipc_receive_raw_client(socket_client))) {
return NULL;
}

Expand All @@ -76,25 +83,20 @@ struct IpcRequest *ipc_request_receive(int fd_sock) {
if (!request) {
request = (struct IpcRequest*)calloc(1, sizeof(struct IpcRequest));
request->bad = true;
request->fd = fd;
request->socket_client = socket_client;
return request;
}

request->fd = fd;
request->socket_client = socket_client;

return request;
}

struct IpcResponse *ipc_response_receive(int fd) {
struct IpcResponse *ipc_receive_response_client(int socket_client) {
struct IpcResponse *response = NULL;
char *yaml = NULL;

if (fd == -1) {
log_error("invalid fd for ipc response receive");
return NULL;
}

if (!(yaml = socket_read(fd))) {
if (!(yaml = ipc_receive_raw_client(socket_client))) {
return NULL;
}

Expand Down
Loading

0 comments on commit 3da0c77

Please sign in to comment.