Better Makefile : headers are now dependencies

Better CLI : there are colors !
This commit is contained in:
Adrien Bourmault 2021-07-14 00:41:21 +02:00
parent c6d99559b5
commit 452bb52394
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
5 changed files with 65 additions and 54 deletions

View File

@ -21,17 +21,33 @@
CCOPTS=-pthread -Wall -g -Os CCOPTS=-pthread -Wall -g -Os
LDFLAGS= -lc -lpthread LDFLAGS= -lc -lpthread
BINDIR=bin BINDIR=bin
INCDIR=include INCDIR=include
SRCDIR=src SRCDIR=src
DEBDIR=debian DEBDIR=debian
SERVEROBJ= $(BINDIR)/scheduler.o $(BINDIR)/server.o $(BINDIR)/localworker.o \ SERVEROBJ= $(BINDIR)/scheduler.o $(BINDIR)/server.o $(BINDIR)/localworker.o \
$(BINDIR)/centers.o $(BINDIR)/cmds.o $(BINDIR)/model.o \ $(BINDIR)/centers.o $(BINDIR)/cmds.o $(BINDIR)/model.o \
$(BINDIR)/main.o $(BINDIR)/arrows.o $(BINDIR)/main.o $(BINDIR)/arrows.o
CLIOBJ= $(BINDIR)/cli.o CLIOBJ= $(BINDIR)/cli.o
SRCS=$(patsubst $(BINDIR)/%.o,$(SRCDIR)/%.c,$(SERVEROBJ)) \
$(patsubst $(BINDIR)/%.o,$(SRCDIR)/%.c,$(CLIOBJ))
DEPS=$(patsubst $(BINDIR)/%.o,$(BINDIR)/%.d,$(SERVEROBJ)) \
$(patsubst $(BINDIR)/%.o,$(BINDIR)/%.d,$(CLIOBJ))
.DEFAULT_GOAL:= all .DEFAULT_GOAL:= all
.PHONY: all clean deb tests install run run-cli run-tests .PHONY: all clean deb tests install run run-cli run-tests dependencies
# ---- Dependencies enumeration ---------------------------------------------- #
dependencies: $(DEPS)
$(BINDIR)/%.d: $(BINDIR)/%.o $(SRCDIR)/%.c
@$(CC) $(CFLAGS) -MM -MT $< $(word 2,$^) -MF $@
# ---- Tests enumeration ----------------------------------------------------- # # ---- Tests enumeration ----------------------------------------------------- #
TEST_SCHEDULER=$(BINDIR)/tests/centers TEST_SCHEDULER=$(BINDIR)/tests/centers
@ -43,12 +59,12 @@ $(BINDIR)/tests/centers: $(SRCDIR)/tests/centers.c
# ---- General recipes ------------------------------------------------------- # # ---- General recipes ------------------------------------------------------- #
$(BINDIR)/%.o: $(SRCDIR)/%.c $(INCDIR)/%.h | $(INCDIR)/base.h $(BINDIR)/%.o: $(SRCDIR)/%.c
@echo "Compiling $<" @echo "Compiling $<"
@$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $< @$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $<
# ---- Main recipe ----------------------------------------------------------- # # ---- Main recipe ----------------------------------------------------------- #
$(BINDIR)/main.o: $(SRCDIR)/main.c | $(INCDIR)/base.h $(BINDIR)/main.o: $(SRCDIR)/main.c
@echo "Compiling $<" @echo "Compiling $<"
@$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $< @$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $<
@ -66,7 +82,7 @@ $(BINDIR)/gem-graph-ctl: $(CLIOBJ)
clean: clean:
-rm -f $(SRCDIR)/*.o $(BINDIR)/* $(BINDIR)/tests/* *.deb -rm -f $(SRCDIR)/*.o $(BINDIR)/* $(BINDIR)/tests/* *.deb
all: $(BINDIR)/gem-graph-server $(BINDIR)/gem-graph-ctl all: dependencies $(BINDIR)/gem-graph-server $(BINDIR)/gem-graph-ctl
tests: $(TESTS) tests: $(TESTS)
@ -95,3 +111,4 @@ run-cli: all
run-tests: tests run-tests: tests
bin/tests/scheduler bin/tests/scheduler
include $(DEPS)

View File

@ -32,6 +32,15 @@
#define BASE_H #define BASE_H
/* -------------------------------------------------------------------------- */
#define LOGMSG "[%s]"
#define printLog(FORMAT, ...) printf("\e[0m" LOGMSG " " FORMAT "\e[0m", \
__func__, ##__VA_ARGS__)
#define LEN(x) (sizeof(x) / sizeof((x)[0]))
/* -------------------------------------------------------------------------- */
struct { struct {
size_t size; size_t size;
int *space; int *space;
@ -69,25 +78,20 @@ struct {
SpaceUnit_t *space; SpaceUnit_t *space;
} typedef Space_t; } typedef Space_t;
#define LOGMSG "[%s]"
#define printLog(FORMAT, ...) printf(LOGMSG " " FORMAT, __func__, ##__VA_ARGS__)
#define LEN(x) (sizeof(x) / sizeof((x)[0]))
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
// //
// Scheduler // Scheduler
// //
struct { struct {
pthread_t id;
bool pleaseStop;
Space_t *globalDrawingSpace; Space_t *globalDrawingSpace;
IntArray_t *conditionTree; IntArray_t *conditionTree;
ArrowArray_t *arrowArray; ArrowArray_t *arrowArray;
int nMaxThread; int nMaxThread;
int nMaxCycles; int nMaxCycles;
int ruleRadius; int ruleRadius;
pthread_t id;
bool pleaseStop;
} typedef Scheduler_t; } typedef Scheduler_t;
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -99,7 +103,6 @@ struct Center_t {
int x; int x;
int y; int y;
int z; int z;
struct Center_t *next; struct Center_t *next;
struct Center_t *prev; struct Center_t *prev;
} typedef Center_t; } typedef Center_t;

View File

@ -1,24 +0,0 @@
//=-------------------------------------------------------------------------=//
// CLI definition //
// //
// Copyright © 2021 The Gem-graph Project //
// //
// This file is part of gem-graph. //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as //
// published by the Free Software Foundation, either version 3 of the //
// License, or (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU Affero General Public License for more details. //
// //
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
//=-------------------------------------------------------------------------=//
#ifndef BASE_H
#include "../include/base.h"
#endif

View File

@ -48,6 +48,9 @@
#define C_CURSORRIGHT "\e[1C" #define C_CURSORRIGHT "\e[1C"
#define C_SAVE_CURSORPOS "\e7" #define C_SAVE_CURSORPOS "\e7"
#define C_RESTORE_CURSORPOS "\e8" #define C_RESTORE_CURSORPOS "\e8"
#define C_COLOR_RED "\e[01;31m"
#define C_COLOR_YELLOW "\e[01;33m"
#define C_COLOR_NORMAL "\e[0m"
static char getch(void) static char getch(void)
{ {
@ -59,7 +62,9 @@ static char getch(void)
fflush(stdout); fflush(stdout);
if(tcgetattr(0, &old) < 0) { if(tcgetattr(0, &old) < 0) {
printLog("Error getting terminal settings! (%s)\n", strerror(errno)); printLog("%sError getting terminal settings! (%s)\n",
C_COLOR_RED,
strerror(errno));
return -1; return -1;
} }
@ -69,12 +74,16 @@ static char getch(void)
old.c_cc[VTIME] = 0; old.c_cc[VTIME] = 0;
if(tcsetattr(0, TCSANOW, &old) < 0) { if(tcsetattr(0, TCSANOW, &old) < 0) {
printLog("Error setting terminal settings! (%s)\n", strerror(errno)); printLog("%sError setting terminal settings! (%s)\n",
C_COLOR_RED,
strerror(errno));
return -1; return -1;
} }
if(read(0, &buf, 1) < 0) { if(read(0, &buf, 1) < 0) {
printLog("Error reading character! (%s)\n", strerror(errno)); printLog("%sError reading character! (%s)\n",
C_COLOR_RED,
strerror(errno));
return -1; return -1;
} }
@ -82,7 +91,9 @@ static char getch(void)
old.c_lflag |= ECHO; // set echo mode old.c_lflag |= ECHO; // set echo mode
if(tcsetattr(0, TCSADRAIN, &old) < 0) { if(tcsetattr(0, TCSADRAIN, &old) < 0) {
printLog("Error resetting terminal settings! (%s)\n", strerror(errno)); printLog("%sError resetting terminal settings! (%s)\n",
C_COLOR_RED,
strerror(errno));
return -1; return -1;
} }
return buf; return buf;
@ -250,7 +261,7 @@ void connectedCommunication(int sockfd)
// Quit if asked // Quit if asked
if (strcmp(exitCommand, sendBuff) == 0) { if (strcmp(exitCommand, sendBuff) == 0) {
return; break;
} }
// Otherwise send command to server // Otherwise send command to server
@ -264,17 +275,17 @@ void connectedCommunication(int sockfd)
// Detect disconnection // Detect disconnection
if (answerLength == 0) { if (answerLength == 0) {
printf("Disconnected\n");
break; break;
} }
// Detect null-string returned // Detect null-string returned
if (receiveBuff[0] == '\0') { if (receiveBuff[0] == '\0') {
printf("Invalid command!\n"); printf("%sInvalid command!\n\e[0m", C_COLOR_YELLOW);
} }
printf("%s\n", receiveBuff); printf("%s\n", receiveBuff);
} }
printf("%sDisconnected\n\e[0m", C_COLOR_YELLOW);
} }
int main(void) int main(void)
@ -285,7 +296,9 @@ int main(void)
// Socket creation // Socket creation
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) { if (sockfd == -1) {
printLog("Socket creation failed! (%s)\n", strerror(errno)); printLog("%sSocket creation failed! (%s)\n",
C_COLOR_RED,
strerror(errno));
return 1; return 1;
} }
@ -298,7 +311,9 @@ int main(void)
// Connect to the server // Connect to the server
if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) { if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) {
printLog("Connection failed! (%s)\n", strerror(errno)); printLog("%sConnection failed! (%s)\n",
C_COLOR_RED,
strerror(errno));
return 2; return 2;
} }

View File

@ -54,6 +54,15 @@ void *serverCommunicationInstance(void *server)
args = (ServerCommunication_t*) server; args = (ServerCommunication_t*) server;
// Get ip address from client
inet_ntop(AF_INET,
&args->clientAddr.sin_addr,
clientIP,
args->socklen);
// Get port number from client
clientPort = ntohs(args->clientAddr.sin_port);
//Accept and incoming connection //Accept and incoming connection
while(!args->pleaseStop) { while(!args->pleaseStop) {
@ -72,15 +81,6 @@ void *serverCommunicationInstance(void *server)
if (bytesReceived == 0) if (bytesReceived == 0)
break; break;
// Get ip address from client
inet_ntop(AF_INET,
&args->clientAddr.sin_addr,
clientIP,
args->socklen);
// Get port number from client
clientPort = ntohs(args->clientAddr.sin_port);
// Print buffer which contains the client request // Print buffer which contains the client request
printLog("Client %s:%d request : '%s'\n", printLog("Client %s:%d request : '%s'\n",
clientIP, clientIP,