2016-06-05 20:48:13 +02:00
|
|
|
#include "../lib/pubsubd.h"
|
2016-05-28 19:34:23 +02:00
|
|
|
#include <stdlib.h>
|
2016-06-08 17:57:05 +02:00
|
|
|
#include <pthread.h>
|
2016-05-28 19:34:23 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
ohshit(int rvalue, const char* str) {
|
2016-06-04 20:33:44 +02:00
|
|
|
fprintf(stderr, "%s\n", str);
|
|
|
|
exit(rvalue);
|
2016-05-28 19:34:23 +02:00
|
|
|
}
|
|
|
|
|
2016-06-08 17:57:05 +02:00
|
|
|
// give this structure to the thread worker function
|
|
|
|
struct worker_params {
|
|
|
|
struct channels *chans;
|
|
|
|
struct channel *chan;
|
|
|
|
struct app_list_elm *ale;
|
|
|
|
};
|
|
|
|
|
|
|
|
void * pubsubd_worker_thread (void *params)
|
|
|
|
{
|
|
|
|
struct worker_params *wp = (struct worker_params *) params;
|
|
|
|
|
|
|
|
// each chan has a list of subscribers
|
|
|
|
// someone who only push a msg doesn't need to be registered
|
|
|
|
if (wp->ale->action == PUBSUB_BOTH || wp->ale->action == PUBSUB_PUB) {
|
|
|
|
// TODO add it to the application to follow
|
|
|
|
// TODO publish a message
|
|
|
|
printf ("publish or publish and subscribe to something\n");
|
|
|
|
|
|
|
|
struct pubsub_msg m;
|
|
|
|
bzero (&m, sizeof (struct pubsub_msg));
|
|
|
|
pubsubd_msg_recv (wp->ale->p, &m);
|
|
|
|
|
|
|
|
pubsubd_msg_print (&m);
|
|
|
|
|
|
|
|
if (m.type == PUBSUB_TYPE_DISCONNECT) {
|
|
|
|
// TODO remove the application from the subscribers
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
struct channel *chan = pubsubd_channel_get (wp->chans, wp->chan);
|
|
|
|
pubsubd_msg_send (chan->alh, &m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (wp->ale->action == PUBSUB_SUB) {
|
|
|
|
// TODO
|
|
|
|
printf ("subscribe to something\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf ("\033[31mdo not know what you want to do\033[00m\n");
|
|
|
|
printf ("\tale->p : %p\n", wp->ale->p);
|
|
|
|
}
|
|
|
|
|
|
|
|
pubsubd_app_list_elm_free (wp->ale);
|
|
|
|
|
|
|
|
pthread_exit (NULL);
|
|
|
|
}
|
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
2016-06-05 20:48:13 +02:00
|
|
|
struct service srv;
|
2016-06-07 17:44:18 +02:00
|
|
|
bzero (&srv, sizeof (struct service));
|
2016-06-05 20:48:13 +02:00
|
|
|
srv_init (&srv, PUBSUB_SERVICE_NAME);
|
2016-06-06 02:25:28 +02:00
|
|
|
printf ("Listening on %s.\n", srv.spath);
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
// creates the service named pipe, that listens to client applications
|
2016-06-06 02:25:28 +02:00
|
|
|
if (srv_create (&srv))
|
2016-06-04 20:33:44 +02:00
|
|
|
ohshit(1, "service_create error");
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-07 11:45:21 +02:00
|
|
|
// init chans list
|
2016-06-04 20:33:44 +02:00
|
|
|
struct channels chans;
|
2016-06-07 17:44:18 +02:00
|
|
|
bzero (&chans, sizeof (struct channels));
|
2016-06-04 20:33:44 +02:00
|
|
|
pubsubd_channels_init (&chans);
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
for (;;) {
|
2016-06-07 11:45:21 +02:00
|
|
|
// for each new process
|
|
|
|
struct app_list_elm ale;
|
2016-06-07 17:44:18 +02:00
|
|
|
bzero (&ale, sizeof (struct app_list_elm));
|
2016-06-08 17:57:05 +02:00
|
|
|
struct channel *chan;
|
|
|
|
pubsubd_get_new_process (&srv, &ale, &chans, &chan);
|
2016-06-07 17:44:18 +02:00
|
|
|
pubsubd_channels_print (&chans);
|
|
|
|
|
|
|
|
// end the application
|
|
|
|
if (ale.action == PUBSUB_QUIT) {
|
2016-06-07 13:49:23 +02:00
|
|
|
printf ("Quitting ...\n");
|
2016-06-07 17:44:18 +02:00
|
|
|
|
|
|
|
pubsubd_channels_del_all (&chans);
|
|
|
|
srv_close (&srv);
|
|
|
|
|
|
|
|
// TODO end the threads
|
|
|
|
|
2016-06-07 13:49:23 +02:00
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
2016-06-07 17:44:18 +02:00
|
|
|
// TODO thread to handle multiple clients at a time
|
2016-06-08 17:57:05 +02:00
|
|
|
struct worker_params *wp;
|
|
|
|
wp = malloc (sizeof (struct worker_params));
|
|
|
|
wp->ale = pubsubd_app_list_elm_copy (&ale);
|
|
|
|
wp->chans = &chans;
|
|
|
|
wp->chan = chan;
|
2016-06-07 13:49:23 +02:00
|
|
|
|
2016-06-08 17:57:05 +02:00
|
|
|
pthread_t thr;
|
2016-06-07 13:49:23 +02:00
|
|
|
|
2016-06-08 17:57:05 +02:00
|
|
|
pthread_create (&thr, NULL, pubsubd_worker_thread, wp);
|
|
|
|
pthread_detach (thr);
|
2016-06-07 11:45:21 +02:00
|
|
|
|
2016-06-07 17:44:18 +02:00
|
|
|
pubsubd_app_list_elm_free (&ale);
|
2016-06-04 20:33:44 +02:00
|
|
|
}
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
// the application will shut down, and remove the service named pipe
|
2016-06-06 02:25:28 +02:00
|
|
|
if (srv_close (&srv))
|
2016-06-07 13:49:23 +02:00
|
|
|
ohshit (1, "service_close error");
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
return EXIT_SUCCESS;
|
2016-05-28 19:34:23 +02:00
|
|
|
}
|
|
|
|
|
2016-06-06 02:25:28 +02:00
|
|
|
#if 0
|
2016-06-05 20:48:13 +02:00
|
|
|
void main_loop (const struct service *srv)
|
2016-06-04 20:33:44 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct process proc;
|
|
|
|
|
|
|
|
int cnt = 10;
|
|
|
|
|
|
|
|
while (cnt--) {
|
|
|
|
// -1 : error, 0 = no new process, 1 = new process
|
2016-06-05 20:48:13 +02:00
|
|
|
ret = srv_get_new_process (&proc, srv);
|
2016-06-04 20:33:44 +02:00
|
|
|
if (ret == -1) {
|
|
|
|
fprintf (stderr, "error service_get_new_process\n");
|
|
|
|
continue;
|
|
|
|
} else if (ret == 0) { // that should not happen
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// printf ("before print\n");
|
|
|
|
process_print (&proc);
|
|
|
|
// printf ("after print\n");
|
|
|
|
|
|
|
|
// about the message
|
|
|
|
size_t msize = BUFSIZ;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
bzero(buf, BUFSIZ);
|
|
|
|
|
|
|
|
// printf ("before read\n");
|
2016-06-05 12:45:45 +02:00
|
|
|
if ((ret = srv_read (&proc, &buf, &msize))) {
|
2016-06-04 20:33:44 +02:00
|
|
|
fprintf(stdout, "error service_read %d\n", ret);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// printf ("after read\n");
|
|
|
|
printf ("read, size %ld : %s\n", msize, buf);
|
|
|
|
|
|
|
|
// printf ("before proc write\n");
|
2016-06-05 12:45:45 +02:00
|
|
|
if ((ret = srv_write (&proc, &buf, msize))) {
|
2016-06-04 20:33:44 +02:00
|
|
|
fprintf(stdout, "error service_write %d\n", ret);
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-05 12:45:45 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
// printf ("after proc write\n");
|
|
|
|
printf ("\033[32mStill \033[31m%d\033[32m applications to serve\n",cnt);
|
|
|
|
}
|
|
|
|
}
|
2016-06-06 02:25:28 +02:00
|
|
|
#endif
|