2017-01-19 22:07:52 +01:00
|
|
|
// int main(void) { return 0; }
|
|
|
|
|
|
|
|
// TODO: select on service + input instead of threads
|
|
|
|
|
|
|
|
#include "../../core/error.h"
|
|
|
|
#include "../lib/pubsub.h"
|
2016-09-11 16:55:02 +02:00
|
|
|
#include "../lib/pubsubd.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
2017-01-19 22:07:52 +01:00
|
|
|
#include <unistd.h>
|
2016-09-11 16:55:02 +02:00
|
|
|
|
2017-01-19 22:07:52 +01:00
|
|
|
void usage (char **argv) {
|
|
|
|
printf ( "usage: %s [chan [pub]]\n", argv[0]);
|
2016-09-11 16:55:02 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 19:30:28 +02:00
|
|
|
void print_cmd (void) {
|
|
|
|
printf ("\033[32m>\033[00m ");
|
|
|
|
fflush (stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void * listener (void *params)
|
|
|
|
{
|
|
|
|
int s = 0;
|
2017-01-19 22:07:52 +01:00
|
|
|
s = pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
|
|
|
|
if (s != 0) {
|
|
|
|
handle_err ("listener", "pthread_setcancelstate != 0");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct service *srv = NULL;
|
|
|
|
srv = (struct service *) params;
|
|
|
|
if (srv == NULL) {
|
|
|
|
handle_err ("listener", "no service passed");
|
2016-09-12 19:30:28 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// main loop
|
|
|
|
while (1) {
|
|
|
|
struct pubsub_msg m;
|
|
|
|
memset (&m, 0, sizeof (struct pubsub_msg));
|
|
|
|
|
2017-01-19 22:07:52 +01:00
|
|
|
pubsub_msg_recv (srv, &m);
|
|
|
|
printf ("\r\033[31m>\033[00m %s\n", m.data);
|
2016-09-12 19:30:28 +02:00
|
|
|
print_cmd ();
|
|
|
|
|
2017-01-19 22:07:52 +01:00
|
|
|
pubsub_msg_free (&m);
|
2016-09-12 19:30:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_exit (NULL);
|
|
|
|
}
|
|
|
|
|
2017-01-19 22:07:52 +01:00
|
|
|
void chan_sub (struct service *srv, char *chan)
|
|
|
|
{
|
|
|
|
struct pubsub_msg msg;
|
|
|
|
memset (&msg, 0, sizeof (struct pubsub_msg));
|
|
|
|
|
|
|
|
// meta data on the message
|
|
|
|
msg.type = PUBSUB_MSG_TYPE_SUB;
|
|
|
|
msg.chanlen = strlen (chan) + 1;
|
|
|
|
msg.chan = malloc (msg.chanlen);
|
|
|
|
memset (msg.chan, 0, msg.chanlen);
|
|
|
|
strncpy (msg.chan, chan, msg.chanlen);
|
|
|
|
msg.chan[strlen (chan)] = '\0';
|
|
|
|
|
|
|
|
pubsub_msg_send (srv, &msg);
|
|
|
|
printf ("subscribed to %s\n", chan);
|
|
|
|
|
|
|
|
pubsub_msg_free (&msg);
|
|
|
|
}
|
2016-09-12 19:30:28 +02:00
|
|
|
|
2016-09-11 16:55:02 +02:00
|
|
|
void main_loop (int argc, char **argv, char **env
|
2017-01-19 22:07:52 +01:00
|
|
|
, int index, int version
|
2016-09-11 16:55:02 +02:00
|
|
|
, char *cmd, char *chan)
|
|
|
|
{
|
2017-01-19 22:07:52 +01:00
|
|
|
printf ("connection to pubsubd: index %d version %d "
|
2016-09-11 16:55:02 +02:00
|
|
|
"cmd %s chan %s\n"
|
2017-01-19 22:07:52 +01:00
|
|
|
, index, version, cmd, chan );
|
2016-09-11 16:55:02 +02:00
|
|
|
|
|
|
|
struct service srv;
|
|
|
|
memset (&srv, 0, sizeof (struct service));
|
2017-01-19 22:07:52 +01:00
|
|
|
pubsub_connection (argc, argv, env, &srv);
|
|
|
|
printf ("connected\n");
|
2016-09-11 16:55:02 +02:00
|
|
|
|
2017-01-19 22:07:52 +01:00
|
|
|
if (strncmp (cmd, "sub", 3) == 0) {
|
|
|
|
chan_sub (&srv, chan);
|
|
|
|
}
|
2016-09-11 16:55:02 +02:00
|
|
|
|
2016-09-12 19:30:28 +02:00
|
|
|
pthread_t thr;
|
|
|
|
memset (&thr, 0, sizeof (pthread_t));
|
|
|
|
|
2017-01-19 22:07:52 +01:00
|
|
|
pthread_create (&thr, NULL, listener, &srv);
|
2016-09-12 19:30:28 +02:00
|
|
|
pthread_detach (thr);
|
|
|
|
|
2016-09-11 16:55:02 +02:00
|
|
|
printf ("main_loop\n");
|
|
|
|
|
2016-09-12 12:45:31 +02:00
|
|
|
struct pubsub_msg msg;
|
|
|
|
memset (&msg, 0, sizeof (struct pubsub_msg));
|
2016-09-11 16:55:02 +02:00
|
|
|
|
|
|
|
// meta data on the message
|
2017-01-19 22:07:52 +01:00
|
|
|
msg.type = PUBSUB_MSG_TYPE_PUB;
|
|
|
|
msg.chanlen = strlen (chan) + 1;
|
|
|
|
msg.chan = malloc (msg.chanlen);
|
|
|
|
memset (msg.chan, 0, msg.chanlen);
|
|
|
|
strncpy ((char *) msg.chan, chan, msg.chanlen);
|
2016-09-12 12:45:31 +02:00
|
|
|
msg.chan[strlen (chan)] = '\0';
|
2016-09-11 16:55:02 +02:00
|
|
|
|
|
|
|
// msg loop
|
|
|
|
for (;;) {
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
memset (buf, 0, BUFSIZ);
|
2016-09-12 19:30:28 +02:00
|
|
|
print_cmd ();
|
2016-09-11 16:55:02 +02:00
|
|
|
fflush (stdout);
|
|
|
|
|
|
|
|
size_t mlen = read (0, buf, BUFSIZ);
|
|
|
|
|
2017-01-19 22:07:52 +01:00
|
|
|
// remove \n
|
2016-09-12 19:30:28 +02:00
|
|
|
if (mlen > 1) {
|
|
|
|
mlen--;
|
|
|
|
}
|
|
|
|
buf[mlen] = '\0';
|
|
|
|
|
|
|
|
if (strncmp(buf, "quit", strlen ("quit")) == 0) {
|
2016-09-11 16:55:02 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-19 22:07:52 +01:00
|
|
|
msg.datalen = strlen (buf) + 1;
|
|
|
|
msg.data = malloc (msg.datalen);
|
|
|
|
memset (msg.data, 0, msg.datalen);
|
|
|
|
strncpy ((char *) msg.data, buf, msg.datalen);
|
|
|
|
msg.data[strlen(buf)] = '\0';
|
2016-09-11 16:55:02 +02:00
|
|
|
|
2017-01-19 22:07:52 +01:00
|
|
|
pubsub_msg_send (&srv, &msg);
|
2016-09-12 12:45:31 +02:00
|
|
|
free (msg.data);
|
|
|
|
msg.data = NULL;
|
|
|
|
msg.datalen = 0;
|
2016-09-11 16:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// free everything
|
2017-01-19 22:07:52 +01:00
|
|
|
pubsub_msg_free (&msg);
|
2016-09-11 16:55:02 +02:00
|
|
|
|
2016-09-12 19:30:28 +02:00
|
|
|
pthread_cancel (thr);
|
|
|
|
pthread_join (thr, NULL);
|
|
|
|
|
2016-09-11 16:55:02 +02:00
|
|
|
printf ("disconnection...\n");
|
|
|
|
// disconnect from the server
|
2017-01-19 22:07:52 +01:00
|
|
|
pubsub_disconnect (&srv);
|
2016-09-11 16:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv, char **env)
|
|
|
|
{
|
2017-01-19 22:07:52 +01:00
|
|
|
char *cmd = "sub";
|
|
|
|
char *chan = "chan1";
|
2016-09-11 16:55:02 +02:00
|
|
|
|
2017-01-19 22:07:52 +01:00
|
|
|
if (argc == 2 && strncmp("-h", argv[1], 2) == 0) {
|
2016-09-11 16:55:02 +02:00
|
|
|
usage (argv);
|
2017-01-19 22:07:52 +01:00
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc >= 2) {
|
|
|
|
chan = argv[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc >= 3) {
|
|
|
|
cmd = argv[2];
|
2016-09-11 16:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
// don't care about the version
|
2017-01-19 22:07:52 +01:00
|
|
|
int version = 0;
|
2016-09-11 16:55:02 +02:00
|
|
|
|
2017-01-19 22:07:52 +01:00
|
|
|
main_loop (argc, argv, env, index, version, cmd, chan);
|
2016-09-11 16:55:02 +02:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|