2016-06-05 03:19:36 +02:00
|
|
|
#include "pubsubd.h"
|
2016-05-28 19:34:23 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
const char* service_name = "pubsub";
|
|
|
|
|
|
|
|
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-05 12:45:45 +02:00
|
|
|
// CHANNELS
|
2016-06-04 20:33:44 +02:00
|
|
|
|
2016-06-05 12:45:45 +02:00
|
|
|
void pubsubd_channels_init (struct channels *chans) { LIST_INIT(chans); }
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
void
|
2016-06-05 12:45:45 +02:00
|
|
|
pubsubd_channels_add (struct channels *chans, struct channel *c)
|
2016-06-04 20:33:44 +02:00
|
|
|
{
|
|
|
|
if(!chans || !c)
|
|
|
|
return;
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-05 12:45:45 +02:00
|
|
|
struct channel *n = pubsubd_channel_copy (c);
|
2016-06-04 20:33:44 +02:00
|
|
|
LIST_INSERT_HEAD(al, n, entries);
|
|
|
|
}
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
void
|
2016-06-05 12:45:45 +02:00
|
|
|
pubsubd_channels_del (struct app_list *al, struct channel *c)
|
2016-06-04 20:33:44 +02:00
|
|
|
{
|
2016-06-05 12:45:45 +02:00
|
|
|
struct channel *todel = pubsubd_channel_get (al, c);
|
2016-06-04 20:33:44 +02:00
|
|
|
if(todel != NULL) {
|
|
|
|
LIST_REMOVE(todel, entries);
|
|
|
|
srv_process_free (mfree, todel);
|
|
|
|
mfree (todel);
|
|
|
|
todel = NULL;
|
|
|
|
}
|
|
|
|
}
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-05 12:45:45 +02:00
|
|
|
struct channel * pubsubd_channel_copy (struct channel *c)
|
|
|
|
{
|
|
|
|
struct channel *copy;
|
|
|
|
copy = malloc (sizeof(struct channel));
|
|
|
|
memcpy (copy, c, sizeof(struct channel));
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pubsubd_channels_eq (const struct channel *c1, const struct channel *c2)
|
|
|
|
{
|
|
|
|
return (strncmp (c1->chan, c2->chan, c1->chanlen) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// SUBSCRIBER
|
|
|
|
|
|
|
|
void pubsubd_subscriber_init (struct app_list *al) { LIST_INIT(al); }
|
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
void
|
|
|
|
pubsubd_subscriber_add (struct app_list *al, struct process *p)
|
|
|
|
{
|
|
|
|
if(!al || !p)
|
|
|
|
return;
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
struct process *n = srv_process_copy (p);
|
|
|
|
LIST_INSERT_HEAD(al, n, entries);
|
|
|
|
}
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
struct process *
|
|
|
|
pubsubd_subscriber_get (const struct app_list *al
|
|
|
|
, const struct process *p)
|
|
|
|
{
|
|
|
|
struct process *np, *res = NULL;
|
|
|
|
LIST_FOREACH(np, al, entries) {
|
|
|
|
if(srv_process_eq (np, p)) {
|
|
|
|
res = np;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
void
|
|
|
|
pubsubd_subscriber_del (struct app_list *al, struct process *p)
|
|
|
|
{
|
2016-06-05 12:45:45 +02:00
|
|
|
struct process *todel = pubsubd_subscriber_get (al, p);
|
2016-06-04 20:33:44 +02:00
|
|
|
if(todel != NULL) {
|
|
|
|
LIST_REMOVE(todel, entries);
|
|
|
|
srv_process_free (mfree, todel);
|
|
|
|
mfree (todel);
|
|
|
|
todel = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pubsubd_msg_send (struct service *s, struct message * m, struct process *p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void pubsubd_msg_recv (struct service *s, struct message * m, struct process *p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void pubsub_msg_send (struct service *s, struct message * m)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void pubsub_msg_recv (struct service *s, struct message * m)
|
|
|
|
{
|
|
|
|
}
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
// gets the service path, such as /tmp/<service>
|
|
|
|
char s_path[PATH_MAX];
|
|
|
|
service_path (s_path, service_name);
|
|
|
|
printf ("Listening on %s.\n", s_path);
|
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
|
|
|
|
if (service_create (s_path))
|
|
|
|
ohshit(1, "service_create error");
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
struct channels chans;
|
|
|
|
pubsubd_channels_init (&chans);
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
for (;;) {
|
|
|
|
struct process proc;
|
|
|
|
int proc_count, i;
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
service_get_new_process (&proc, s_path);
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
printf("> %i proc\n", proc_count);
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
for (i = 0; i < proc_count; i++) {
|
|
|
|
size_t message_size = BUFSIZ;
|
|
|
|
char buffer[BUFSIZ];
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
process_print(proc + i);
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
if (process_read (&proc[i], &buffer, &message_size))
|
|
|
|
ohshit(1, "process_read error");
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
printf(": %s\n", buffer);
|
2016-05-28 19:34:23 +02:00
|
|
|
|
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
}
|
2016-05-28 19:34:23 +02:00
|
|
|
|
2016-06-04 20:33:44 +02:00
|
|
|
service_free_processes(&proc, proc_count);
|
|
|
|
}
|
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
|
|
|
|
if (service_close (s_path))
|
|
|
|
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-04 20:33:44 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* main loop
|
|
|
|
*
|
|
|
|
* opens the application pipes,
|
|
|
|
* reads then writes the same message,
|
|
|
|
* then closes the pipes
|
|
|
|
*/
|
|
|
|
|
|
|
|
void main_loop (const char *spath)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct process proc;
|
|
|
|
|
|
|
|
int cnt = 10;
|
|
|
|
|
|
|
|
while (cnt--) {
|
|
|
|
// -1 : error, 0 = no new process, 1 = new process
|
2016-06-05 12:45:45 +02:00
|
|
|
ret = srv_get_new_process (&proc, spath);
|
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);
|
|
|
|
}
|
|
|
|
}
|