Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make event base global

  • Loading branch information...
commit 93b46761b0440f4e6a601b7a1d7b2d65446bc040 1 parent 397fb0d
@robinedwards authored
View
19 event_server.c
@@ -15,6 +15,9 @@
#include "message_handler.h"
+struct event_base *event_base;
+
+
static void EventServer_write_callback(struct bufferevent *bev, void *arg) {
bufferevent_free(bev);
}
@@ -37,7 +40,7 @@ static void EventServer_read_callback(struct bufferevent *bev, void *arg) {
output = bufferevent_get_output(bev);
while ((request = evbuffer_readln(input, &n, EVBUFFER_EOL_LF))) {
- char *response = message_handler(request, n, server->base);
+ char *response = message_handler(request, n, event_base);
free(request);
evbuffer_add(output, response, strlen(response)+1);
bufferevent_setcb(bev, EventServer_read_callback, EventServer_write_callback, EventServer_error_callback, server);
@@ -48,19 +51,17 @@ static void EventServer_read_callback(struct bufferevent *bev, void *arg) {
static void EventServer_accept_callback(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *address, int socklen, void *ctx) {
EventServer * server = ctx;
/* We got a new connection! Set up a bufferevent for it. */
- struct event_base *base = evconnlistener_get_base(listener);
- struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
+ struct bufferevent *bev = bufferevent_socket_new(event_base, fd, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setwatermark(bev, EV_WRITE, 1, 9999);
bufferevent_setcb(bev, EventServer_read_callback, NULL, EventServer_error_callback, server);
bufferevent_enable(bev, EV_READ|EV_WRITE);
}
static void EventServer_accept_error_callback(struct evconnlistener *listener, void *ctx) {
- struct event_base *base = evconnlistener_get_base(listener);
int err = EVUTIL_SOCKET_ERROR();
fprintf(stderr, "Got an error %d (%s) on the listener. "
"Shutting down.\n", err, evutil_socket_error_to_string(err));
- event_base_loopexit(base, NULL);
+ event_base_loopexit(event_base, NULL);
}
EventServer * EventServer_create(int port) {
@@ -70,8 +71,8 @@ EventServer * EventServer_create(int port) {
server->port = port;
- server->base = event_base_new();
- if (!server->base)
+ event_base = event_base_new();
+ if (!event_base)
return NULL;
memset(&(server->sin), 0, sizeof(struct sockaddr_in));
@@ -79,7 +80,7 @@ EventServer * EventServer_create(int port) {
server->sin.sin_addr.s_addr = 0; // listen on 0.0.0.0
server->sin.sin_port = htons(server->port);
- server->listener = evconnlistener_new_bind(server->base, EventServer_accept_callback, server,
+ server->listener = evconnlistener_new_bind(event_base, EventServer_accept_callback, server,
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1,
(struct sockaddr*)&(server->sin), sizeof(struct sockaddr_in));
if (!server->listener)
@@ -89,7 +90,7 @@ EventServer * EventServer_create(int port) {
}
void EventServer_run(EventServer *server) {
- event_base_dispatch(server->base);
+ event_base_dispatch(event_base);
}
// TODO EventServer_destory
View
1  event_server.h
@@ -4,7 +4,6 @@ typedef struct {
int port;
struct evconnlistener *listener;
struct sockaddr_in sin;
- struct event_base *base;
} EventServer;
EventServer * EventServer_create(int port);
View
14 job.c
@@ -8,12 +8,12 @@
#include "die.h"
#include "job.h"
-Job *Job_create(int id, char *command, int time_limit, struct event_base *eb) {
+extern struct event_base *event_base;
+
+Job *Job_create(int id, char *command, int time_limit) {
assert(command != NULL);
Job *job = malloc(sizeof(Job));
assert(job != NULL);
- assert(eb != NULL);
- job->event_base = eb;
job->stderr_output = NULL;
job->stdout_output = NULL;
@@ -141,7 +141,7 @@ static void Job_read_stdout_pipe(int fd, short flags, void *data) {
else if (ret > 0) {
buf[ret] = 0;
Job_grow_and_append(&(job->stdout_output), buf, ret);
- struct event * stdout_ev = event_new(job->event_base, fd, EV_READ, Job_read_stdout_pipe, (void*) job);
+ struct event * stdout_ev = event_new(event_base, fd, EV_READ, Job_read_stdout_pipe, (void*) job);
event_add(stdout_ev, NULL);
}
else if (ret == 0)
@@ -159,7 +159,7 @@ static void Job_read_stderr_pipe(int fd, short flags, void *data) {
else if (ret > 0) {
buf[ret] = 0;
Job_grow_and_append(&(job->stderr_output), buf, ret);
- struct event * stderr_ev = event_new(job->event_base, fd, EV_READ, Job_read_stderr_pipe, (void*) job);
+ struct event * stderr_ev = event_new(event_base, fd, EV_READ, Job_read_stderr_pipe, (void*) job);
event_add(stderr_ev, NULL);
}
else if (ret == 0)
@@ -168,11 +168,11 @@ static void Job_read_stderr_pipe(int fd, short flags, void *data) {
int Job_setup_pipes(Job *job) {
assert(job != NULL);
- struct event * stderr_ev = event_new(job->event_base, job->stderr_fd,
+ struct event * stderr_ev = event_new(event_base, job->stderr_fd,
EV_READ, Job_read_stderr_pipe, (void*) job);
- struct event * stdout_ev = event_new(job->event_base, job->stdout_fd,
+ struct event * stdout_ev = event_new(event_base, job->stdout_fd,
EV_READ, Job_read_stdout_pipe, (void*) job);
event_add(stdout_ev, NULL);
View
3  job.h
@@ -13,10 +13,9 @@ typedef struct {
int stdout_fd;
char *stderr_output;
char *stdout_output;
- struct event_base *event_base;
} Job;
-Job *Job_create(int id, char *command, int time_limit, struct event_base *base);
+Job *Job_create(int id, char *command, int time_limit);
void Job_destroy(Job *job);
void Job_setup_child_handler(void);
void Job_print(Job *job);
View
5 message_handler.c
@@ -6,12 +6,11 @@
#include <stdio.h>
#include <unistd.h>
-char * message_handler(char * request, int n, struct event_base *event_base) {
+char * message_handler(char * request, int n) {
static int job_id = 1;
char * cmd = strndup(request, n);
- Job *job = Job_create(job_id++, cmd, 99, event_base);
-
+ Job *job = Job_create(job_id++, cmd, 99);
if (job == NULL)
return "FAILED TO CREATE JOB\n";
Please sign in to comment.
Something went wrong with that request. Please try again.