Archived
3
0

msg format: fonctionne sur les programmes de test (core-test/app/communication-*)

This commit is contained in:
Philippe PITTOLI 2016-12-21 02:56:52 +01:00
parent a0d82c2df0
commit 3dd3033273
7 changed files with 74 additions and 58 deletions

View File

@ -10,7 +10,7 @@ TESTS=$(addsuffix .test, $(EXEC))
all: $(SOURCES) $(EXEC) all: $(SOURCES) $(EXEC)
$(EXEC): $(OBJECTS) $(CFILES) $(EXEC): $(OBJECTS) $(CFILES)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -lcbor -o $@.bin $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@.bin
.c.o: .c.o:
$(CC) -c $(CFLAGS) $< -o $@ $(CC) -c $(CFLAGS) $< -o $@

View File

@ -2,26 +2,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "../../core/msg.h"
#include "../../core/error.h"
#include "../../core/communication.h" #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 MSG "coucou"
#define SERVICE_NAME "test" #define SERVICE_NAME "test"
int main (int argc, char *argv[], char *env[]) int main (int argc, char *argv[], char *env[])
{ {
size_t msize = BUFSIZ; struct msg m;
char *buf = NULL; memset (&m, 0, sizeof (struct msg));
if ( (buf = malloc (BUFSIZ)) == NULL) {
handle_err ("main", "malloc");
return EXIT_FAILURE;
}
memset (buf, 0, BUFSIZ);
struct service srv; struct service srv;
memset (&srv, 0, sizeof (struct service)); memset (&srv, 0, sizeof (struct service));
@ -36,25 +28,27 @@ int main (int argc, char *argv[], char *env[])
} }
printf ("msg to send: %s\n", MSG); printf ("msg to send: %s\n", MSG);
if (app_write (&srv, MSG, strlen(MSG)) < 0) { msg_format_data (&m, MSG, strlen(MSG) +1);
printf ("msg to send in the client: ");
print_msg (&m);
if (app_write (&srv, &m) < 0) {
handle_err("main", "app_write < 0"); handle_err("main", "app_write < 0");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
msg_free (&m);
if (app_read (&srv, &buf, &msize) < 0) { if (app_read (&srv, &m) < 0) {
handle_err("main", "app_read < 0"); handle_err("main", "app_read < 0");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
printf ("msg recv: %s\n", buf); printf ("msg recv: %s\n", m.val);
msg_free (&m);
if (app_close (&srv) < 0) { if (app_close (&srv) < 0) {
handle_err("main", "app_close < 0"); handle_err("main", "app_close < 0");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (buf != NULL)
free (buf);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -2,26 +2,17 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "../../core/error.h"
#include "../../core/communication.h" #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 MSG "coucou"
#define SERVICE_NAME "test" #define SERVICE_NAME "test"
int main (int argc, char *argv[], char *env[]) int main (int argc, char *argv[], char *env[])
{ {
size_t msize = BUFSIZ; struct msg m;
char *buf = NULL; memset (&m, 0, sizeof (struct msg));
if ( (buf = malloc (BUFSIZ)) == NULL) {
handle_err ("main", "malloc");
return EXIT_FAILURE;
}
memset (buf, 0, BUFSIZ);
struct service srv; struct service srv;
memset(&srv, 0, sizeof (struct service)); memset(&srv, 0, sizeof (struct service));
@ -43,17 +34,31 @@ int main (int argc, char *argv[], char *env[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (srv_read (&p, &buf, &msize) < 0) { if (srv_read (&p, &m) < 0) {
handle_err("main", "srv_read < 0"); handle_err("main", "srv_read < 0");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
printf ("msg recv: %s\n", buf); printf ("msg recv: %s\n", m.val);
if (srv_write (&p, buf, msize) < 0) { if (srv_write (&p, &m) < 0) {
handle_err("main", "srv_write < 0"); handle_err("main", "srv_write < 0");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
msg_free (&m);
// client quits
if (srv_read (&p, &m) < 0) {
handle_err("main", "srv_read < 0");
return EXIT_FAILURE;
}
if (m.type == MSG_TYPE_DIS) {
printf ("the client quits\n");
}
else {
fprintf (stderr, "err: should have received the client dis msg\n");
}
msg_free (&m);
if (srv_close_proc (&p) < 0) { if (srv_close_proc (&p) < 0) {
handle_err("main", "srv_close_proc < 0"); handle_err("main", "srv_close_proc < 0");
@ -65,8 +70,5 @@ int main (int argc, char *argv[], char *env[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (buf != NULL)
free (buf);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -69,13 +69,6 @@ int srv_close (struct service *srv)
int srv_close_proc (struct process *p) int srv_close_proc (struct process *p)
{ {
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); return usock_close (p->proc_fd);
} }

View File

@ -4,12 +4,16 @@
#include <assert.h> #include <assert.h>
void print_msg (const struct msg *m)
{
assert (m != NULL);
printf ("msg: type %d len %d\n", m->type, m->valsize);
}
int msg_format_read (struct msg *m, const char *buf, size_t msize) int msg_format_read (struct msg *m, const char *buf, size_t msize)
{ {
assert (m != NULL); assert (m != NULL);
assert (buf != NULL); assert (buf != NULL);
assert (msize >= 3);
assert (msize <= BUFSIZ - 3); assert (msize <= BUFSIZ - 3);
if (m == NULL) if (m == NULL)
@ -19,11 +23,13 @@ int msg_format_read (struct msg *m, const char *buf, size_t msize)
memcpy (&m->valsize, buf+1, 2); memcpy (&m->valsize, buf+1, 2);
assert (m->valsize <= BUFSIZ -3); assert (m->valsize <= BUFSIZ -3);
// printf ("type %d : msize = %ld, valsize = %d\n", m->type, msize, m->valsize);
assert (m->valsize == msize - 3);
if (m->val != NULL) if (m->val != NULL)
free (m->val), m->val = NULL; free (m->val), m->val = NULL;
if (m->val == NULL) { if (m->val == NULL && m->valsize > 0) {
m->val = malloc (m->valsize); m->val = malloc (m->valsize);
memcpy (m->val, buf+3, m->valsize); memcpy (m->val, buf+3, m->valsize);
} }
@ -31,7 +37,7 @@ int msg_format_read (struct msg *m, const char *buf, size_t msize)
return 0; return 0;
} }
int msg_format_write (struct msg *m, char **buf, size_t *msize) int msg_format_write (const struct msg *m, char **buf, size_t *msize)
{ {
assert (m != NULL); assert (m != NULL);
assert (buf != NULL); assert (buf != NULL);
@ -47,11 +53,17 @@ int msg_format_write (struct msg *m, char **buf, size_t *msize)
if (msize == NULL) if (msize == NULL)
return -3; return -3;
if (*buf == NULL) {
*buf = malloc (3 + m->valsize);
}
char *buffer = *buf; char *buffer = *buf;
buffer[0] = m->type; buffer[0] = m->type;
memcpy (buffer + 1, &m->valsize, 2); memcpy (buffer + 1, &m->valsize, 2);
memcpy (buffer + 3, &m->val, (m->valsize <= BUFSIZ) ? m->valsize : BUFSIZ); memcpy (buffer + 3, m->val, m->valsize);
*msize = 3 + m->valsize;
return 0; return 0;
} }
@ -61,7 +73,7 @@ int msg_read (int fd, struct msg *m)
assert (m != NULL); assert (m != NULL);
char *buf = NULL; char *buf = NULL;
size_t msize = 0; size_t msize = BUFSIZ;
int ret = usock_recv (fd, &buf, &msize); int ret = usock_recv (fd, &buf, &msize);
if (ret < 0) { if (ret < 0) {
@ -74,6 +86,9 @@ int msg_read (int fd, struct msg *m)
} }
free (buf); free (buf);
// printf ("msg received: ");
// print_msg (m);
return 0; return 0;
} }
@ -81,10 +96,14 @@ int msg_write (int fd, const struct msg *m)
{ {
assert (m != NULL); assert (m != NULL);
// printf ("msg to write: ");
// print_msg (m);
char *buf = NULL; char *buf = NULL;
size_t msize = 0; size_t msize = 0;
msg_format_write (m, &buf, &msize); msg_format_write (m, &buf, &msize);
// printf ("msg to send, real size %ld\n", msize);
int ret = usock_send (fd, buf, msize); int ret = usock_send (fd, buf, msize);
if (ret < 0) { if (ret < 0) {
handle_err ("msg_write", "usock_send"); handle_err ("msg_write", "usock_send");
@ -103,7 +122,7 @@ int msg_format (struct msg *m, char type, const char *val, size_t valsize)
{ {
assert (m != NULL); assert (m != NULL);
assert (valsize + 3 <= BUFSIZ); assert (valsize + 3 <= BUFSIZ);
assert (valsize == 0 && val == NULL || valsize > 0 && val != NULL); assert ((valsize == 0 && val == NULL) || (valsize > 0 && val != NULL));
if (valsize + 3 > BUFSIZ) { if (valsize + 3 > BUFSIZ) {
handle_err ("msg_format_con", "msgsize > BUFSIZ"); handle_err ("msg_format_con", "msgsize > BUFSIZ");
@ -112,6 +131,10 @@ int msg_format (struct msg *m, char type, const char *val, size_t valsize)
m->type = type; m->type = type;
m->valsize = (short) valsize; m->valsize = (short) valsize;
if (m->val == NULL)
m->val = malloc (valsize);
memcpy (m->val, val, valsize); memcpy (m->val, val, valsize);
return 0; return 0;
} }
@ -139,7 +162,9 @@ int msg_free (struct msg *m)
return -1; return -1;
if (m->val != NULL) if (m->val != NULL)
free (m->val); free (m->val), m->val = NULL;
m->valsize = 0;
return 0; return 0;
} }

View File

@ -20,7 +20,7 @@ struct msg {
// used to create msg structure from buffer // used to create msg structure from buffer
int msg_format_read (struct msg *m, const char *buf, size_t msize); int msg_format_read (struct msg *m, const char *buf, size_t msize);
// used to create buffer from msg structure // used to create buffer from msg structure
int msg_format_write (struct msg *m, char **buf, size_t *msize); int msg_format_write (const struct msg *m, char **buf, size_t *msize);
// read a structure msg from fd // read a structure msg from fd
int msg_read (int fd, struct msg *m); int msg_read (int fd, struct msg *m);
@ -32,5 +32,6 @@ int msg_format_data (struct msg *m, const char *val, size_t valsize);
int msg_format_ack (struct msg *m, const char *val, size_t valsize); int msg_format_ack (struct msg *m, const char *val, size_t valsize);
int msg_free (struct msg *m); int msg_free (struct msg *m);
void print_msg (const struct msg *m);
#endif #endif

View File

@ -1,18 +1,16 @@
#include "usocket.h" #include "usocket.h"
#include "utils.h" #include "utils.h"
#include "error.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
#define handle_err(fun,msg)\
fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg);
int usock_send (const int fd, const char *buf, const int msize) int usock_send (const int fd, const char *buf, const int msize)
{ {
print_hexa ("msg send", (unsigned char *)buf, msize); print_hexa ("msg send", (unsigned char *)buf, msize);
printf ("msg to send: (%d) \n", msize);
fflush(stdout); fflush(stdout);
int ret = 0; int ret = 0;
@ -42,6 +40,8 @@ int usock_recv (const int fd, char **buf, size_t *msize)
// do not allocate too much memory // do not allocate too much memory
if (*msize > BUFSIZ) if (*msize > BUFSIZ)
handle_err ("usock_recv", "msize > BUFSIZ"); handle_err ("usock_recv", "msize > BUFSIZ");
if (*msize == 0)
*msize = BUFSIZ;
*buf = malloc ((*msize < BUFSIZ) ? *msize : BUFSIZ); *buf = malloc ((*msize < BUFSIZ) ? *msize : BUFSIZ);
} }
@ -54,6 +54,7 @@ int usock_recv (const int fd, char **buf, size_t *msize)
} }
*msize = (size_t) ret; *msize = (size_t) ret;
print_hexa ("msg recv", (unsigned char *)*buf, *msize); print_hexa ("msg recv", (unsigned char *)*buf, *msize);
fflush(stdout);
return ret; return ret;
} }