Skip to content
Browse files

Merge pull request #13 from jehiah/split_run_methods

split apart _main() functions to _listen, _run, _free
  • Loading branch information...
2 parents cd0958c + 9ada0f0 commit c7f60cb7145c3ce89313c45253492025afdc321a @mreiferson mreiferson committed Aug 23, 2011
View
3 ps_to_file/ps_to_file.c
@@ -106,6 +106,9 @@ int main(int argc, char **argv)
free(data);
free_options();
+ free(pubsub_url);
+ free(address);
+ free(path);
return 0;
}
View
3 ps_to_http/ps_to_http.c
@@ -183,6 +183,9 @@ int main(int argc, char **argv)
free_destination_urls();
free_async_connection_pool();
free_options();
+ free(pubsub_url);
+ free(address);
+ free(path);
return 0;
}
View
10 pubsub_filtered/pubsub_filtered.c
@@ -204,7 +204,6 @@ void process_message_cb(char *source, void *arg)
msgRecv++;
- fprintf(stderr, "%s\n", source);
json_in = json_tokener_parse(source);
if (json_in == NULL) {
@@ -580,9 +579,14 @@ int main(int argc, char **argv)
simplehttp_set_cb("/stats*", stats_cb, NULL);
simplehttp_set_cb("/clients", clients_cb, NULL);
- pubsubclient_main(source_address, source_port, source_path, process_message_cb, error_cb, data);
+ pubsubclient_init(source_address, source_port, source_path, process_message_cb, error_cb, data);
+ simplehttp_main();
+ pubsubclient_free();
free_options();
-
+ free(pubsub_url);
+ free(source_address);
+ free(source_path);
+
return 0;
}
View
32 pubsubclient/pubsubclient.c
@@ -18,6 +18,8 @@ static struct GlobalData *data = NULL;
static struct StreamRequest *stream_request = NULL;
static struct evhttp_connection *evhttp_source_connection = NULL;
static struct evhttp_request *evhttp_source_request = NULL;
+static struct StreamRequest *autodetect_sr = NULL;
+extern struct event_base *current_base;
struct GlobalData {
void (*message_cb)(char *data, void *cbarg);
@@ -160,19 +162,20 @@ void pubsubclient_autodetect_headercb(struct bufferevent *bev, struct evkeyvalq
}
}
-int pubsubclient_main(const char *source_address, int source_port, const char *path,
+void pubsubclient_init(const char *source_address, int source_port, const char *path,
void (*message_cb)(char *data, void *arg),
void (*error_cb)(int status_code, void *arg),
void *cbarg)
{
- struct StreamRequest *autodetect_sr = NULL;
signal(SIGINT, pubsubclient_termination_handler);
signal(SIGQUIT, pubsubclient_termination_handler);
signal(SIGTERM, pubsubclient_termination_handler);
signal(SIGHUP, pubsubclient_termination_handler);
- event_init();
+ if (!current_base) {
+ event_init();
+ }
data = calloc(1, sizeof(struct GlobalData));
data->message_cb = message_cb;
@@ -191,9 +194,26 @@ int pubsubclient_main(const char *source_address, int source_port, const char *p
fprintf(stderr, "ERROR: new_stream_request() failed for %s:%d%s\n", source_address, source_port, path);
exit(1);
}
-
+}
+
+int pubsubclient_main(const char *source_address, int source_port, const char *path,
+ void (*message_cb)(char *data, void *arg),
+ void (*error_cb)(int status_code, void *arg),
+ void *cbarg)
+{
+ pubsubclient_init(source_address, source_port, path, message_cb, error_cb, cbarg);
event_dispatch();
-
+ pubsubclient_free();
+ return 0;
+}
+
+void pubsubclient_run()
+{
+ event_dispatch();
+}
+
+void pubsubclient_free()
+{
free_stream_request(autodetect_sr);
if (chunked) {
@@ -203,6 +223,4 @@ int pubsubclient_main(const char *source_address, int source_port, const char *p
}
free(data);
-
- return 0;
}
View
7 pubsubclient/pubsubclient.h
@@ -9,7 +9,13 @@ int pubsubclient_main(const char *source_address, int source_port, const char *p
void (*message_cb)(char *data, void *arg),
void (*error_cb)(int status_code, void *arg),
void *cbarg);
+void pubsubclient_init(const char *source_address, int source_port, const char *path,
+ void (*message_cb)(char *data, void *arg),
+ void (*error_cb)(int status_code, void *arg),
+ void *cbarg);
int pubsubclient_connect();
+void pubsubclient_run();
+void pubsubclient_free();
struct StreamRequest *new_stream_request(const char *method, const char *source_address, int source_port, const char *path,
void (*header_cb)(struct bufferevent *bev, struct evkeyvalq *headers, void *arg),
@@ -18,4 +24,5 @@ struct StreamRequest *new_stream_request(const char *method, const char *source_
void *arg);
void free_stream_request(struct StreamRequest *sr);
+
#endif
View
39 simplehttp/simplehttp.c
@@ -29,6 +29,9 @@ TAILQ_HEAD(, cb_entry) callbacks;
int simplehttp_logging = 0;
int callback_count = 0;
uint64_t request_count = 0;
+struct evhttp *httpd;
+extern struct event_base *current_base;
+
int help_cb(int *value);
@@ -146,7 +149,9 @@ void generic_request_handler(struct evhttp_request *req, void *arg)
void simplehttp_init()
{
- event_init();
+ if (!current_base) {
+ event_init();
+ }
TAILQ_INIT(&callbacks);
TAILQ_INIT(&simplehttp_reqs);
}
@@ -160,6 +165,8 @@ void simplehttp_free()
free(entry->path);
free(entry);
}
+ evhttp_free(httpd);
+ simplehttp_stats_destruct();
}
void simplehttp_set_cb(char *path, void (*cb)(struct evhttp_request *, struct evbuffer *, void *), void *ctx)
@@ -187,13 +194,12 @@ void define_simplehttp_options() {
option_define_str("group", OPT_OPTIONAL, NULL, NULL, NULL, "run as this group");
}
-int simplehttp_main()
+int simplehttp_listen()
{
uid_t uid = 0;
gid_t gid = 0;
pid_t pid, sid;
int errno;
- struct evhttp *httpd;
struct event pipe_ev;
char *address = option_get_str("address");
@@ -208,15 +214,15 @@ int simplehttp_main()
if (daemon) {
pid = fork();
if (pid < 0) {
- exit(EXIT_FAILURE);
+ return 0;
} else if (pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if (sid < 0) {
- exit(EXIT_FAILURE);
+ return 0;
}
}
@@ -280,21 +286,26 @@ int simplehttp_main()
httpd = evhttp_start(address, port);
if (!httpd) {
printf("could not bind to %s:%d\n", address, port);
- return 1;
+ return 0;
}
printf("listening on %s:%d\n", address, port);
evhttp_set_gencb(httpd, generic_request_handler, NULL);
+ return 1;
+}
+
+void simplehttp_run()
+{
event_dispatch();
-
- printf("exiting\n");
-
- evhttp_free(httpd);
-
- simplehttp_stats_destruct();
-
+}
+
+int simplehttp_main()
+{
+ if (!simplehttp_listen()) {
+ return 1;
+ }
+ simplehttp_run();
simplehttp_free();
-
return 0;
}
View
5 simplehttp/simplehttp.h
@@ -6,7 +6,7 @@
#include <event.h>
#include <evhttp.h>
-#define SIMPLEHTTP_VERSION "0.1.1"
+#define SIMPLEHTTP_VERSION "0.1.2"
#ifndef DUPE_N_TERMINATE
#define DUPE_N_TERMINATE(buf, len, tmp) \
tmp = malloc((len) + 1); \
@@ -42,6 +42,9 @@ struct simplehttp_stats {
void simplehttp_init();
int simplehttp_main();
+int simplehttp_listen();
+void simplehttp_run();
+void simplehttp_free();
void simplehttp_set_cb(char *path, void (*cb)(struct evhttp_request *, struct evbuffer *,void *), void *ctx);
uint64_t simplehttp_request_id(struct evhttp_request *req);

0 comments on commit c7f60cb

Please sign in to comment.
Something went wrong with that request. Please try again.