Archived
3
0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues or pull requests.
libipc-old/remote/lib/remoted.c

150 lines
4.5 KiB
C
Raw Normal View History

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"
#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>
/**
2018-10-04 01:22:50 +02:00
* new connection, once accepted the client is added to the array_proc
* structure to be checked periodically for new messages
*/
void handle_new_connection (struct ipc_service *srv, struct ipc_clients *ap)
{
2018-10-04 00:30:47 +02:00
struct ipc_client *p = malloc(sizeof(struct ipc_client));
memset(p, 0, sizeof(struct ipc_client));
2018-10-04 01:22:50 +02:00
if (ipc_server_accept (srv, p) < 0) {
handle_error("ipc_server_accept < 0");
} 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");
}
}
void handle_new_msg (struct ipc_clients *ap, struct ipc_clients *proc_to_read)
{
2018-10-03 22:02:37 +02:00
struct ipc_message m;
memset (&m, 0, sizeof (struct ipc_message));
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");
}
2018-10-04 01:54:12 +02:00
mprint_hexa ("msg received: ", (unsigned char *) m.payload, m.length);
2018-10-04 01:22:50 +02:00
// close the client then delete it from the client array
if (m.type == MSG_TYPE_CLOSE) {
2018-10-04 01:54:12 +02:00
struct ipc_client *p = proc_to_read->clients[i];
2018-10-04 01:54:12 +02:00
log_debug ("remoted, client %d disconnecting", p->proc_fd);
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");
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");
2018-10-03 21:52:11 +02:00
ipc_message_free (&m);
2018-10-04 01:22:50 +02:00
// free client
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);
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
, m_data.chan);
pubsubd_channels_subscribe (chans
2018-10-04 01:54:12 +02:00
, m_data.chan, proc_to_read->clients[i]);
}
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
, m_data.chan);
pubsubd_channels_unsubscribe (chans
2018-10-04 01:54:12 +02:00
, m_data.chan, proc_to_read->clients[i]);
}
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
, 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);
return ;
}
pubsubd_send (chan->subs, &m_data);
}
2018-10-03 21:52:11 +02:00
pubsub_message_free (&m_data);
#endif
2018-10-03 21:52:11 +02:00
ipc_message_free (&m);
}
}
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
{
log_debug ("remoted entering main loop");
int i, ret = 0;
struct ipc_clients ap;
memset(&ap, 0, sizeof(struct ipc_clients));
struct ipc_clients proc_to_read;
memset(&proc_to_read, 0, sizeof(struct ipc_clients));
while(1) {
/* TODO: authorizations */
2018-10-03 21:52:11 +02:00
ret = ipc_server_select (&ap, srv, &proc_to_read);
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-04 01:22:50 +02:00
ipc_client_array_free (&proc_to_read);
}
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");
}
}
}
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
}