diff --git a/Makefile b/Makefile index a83b960..8b9bc87 100644 --- a/Makefile +++ b/Makefile @@ -21,17 +21,33 @@ CCOPTS=-pthread -Wall -g -Os LDFLAGS= -lc -lpthread + BINDIR=bin INCDIR=include SRCDIR=src DEBDIR=debian + SERVEROBJ= $(BINDIR)/scheduler.o $(BINDIR)/server.o $(BINDIR)/localworker.o \ $(BINDIR)/centers.o $(BINDIR)/cmds.o $(BINDIR)/model.o \ $(BINDIR)/main.o $(BINDIR)/arrows.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 -.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 ----------------------------------------------------- # TEST_SCHEDULER=$(BINDIR)/tests/centers @@ -43,12 +59,12 @@ $(BINDIR)/tests/centers: $(SRCDIR)/tests/centers.c # ---- General recipes ------------------------------------------------------- # -$(BINDIR)/%.o: $(SRCDIR)/%.c $(INCDIR)/%.h | $(INCDIR)/base.h +$(BINDIR)/%.o: $(SRCDIR)/%.c @echo "Compiling $<" @$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $< # ---- Main recipe ----------------------------------------------------------- # -$(BINDIR)/main.o: $(SRCDIR)/main.c | $(INCDIR)/base.h +$(BINDIR)/main.o: $(SRCDIR)/main.c @echo "Compiling $<" @$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $< @@ -66,7 +82,7 @@ $(BINDIR)/gem-graph-ctl: $(CLIOBJ) clean: -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) @@ -95,3 +111,4 @@ run-cli: all run-tests: tests bin/tests/scheduler +include $(DEPS) diff --git a/include/base.h b/include/base.h index 2256ab9..97ea250 100644 --- a/include/base.h +++ b/include/base.h @@ -32,6 +32,15 @@ #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 { size_t size; int *space; @@ -69,25 +78,20 @@ struct { SpaceUnit_t *space; } typedef Space_t; -#define LOGMSG "[%s]" -#define printLog(FORMAT, ...) printf(LOGMSG " " FORMAT, __func__, ##__VA_ARGS__) - -#define LEN(x) (sizeof(x) / sizeof((x)[0])) - /* -------------------------------------------------------------------------- */ // // Scheduler // struct { + pthread_t id; + bool pleaseStop; Space_t *globalDrawingSpace; IntArray_t *conditionTree; ArrowArray_t *arrowArray; int nMaxThread; int nMaxCycles; int ruleRadius; - pthread_t id; - bool pleaseStop; } typedef Scheduler_t; /* -------------------------------------------------------------------------- */ @@ -99,7 +103,6 @@ struct Center_t { int x; int y; int z; - struct Center_t *next; struct Center_t *prev; } typedef Center_t; diff --git a/include/cli.h b/include/cli.h deleted file mode 100644 index 50a3aa2..0000000 --- a/include/cli.h +++ /dev/null @@ -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 . // -//=-------------------------------------------------------------------------=// - -#ifndef BASE_H - #include "../include/base.h" -#endif diff --git a/src/cli.c b/src/cli.c index 9276db0..e02fcb3 100644 --- a/src/cli.c +++ b/src/cli.c @@ -48,6 +48,9 @@ #define C_CURSORRIGHT "\e[1C" #define C_SAVE_CURSORPOS "\e7" #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) { @@ -59,7 +62,9 @@ static char getch(void) fflush(stdout); 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; } @@ -69,12 +74,16 @@ static char getch(void) old.c_cc[VTIME] = 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; } 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; } @@ -82,7 +91,9 @@ static char getch(void) old.c_lflag |= ECHO; // set echo mode 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 buf; @@ -250,7 +261,7 @@ void connectedCommunication(int sockfd) // Quit if asked if (strcmp(exitCommand, sendBuff) == 0) { - return; + break; } // Otherwise send command to server @@ -264,17 +275,17 @@ void connectedCommunication(int sockfd) // Detect disconnection if (answerLength == 0) { - printf("Disconnected\n"); break; } // Detect null-string returned if (receiveBuff[0] == '\0') { - printf("Invalid command!\n"); + printf("%sInvalid command!\n\e[0m", C_COLOR_YELLOW); } printf("%s\n", receiveBuff); } + printf("%sDisconnected\n\e[0m", C_COLOR_YELLOW); } int main(void) @@ -285,7 +296,9 @@ int main(void) // Socket creation sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { - printLog("Socket creation failed! (%s)\n", strerror(errno)); + printLog("%sSocket creation failed! (%s)\n", + C_COLOR_RED, + strerror(errno)); return 1; } @@ -298,7 +311,9 @@ int main(void) // Connect to the server 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; } diff --git a/src/server.c b/src/server.c index 90a953c..c898066 100644 --- a/src/server.c +++ b/src/server.c @@ -54,6 +54,15 @@ void *serverCommunicationInstance(void *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 while(!args->pleaseStop) { @@ -72,15 +81,6 @@ void *serverCommunicationInstance(void *server) if (bytesReceived == 0) 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 printLog("Client %s:%d request : '%s'\n", clientIP,