Skip to content

Commit

Permalink
Agregada funcionalidad para deshabilitar el logueo interno de las kem…
Browse files Browse the repository at this point in the history
…mens
  • Loading branch information
CorridoniMatias committed Dec 6, 2018
1 parent 0edfcc0 commit ce44fce
Show file tree
Hide file tree
Showing 19 changed files with 150 additions and 89 deletions.
7 changes: 3 additions & 4 deletions ChatApp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void* ClientManageReceptions(void* socket)
void Client(char* texto)
{
printf("Conectando al server...\n");
int sock = SocketClient_ConnectToServer("8086");
int sock = SocketClient_ConnectToServerIP("127.0.0.1", "8086");
printf("Socket asignado %d\n", sock);
int m;
pthread_t threadRecv;
Expand Down Expand Up @@ -255,7 +255,7 @@ void Client(char* texto)
void* ClientServer(void* port)
{
printf("Conectando al server...\n");
int sock = SocketClient_ConnectToServer( ((char*)port) );
int sock = SocketClient_ConnectToServerIP("127.0.0.1", ((char*)port) );
printf("Socket asignado %d\n", sock);
int m;
char* asd = (char*)malloc(1);
Expand Down Expand Up @@ -462,8 +462,7 @@ int main(int argc, char **argv)
//ProbarCommandInterpreter();
//ThreadPoolFunc();
//TestSerialization();
TestDeserialization();
return 0;
//TestDeserialization();
Logger_CreateLog("./chatapp.log", "CHARAPP", true);


Expand Down
20 changes: 6 additions & 14 deletions kemmens/CommandInterpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ void CommandInterpreter_RegisterCommand(char* command, void (*runner)(int argC,
{
if(interpreters == NULL)
{
#ifdef KEMMENS_ENABLE_LOGGING
#ifdef CI_ENABLE_LOGGING
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef COMMANDINTERPRETER_DISABLE_LOGGING
Logger_Log(LOG_ERROR, "KEMMENSLIB - CommandInterpreter::CommandInterpreter_Do -> No se llamo al metodo CommandInterpreter_Init(...)!!");
#endif
#endif
Expand All @@ -52,11 +52,7 @@ bool CommandInterpreter_DeRegisterCommand(char* command)
{
if(interpreters == NULL)
{
#ifdef KEMMENS_ENABLE_LOGGING
#ifdef CI_ENABLE_LOGGING
Logger_Log(LOG_ERROR, "KEMMENSLIB - CommandInterpreter::CommandInterpreter_Do -> No se llamo al metodo CommandInterpreter_Init(...)!!");
#endif
#endif
Logger_Log(LOG_ERROR, "KEMMENSLIB - CommandInterpreter::CommandInterpreter_Do -> No se llamo al metodo CommandInterpreter_Init(...)! No se puede llamar a Do().");
return false;
}

Expand Down Expand Up @@ -105,11 +101,7 @@ bool CommandInterpreter_Do(char* command, char* separator, void* extraData)
{
if(interpreters == NULL)
{
#ifdef KEMMENS_ENABLE_LOGGING
#ifdef CI_ENABLE_LOGGING
Logger_Log(LOG_ERROR, "KEMMENSLIB - CommandInterpreter::CommandInterpreter_Do -> No se llamo al metodo CommandInterpreter_Init(...)!!");
#endif
#endif
Logger_Log(LOG_ERROR, "KEMMENSLIB - CommandInterpreter::CommandInterpreter_Do -> No se llamo al metodo CommandInterpreter_Init(...)! No se puede llamar al DO().");
return false;
}

Expand Down Expand Up @@ -164,8 +156,8 @@ void CommandInterpreter_Destroy()
void deregistercommands(void* cmd)
{
CommandRunnerStructure* com = (CommandRunnerStructure*)cmd;
#ifdef KEMMENS_ENABLE_LOGGING
#ifdef CI_ENABLE_LOGGING
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef COMMANDINTERPRETER_DISABLE_LOGGING
Logger_Log(LOG_INFO, "KEMMENSLIB - CommandInterpreter::CommandInterpreter_Destroy -> Eliminando comando '%s'", com->command);
#endif
#endif
Expand Down
17 changes: 10 additions & 7 deletions kemmens/SocketCommons.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include "kemmens/SocketCommons.h"
#include <errno.h>
#include "stdio.h"
#include <unistd.h>

static ContentHeader* SocketCommons_CreateHeader()
{
Expand All @@ -28,7 +25,9 @@ static ContentHeader* SocketCommons_ReceiveHeader(int socket, int* error_status)

if(ret < 1)
{
Logger_Log(LOG_ERROR, "KEMMENSLIB::SOCKETCOMMONS->SocketCommons_ReceiveHeader - Error al recibir header, return recv: %d", ret);
if(ret < 0) //No mostrar error si es un client disconnect
Logger_Log(LOG_ERROR, "KEMMENSLIB::SOCKETCOMMONS->SocketCommons_ReceiveHeader - Error al recibir header, return recv: %d", ret);

free(header);

if(error_status != 0)
Expand Down Expand Up @@ -74,7 +73,7 @@ static void* SocketCommons_ReceiveDataWithoutHeader(int socket, int expected_siz
if(error_status != NULL)
{
*error_status = errno;
Logger_Log(LOG_DEBUG, "KEMMENSLIB::SOCKETCOMMONS->SocketCommons_ReceiveData - Return recv: %d, errno: %d, error: %s", status, *error_status, strerror(*error_status));
Logger_Log(LOG_ERROR, "KEMMENSLIB::SOCKETCOMMONS->SocketCommons_ReceiveData - Return recv: %d, errno: %d, error: %s", status, *error_status, strerror(*error_status));
}

Logger_Log(LOG_ERROR, "KEMMENSLIB::SOCKETCOMMONS->SocketCommons_ReceiveDataWithoutHeader - Error al recibir datos!");
Expand Down Expand Up @@ -111,7 +110,11 @@ void* SocketCommons_ReceiveData(int socket, int* message_type, int* message_leng
*message_type = header->message_type;
free(header);

#ifndef KEMMENS_DISABLE_LOGGING
#ifndef SOCKETCOMMONS_DISABLE_LOGGING
Logger_Log(LOG_DEBUG, "KEMMENSLIB::SOCKETCOMMONS->SocketCommons_ReceiveData - Recibiendo datos, length: %d, content type: %d", len, *message_type);
#endif
#endif

if(len == 0)
return NULL;
Expand Down Expand Up @@ -146,10 +149,10 @@ void SocketCommons_CloseSocket(int socketFD)
{
if((close(socketFD) !=0 ))
{
Logger_Log(LOG_ERROR, "No se pudo cerrar el socket descripto por %d", socketFD);
Logger_Log(LOG_ERROR, "KEMMENSLIB::SOCKETCOMMONS->No se pudo cerrar el socket descripto por %d", socketFD);
return;
}
Logger_Log(LOG_ERROR, "Socket descripto por %d cerrado, fin de la conexion a traves de el", socketFD);
Logger_Log(LOG_ERROR, "KEMMENSLIB::SOCKETCOMMONS->Socket descripto por %d cerrado, fin de la conexion a traves de el", socketFD);
return;
}

Expand Down
70 changes: 46 additions & 24 deletions kemmens/SocketServer.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
#include "kemmens/SocketServer.h"

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <commons/string.h>
#include <sys/types.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <readline/readline.h>

#define STDIN 0 //Input basico

int sock = -1;
Expand Down Expand Up @@ -49,11 +37,15 @@ void SocketServer_Start(char name[5], int port)

if(sock == -1)
{
Logger_Log(LOG_ERROR, "Error al abrir socket del servidor!");
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef SOCKETSERVER_DISABLE_LOGGING
Logger_Log(LOG_ERROR, "KEMMENSLIB::SocketServer-> Error al abrir socket del servidor!");
#endif
#endif
exit_gracefully(EXIT_FAILURE);
}

Logger_Log(LOG_INFO, "Socket de servidor '%s' creado con exito.", alias);
Logger_Log(LOG_DEBUG, "KEMMENSLIB::SocketServer-> Socket de servidor '%s' creado con exito.", alias);


serv_addr.sin_family = AF_INET;
Expand All @@ -62,7 +54,11 @@ void SocketServer_Start(char name[5], int port)

if( bind(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
Logger_Log(LOG_ERROR, "Error al bindear servidor '%s'!", alias);
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef SOCKETSERVER_DISABLE_LOGGING
Logger_Log(LOG_ERROR, "KEMMENSLIB::SocketServer-> Error al bindear servidor '%s'!", alias);
#endif
#endif
exit_gracefully(EXIT_FAILURE);
}
}
Expand All @@ -74,12 +70,19 @@ void SocketServer_ListenForConnection(SocketServer_ActionsListeners actions)

if(listen(sock, MAXWAITCONNECTIONS) < 0)
{
Logger_Log(LOG_ERROR,"'%s' -> Error al escuchar conexiones!", alias);
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef SOCKETSERVER_DISABLE_LOGGING
Logger_Log(LOG_ERROR,"KEMMENSLIB::SocketServer-> '%s' -> Error al escuchar conexiones!", alias);
#endif
#endif
exit_gracefully(EXIT_FAILURE);
}

Logger_Log(LOG_INFO, "Servidor '%s' escuchando por conexiones entrantes...", alias);

#ifndef KEMMENS_DISABLE_LOGGING
#ifndef SOCKETSERVER_DISABLE_LOGGING
Logger_Log(LOG_DEBUG, "KEMMENSLIB::SocketServer-> Servidor '%s' escuchando por conexiones entrantes...", alias);
#endif
#endif
connections = list_create();
//ignoredSockets = list_create();
pthread_mutex_init(&connections_lock, NULL);
Expand Down Expand Up @@ -124,7 +127,11 @@ void SocketServer_ListenForConnection(SocketServer_ActionsListeners actions)

if ((sel < 0) && (errno!=EINTR))
{
Logger_Log(LOG_ERROR, "'%s' -> Error en select = %d", alias, sel);
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef SOCKETSERVER_DISABLE_LOGGING
Logger_Log(LOG_ERROR, "KEMMENSLIB::SocketServer-> '%s' -> Error en select = %d", alias, sel);
#endif
#endif
continue;
}

Expand All @@ -134,11 +141,19 @@ void SocketServer_ListenForConnection(SocketServer_ActionsListeners actions)
connected_socket = accept(sock, (struct sockaddr*)NULL, NULL);
if(connected_socket < 0)
{
Logger_Log(LOG_ERROR, "'%s' -> Error al aceptar conexion entrante... skipeando conexion.", alias);
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef SOCKETSERVER_DISABLE_LOGGING
Logger_Log(LOG_ERROR, "KEMMENSLIB::SocketServer-> '%s' -> Error al aceptar conexion entrante... skipeando conexion.", alias);
#endif
#endif
continue;
}

Logger_Log(LOG_INFO, "'%s' -> Conexion entrante aceptada en %d", alias, connected_socket);
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef SOCKETSERVER_DISABLE_LOGGING
Logger_Log(LOG_DEBUG, "KEMMENSLIB::SocketServer-> '%s' -> Conexion entrante aceptada en %d", alias, connected_socket);
#endif
#endif

ServerClient* client = SocketServer_CreateClient(connected_socket);
pthread_mutex_lock(&connections_lock);
Expand Down Expand Up @@ -231,8 +246,11 @@ void SocketServer_ListenForConnection(SocketServer_ActionsListeners actions)
//Si hay algun thread esperando datos en este socketID entonces no tenemos que llamar a OnPacketArrived, le derivamos la informacion al thread que la espera. Hablo en singular porque no tiene sentido que dos threads esten esperando en paralelo datos sobre EL MISMO socket.
if(currclient->isWaitingForData)
{
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef SOCKETSERVER_DISABLE_LOGGING
Logger_Log(LOG_DEBUG, "KEMMENSLIB -> SOCKETSERVER :: Datos recibidos de %d, derivando al thread a la espera de los mismos. OnPacketArrived no sera llamado!", currclient->socketID);

#endif
#endif

OnArrivedData* arriveData = SocketServer_CreateOnArrivedData();
arriveData->calling_SocketID = currclient->socketID;
Expand Down Expand Up @@ -289,11 +307,15 @@ void SocketServer_TerminateAllConnections()
sem_post(&client->waitForData);
}

Logger_Log(LOG_INFO, "'%s' -> Cerrando conexion %d ", alias, client->socketID);
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef SOCKETSERVER_DISABLE_LOGGING
Logger_Log(LOG_DEBUG, "KEMMENSLIB::SocketServer-> '%s' -> Cerrando conexion %d ", alias, client->socketID);
#endif
#endif

if(close( client->socketID ) < 0)
{
Logger_Log(LOG_ERROR, "'%s' -> Error al cerrar conexion %d!", alias, client->socketID);
Logger_Log(LOG_ERROR, "KEMMENSLIB::SocketServer-> '%s' -> Error al cerrar conexion %d!", alias, client->socketID);
}
}
pthread_mutex_lock(&connections_lock);
Expand Down
9 changes: 8 additions & 1 deletion kemmens/ThreadManager.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ int ThreadManager_CreateThread(pthread_t* thread, void *(*startingPoint) (void *
{
int status = pthread_create(thread, NULL, (void*) startingPoint, (void*) argument);

#ifndef KEMMENS_DISABLE_LOGGING
#ifndef THREADMANAGER_DISABLE_LOGGING
if(status != 0)
Logger_Log(LOG_ERROR, "KEMMENSLIB::THREADMANAGER->CREATETHREAD - Error al crear thread. Status = '%d'", status);

#endif
#endif
return status;
}

Expand All @@ -24,8 +27,12 @@ int ThreadManager_CreateDetachedThread(pthread_t* thread, void *(*startingPoint)
int status = pthread_create(thread, &attr, startingPoint, argument);
pthread_attr_destroy(&attr);

#ifndef KEMMENS_DISABLE_LOGGING
#ifndef THREADMANAGER_DISABLE_LOGGING
if(status != 0)
Logger_Log(LOG_ERROR, "KEMMENSLIB::THREADMANAGER->CREATEDETACHEDTHREAD - Error al crear detached thread. Status = '%d'", status);
#endif
#endif

return status;
}
46 changes: 36 additions & 10 deletions kemmens/ThreadPool.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
static void* ThreadPool_BasicOperation(void* ownerPool)
{
ThreadPool* owner = (ThreadPool*)ownerPool;
pthread_t this = pthread_self();

Logger_Log(LOG_DEBUG, "Thread %p Registering", &this);

#ifndef KEMMENS_DISABLE_LOGGING
#ifndef THREADPOOL_DISABLE_LOGGING
pthread_t this = pthread_self();
Logger_Log(LOG_DEBUG, "KEMMENSLIB::ThreadPool->Thread %p Registering", &this);
#endif
#endif
while(!owner->terminate_all)
{
pthread_mutex_lock(&owner->lock);
Expand All @@ -24,7 +27,11 @@ static void* ThreadPool_BasicOperation(void* ownerPool)
{
ThreadPoolRunnable* run = queue_pop(owner->jobs);
owner->free_threads--;
Logger_Log(LOG_DEBUG, "Thread %p orking on job... Pool job queue count: %d. Free threads: %d", &this, queue_size(owner->jobs), owner->free_threads);
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef THREADPOOL_DISABLE_LOGGING
Logger_Log(LOG_DEBUG, "KEMMENSLIB::ThreadPool->Thread %p working on job... Pool job queue count: %d. Free threads: %d", &this, queue_size(owner->jobs), owner->free_threads);
#endif
#endif
pthread_mutex_unlock(&owner->lock);

run->runnable(run->data); //adentro de runnable se tiene que liberar la data! Responsabilidad del usuario!
Expand All @@ -34,13 +41,21 @@ static void* ThreadPool_BasicOperation(void* ownerPool)
owner->free_threads++;
pthread_mutex_unlock(&owner->lock);

Logger_Log(LOG_DEBUG, "Thread %p Finished, free threads: %d", &this, owner->free_threads+1);
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef THREADPOOL_DISABLE_LOGGING
Logger_Log(LOG_DEBUG, "KEMMENSLIB::ThreadPool->Thread %p Finished, free threads: %d", &this, owner->free_threads+1);
#endif
#endif
continue;
}

pthread_mutex_unlock(&owner->lock);
}
Logger_Log(LOG_DEBUG, "Thread %p EXITING!", &this);
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef THREADPOOL_DISABLE_LOGGING
Logger_Log(LOG_DEBUG, "KEMMENSLIB::ThreadPool->Thread %p EXITING!", &this);
#endif
#endif
pthread_exit(0);
return 0;
}
Expand Down Expand Up @@ -95,8 +110,11 @@ void ThreadPool_AddJob(ThreadPool* pool, ThreadPoolRunnable* job)

if(!pool->terminate_all)
{
Logger_Log(LOG_DEBUG, "Adding job... Pool job queue count: %d", queue_size(pool->jobs));

#ifndef KEMMENS_DISABLE_LOGGING
#ifndef THREADPOOL_DISABLE_LOGGING
Logger_Log(LOG_DEBUG, "KEMMENSLIB::ThreadPool->Adding job... Pool job queue count: %d", queue_size(pool->jobs));
#endif
#endif
queue_push(pool->jobs, job);

pthread_cond_broadcast(&pool->cond);
Expand Down Expand Up @@ -139,7 +157,11 @@ void ThreadPool_JoinAllThreads(ThreadPool* pool)
{
void joinAll(void* thread)
{
Logger_Log(LOG_INFO, "KEMMENSLIB::ThreadPool->ThreadPool_ExitWhenPossibleWait - Joining thread %p...", thread);
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef THREADPOOL_DISABLE_LOGGING
Logger_Log(LOG_DEBUG, "KEMMENSLIB::ThreadPool->ThreadPool_ExitWhenPossibleWait - Joining thread %p...", thread);
#endif
#endif
pthread_join( *((pthread_t*)thread), NULL );
}

Expand All @@ -163,7 +185,11 @@ void ThreadPool_ClearPendingJobs(ThreadPool* pool)
if(actual)
free(actual);

Logger_Log(LOG_INFO, "KEMMENSLIB::ThreadPool->ThreadPool_ClearPendingJobs - Removed job %p", job);
#ifndef KEMMENS_DISABLE_LOGGING
#ifndef THREADPOOL_DISABLE_LOGGING
Logger_Log(LOG_DEBUG, "KEMMENSLIB::ThreadPool->ThreadPool_ClearPendingJobs - Removed job %p", job);
#endif
#endif
}

pthread_mutex_lock(&pool->lock);
Expand Down
Loading

0 comments on commit ce44fce

Please sign in to comment.