commit 13eedbf89b03b6458db4983dc6b0ab63d379e22e
parent d4b6b1ad5828f78af7baa8a13bdddc6de5be40d0
Author: kocotian <kocotian@kocotian.pl>
Date: Mon, 29 Mar 2021 19:34:24 +0200
Adding to a queue
Diffstat:
M | vpc.c | | | 55 | +++++++++++++++++++++++++++++++++++++------------------ |
M | vpd.c | | | 25 | +++++++++++++++++++++++++ |
2 files changed, 62 insertions(+), 18 deletions(-)
diff --git a/vpc.c b/vpc.c
@@ -14,32 +14,49 @@
typedef enum {
ActionNop,
ActionGetQueue,
+ ActionAddQueue,
ActionExitDaemon
} Action;
static int action_getQueue(int fd);
+static int action_addQueue(int fd, int argc, char *argv[]);
static int action_exitDaemon(int fd);
-static int handle(int fd, char *action);
+static int handle(int fd, int argc, char *argv[]);
static void usage(void);
char *argv0;
+static union {
+ uint16_t value16;
+ char bytes16[2];
+} converter;
+
static int
action_getQueue(int fd)
{
int i;
char filename[PATH_MAX];
- union {
- char input[2];
- uint16_t output;
- } converter;
i = 0;
- while (read(fd, converter.input, 2) == 2 && converter.output) {
- read(fd, filename, PATH_MAX);
- printf("* item #%d: %.*s\n", i, converter.output, filename);
+ while (read(fd, converter.bytes16, 2) == 2 && converter.value16) {
+ read(fd, filename, MIN(converter.value16, PATH_MAX));
+ printf(" * item #%d: %.*s\n", i, converter.value16, filename);
++i;
}
- printf("queue has %d videos\n", i);
+ printf(":: queue has %d videos\n", i);
+ return 0;
+}
+
+static int
+action_addQueue(int fd, int argc, char *argv[])
+{
+ int i;
+ for (i = 0; i < argc; ++i) {
+ converter.value16 = (uint16_t)strlen(argv[i]);
+ write(fd, converter.bytes16, 2);
+ write(fd, argv[i], converter.value16);
+ }
+ i = 0;
+ write(fd, &i, 2);
return 0;
}
@@ -54,18 +71,22 @@ action_exitDaemon(int fd)
}
static int
-handle(int fd, char *arg)
+handle(int fd, int argc, char *argv[])
{
Action action;
+ char *arg = *argv;
if (!strcmp(arg, "queue"))
action = ActionGetQueue;
+ else if (!strcmp(arg, "add"))
+ action = ActionAddQueue;
else if (!strcmp(arg, "exit"))
action = ActionExitDaemon;
write(fd, &action, 1);
switch (action) {
- case ActionGetQueue: action_getQueue(fd); break;
- case ActionExitDaemon: action_exitDaemon(fd); break;
+ case ActionGetQueue: action_getQueue(fd); break;
+ case ActionAddQueue: action_addQueue(fd, argc - 1, argv + 1); break;
+ case ActionExitDaemon: action_exitDaemon(fd); break;
default: break;
}
return 0;
@@ -80,7 +101,7 @@ usage(void)
int
main(int argc, char *argv[])
{
- char *action;
+ char *port_astext;
char *host = "127.0.0.1";
uint16_t port = 7111;
int sockfd;
@@ -92,17 +113,15 @@ main(int argc, char *argv[])
usage();
break;
case 'p':
- if ((action = ARGF()) == NULL)
+ if ((port_astext = ARGF()) == NULL)
usage();
/* FIXME: some comparsion to make sure that value will not overflow */
- port = (uint16_t)strtol(action, NULL, 10);
+ port = (uint16_t)strtol(port_astext, NULL, 10);
break;
default:
usage(); break;
} ARGEND
- action = argv[0];
-
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
die("socket:");
@@ -114,7 +133,7 @@ main(int argc, char *argv[])
sizeof sockaddr_in) < 0)
die("connect:");
- handle(sockfd, action);
+ handle(sockfd, argc, argv);
close(sockfd);
return 0;
diff --git a/vpd.c b/vpd.c
@@ -3,6 +3,7 @@
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
@@ -15,6 +16,7 @@
typedef enum {
ActionNop,
ActionGetQueue,
+ ActionAddQueue,
ActionExitDaemon
} Action;
@@ -24,6 +26,7 @@ typedef struct {
} QueueItem;
static int action_getQueue(int fd);
+static int action_addQueue(int fd);
static int action_exitDaemon(int fd);
static void setup(void);
static int takeAction(int fd);
@@ -54,6 +57,27 @@ action_getQueue(int fd)
}
static int
+action_addQueue(int fd)
+{
+ int i;
+ char filename[PATH_MAX];
+ union {
+ char input[2];
+ uint16_t output;
+ } converter;
+ i = 0;
+ while (read(fd, converter.input, 2) == 2 && converter.output) {
+ read(fd, filename, MIN(converter.output, PATH_MAX));
+ strncpy(queue[queueLength].filename, filename,
+ queue[queueLength].fnlen = MIN(converter.output + 1, PATH_MAX));
+ --queue[queueLength++].fnlen;
+ ++i;
+ }
+ /* TODO: return amount of added items */
+ return 0;
+}
+
+static int
action_exitDaemon(int fd)
{
char i = 0;
@@ -70,6 +94,7 @@ takeAction(int fd)
return 1;
switch (action) {
case ActionGetQueue: action_getQueue(fd); break;
+ case ActionAddQueue: action_addQueue(fd); break;
case ActionExitDaemon: action_exitDaemon(fd); break;
default: break;
}