Skip to content

Commit

Permalink
Added preliminary signal handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil Samith Bysani committed Dec 20, 2008
1 parent 0bd9951 commit 22417a3
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 19 deletions.
23 changes: 18 additions & 5 deletions Makefile
@@ -1,27 +1,40 @@
####################################################
# The Makefile, builds the project
####################################################

EXENAME=bang-machine

CC=gcc
COPTS=-Wall -Werror -g -lpthread

OBJS=bang-net.o bang-signals.o core.o main.o

MAINSRC=src/app/main.c
CORESRC=src/base/core.c
NETWORKSRC=src/base/bang-net.c
SIGNALSSRC=src/base/bang-signals.c

$(EXENAME): bang-net.o core.o main.o
.PHONY: doc

$(EXENAME): $(OBJS)
$(CC) $(COPTS) $^ -o $(EXENAME)

main.o: $(MAINSRC)
$(CC) -c $(COPTS) $^

core.o: bang-net.o $(CORESRC)
core.o: $(CORESRC)
$(CC) -c $(COPTS) $^

bang-signals.o: $(SIGNALSSRC)
$(CC) -c $(COPTS) $^

bang-net.o: $(NETWORKSRC)
$(CC) -c $(COPTS) $^

.PHONY: doc
all: $(EXENAME) doc

doc:
cd doc; \
doxygen Doxygen;
cd doc && doxygen Doxygen;

clean:
rm -f $(EXENAME)
Expand Down
62 changes: 62 additions & 0 deletions src/base/bang-signals.c
@@ -0,0 +1,62 @@
#include"bang-signals.h"
#include"bang-types.h"
#include<stdlib.h>

struct _signal_node {
BANGSignalHandler *handler;
struct _signal_node *next;
};

typedef struct _signal_node signal_node;

/**
* \brief The handlers for each of the signals is kept in a linked list stored
* in an array which is index by the signal's number
*/
signal_node **signal_handlers;

void BANG_sig_init() {
int i;

signal_handlers = (signal_node**) calloc(BANG_NUM_SIGS,sizeof(signal_node*));
for (i = 0; i < BANG_NUM_SIGS; ++i) {
signal_handlers[i] = NULL;
}
}

void recursive_sig_free(signal_node *head) {
if (head->next != NULL) {
recursive_sig_free(head->next);
}
free(head);
head = NULL;
}

void BANG_sig_close() {
int i;
for (i = 0; i < BANG_NUM_SIGS; ++i) {
recursive_sig_free(signal_handlers[i]);
}
free(signal_handlers);
signal_handlers = NULL;
}

int BANG_install_sighandler(int signal, BANGSignalHandler *handler) {
if (signal_handlers[signal] == NULL) {
signal_handlers[signal] = (signal_node*) malloc(sizeof(signal_node));
signal_handlers[signal]->handler = handler;
signal_handlers[signal]->next = NULL;
return 0;
} else {
signal_node *cur;
for (cur = signal_handlers[signal]; cur != NULL; cur = cur->next) {
if (cur->next == NULL) {
cur->next =(signal_node*) malloc(sizeof(signal_node));
cur->next->handler = handler;
cur->next->next = NULL;
return 0;
}
}
}
return -1;
}
42 changes: 42 additions & 0 deletions src/base/bang-signals.h
@@ -0,0 +1,42 @@
/**
* \file bang-signals.h
* \author Nikhil Bysani
* \date December 20, 2009
*
* \brief Allows the library to send out signals, and for the applications to
* receive signals.
*/
#ifndef __BANG_SIGNALS_H
#define __BANG_SIGNALS_H

#include"bang-types.h"
/**
* \param signal The signal that you want to catch.
* \param handler Function that handles the signal.
*
* \brief Installs a signal such that whenever said signal gets generated
* handler gets called with the appropriate arguments.
*/
int BANG_install_sighandler(int signal, BANGSignalHandler *handler);

/**
* \param signal The signal type to acknowledge.
* \param sig_id The id of the signal
*
* \brief Acknowledges that the signal has been received, and that
* the function is ready to take another.
*/
void BANG_acknowledge_signal(int signal, int sig_id);

/**
*
* \brief Initializes the signal porition of the library.
*/
void BANG_sig_init();

/**
*
* \brief Frees and stops signals from working.
*/
void BANG_sig_close();
#endif
15 changes: 14 additions & 1 deletion src/base/bang-types.h
@@ -1,7 +1,20 @@
/**
* \file bang-types.h
* \author Nikhil Bysani
* \date December 20, 2009
*
* \brief Defines commonly used types in the BANG library.
*
*/
#ifndef __BANG_TYPES_H
#define __BANG_TYPES_H

/// A signal handler function, it receives the BANGSIGNUM, and arguments depending on the signal.
typedef void BANGSignalHandler(int,void **args);
typedef void BANGSignalHandler(int,int,void **args);

#define BANG_NUM_SIGS 1
enum BANG_signals {
BANG_BIND_SUC = 0
} BANG_signal;

#endif
7 changes: 5 additions & 2 deletions src/base/core.c
Expand Up @@ -3,11 +3,12 @@
* \author Nikhil Bysani
* \date December 20, 2009
*
* \brief Implememnts the main interface to the BANG project. This is mainly accomplished
* through use of BANG_init, BANG_close, and BANG_install_sighandler.
* \brief This implements the main public high level interface to the BANG project. This is mainly accomplished
* through use of BANG_init and BANG_close.
*/
#include"core.h"
#include"bang-net.h"
#include"bang-signals.h"
#include<pthread.h>
#include<stdlib.h>
#include<stdio.h>
Expand All @@ -22,6 +23,7 @@ pthread_t *netthread;

void BANG_init(int *argc, char **argv) {
int i = 0;
BANG_sig_init();
for (i = 0; i < *argc; ++i) {
if (!strcmp(argv[i],"--port")) {
if (i + 1 < *argc) {
Expand All @@ -38,6 +40,7 @@ void BANG_init(int *argc, char **argv) {
}

void BANG_close() {
BANG_sig_close();
pthread_join(*netthread,NULL);
free(netthread);
}
14 changes: 3 additions & 11 deletions src/base/core.h
Expand Up @@ -3,14 +3,15 @@
* \author Nikhil Bysani
* \date December 20, 2009
*
* \brief This defines the main _public_ interface to the BANG project. This is mainly accomplished
* through use of BANG_init, BANG_close, and BANG_install_sighandler.
* \brief This defines the main public high level interface to the BANG project. This is mainly accomplished
* through use of BANG_init and BANG_close.
*/
#ifndef __CORE_H
#define __CORE_H

#include"bang-net.h"
#include"bang-types.h"
#include"bang-signals.h"

/**
* \param argc The number of arguments of the program.
Expand All @@ -32,15 +33,6 @@ void BANG_close();
*/
int BANG_load_module(char *path);

/**
* \param signal The signal that you want to catch.
* \param handler Function that handles the signal.
*
* \brief Installs a signal such that whenever said signal gets generated
* handler gets called with the appropriate arguments.
*/
int BANG_install_sighandler(int signal, BANGSignalHandler handler);

/**
* \brief Potientally finds peers using zero-conf. This is a candidate to be moved to another file.
*/
Expand Down

0 comments on commit 22417a3

Please sign in to comment.