Archived
3
0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues or pull requests.
libipc-old/pubsub/tests/pubsub-test-send-params.c

132 lines
3.4 KiB
C
Raw Normal View History

2017-01-19 22:07:52 +01:00
int main() { return 0; }
#if 0
#include "../lib/pubsubd.h"
#include <stdlib.h>
#include <pthread.h>
#define MYMESSAGE "coucou"
void
ohshit(int rvalue, const char* str) {
fprintf (stderr, "\033[31merr: %s\033[00m\n", str);
exit (rvalue);
}
void usage (char **argv)
{
printf ( "usage : %s pid index (pub|sub|both|quit) [chan]\n", argv[0]);
}
2016-06-12 14:41:25 +02:00
void sim_connection (int argc, char **argv, char **env, pid_t pid, int index, int version, char *cmd, char *chan)
{
2016-09-08 23:40:48 +02:00
printf ("Simulate connection : pid %d index %d version %d "
"cmd %s chan %s\n"
, pid, index, version, cmd, chan );
2018-10-04 00:30:47 +02:00
struct ipc_service srv;
memset (&srv, 0, sizeof (struct ipc_service));
2018-10-03 21:52:11 +02:00
ipc_server_init (argc, argv, env, &srv, PUBSUB_SERVICE_NAME, NULL);
printf ("Writing on %s.\n", srv.spath);
2018-10-04 00:30:47 +02:00
struct ipc_client p;
memset (&p, 0, sizeof (struct ipc_client));
2016-09-08 23:40:48 +02:00
printf ("app creation\n");
2018-10-03 21:24:20 +02:00
if (application_create (&p, pid, index, version)) // called by the application
ohshit (1, "application_create");
2016-09-08 23:40:48 +02:00
printf ("connection\n");
// send a message to warn the service we want to do something
// line : pid index version action chan
2016-06-13 09:47:19 +02:00
pubsub_connection (&srv, &p, PUBSUB_PUB, chan);
struct pubsub_msg m;
2016-06-13 09:47:19 +02:00
memset (&m, 0, sizeof (struct pubsub_msg));
2016-09-09 01:36:44 +02:00
if (strcmp (cmd, "pub") == 0) {
// first message, "coucou"
m.type = PUBSUB_TYPE_MESSAGE;
m.chan = malloc (strlen (chan) + 1);
memset (m.chan, 0, strlen (chan) + 1);
m.chan[strlen (chan)] = '\0';
m.chanlen = strlen (chan);
m.data = malloc (strlen (MYMESSAGE) + 1);
memset (m.data, 0, strlen (MYMESSAGE) + 1);
strncpy ((char *) m.data, MYMESSAGE, strlen (MYMESSAGE) + 1);
m.datalen = strlen (MYMESSAGE);
printf ("send message\n");
2018-10-03 21:52:11 +02:00
pubsub_message_send (&p, &m);
2016-09-09 01:36:44 +02:00
}
else {
2018-10-03 21:52:11 +02:00
pubsub_message_recv (&p, &m);
pubsubd_message_print (&m);
2016-09-09 01:36:44 +02:00
}
// free everything
2018-10-03 21:52:11 +02:00
pubsubd_message_free (&m);
2016-09-08 23:40:48 +02:00
printf ("disconnection\n");
2016-06-13 09:47:19 +02:00
// disconnect from the server
pubsub_disconnect (&p);
2016-09-08 23:40:48 +02:00
printf ("destroying app\n");
// the application will shut down, and remove the application named pipes
2018-10-03 21:24:20 +02:00
if (application_destroy (&p))
ohshit (1, "application_destroy");
}
2016-06-12 14:41:25 +02:00
void sim_disconnection (int argc, char **argv, char **env, pid_t pid, int index, int version)
{
2018-10-04 00:30:47 +02:00
struct ipc_service srv;
memset (&srv, 0, sizeof (struct ipc_service));
2018-10-03 21:52:11 +02:00
ipc_server_init (argc, argv, env, &srv, PUBSUB_SERVICE_NAME, NULL);
printf ("Disconnecting from %s.\n", srv.spath);
2018-10-04 00:30:47 +02:00
struct ipc_client p;
memset (&p, 0, sizeof (struct ipc_client));
2018-10-04 01:22:50 +02:00
// create the fake client
ipc_server_client_gen (&p, pid, index, version);
// send a message to disconnect
// line : pid index version action chan
2016-06-13 09:47:19 +02:00
pubsub_disconnect (&p);
}
int
2016-06-12 14:41:25 +02:00
main(int argc, char **argv, char **env)
{
if (argc < 3) {
usage (argv);
exit (1);
}
pid_t pid = 0;
pid = atol(argv[1]);
int index = 0;
index = atoi (argv[2]);
// don't care about the version
int version = COMMUNICATION_VERSION;
char * cmd = NULL;
cmd = argv[3];
if (strcmp(cmd, "quit") != 0) {
char *chan = NULL;
chan = argv[4];
2016-06-12 14:41:25 +02:00
sim_connection (argc, argv, env, pid, index, version, cmd, chan);
}
else {
2016-06-12 14:41:25 +02:00
sim_disconnection (argc, argv, env, pid, index, version);
}
return EXIT_SUCCESS;
}
2017-01-19 22:07:52 +01:00
#endif