2016-05-26 18:27:59 +02:00
|
|
|
#include "communication.h"
|
2016-12-17 18:00:04 +01:00
|
|
|
#include "usocket.h"
|
2016-12-19 18:16:38 +01:00
|
|
|
#include "msg-format.h"
|
2016-12-19 19:20:27 +01:00
|
|
|
#include "utils.h"
|
2016-12-17 18:00:04 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
2016-05-30 01:54:19 +02:00
|
|
|
#include <stdio.h>
|
2016-09-11 14:37:41 +02:00
|
|
|
#include <errno.h>
|
2016-10-28 13:58:04 +02:00
|
|
|
|
|
|
|
#define handle_error(msg) \
|
|
|
|
do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
2016-05-26 18:27:59 +02:00
|
|
|
|
2016-12-19 18:16:38 +01:00
|
|
|
#define handle_err(fun,msg)\
|
|
|
|
fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg);
|
|
|
|
|
2016-12-18 00:54:43 +01:00
|
|
|
void service_path (char *path, const char *sname, int index, int version)
|
2016-12-15 00:28:42 +01:00
|
|
|
{
|
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
|
|
|
|
2016-12-18 00:54:43 +01:00
|
|
|
usock_init (&srv->service_fd, srv->spath);
|
2016-05-26 18:27:59 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-19 18:16:38 +01:00
|
|
|
// TODO
|
|
|
|
int srv_accept (struct service *srv, struct process *p)
|
|
|
|
{
|
|
|
|
usock_accept (srv->service_fd, &p->proc_fd);
|
2016-12-19 22:49:26 +01:00
|
|
|
char *buf = NULL;
|
2016-12-19 19:20:27 +01:00
|
|
|
size_t msgsize = BUFSIZ;
|
2016-12-19 18:16:38 +01:00
|
|
|
|
2016-12-19 19:20:27 +01:00
|
|
|
srv_read (p, &buf, &msgsize);
|
2016-12-19 18:16:38 +01:00
|
|
|
|
2016-12-19 19:20:27 +01:00
|
|
|
msgsize = 0;
|
|
|
|
msg_format_ack (buf, NULL, &msgsize);
|
|
|
|
|
|
|
|
srv_write (p, buf, msgsize);
|
2016-12-19 18:16:38 +01:00
|
|
|
|
2016-12-19 22:49:26 +01:00
|
|
|
free (buf);
|
|
|
|
|
2016-12-19 18:16:38 +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);
|
2016-12-18 00:54:43 +01:00
|
|
|
return usock_remove (srv->spath);
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
|
|
|
|
2016-12-19 18:16:38 +01:00
|
|
|
int srv_close_proc (struct process *p)
|
2016-05-30 01:54:19 +02:00
|
|
|
{
|
2016-12-19 18:16:38 +01:00
|
|
|
return usock_close (p->proc_fd);
|
2016-05-26 21:56:43 +02:00
|
|
|
}
|
|
|
|
|
2016-12-19 18:16:38 +01:00
|
|
|
int srv_read (const struct process *p, char **buf, size_t *msize)
|
2016-05-26 21:56:43 +02:00
|
|
|
{
|
2016-12-19 18:16:38 +01:00
|
|
|
return usock_recv (p->proc_fd, buf, msize);
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
|
|
|
|
2016-12-19 18:16:38 +01:00
|
|
|
int srv_write (const struct process *p, const char * buf, size_t msize)
|
|
|
|
{
|
|
|
|
return usock_send (p->proc_fd, buf, msize);
|
|
|
|
}
|
|
|
|
|
|
|
|
int app_connection (int argc, char **argv, char **env
|
|
|
|
, struct service *srv, const char *sname
|
2016-12-15 00:28:42 +01:00
|
|
|
, const char *connectionstr, size_t msize)
|
2016-06-10 01:21:04 +02:00
|
|
|
{
|
2016-12-19 18:16:38 +01:00
|
|
|
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);
|
2016-12-15 00:28:42 +01:00
|
|
|
|
2016-12-19 18:16:38 +01:00
|
|
|
usock_connect (&srv->service_fd, srv->spath);
|
2016-10-28 13:58:04 +02:00
|
|
|
|
2016-12-17 18:00:04 +01:00
|
|
|
// TODO: connection algorithm
|
2016-12-15 00:28:42 +01:00
|
|
|
// send connection string and receive acknowledgement
|
2016-12-19 18:16:38 +01:00
|
|
|
char send_buffer [BUFSIZ];
|
2016-12-19 22:49:26 +01:00
|
|
|
memset (send_buffer, 0, BUFSIZ);
|
2016-12-19 18:16:38 +01:00
|
|
|
if (msg_format_con (send_buffer, connectionstr, &msize) < 0) {
|
|
|
|
handle_err ("app_connection", "msg_format_con");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
app_write (srv, send_buffer, msize);
|
|
|
|
|
2016-12-19 22:49:26 +01:00
|
|
|
char *buffer = NULL;
|
2016-12-19 19:20:27 +01:00
|
|
|
size_t read_msg_size = BUFSIZ;
|
2016-12-19 18:16:38 +01:00
|
|
|
|
|
|
|
app_read (srv, &buffer, &read_msg_size);
|
|
|
|
|
|
|
|
assert (buffer[0] == MSG_TYPE_ACK);
|
2016-12-19 19:20:27 +01:00
|
|
|
assert (read_msg_size == 3);
|
2016-10-28 13:58:04 +02:00
|
|
|
|
2016-12-19 22:49:26 +01:00
|
|
|
free (buffer);
|
|
|
|
|
2016-10-28 13:58:04 +02:00
|
|
|
return 0;
|
2016-06-10 01:21:04 +02:00
|
|
|
}
|
|
|
|
|
2016-12-15 00:28:42 +01:00
|
|
|
int app_close (struct service *srv)
|
2016-05-26 18:27:59 +02:00
|
|
|
{
|
2016-12-17 18:00:04 +01:00
|
|
|
return usock_close (srv->service_fd);
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
|
|
|
|
2016-12-17 18:00:04 +01:00
|
|
|
int app_read (struct service *srv, char ** buf, size_t *msize)
|
2016-10-28 14:24:15 +02:00
|
|
|
{
|
2016-12-17 18:00:04 +01:00
|
|
|
return usock_recv (srv->service_fd, buf, msize);
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
|
|
|
|
2016-12-15 00:28:42 +01:00
|
|
|
int app_write (struct service *srv, char * buf, size_t msize)
|
2016-05-26 18:27:59 +02:00
|
|
|
{
|
2016-12-17 18:00:04 +01:00
|
|
|
return usock_send (srv->service_fd, buf, msize);
|
2016-10-28 13:58:04 +02:00
|
|
|
}
|