2019-07-27 15:48:10 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include "../src/ipc.h"
|
|
|
|
#include "../src/utils.h"
|
|
|
|
|
|
|
|
#define WEBSOCKETD_BULLSHIT
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
void chomp (char *str, ssize_t len)
|
|
|
|
{
|
|
|
|
if (str[len - 1] == '\n') {
|
|
|
|
str[len - 1] = '\0';
|
2019-07-27 15:48:10 +02:00
|
|
|
}
|
2020-01-01 12:11:34 +01:00
|
|
|
if (str[len - 2] == '\n') {
|
|
|
|
str[len - 2] = '\0';
|
2019-07-27 15:48:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
struct ipc_ctx *ctx;
|
2019-07-27 15:48:10 +02:00
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
void interactive ()
|
2019-07-27 15:48:10 +02:00
|
|
|
{
|
2020-07-13 14:12:08 +02:00
|
|
|
int timer = 10000; // 10 seconds
|
2020-01-01 12:11:34 +01:00
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
SECURE_DECLARATION (struct ipc_event, event);
|
2019-07-27 15:48:10 +02:00
|
|
|
SECURE_BUFFER_DECLARATION (char, service_name, 100);
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
char *sn = getenv ("PATH_TRANSLATED");
|
2019-07-27 15:48:10 +02:00
|
|
|
|
|
|
|
if (sn != NULL) {
|
2020-01-01 12:11:34 +01:00
|
|
|
memcpy (service_name, sn, strlen (sn));
|
|
|
|
} else {
|
2019-07-27 15:48:10 +02:00
|
|
|
fprintf (stderr, "cannot see PATH_TRANSLATED variable\n");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
// init service
|
2020-07-13 18:37:33 +02:00
|
|
|
TEST_IPC_Q (ipc_connection (ctx, service_name, NULL), EXIT_FAILURE);
|
2019-07-27 15:48:10 +02:00
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
ipc_add_fd (ctx, 0); // add STDIN
|
2019-07-27 15:48:10 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
while (1) {
|
2020-07-13 14:12:08 +02:00
|
|
|
TEST_IPC_WAIT_EVENT_Q (ipc_wait_event (ctx, &event, &timer), EXIT_FAILURE);
|
2019-07-27 15:48:10 +02:00
|
|
|
|
|
|
|
switch (event.type) {
|
2020-01-01 12:11:34 +01:00
|
|
|
case IPC_EVENT_TYPE_TIMER:{
|
|
|
|
fprintf (stderr, "time up!\n");
|
2020-07-13 14:12:08 +02:00
|
|
|
timer = 10000;
|
2020-01-01 12:11:34 +01:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_EXTRA_SOCKET:
|
|
|
|
{
|
|
|
|
// structure not read, should read the message here
|
|
|
|
SECURE_BUFFER_DECLARATION (char, buf, 4096);
|
|
|
|
ssize_t len;
|
2019-07-27 15:48:10 +02:00
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
len = read (event.origin, buf, 4096);
|
2019-07-27 15:48:10 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
// in case we want to quit the program
|
|
|
|
if (len == 0 || strncmp (buf, "quit", 4) == 0 || strncmp (buf, "exit", 4) == 0) {
|
2019-07-27 15:48:10 +02:00
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
TEST_IPC_Q (ipc_close_all (ctx), EXIT_FAILURE);
|
2019-07-27 15:48:10 +02:00
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
ipc_ctx_free (ctx);
|
2019-07-27 15:48:10 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
// send the message read on STDIN
|
2020-07-13 14:12:08 +02:00
|
|
|
ssize_t len_sent = write (ctx->pollfd[0].fd, buf, len);
|
2020-01-01 12:11:34 +01:00
|
|
|
if (len_sent != len) {
|
|
|
|
fprintf (stderr, "cannot send the message %lu-byte message, sent %lu bytes",
|
|
|
|
len, len_sent);
|
|
|
|
exit (EXIT_FAILURE);
|
2019-07-27 15:48:10 +02:00
|
|
|
}
|
2020-01-01 12:11:34 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_MESSAGE:
|
|
|
|
{
|
|
|
|
struct ipc_message *m = event.m;
|
|
|
|
SECURE_BUFFER_DECLARATION (char, buf, 4096);
|
|
|
|
uint32_t size;
|
|
|
|
size = ipc_message_raw_serialize (buf, m->type, m->user_type, m->payload, m->length);
|
|
|
|
|
|
|
|
write (1, buf, size);
|
2019-07-27 15:48:10 +02:00
|
|
|
#ifdef WEBSOCKETD_BULLSHIT
|
2020-01-01 12:11:34 +01:00
|
|
|
printf ("\n");
|
2019-07-27 15:48:10 +02:00
|
|
|
#endif
|
2020-01-01 12:11:34 +01:00
|
|
|
fflush (stdout);
|
|
|
|
};
|
|
|
|
break;
|
2020-07-13 14:12:08 +02:00
|
|
|
case IPC_EVENT_TYPE_TX: {
|
|
|
|
printf ("Message sent\n");
|
|
|
|
}
|
|
|
|
break;
|
2020-01-01 12:11:34 +01:00
|
|
|
ERROR_CASE (IPC_EVENT_TYPE_DISCONNECTION, "main loop", "disconnection: should not happen");
|
|
|
|
ERROR_CASE (IPC_EVENT_TYPE_NOT_SET , "main loop", "not set: should not happen");
|
|
|
|
ERROR_CASE (IPC_EVENT_TYPE_CONNECTION , "main loop", "connection: should not happen");
|
|
|
|
// ERROR_CASE (IPC_EVENT_TYPE_ERROR , "main loop", "error");
|
|
|
|
default:
|
|
|
|
fprintf (stderr, "event type error: should not happen, event type %d\n", event.type);
|
2019-07-27 15:48:10 +02:00
|
|
|
}
|
2020-01-01 12:11:34 +01:00
|
|
|
}
|
2019-07-27 15:48:10 +02:00
|
|
|
}
|
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
int main (void)
|
2019-07-27 15:48:10 +02:00
|
|
|
{
|
2020-07-13 14:12:08 +02:00
|
|
|
ctx = malloc (sizeof (struct ipc_ctx));
|
|
|
|
memset (ctx, 0, sizeof (struct ipc_ctx));
|
2019-07-27 15:48:10 +02:00
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
interactive ();
|
2019-07-27 15:48:10 +02:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|