Skip to content

Commit

Permalink
Made the server starting more stable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil Samith Bysani committed Dec 24, 2008
1 parent 53ff115 commit b0fb440
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/app/main.c
Expand Up @@ -67,19 +67,30 @@ GtkWidget *ssserver;
* \brief Updates the statusbar depending on what signals it catches.
* NOTE: GTK IS NOT THREAD SAFE! Umm, let me think how we should fix this.
*/
void bind_status(int signal, int sig_id, void *args) {
//bang callback, gtk needs to be locked
void server_status(int signal, int sig_id, void *args) {
gdk_threads_enter();
guint context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar),"bind_status");
guint context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar),"server_status");
gtk_statusbar_pop(GTK_STATUSBAR(statusbar),context_id);
if (signal == BANG_BIND_SUC) {
gtk_statusbar_push(GTK_STATUSBAR(statusbar),context_id,"!bang Machine has been bound.");
} else {
} else if (signal == BANG_BIND_FAIL){
gtk_statusbar_push(GTK_STATUSBAR(statusbar),context_id,"!bang Machine could not bind.");
} else if (signal == BANG_SERVER_STARTED) {
gtk_widget_set_sensitive(ssserver,TRUE);
gtk_label_set_text(GTK_LABEL(gtk_bin_get_child(GTK_BIN(ssserver))),"Stop Server");
gtk_statusbar_push(GTK_STATUSBAR(statusbar),context_id,"!bang Machine server started.");
} else {
gtk_widget_set_sensitive(ssserver,TRUE);
gtk_label_set_text(GTK_LABEL(gtk_bin_get_child(GTK_BIN(ssserver))),"Start Server");
gtk_statusbar_push(GTK_STATUSBAR(statusbar),context_id,"!bang Machine server stopped.");
}
gdk_flush();
gdk_threads_leave();
free(args);
}

//bang callback, gtk needs to be locked
void client_con(int signal, int sig_id, void *args) {
///We could make this buffer smaller.
char buf[30];
Expand All @@ -92,21 +103,24 @@ void client_con(int signal, int sig_id, void *args) {
sprintf(buf,"Peer %d has been removed.",*((int*)args));
gtk_statusbar_push(GTK_STATUSBAR(statusbar),context_id,buf);
}
gdk_flush();
gdk_threads_leave();
free(args);
}

//gtk callback, does not need to get locked is automatic
static void change_server_status(GtkWidget *widget, gpointer data) {
///Just make the stop button inactive. We'll make it active when we get the signal.
if(BANG_is_server_running()) {
BANG_server_stop();
gtk_label_set_text(GTK_LABEL(gtk_bin_get_child(GTK_BIN(widget))),"Start Server");
gtk_widget_set_sensitive(widget,FALSE);
} else {
BANG_server_start(NULL);
gtk_label_set_text(GTK_LABEL(gtk_bin_get_child(GTK_BIN(widget))),"Stop Server");
gtk_widget_set_sensitive(widget,FALSE);
}
}


//gtk callback, does not need to get locked is automatic
static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data) {
/* If you return FALSE in the "delete_event" signal handler,
* GTK will emit the "destroy" signal. Returning TRUE means
Expand All @@ -119,6 +133,7 @@ static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
return FALSE;
}

//gtk callback, does not need to get locked is automatic
static void destroy(GtkWidget *widget, gpointer data) {
BANG_close();
gtk_main_quit();
Expand All @@ -127,8 +142,10 @@ static void destroy(GtkWidget *widget, gpointer data) {
int main(int argc, char **argv) {

BANG_init(&argc,argv);
BANG_install_sighandler(BANG_BIND_SUC,&bind_status);
BANG_install_sighandler(BANG_BIND_FAIL,&bind_status);
BANG_install_sighandler(BANG_BIND_SUC,&server_status);
BANG_install_sighandler(BANG_BIND_FAIL,&server_status);
BANG_install_sighandler(BANG_SERVER_STARTED,&server_status);
BANG_install_sighandler(BANG_SERVER_STOPPED,&server_status);
BANG_install_sighandler(BANG_PEER_CONNECTED,&client_con);
BANG_install_sighandler(BANG_PEER_REMOVED,&client_con);

Expand Down
7 changes: 7 additions & 0 deletions src/base/bang-net.c
Expand Up @@ -97,6 +97,9 @@ void* BANG_server_thread(void *not_used) {
return NULL;
}

args.args = NULL;
args.length = 0;
BANG_send_signal(BANG_SERVER_STARTED,args);
//accepted client
args.length = sizeof(int);
int accptsock;
Expand Down Expand Up @@ -196,6 +199,10 @@ void BANG_server_stop() {
}
free(server_thread);
server_thread = NULL;
BANG_sigargs args;
args.args = NULL;
args.length = 0;
BANG_send_signal(BANG_SERVER_STOPPED,args);
}
sem_post(&server_status_lock);
}
Expand Down
2 changes: 2 additions & 0 deletions src/base/bang-types.h
Expand Up @@ -37,6 +37,8 @@ enum BANG_signals {
BANG_PEER_ADDED,
BANG_PEER_REMOVED,
BANG_RUNNING_MODULE,
BANG_SERVER_STARTED,
BANG_SERVER_STOPPED,

///Error signals.
BANG_BIND_FAIL,
Expand Down

0 comments on commit b0fb440

Please sign in to comment.