diff --git a/core-test/app/communication-client.c b/core-test/app/communication-client.c new file mode 100644 index 0000000..1bb10fe --- /dev/null +++ b/core-test/app/communication-client.c @@ -0,0 +1,56 @@ +#include +#include +#include + +#include "../../core/communication.h" + +#define handle_err(fun,msg)\ + fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg); + +#define MSG "coucou" +#define SERVICE_NAME "test" + +int main (int argc, char *argv[], char *env[]) +{ + + size_t msize = BUFSIZ; + char *buf = NULL; + + if ( (buf = malloc (BUFSIZ)) == NULL) { + handle_err ("main", "malloc"); + return EXIT_FAILURE; + } + memset (buf, 0, BUFSIZ); + + struct service srv; + memset(&srv, 0, sizeof (struct service)); + + // index and version should be filled + srv.index = 0; + srv.version = 0; + + // init service + if (app_connection (argc, argv, env, &srv, SERVICE_NAME, NULL, 0) < 0) { + handle_err("main", "srv_init < 0"); + return EXIT_FAILURE; + } + + if (app_write (&srv, MSG, strlen(MSG)) < 0) { + handle_err("main", "app_write < 0"); + return EXIT_FAILURE; + } + + if (app_read (&srv, &buf, &msize) < 0) { + handle_err("main", "app_read < 0"); + return EXIT_FAILURE; + } + + printf ("msg recv: %s\n", buf); + + if (app_close (&srv) < 0) { + handle_err("main", "app_close < 0"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/core-test/app/communication-server.c b/core-test/app/communication-server.c new file mode 100644 index 0000000..af126ec --- /dev/null +++ b/core-test/app/communication-server.c @@ -0,0 +1,68 @@ +#include +#include +#include + +#include "../../core/communication.h" + +#define handle_err(fun,msg)\ + fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg); + +#define MSG "coucou" +#define SERVICE_NAME "test" + +int main (int argc, char *argv[], char *env[]) +{ + + size_t msize = BUFSIZ; + char *buf = NULL; + + if ( (buf = malloc (BUFSIZ)) == NULL) { + handle_err ("main", "malloc"); + return EXIT_FAILURE; + } + memset (buf, 0, BUFSIZ); + + struct service srv; + memset(&srv, 0, sizeof (struct service)); + + // index and version should be filled + srv.index = 0; + srv.version = 0; + + struct process p; + + // init service + if (srv_init (argc, argv, env, &srv, SERVICE_NAME) < 0) { + handle_err("main", "srv_init < 0"); + return EXIT_FAILURE; + } + + if (srv_accept (&srv, &p) < 0) { + handle_err("main", "srv_accept < 0"); + return EXIT_FAILURE; + } + + if (srv_read (&p, &buf, &msize) < 0) { + handle_err("main", "srv_read < 0"); + return EXIT_FAILURE; + } + + printf ("msg recv: %s\n", buf); + + if (srv_write (&p, buf, msize) < 0) { + handle_err("main", "srv_write < 0"); + return EXIT_FAILURE; + } + + if (srv_close_proc (&p) < 0) { + handle_err("main", "srv_close_proc < 0"); + return EXIT_FAILURE; + } + + if (srv_close (&srv) < 0) { + handle_err("main", "srv_close < 0"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/core/communication.c b/core/communication.c index 9494a5b..d66cb16 100644 --- a/core/communication.c +++ b/core/communication.c @@ -1,14 +1,17 @@ #include "communication.h" #include "usocket.h" +#include "msg-format.h" #include - #include #include #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) +#define handle_err(fun,msg)\ + fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg); + void service_path (char *path, const char *sname, int index, int version) { memset (path, 0, PATH_MAX); @@ -39,25 +42,47 @@ int srv_init (int argc, char **argv, char **env return 0; } +// TODO +int srv_accept (struct service *srv, struct process *p) +{ + usock_accept (srv->service_fd, &p->proc_fd); + + char buf[3]; + msg_format_ack (buf); + + srv_write (p, buf, 3); + + return 0; +} + int srv_close (struct service *srv) { usock_close (srv->service_fd); return usock_remove (srv->spath); } -int srv_read (const struct service *srv, char ** buf, size_t *msize) +int srv_close_proc (struct process *p) { - return usock_recv (srv->service_fd, buf, msize); + return usock_close (p->proc_fd); } -int srv_write (const struct service *srv, const char * buf, size_t msize) +int srv_read (const struct process *p, char **buf, size_t *msize) { - return usock_send (srv->service_fd, buf, msize); + return usock_recv (p->proc_fd, buf, msize); } -int app_connection (struct service *srv, const char *sname +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 , const char *connectionstr, size_t msize) { + argc = argc; + argv = argv; + env = env; assert (srv != NULL); assert (sname != NULL); @@ -69,11 +94,24 @@ int app_connection (struct service *srv, const char *sname // gets the service path service_path (srv->spath, sname, srv->index, srv->version); - usock_connect(&srv->service_fd, srv->spath); + usock_connect (&srv->service_fd, srv->spath); // TODO: connection algorithm // send connection string and receive acknowledgement - srv_write(srv, connectionstr, msize); + char send_buffer [BUFSIZ]; + if (msg_format_con (send_buffer, connectionstr, &msize) < 0) { + handle_err ("app_connection", "msg_format_con"); + return -1; + } + app_write (srv, send_buffer, msize); + + char *buffer; + size_t read_msg_size; + + app_read (srv, &buffer, &read_msg_size); + + assert (read_msg_size == 3); + assert (buffer[0] == MSG_TYPE_ACK); return 0; } diff --git a/core/communication.h b/core/communication.h index d12fcd8..22da922 100644 --- a/core/communication.h +++ b/core/communication.h @@ -4,16 +4,10 @@ #include #include #include -//#include - -#include // unlink - -#include // mkfifo -#include // mkfifo -#include // open - #include // error numbers +#include "process.h" + #define COMMUNICATION_VERSION 1 #define ER_FILE_OPEN 1 @@ -44,16 +38,19 @@ struct service { int srv_init (int argc, char **argv, char **env , struct service *srv, const char *sname); int srv_close (struct service *srv); +int srv_close_proc (struct process *p); +int srv_accept (struct service *srv, struct process *p); -int srv_read (const struct service *srv, char ** buf, size_t *msize); -int srv_write (const struct service *, const char * buf, size_t); +int srv_read (const struct process *, char **buf, size_t *msize); +int srv_write (const struct process *, const char * buf, size_t); // APPLICATION // Initialize connection with unix socket // send the connection string to $TMP/ // fill srv->spath && srv->service_fd -int app_connection (struct service *, const char *, const char *, size_t); +int app_connection (int argc, char **argv, char **env + , struct service *, const char *, const char *, size_t); int app_close (struct service *); int app_read (struct service *srv, char ** buf, size_t *msize); diff --git a/core/msg-format.c b/core/msg-format.c new file mode 100644 index 0000000..26eae8d --- /dev/null +++ b/core/msg-format.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include "msg-format.h" + +#define handle_err(fun,msg)\ + fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg); + +// [type] [len] [val] +// 3 valsize constr +int msg_format_con (char *buf, const char *constr, size_t *msgsize) +{ + assert (buf != NULL); + assert (*msgsize + 3 <= BUFSIZ); + + if (*msgsize + 3 > BUFSIZ) { + handle_err ("msg_format_con", "msgsize > BUFSIZ"); + return 1; + } + + buf[0] = MSG_TYPE_CON; + short final_size = (short) *msgsize; + memcpy (buf + 1, &final_size, 2); + memcpy (buf + 3, constr, *msgsize); + + return 0; +} + +// [type] [len] +// 4 0 +int msg_format_ack (char *buf) +{ + assert (buf != NULL); + memset(buf, 0, 3); + buf[0] = MSG_TYPE_ACK; + + return 0; +} diff --git a/core/msg-format.h b/core/msg-format.h new file mode 100644 index 0000000..a815db5 --- /dev/null +++ b/core/msg-format.h @@ -0,0 +1,13 @@ +#ifndef __MSG_FORMAT_H__ +#define __MSG_FORMAT_H__ + +#include + +#define MSG_TYPE_CON 1 +#define MSG_TYPE_ERR 2 +#define MSG_TYPE_ACK 4 + +int msg_format_con (char *buf, const char *constr, size_t *msgsize); +int msg_format_ack (char *buf); + +#endif diff --git a/core/process.h b/core/process.h index c2f3601..e45ce08 100644 --- a/core/process.h +++ b/core/process.h @@ -1,11 +1,17 @@ #ifndef __PROCESS_H__ #define __PROCESS_H__ -#if 0 +struct process { + unsigned int version; + unsigned int index; + int proc_fd; +}; // TODO // tout revoir +#if 0 + #include #include #include @@ -13,11 +19,6 @@ #include -struct process { - unsigned int version; - unsigned int index; - int proc_fd; -}; struct process * srv_process_copy (const struct process *p);