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

159 lines
3.6 KiB
C
Raw Normal View History

2016-05-26 18:27:59 +02:00
#include "communication.h"
2016-12-17 18:00:04 +01:00
#include "usocket.h"
#include "utils.h"
2016-12-21 01:26:47 +01:00
#include "error.h"
2016-12-17 18:00:04 +01:00
#include <assert.h>
#include <stdio.h>
2016-09-11 14:37:41 +02:00
#include <errno.h>
2016-10-28 13:58:04 +02:00
void service_path (char *path, const char *sname, int index, int version)
{
2016-12-21 01:26:47 +01:00
assert (path != NULL);
assert (sname != NULL);
2016-12-17 18:00:04 +01:00
memset (path, 0, PATH_MAX);
snprintf (path, PATH_MAX, "%s/%s-%d-%d", TMPDIR, sname, index, version);
2016-06-12 12:38:43 +02:00
}
2016-12-17 18:00:04 +01:00
int srv_init (int argc, char **argv, char **env
, struct service *srv, const char *sname)
2016-05-26 18:27:59 +02:00
{
2016-06-05 20:48:13 +02:00
if (srv == NULL)
2016-06-13 09:47:19 +02:00
return ER_PARAMS;
2016-05-26 18:27:59 +02:00
2016-06-12 14:41:25 +02:00
// TODO
// use the argc, argv and env parameters
// it will be useful to change some parameters transparently
// ex: to get resources from other machines, choosing the
// remote with environment variables
argc = argc;
argv = argv;
env = env;
2016-12-17 18:00:04 +01:00
// gets the service path
service_path (srv->spath, sname, srv->index, srv->version);
2016-06-13 09:47:19 +02:00
usock_init (&srv->service_fd, srv->spath);
2016-05-26 18:27:59 +02:00
return 0;
}
int srv_accept (struct service *srv, struct process *p)
{
2016-12-21 01:26:47 +01:00
assert (srv != NULL);
assert (p != NULL);
2016-12-21 01:26:47 +01:00
usock_accept (srv->service_fd, &p->proc_fd);
2016-12-21 01:26:47 +01:00
struct msg m_con;
memset (&m_con, 0, sizeof (struct msg));
srv_read (p, &m_con);
// TODO: handle the parameters in the first message
msg_free (&m_con);
2016-12-21 01:26:47 +01:00
struct msg m_ack;
memset (&m_ack, 0, sizeof (struct msg));
msg_format_ack (&m_ack, NULL, 0);
srv_write (p, &m_ack);
msg_free (&m_ack);
2016-12-19 22:49:26 +01:00
return 0;
}
2016-06-05 20:48:13 +02:00
int srv_close (struct service *srv)
2016-05-26 18:27:59 +02:00
{
2016-12-17 18:00:04 +01:00
usock_close (srv->service_fd);
return usock_remove (srv->spath);
2016-05-26 18:27:59 +02:00
}
int srv_close_proc (struct process *p)
{
2016-12-21 01:26:47 +01:00
struct msg m;
memset (&m, 0, sizeof (struct msg));
m.type = MSG_TYPE_DIS;
if (msg_write (p->proc_fd, &m) < 0) {
handle_err ("srv_close_proc", "msg_write < 0");
}
return usock_close (p->proc_fd);
2016-05-26 21:56:43 +02:00
}
2016-12-21 01:26:47 +01:00
int srv_read (const struct process *p, struct msg *m)
2016-05-26 21:56:43 +02:00
{
2016-12-21 01:26:47 +01:00
return msg_read (p->proc_fd, m);
2016-05-26 18:27:59 +02:00
}
2016-12-21 01:26:47 +01:00
int srv_write (const struct process *p, const struct msg *m)
{
2016-12-21 01:26:47 +01:00
return msg_write (p->proc_fd, m);
}
int app_connection (int argc, char **argv, char **env
, struct service *srv, const char *sname
, const char *connectionstr, size_t msize)
{
argc = argc;
argv = argv;
env = env;
2016-11-03 22:44:35 +01:00
2016-12-17 18:00:04 +01:00
assert (srv != NULL);
assert (sname != NULL);
2016-10-28 13:58:04 +02:00
2016-12-17 18:00:04 +01:00
if (srv == NULL) {
2016-10-28 13:58:04 +02:00
return -1;
2016-12-17 18:00:04 +01:00
}
2016-10-28 13:58:04 +02:00
2016-12-17 18:00:04 +01:00
// gets the service path
service_path (srv->spath, sname, srv->index, srv->version);
usock_connect (&srv->service_fd, srv->spath);
2016-10-28 13:58:04 +02:00
// send connection string and receive acknowledgement
2016-12-21 01:26:47 +01:00
struct msg m_con;
memset (&m_con, 0, sizeof (struct msg));
// format the connection msg
if (msg_format_con (&m_con, connectionstr, msize) < 0) {
handle_err ("app_connection", "msg_format_con");
return -1;
}
2016-12-21 01:26:47 +01:00
// send connection msg
app_write (srv, &m_con);
msg_free (&m_con);
2016-12-21 01:26:47 +01:00
// receive ack msg
struct msg m_ack;
memset (&m_ack, 0, sizeof (struct msg));
app_read (srv, &m_ack);
2016-12-21 01:26:47 +01:00
assert (m_ack.type == MSG_TYPE_ACK);
assert (m_ack.valsize == 0);
msg_free (&m_ack);
2016-12-19 22:49:26 +01:00
2016-10-28 13:58:04 +02:00
return 0;
}
int app_close (struct service *srv)
2016-05-26 18:27:59 +02:00
{
2016-12-21 01:26:47 +01:00
struct msg m;
memset (&m, 0, sizeof (struct msg));
m.type = MSG_TYPE_DIS;
if (msg_write (srv->service_fd, &m) < 0) {
handle_err ("app_close", "msg_write < 0");
}
2016-12-17 18:00:04 +01:00
return usock_close (srv->service_fd);
2016-05-26 18:27:59 +02:00
}
2016-12-21 01:26:47 +01:00
int app_read (struct service *srv, struct msg *m)
2016-10-28 14:24:15 +02:00
{
2016-12-21 01:26:47 +01:00
return msg_read (srv->service_fd, m);
2016-05-26 18:27:59 +02:00
}
2016-12-21 01:26:47 +01:00
int app_write (struct service *srv, const struct msg *m)
2016-05-26 18:27:59 +02:00
{
2016-12-21 01:26:47 +01:00
return msg_write (srv->service_fd, m);
2016-10-28 13:58:04 +02:00
}