some modifications and pingpong is now working with new version
This commit is contained in:
commit
7f0e5b62dd
@ -17,16 +17,22 @@ int file_write (const char *path, const char *buf, size_t msize)
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int file_read (const char *path, char **buf, size_t *msize)
|
||||
{
|
||||
if (buf == NULL)
|
||||
return -1;
|
||||
|
||||
int fd = open (path, O_RDONLY);
|
||||
if (fd <= 0) {
|
||||
return ER_FILE_OPEN;
|
||||
}
|
||||
if (*buf == NULL)
|
||||
printf("FILE_READ: opened file %s\n", path);
|
||||
|
||||
if (*buf == NULL) {
|
||||
*buf = malloc (BUFSIZ);
|
||||
memset (*buf, 0, BUFSIZ);
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
int ret2 = 0;
|
||||
ret = read (fd, *buf, BUFSIZ);
|
||||
@ -36,11 +42,13 @@ int file_read (const char *path, char **buf, size_t *msize)
|
||||
else {
|
||||
*msize = ret;
|
||||
}
|
||||
|
||||
ret2 = close (fd);
|
||||
if (ret2 < 0) {
|
||||
fprintf (stderr, "err: close [err: %d] %s\n", ret2, path);
|
||||
perror ("closing");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
@ -161,7 +169,6 @@ int srv_get_new_process (const struct service *srv, struct process *p)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (buf != NULL)
|
||||
free (buf);
|
||||
srv_process_gen (p, pid, index, version);
|
||||
|
@ -29,17 +29,6 @@ void srv_process_gen (struct process *p
|
||||
|
||||
snprintf(p->path_in , PATH_MAX, "%s%d-%d-in" , TMPDIR, pid, index);
|
||||
snprintf(p->path_out, PATH_MAX, "%s%d-%d-out", TMPDIR, pid, index);
|
||||
//printf("path-in : %s\n", p->path_in );
|
||||
//printf("path-out : %s\n", p->path_out );
|
||||
|
||||
}
|
||||
|
||||
void srv_process_free (struct process * p)
|
||||
{
|
||||
// TODO nothing to do now
|
||||
|
||||
// snprintf(p->path_in , PATH_MAX, "%s/%d-%d-in" , TMPDIR, pid, index);
|
||||
// snprintf(p->path_out, PATH_MAX, "%s/%d-%d-out", TMPDIR, pid, index);
|
||||
|
||||
}
|
||||
|
||||
|
@ -78,8 +78,10 @@ int pubsubd_channel_new (struct channel *c, const char * name)
|
||||
|
||||
printf ("NAME : %s, SIZE : %ld\n", name, nlen);
|
||||
|
||||
if (c->chan == NULL)
|
||||
if (c->chan == NULL) {
|
||||
c->chan = malloc (nlen +1);
|
||||
memset (c->chan, 0, nlen +1);
|
||||
}
|
||||
|
||||
memset (c->chan, 0, nlen +1);
|
||||
memcpy (c->chan, name, nlen);
|
||||
@ -126,8 +128,10 @@ void pubsubd_subscriber_init (struct app_list_head **chans) {
|
||||
if (chans == NULL)
|
||||
return;
|
||||
|
||||
if (*chans == NULL)
|
||||
if (*chans == NULL) {
|
||||
*chans = malloc (sizeof(struct channels));
|
||||
memset (*chans, 0, sizeof(struct channels));
|
||||
}
|
||||
LIST_INIT(*chans);
|
||||
}
|
||||
|
||||
@ -149,12 +153,7 @@ void pubsubd_channel_print (const struct channel *c)
|
||||
if (c == NULL || c->chan == NULL)
|
||||
return;
|
||||
|
||||
if (c->chan == NULL) {
|
||||
printf ( "\033[32mchan name not available\033[00m\n");
|
||||
}
|
||||
else {
|
||||
printf ( "\033[32mchan %s\033[00m\n", c->chan);
|
||||
}
|
||||
printf ( "\033[32mchan %s\033[00m\n", c->chan);
|
||||
|
||||
if (c->alh == NULL)
|
||||
return;
|
||||
@ -173,6 +172,7 @@ struct app_list_elm * pubsubd_app_list_elm_copy (const struct app_list_elm *ale)
|
||||
|
||||
struct app_list_elm * n = NULL;
|
||||
n = malloc (sizeof (struct app_list_elm));
|
||||
memset (n, 0, sizeof (struct app_list_elm));
|
||||
|
||||
if (ale->p != NULL)
|
||||
n->p = srv_process_copy (ale->p);
|
||||
@ -248,6 +248,9 @@ void pubsubd_app_list_elm_create (struct app_list_elm *ale, struct process *p)
|
||||
if (ale == NULL)
|
||||
return;
|
||||
|
||||
if (ale->p != NULL)
|
||||
free (ale->p);
|
||||
|
||||
ale->p = srv_process_copy (p);
|
||||
}
|
||||
|
||||
@ -273,6 +276,7 @@ void pubsubd_msg_serialize (const struct pubsub_msg *msg, char **data, size_t *l
|
||||
*data = NULL;
|
||||
}
|
||||
*data = malloc(*len);
|
||||
memset (*data, 0, *len);
|
||||
data[0][0] = msg->type;
|
||||
return;
|
||||
}
|
||||
@ -286,6 +290,7 @@ void pubsubd_msg_serialize (const struct pubsub_msg *msg, char **data, size_t *l
|
||||
*data = NULL;
|
||||
}
|
||||
*data = malloc(*len);
|
||||
memset (*data, 0, *len);
|
||||
|
||||
size_t i = 0;
|
||||
|
||||
@ -334,6 +339,7 @@ void pubsubd_msg_unserialize (struct pubsub_msg *msg, const char *data, size_t l
|
||||
return;
|
||||
}
|
||||
msg->chan = malloc (msg->chanlen);
|
||||
memset (msg->chan, 0, msg->chanlen);
|
||||
memcpy (msg->chan, data + i, msg->chanlen); i += msg->chanlen;
|
||||
|
||||
memcpy (&msg->datalen, data + i, sizeof(size_t)); i += sizeof(size_t);
|
||||
@ -342,6 +348,7 @@ void pubsubd_msg_unserialize (struct pubsub_msg *msg, const char *data, size_t l
|
||||
return;
|
||||
}
|
||||
msg->data = malloc (msg->datalen);
|
||||
memset (msg->data, 0, msg->datalen);
|
||||
memcpy (msg->data, data + i, msg->datalen); i += msg->datalen;
|
||||
}
|
||||
|
||||
@ -389,9 +396,15 @@ int pubsubd_get_new_process (const char *spath, struct app_list_elm *ale
|
||||
char chan[BUFSIZ];
|
||||
memset (chan, 0, BUFSIZ);
|
||||
|
||||
if (buf == NULL) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
printf ("INIT: %s\n", buf);
|
||||
|
||||
for (str = buf, i = 1; ; str = NULL, i++) {
|
||||
if (str == NULL)
|
||||
break;
|
||||
token = strtok_r(str, " ", &saveptr);
|
||||
if (token == NULL)
|
||||
break;
|
||||
@ -441,17 +454,19 @@ int pubsubd_get_new_process (const char *spath, struct app_list_elm *ale
|
||||
}
|
||||
|
||||
ale->p = malloc (sizeof (struct process));
|
||||
memset (ale->p, 0, sizeof (struct process));
|
||||
srv_process_gen (ale->p, pid, index, version);
|
||||
|
||||
if (*c == NULL) {
|
||||
*c = malloc (sizeof (struct channel));
|
||||
memset (*c, 0, sizeof (struct channel));
|
||||
}
|
||||
|
||||
chan[BUFSIZ -1] = '\0';
|
||||
pubsubd_channel_new (*c, chan);
|
||||
|
||||
|
||||
struct channel *new_chan = NULL;
|
||||
|
||||
new_chan = pubsubd_channel_get (chans, *c);
|
||||
if (new_chan == NULL) {
|
||||
new_chan = pubsubd_channels_add (chans, *c);
|
||||
|
Binary file not shown.
@ -77,6 +77,7 @@ int main(int argc, char * argv[], char **env)
|
||||
fprintf(stdout, "error service_create %d\n", ret);
|
||||
exit (1);
|
||||
}
|
||||
printf("MAIN: server created\n" );
|
||||
|
||||
// the service will loop until the end of time, a specific message, a signal
|
||||
main_loop (&srv);
|
||||
|
@ -2,15 +2,13 @@
|
||||
|
||||
REP=/tmp/ipc/
|
||||
SERVICE="pongd"
|
||||
NB=3
|
||||
NB=10
|
||||
|
||||
# CLEAN UP !
|
||||
if [ $# -ne 0 ] && [ "$1" = clean ]
|
||||
then
|
||||
echo "clean rep ${REP}"
|
||||
rm ${REP}/${SERVICE}
|
||||
rm ${REP}/*-in
|
||||
rm ${REP}/*-out
|
||||
|
||||
exit 0
|
||||
fi
|
||||
@ -22,8 +20,6 @@ fi
|
||||
|
||||
for pid in `seq 1 ${NB}`
|
||||
do
|
||||
echo "${pid}"
|
||||
|
||||
# we make the application pipes
|
||||
mkfifo ${REP}${pid}-1-in 2>/dev/null
|
||||
mkfifo ${REP}${pid}-1-out 2>/dev/null
|
||||
@ -36,5 +32,10 @@ do
|
||||
# echo "hello world" > ${REP}${pid}-1-out
|
||||
|
||||
# the the service will answer with our message
|
||||
echo "pid : ${pid}"
|
||||
cat ${REP}/${pid}-1-in
|
||||
done
|
||||
|
||||
echo "clean rep"
|
||||
rm ${REP}/*-in
|
||||
rm ${REP}/*-out
|
@ -27,7 +27,7 @@ main(int argc, char **argv)
|
||||
struct channels chans;
|
||||
memset (&chans, 0, sizeof (struct channels));
|
||||
|
||||
for (int nb = 10, i = 0 ; nb > 0; i++, nb--) {
|
||||
for (int nb = 1, i = 0 ; nb > 0; i++, nb--) {
|
||||
struct app_list_elm ale;
|
||||
memset (&ale, 0, sizeof (struct app_list_elm));
|
||||
|
||||
@ -42,6 +42,8 @@ main(int argc, char **argv)
|
||||
pubsubd_channels_print (&chans);
|
||||
printf ("--\n");
|
||||
printf ("still %d remaining processes\n", nb);
|
||||
|
||||
pubsubd_app_list_elm_free (&ale);
|
||||
}
|
||||
|
||||
pubsubd_channels_del_all (&chans);
|
||||
|
Reference in New Issue
Block a user