ipc-debug now uses ipc_application_poll_event
This commit is contained in:
parent
d71640e71b
commit
9865fcefff
@ -13,7 +13,7 @@ void service_path (char *path, const char *sname, int index, int version)
|
||||
assert (path != NULL);
|
||||
assert (sname != NULL);
|
||||
memset (path, 0, PATH_MAX);
|
||||
snprintf (path, PATH_MAX, "%s/%s-%d-%d", TMPDIR, sname, index, version);
|
||||
snprintf (path, PATH_MAX, "%s/%s-%d-%d", RUNDIR, sname, index, version);
|
||||
}
|
||||
|
||||
int ipc_server_init (char **env
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
#define LISTEN_BACKLOG 128
|
||||
|
||||
#define TMPDIR "/run/ipc/"
|
||||
#define RUNDIR "/run/ipc/"
|
||||
#define PATH_MAX 4096
|
||||
|
||||
#define IPC_HEADER_SIZE 5
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
#define SERVICE_NAME "pongd"
|
||||
|
||||
#define MAX_MESSAGE_SIZE 20000
|
||||
#define MAX_MESSAGE_SIZE IPC_MAX_MESSAGE_SIZE
|
||||
#define MESSAGE "salut ça va ?"
|
||||
|
||||
void interactive (char * service_name, char *env[])
|
||||
@ -30,94 +30,82 @@ void interactive (char * service_name, char *env[])
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char msg_type = 0;
|
||||
struct ipc_event event;
|
||||
memset (&event, 0, sizeof (struct ipc_event));
|
||||
|
||||
struct ipc_services services;
|
||||
memset (&services, 0, sizeof (struct ipc_services));
|
||||
ipc_service_add (&services, &srv);
|
||||
int ret = 0;
|
||||
|
||||
while (1) {
|
||||
#if 0 // version with readline, slow, valgrind-incompatible
|
||||
|
||||
char * mtype_str = readline("msg type: ");
|
||||
if (strlen(mtype_str) == 0 || strncmp (mtype_str, "exit", 4) == 0) {
|
||||
free (mtype_str);
|
||||
break;
|
||||
}
|
||||
msg_type = atoi(mtype_str);
|
||||
free(mtype_str);
|
||||
printf ("msg to send: ");
|
||||
fflush (stdout);
|
||||
ret = ipc_application_peek_event (&services, &event);
|
||||
|
||||
char * buf = readline ("msg: ");
|
||||
if (strlen(buf) == 0 || strncmp (buf, "exit", 4) == 0) {
|
||||
free (buf);
|
||||
break;
|
||||
if (ret != 0) {
|
||||
handle_error("ipc_application_peek_event != 0");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
ipc_message_format (&m, msg_type, buf, strlen(buf));
|
||||
free (buf);
|
||||
switch (event.type) {
|
||||
case IPC_EVENT_TYPE_STDIN:
|
||||
{
|
||||
struct ipc_message *m = event.m;
|
||||
if ( m->length == 0 || strncmp (m->payload, "exit", 4) == 0) {
|
||||
|
||||
// print_msg (&m);
|
||||
ipc_message_empty (m);
|
||||
free (m);
|
||||
|
||||
if (ipc_application_write (&srv, &m) < 0) {
|
||||
handle_err("main", "application_write < 0");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
ipc_message_empty (&m);
|
||||
ipc_services_free (&services);
|
||||
|
||||
if (ipc_application_read (&srv, &m) < 0) {
|
||||
handle_err("main", "application_read < 0");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (ipc_application_close (&srv) < 0) {
|
||||
handle_err("main", "application_close < 0");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf ("\nmsg recv: %s", m.payload);
|
||||
ipc_message_empty (&m);
|
||||
#else
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
char mtype_str[50];
|
||||
memset(mtype_str, 0, 50);
|
||||
printf ("message type: ");
|
||||
fflush(stdout);
|
||||
read(0, mtype_str, 50);
|
||||
msg_type = atoi(mtype_str);
|
||||
memset(mtype_str, 0, 50);
|
||||
char mtype_str[5];
|
||||
memset(mtype_str, 0, 5);
|
||||
printf ("message type: ");
|
||||
fflush(stdout);
|
||||
read(0, mtype_str, 5);
|
||||
m->type = atoi(mtype_str);
|
||||
memset(mtype_str, 0, 5);
|
||||
|
||||
char mpayload[MAX_MESSAGE_SIZE];
|
||||
memset(mpayload, 0, MAX_MESSAGE_SIZE);
|
||||
printf ("message payload: ");
|
||||
fflush(stdout);
|
||||
read(0, mpayload, MAX_MESSAGE_SIZE);
|
||||
if (strlen(mpayload) == 0 || strncmp (mpayload, "exit", 4) == 0) {
|
||||
break;
|
||||
if (ipc_application_write (&srv, m) < 0) {
|
||||
handle_err("main", "application_write < 0");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case IPC_EVENT_TYPE_MESSAGE:
|
||||
{
|
||||
struct ipc_message *m = event.m;
|
||||
printf ("msg recv: %.*s", m->length, m->payload);
|
||||
};
|
||||
break;
|
||||
case IPC_EVENT_TYPE_DISCONNECTION:
|
||||
{
|
||||
printf ("server disconnected: quitting...\n");
|
||||
|
||||
// just remove srv from services, it's already closed
|
||||
ipc_services_free (&services);
|
||||
|
||||
exit (EXIT_SUCCESS);
|
||||
};
|
||||
case IPC_EVENT_TYPE_NOT_SET:
|
||||
case IPC_EVENT_TYPE_CONNECTION:
|
||||
case IPC_EVENT_TYPE_ERROR:
|
||||
default :
|
||||
fprintf (stderr, "should not happen, event type %d\n", event.type);
|
||||
}
|
||||
|
||||
ipc_message_format (&m, msg_type, mpayload, strlen(mpayload));
|
||||
// print_msg (&m);
|
||||
|
||||
if (ipc_application_write (&srv, &m) < 0) {
|
||||
handle_err("main", "application_write < 0");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
ipc_message_empty (&m);
|
||||
|
||||
if (msg_type == 0) { // message type 0 => close the server, no read to do
|
||||
break;
|
||||
}
|
||||
|
||||
if (ipc_application_read (&srv, &m) < 0) {
|
||||
handle_err("main", "application_read < 0");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (m.length > 0) {
|
||||
printf ("msg recv: %.*s", m.length, m.payload);
|
||||
}
|
||||
ipc_message_empty (&m);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (ipc_application_close (&srv) < 0) {
|
||||
handle_err("main", "application_close < 0");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
ipc_message_empty (&m);
|
||||
}
|
||||
|
||||
|
||||
void non_interactive (char msg_type, char *msg, char * service_name, char *env[])
|
||||
{
|
||||
struct ipc_message m;
|
||||
@ -161,27 +149,61 @@ void non_interactive (char msg_type, char *msg, char * service_name, char *env[]
|
||||
ipc_message_empty (&m);
|
||||
}
|
||||
|
||||
// usage: ipc-debug [service-name]
|
||||
int main (int argc, char *argv[], char *env[])
|
||||
{
|
||||
if (argc == 1) {
|
||||
printf ("usage: %s [-n] [service_name [message-type [message]]]\n", argv[0]);
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
char service_name[100];
|
||||
memset (service_name, 0, 100);
|
||||
|
||||
int asked_non_interactive = 0;
|
||||
int current_param = 1;
|
||||
|
||||
if (argc >= 2) {
|
||||
if (memcmp (argv[current_param], "-n", 2) == 0) {
|
||||
// non interactive
|
||||
asked_non_interactive = 1;
|
||||
current_param++;
|
||||
argc--;
|
||||
}
|
||||
}
|
||||
|
||||
if (argc != 1) {
|
||||
ssize_t t = strlen(argv[1]) > 100 ? 100 : strlen(argv[1]);
|
||||
memcpy(service_name, argv[1], t);
|
||||
ssize_t t = strlen(argv[current_param]) > 100 ? 100 : strlen(argv[current_param]);
|
||||
memcpy(service_name, argv[current_param], t);
|
||||
current_param++;
|
||||
}
|
||||
else { memcpy(service_name, SERVICE_NAME, strlen(SERVICE_NAME)); }
|
||||
|
||||
char mtype = 2;
|
||||
if (argc > 2) { mtype = atoi(argv[2]); }
|
||||
if (argc > 2) {
|
||||
mtype = atoi(argv[current_param]);
|
||||
current_param++;
|
||||
}
|
||||
|
||||
char msg[IPC_MAX_MESSAGE_SIZE];
|
||||
memset(msg, 0, IPC_MAX_MESSAGE_SIZE);
|
||||
char *msg = malloc (MAX_MESSAGE_SIZE);
|
||||
if (msg == NULL) {
|
||||
handle_err("main", "not enough memory");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
memset(msg, 0, MAX_MESSAGE_SIZE);
|
||||
|
||||
if (argc > 3) { memcpy(msg, argv[3], strlen(argv[3])); }
|
||||
if (argc > 3) { memcpy(msg, argv[current_param], strlen(argv[current_param])); }
|
||||
else { memcpy(msg, MESSAGE, strlen(MESSAGE)); }
|
||||
|
||||
non_interactive (mtype, msg, service_name, env);
|
||||
if (asked_non_interactive) {
|
||||
non_interactive (mtype, msg, service_name, env);
|
||||
free (msg);
|
||||
}
|
||||
else {
|
||||
|
||||
free (msg);
|
||||
interactive (service_name, env);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
Reference in New Issue
Block a user