2017-08-25 11:42:43 +02:00
|
|
|
#include "../../core/communication.h"
|
|
|
|
#include "../../core/msg.h"
|
2018-10-04 01:22:50 +02:00
|
|
|
#include "../../core/client.h"
|
2017-08-25 11:42:43 +02:00
|
|
|
#include "../../core/utils.h"
|
|
|
|
#include "../../core/error.h"
|
2017-08-26 19:42:54 +02:00
|
|
|
#include "../../core/logger.h"
|
2017-08-25 11:42:43 +02:00
|
|
|
|
|
|
|
#include "remoted.h"
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-08-28 00:03:35 +02:00
|
|
|
/**
|
2018-10-04 01:22:50 +02:00
|
|
|
* new connection, once accepted the client is added to the array_proc
|
2017-08-28 00:03:35 +02:00
|
|
|
* structure to be checked periodically for new messages
|
|
|
|
*/
|
2018-10-04 22:51:31 +02:00
|
|
|
void handle_new_connection (struct ipc_service *srv, struct ipc_clients *ap)
|
2017-08-28 00:03:35 +02:00
|
|
|
{
|
2018-10-04 00:30:47 +02:00
|
|
|
struct ipc_client *p = malloc(sizeof(struct ipc_client));
|
|
|
|
memset(p, 0, sizeof(struct ipc_client));
|
2017-08-28 00:03:35 +02:00
|
|
|
|
2018-10-04 01:22:50 +02:00
|
|
|
if (ipc_server_accept (srv, p) < 0) {
|
|
|
|
handle_error("ipc_server_accept < 0");
|
2017-08-28 00:03:35 +02:00
|
|
|
} else {
|
|
|
|
log_debug ("remoted, new connection", p->proc_fd);
|
|
|
|
}
|
|
|
|
|
2018-10-04 01:22:50 +02:00
|
|
|
if (ipc_client_add (ap, p) < 0) {
|
|
|
|
handle_error("ipc_client_add < 0");
|
2017-08-28 00:03:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 22:51:31 +02:00
|
|
|
void handle_new_msg (struct ipc_clients *ap, struct ipc_clients *proc_to_read)
|
2017-08-28 00:03:35 +02:00
|
|
|
{
|
2018-10-03 22:02:37 +02:00
|
|
|
struct ipc_message m;
|
|
|
|
memset (&m, 0, sizeof (struct ipc_message));
|
2017-08-28 00:03:35 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < proc_to_read->size; i++) {
|
2018-10-04 01:54:12 +02:00
|
|
|
if (ipc_server_read (proc_to_read->clients[i], &m) < 0) {
|
2018-10-04 01:22:50 +02:00
|
|
|
handle_error("ipc_server_read < 0");
|
2017-08-28 00:03:35 +02:00
|
|
|
}
|
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
mprint_hexa ("msg received: ", (unsigned char *) m.payload, m.length);
|
2017-08-28 00:03:35 +02:00
|
|
|
|
2018-10-04 01:22:50 +02:00
|
|
|
// close the client then delete it from the client array
|
2017-08-28 00:03:35 +02:00
|
|
|
if (m.type == MSG_TYPE_CLOSE) {
|
2018-10-04 01:54:12 +02:00
|
|
|
struct ipc_client *p = proc_to_read->clients[i];
|
2017-08-28 00:03:35 +02:00
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
log_debug ("remoted, client %d disconnecting", p->proc_fd);
|
2017-08-28 00:03:35 +02:00
|
|
|
|
2018-10-04 01:22:50 +02:00
|
|
|
// close the connection to the client
|
2018-10-04 01:54:12 +02:00
|
|
|
if (ipc_server_close_client (p) < 0)
|
|
|
|
handle_error( "ipc_server_close_client < 0");
|
2017-08-28 00:03:35 +02:00
|
|
|
|
2018-10-04 01:22:50 +02:00
|
|
|
// remove the client from the clientes list
|
|
|
|
if (ipc_client_del (ap, p) < 0)
|
|
|
|
handle_error( "ipc_client_del < 0");
|
|
|
|
if (ipc_client_del (proc_to_read, p) < 0)
|
|
|
|
handle_err( "handle_new_msg", "ipc_client_del < 0");
|
2017-08-28 00:03:35 +02:00
|
|
|
|
2018-10-03 21:52:11 +02:00
|
|
|
ipc_message_free (&m);
|
2017-08-28 00:03:35 +02:00
|
|
|
|
2018-10-04 01:22:50 +02:00
|
|
|
// free client
|
2017-08-28 00:03:35 +02:00
|
|
|
free (p);
|
|
|
|
|
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
struct pubsub_msg m_data;
|
|
|
|
memset (&m_data, 0, sizeof (struct pubsub_msg));
|
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
pubsub_message_unserialize (&m_data, m.payload, m.length);
|
2017-08-28 00:03:35 +02:00
|
|
|
|
|
|
|
if (m_data.type == PUBSUB_MSG_TYPE_SUB) {
|
2018-10-04 01:54:12 +02:00
|
|
|
printf ("client %d subscribing to %s\n"
|
|
|
|
, proc_to_read->clients[i]->proc_fd
|
2017-08-28 00:03:35 +02:00
|
|
|
, m_data.chan);
|
|
|
|
pubsubd_channels_subscribe (chans
|
2018-10-04 01:54:12 +02:00
|
|
|
, m_data.chan, proc_to_read->clients[i]);
|
2017-08-28 00:03:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_data.type == PUBSUB_MSG_TYPE_UNSUB) {
|
2018-10-04 01:54:12 +02:00
|
|
|
printf ("client %d unsubscribing to %s\n"
|
|
|
|
, proc_to_read->clients[i]->proc_fd
|
2017-08-28 00:03:35 +02:00
|
|
|
, m_data.chan);
|
|
|
|
pubsubd_channels_unsubscribe (chans
|
2018-10-04 01:54:12 +02:00
|
|
|
, m_data.chan, proc_to_read->clients[i]);
|
2017-08-28 00:03:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_data.type == PUBSUB_MSG_TYPE_PUB) {
|
2018-10-04 01:54:12 +02:00
|
|
|
printf ("client %d publishing to %s\n"
|
|
|
|
, proc_to_read->clients[i]->proc_fd
|
2017-08-28 00:03:35 +02:00
|
|
|
, m_data.chan);
|
|
|
|
struct channel *chan = pubsubd_channel_search (chans, m_data.chan);
|
|
|
|
if (chan == NULL) {
|
|
|
|
handle_err ("handle_new_msg", "publish on nonexistent channel");
|
2018-10-03 21:52:11 +02:00
|
|
|
ipc_message_free (&m);
|
2017-08-28 00:03:35 +02:00
|
|
|
return ;
|
|
|
|
}
|
|
|
|
pubsubd_send (chan->subs, &m_data);
|
|
|
|
}
|
|
|
|
|
2018-10-03 21:52:11 +02:00
|
|
|
pubsub_message_free (&m_data);
|
2017-08-28 00:03:35 +02:00
|
|
|
#endif
|
2018-10-03 21:52:11 +02:00
|
|
|
ipc_message_free (&m);
|
2017-08-28 00:03:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 00:30:47 +02:00
|
|
|
void remoted_main_loop (struct ipc_service *srv, struct remoted_ctx *ctx)
|
2017-08-25 11:42:43 +02:00
|
|
|
{
|
2017-08-26 19:42:54 +02:00
|
|
|
log_debug ("remoted entering main loop");
|
2017-08-28 00:03:35 +02:00
|
|
|
int i, ret = 0;
|
|
|
|
|
2018-10-04 22:51:31 +02:00
|
|
|
struct ipc_clients ap;
|
|
|
|
memset(&ap, 0, sizeof(struct ipc_clients));
|
2017-08-28 00:03:35 +02:00
|
|
|
|
2018-10-04 22:51:31 +02:00
|
|
|
struct ipc_clients proc_to_read;
|
|
|
|
memset(&proc_to_read, 0, sizeof(struct ipc_clients));
|
2017-08-28 00:03:35 +02:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
/* TODO: authorizations */
|
2018-10-03 21:52:11 +02:00
|
|
|
ret = ipc_server_select (&ap, srv, &proc_to_read);
|
2017-08-28 00:03:35 +02:00
|
|
|
|
|
|
|
if (ret == CONNECTION) {
|
|
|
|
handle_new_connection (srv, &ap);
|
|
|
|
} else if (ret == APPLICATION) {
|
|
|
|
handle_new_msg (&ap, &proc_to_read);
|
|
|
|
} else { // both new connection and new msg from at least one client
|
|
|
|
handle_new_connection (srv, &ap);
|
|
|
|
handle_new_msg (&ap, &proc_to_read);
|
|
|
|
}
|
2018-10-08 16:15:35 +02:00
|
|
|
ipc_clients_free (&proc_to_read);
|
2017-08-28 00:03:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ap.size; i++) {
|
2018-10-04 01:54:12 +02:00
|
|
|
if (ipc_server_close_client (ap.clients[i]) < 0) {
|
|
|
|
handle_error( "ipc_server_close_client < 0");
|
2017-08-28 00:03:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void remoted_free_ctx (struct remoted_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->unix_socket_dir != NULL)
|
|
|
|
free (ctx->unix_socket_dir), ctx->unix_socket_dir = NULL;
|
2017-08-25 11:42:43 +02:00
|
|
|
}
|