From 21cd9fd4ea6e0059d176232aa012533ad5c65959 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Fri, 10 Jun 2016 21:14:41 +0200
Subject: [PATCH] petit programme de test pour envoyer des messages
individuellement
---
lib/pubsubd.c | 8 +++
lib/pubsubd.h | 1 +
pubsub/pubsub-test-send-params.c | 118 +++++++++++++++++++++++++++++++
3 files changed, 127 insertions(+)
create mode 100644 pubsub/pubsub-test-send-params.c
diff --git a/lib/pubsubd.c b/lib/pubsubd.c
index 7c7815f..cbbbcb2 100644
--- a/lib/pubsubd.c
+++ b/lib/pubsubd.c
@@ -559,6 +559,14 @@ void pubsub_connection (struct service *srv, struct process *p, enum app_list_el
free (straction);
}
+void pubsub_disconnect (struct service *srv, struct process *p, enum app_list_elm_action action, const char *channame)
+{
+ // line fmt : pid index version quit
+ // "quit" action is also possible (see pubsub_disconnect)
+ char line[BUFSIZ];
+ snprintf (line, BUFSIZ, "%d %d %d quit\n" , p->pid, p->index, p->version);
+}
+
void pubsub_msg_send (const struct service *s, struct process *p, const struct pubsub_msg * m)
{
char *buf = NULL;
diff --git a/lib/pubsubd.h b/lib/pubsubd.h
index c27831b..559026c 100644
--- a/lib/pubsubd.h
+++ b/lib/pubsubd.h
@@ -103,5 +103,6 @@ void pubsubd_app_list_elm_create (struct app_list_elm *ale, struct process *p);
void pubsubd_app_list_elm_free (struct app_list_elm *todel);
void pubsub_connection (struct service *srv, struct process *p, enum app_list_elm_action action, const char *channame);
+void pubsub_disconnect (struct service *srv, struct process *p, enum app_list_elm_action action, const char *channame);
#endif
diff --git a/pubsub/pubsub-test-send-params.c b/pubsub/pubsub-test-send-params.c
new file mode 100644
index 0000000..b7379ae
--- /dev/null
+++ b/pubsub/pubsub-test-send-params.c
@@ -0,0 +1,118 @@
+#include "../lib/pubsubd.h"
+#include
+#include
+
+#define MYMESSAGE "coucou"
+#define MYCHAN "chan1"
+
+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]);
+}
+
+void sim_connection (pid_t pid, int index, int version, char *cmd, char *chan)
+{
+
+ printf ("Simulate connnection : pid %d index %d version %d "
+ "cmd %s chan %s\n"
+ , pid, index, version, cmd, chan );
+
+ struct service srv;
+ bzero (&srv, sizeof (struct service));
+ srv_init (&srv, PUBSUB_SERVICE_NAME);
+ printf ("Writing on %s.\n", srv.spath);
+
+ struct process p;
+ bzero (&p, sizeof (struct process));
+
+ if (app_create (&p, index)) // called by the application
+ ohshit (1, "app_create");
+
+ // send a message to warn the service we want to do something
+ // line : pid index version action chan
+ pubsub_connection (&srv, &p, PUBSUB_PUB, MYCHAN);
+
+ struct pubsub_msg m;
+ bzero (&m, sizeof (struct pubsub_msg));
+
+ // first message, "coucou"
+ m.type = PUBSUB_TYPE_INFO;
+ m.chan = malloc (strlen (MYCHAN));
+ m.chanlen = strlen (MYCHAN);
+ m.data = malloc (strlen (MYMESSAGE));
+ m.datalen = strlen (MYMESSAGE);
+ pubsub_msg_send (&srv, &p, &m);
+
+ // second message, to disconnect from the server
+ m.type = PUBSUB_TYPE_DISCONNECT;
+ pubsub_msg_send (&srv, &p, &m);
+
+ // free everything
+
+ pubsubd_msg_free (&m);
+
+ // the application will shut down, and remove the application named pipes
+ if (app_destroy (&p))
+ ohshit (1, "app_destroy");
+
+ srv_process_free (&p);
+}
+
+void sim_disconnection (pid_t pid, int index, int version)
+{
+ struct service srv;
+ bzero (&srv, sizeof (struct service));
+ srv_init (&srv, PUBSUB_SERVICE_NAME);
+ printf ("Disconnecting from %s.\n", srv.spath);
+
+ struct process p;
+ bzero (&p, sizeof (struct process));
+
+ // create the fake process
+ srv_process_gen (&p, pid, index, version);
+
+ // send a message to disconnect
+ // line : pid index version action chan
+ pubsub_disconnect (&srv, &p, PUBSUB_PUB, MYCHAN);
+
+ srv_process_free (&p);
+}
+
+ int
+main(int argc, char* argv[])
+{
+
+ 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];
+ sim_connection (pid, index, version, cmd, chan);
+ }
+ else {
+ sim_disconnection (pid, index, version);
+ }
+
+ return EXIT_SUCCESS;
+}