~skiqqy/ctagd

5910902374534b478f109e37447ea180655d8c55 — Stephen Cochrane 3 years ago 2e08a96 master
Refactor and comments
4 files changed, 30 insertions(+), 24 deletions(-)

M examples/queue/client.c
M examples/queue/server.c
M include/ctagd.h
M src/ctagd.c
M examples/queue/client.c => examples/queue/client.c +3 -3
@@ 17,12 17,12 @@ main(int argc, char *argv[])
		exit(1);
	}

	struct smsg *smsg;
	struct smsg *smsg = malloc(sizeof(struct smsg));

	/* Recieve Message from server */
	smsg = recv_tag('2');
	recv_tag('2', smsg);
	printf("RECIEVE MSG\nTag:%c\nMessage:%s\n", smsg->tag, smsg->payload);
	smsg = recv_tag('1');
	recv_tag('1', smsg);
	printf("RECIEVE MSG\nTag:%c\nMessage:%s\n", smsg->tag, smsg->payload);

	/* Send a message to server */

M examples/queue/server.c => examples/queue/server.c +1 -2
@@ 26,8 26,7 @@ main(int argc, char *argv[])
	csend(client, smsg);

	/* Test recv */
	free(smsg);
	smsg = recv_tag('1');
	recv_tag('1', smsg);
	printf("RECIEVE MSG\nTag:%c\nMessage:%s\n", smsg->tag, smsg->payload);
	free(smsg);
}

M include/ctagd.h => include/ctagd.h +1 -1
@@ 56,4 56,4 @@ int create_smsg(char tag, char *msg, struct smsg *smsg);
/* Message Passing */
int csend(int sock, struct smsg *smsg);
int cfetch(int sock, struct smsg *smsg);
struct smsg * recv_tag(char tag); /* This is used to recv messages */
int recv_tag(char tag, struct smsg *smsg); /* This is used to recv messages */

M src/ctagd.c => src/ctagd.c +25 -18
@@ 69,24 69,6 @@ dequeue(struct queue **q)
	return ret;
}

/* Fetch a smsg from a queue with a specifc tag. This is thread safe and blocking.

 * char tag: The tag representing the queue we are taking the smsg from.
 * returns: Null if the queue is empty, else the smsg at the front of the queue.

 */
struct smsg *
recv_tag(char tag)
{
	struct smsg *smsg = NULL;
	do {
		pthread_mutex_lock(&queue_locks[(int) tag]);
		smsg = dequeue(&queue[(int) tag]);
		pthread_mutex_unlock(&queue_locks[(int) tag]);
	} while (!smsg);
	return smsg;
}

/* This thread recv's messages from a socket and puts them in thier appropraite
 * queues.



@@ 450,3 432,28 @@ cfetch(int sock, struct smsg *smsg)
	if (client && client ->enable_q) return 0; /* We have a queue running, this operation is unsafe */
	return crecv(sock, smsg);
}

/* Fetch a smsg from a queue with a specifc tag. This is thread safe and blocking.

 * char tag: The tag representing the queue we are taking the smsg from.
 * smsg *:   The smsg struct we unpack the recieved message into.
 * returns: 0 if success, 1 if failed.

 */
int
recv_tag(char tag, struct smsg *smsg)
{
	struct smsg *tmp = NULL;
	do {
		pthread_mutex_lock(&queue_locks[(int) tag]);
		tmp = dequeue(&queue[(int) tag]);
		pthread_mutex_unlock(&queue_locks[(int) tag]);
	} while (!tmp);

	// Repack.
	smsg->tag = tmp->tag;
	smsg->len = tmp->len;
	smsg->payload = tmp->payload;

	return 0;
}