~skiqqy/ctagd

2e08a9624ed02812e0ef198db473ff14a434777f — Stephen Cochrane 2 years ago 4e2d320
Big refactor
8 files changed, 94 insertions(+), 34 deletions(-)

M Makefile
M README.md
R examples/{client.c => no_queue/client.c}
A examples/no_queue/server.c
A examples/queue/client.c
R examples/{server.c => queue/server.c}
M src/ctagd.c
M test.sh
M Makefile => Makefile +11 -6
@@ 4,6 4,8 @@ VERSION=0.1
NAME=ctagd
COMP=$(CC) $(FLAGS)

.PHONY: test.sh

all: build

clean:


@@ 22,11 24,14 @@ lib: build

# The following two units are just for testing

test: build server client
	./test.sh
tests: test_q test_nq
	./test.sh nq
	./test.sh q

server: ctagd.o
	$(COMP) -o bin/$@ examples/server.c bin/ctagd.o
test_q: build
	$(COMP) -o bin/client_q examples/queue/client.c bin/ctagd.o
	$(COMP) -o bin/server_q examples/queue/server.c bin/ctagd.o

client: ctagd.o
	$(COMP) -o bin/$@ examples/client.c bin/ctagd.o
test_nq: build
	$(COMP) -o bin/client_nq examples/no_queue/client.c bin/ctagd.o
	$(COMP) -o bin/server_nq examples/no_queue/server.c bin/ctagd.o

M README.md => README.md +2 -2
@@ 34,6 34,6 @@ TODO

## Testing
````
$ make test
$ make tests
````
The above will compile and run the tests.
The above will compile and run the tests (WIP).

R examples/client.c => examples/no_queue/client.c +10 -15
@@ 1,7 1,7 @@
#include <stdlib.h>
#include <stdio.h>

#include "../include/ctagd.h"
#include "../../include/ctagd.h"

int
main(int argc, char *argv[])


@@ 10,7 10,7 @@ main(int argc, char *argv[])

	struct client client;
	init_client_struct(&client); /* Just sets default values, you can still edit the struct as you wish */
	client.enable_q = 1; /* change this to 0 to not use queues */
	client.enable_q = 0; /* 0 -> no queues */

	/* Setup Sockets */
	if (!init_client(&client)) {


@@ 18,19 18,14 @@ main(int argc, char *argv[])
	}

	struct smsg *smsg;
	if (client.enable_q) {
		smsg = recv_tag('2');
		printf("RECIEVE MSG\nTag:%c\nMessage:%s\n", smsg->tag, smsg->payload);
		smsg = recv_tag('1');
		printf("RECIEVE MSG\nTag:%c\nMessage:%s\n", smsg->tag, smsg->payload);
	} else {
		smsg = malloc(sizeof(struct smsg));
		cfetch(client.socket, smsg);
		printf("RECIEVE MSG\nTag:%c\nMessage:%s\n", smsg->tag, smsg->payload);
		cfetch(client.socket, smsg);
		printf("RECIEVE MSG\nTag:%c\nMessage:%s\n", smsg->tag, smsg->payload);
		free(smsg);
	}

	/* Recieve message from server */
	smsg = malloc(sizeof(struct smsg));
	cfetch(client.socket, smsg);
	printf("RECIEVE MSG\nTag:%c\nMessage:%s\n", smsg->tag, smsg->payload);
	cfetch(client.socket, smsg);
	printf("RECIEVE MSG\nTag:%c\nMessage:%s\n", smsg->tag, smsg->payload);
	free(smsg);

	/* Send a message to server */
	smsg = malloc(sizeof(struct smsg));

A examples/no_queue/server.c => examples/no_queue/server.c +32 -0
@@ 0,0 1,32 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "../../include/ctagd.h"

int
main(int argc, char *argv[])
{
	int client;
	struct server server;
	printf("[SERVER] This is a demo!\n");
	init_server_struct(&server);
	server.max_clients = 1;
	server.enable_q = 0; /* 0 -> no queues */

	if (!init_server(&server)) {
		return EXIT_FAILURE;
	}
	client = server_accept();

	struct smsg *smsg = malloc(sizeof(struct smsg));
	create_smsg('1', "Hello, World!", smsg);
	csend(client, smsg);
	create_smsg('2', "Second Message :D", smsg);
	csend(client, smsg);

	/* Test recv */
	cfetch(client, smsg); /* Read the first smsg available from the socket */
	printf("RECIEVE MSG\nTag:%c\nMessage:%s\n", smsg->tag, smsg->payload);
	free(smsg);
}

A examples/queue/client.c => examples/queue/client.c +33 -0
@@ 0,0 1,33 @@
#include <stdlib.h>
#include <stdio.h>

#include "../../include/ctagd.h"

int
main(int argc, char *argv[])
{
	printf("[CLIENT] This is a demo!\n");

	struct client client;
	init_client_struct(&client); /* Just sets default values, you can still edit the struct as you wish */
	client.enable_q = 1; /* 1 -> use queues */

	/* Setup Sockets */
	if (!init_client(&client)) {
		exit(1);
	}

	struct smsg *smsg;

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

	/* Send a message to server */
	smsg = malloc(sizeof(struct smsg));
	create_smsg('1', "This is a response from the client!", smsg);
	csend(client.socket, smsg);
	free(smsg);
}

R examples/server.c => examples/queue/server.c +4 -8
@@ 2,7 2,7 @@
#include <stdio.h>
#include <string.h>

#include "../include/ctagd.h"
#include "../../include/ctagd.h"

int
main(int argc, char *argv[])


@@ 12,7 12,7 @@ main(int argc, char *argv[])
	printf("[SERVER] This is a demo!\n");
	init_server_struct(&server);
	server.max_clients = 1;
	server.enable_q = 1; /* change this to 0 to not use queues */
	server.enable_q = 1; /* 1 -> use queues */

	if (!init_server(&server)) {
		return EXIT_FAILURE;


@@ 26,12 26,8 @@ main(int argc, char *argv[])
	csend(client, smsg);

	/* Test recv */
	if (server.enable_q) { /* Use the queue */
		free(smsg);
		smsg = recv_tag('1');
	} else {
		cfetch(client, smsg); /* Read the first smsg available from the socket */
	}
	free(smsg);
	smsg = recv_tag('1');
	printf("RECIEVE MSG\nTag:%c\nMessage:%s\n", smsg->tag, smsg->payload);
	free(smsg);
}

M src/ctagd.c => src/ctagd.c +0 -1
@@ 448,6 448,5 @@ cfetch(int sock, struct smsg *smsg)
{
	if (server && server->enable_q) return 0; /* We have a queue running, this operation is unsafe */
	if (client && client ->enable_q) return 0; /* We have a queue running, this operation is unsafe */
	printf("here\n");
	return crecv(sock, smsg);
}

M test.sh => test.sh +2 -2
@@ 1,3 1,3 @@
echo "Starting Tests"
./bin/server &
./bin/client
./bin/server_"$1" &
./bin/client_"$1"