Exemplo n.º 1
0
	void init_shogun(void (*print_message)(FILE* target, const char* str),
			void (*print_warning)(FILE* target, const char* str),
			void (*print_error)(FILE* target, const char* str),
			void (*cancel_computations)(bool &delayed, bool &immediately))
	{
		if (!sg_io)
			sg_io = new shogun::SGIO();
		if (!sg_parallel)
			sg_parallel=new shogun::Parallel();
		if (!sg_version)
			sg_version = new shogun::Version();
		if (!sg_math)
			sg_math = new shogun::CMath();
		if (!sg_rand)
			sg_rand = new shogun::CRandom();
#ifdef TRACE_MEMORY_ALLOCS
		if (!sg_mallocs)
			sg_mallocs = new shogun::CMap<void*, MemoryBlock>(631, 1024, false);

		SG_REF(sg_mallocs);
#endif
		SG_REF(sg_io);
		SG_REF(sg_parallel);
		SG_REF(sg_version);
		SG_REF(sg_math);
		SG_REF(sg_rand);

		sg_print_message=print_message;
		sg_print_warning=print_warning;
		sg_print_error=print_error;
		sg_cancel_computations=cancel_computations;
		
		init_from_env();
	}
int main(int argc, char ** argv)
{
    int ret;
    struct MHD_Daemon *daemon;
    int opt;
    riemann_client_t riemann_client;
    struct sockaddr_in localhost_addr;

    init_from_env();
    while ((opt = getopt(argc, argv, "l:r:p:s:h:m:")) != -1) {
        switch (opt) {
        case 'l':
            listen_port = atoi(optarg);
            break;
        case 'r':
            riemann_host = strdup(optarg);
            break;
        case 'p':
            riemann_port = atoi(optarg);
            break;
        case 's':
            riemann_field_service = strdup(optarg);
            break;
        case 'h':
            riemann_field_host = strdup(optarg);
            break;
        case 'm':
            maintainance_page = strdup(optarg);
            break;
        default:
            print_help();
            exit(EXIT_FAILURE);
        }
    }

    if (!listen_port || !riemann_host || !riemann_field_host) {
        if (!listen_port) {
            fprintf(stderr, "port to listen on is not specified (LISTEN_PORT or -l)\n");
        }
        if (!riemann_host) {
            fprintf(stderr, "riemann host is not specified (RIEMANN_HOST or -r)\n");
        }
        if (!riemann_field_host) {
            fprintf(stderr, "host field for riemann payload is not specified (RIEMANN_FIELD_HOST or -h)\n");
        }
        print_help();
        exit(EXIT_FAILURE);
    }
    if (maintainance_page) {
        int ret;
        int fd;
        struct stat stat;

        fd = open(maintainance_page, O_RDONLY);
        if (fd == -1) {
            fprintf(stderr, "Cannot open maintainance page '%s': strerror(%s)\n",
                    maintainance_page, strerror(errno));
            exit(EXIT_FAILURE);
        }
        ret = fstat(fd, &stat);
        if (ret != 0) {
            fprintf(stderr, "Cannot get size of maintainance page '%s': strerror(%s)\n",
                    maintainance_page, strerror(errno));
            close(fd);
            exit(EXIT_FAILURE);
        }
        body_len = stat.st_size;
        body = calloc(body_len, sizeof(char));
        if (body == NULL) {
            fprintf(stderr, "Cannot allocate enough memory for content of maintainance page '%s'\n",
                    maintainance_page);
            close(fd);
            exit(EXIT_FAILURE);
        }
        ret = read(fd, body, body_len);
        if (ret != body_len) {
            fprintf(stderr, "Error while reading maintainance page content '%s': strerror(%s)\n",
                    maintainance_page, strerror(errno));
            close(fd);
            exit(EXIT_FAILURE);
        }
    }
    ret = riemann_client_init(&riemann_client);
    if (ret) {
        fprintf(stderr, "Cannot initialize riemann_client: strerror(%s)\n", strerror(errno));
        exit(EXIT_FAILURE);
    }
    ret = riemann_client_connect(&riemann_client, RIEMANN_TCP, riemann_host, riemann_port);
    if (ret) {
        fprintf(stderr, "Cannot connect: strerror(%s) gai_strerrror(%s)\n", strerror(errno), gai_strerror(ret));
        exit(EXIT_FAILURE);
    }

    memset(&localhost_addr, 0, sizeof(struct sockaddr_in));
    localhost_addr.sin_family = AF_INET;
    localhost_addr.sin_port = htons(listen_port);
    ret = inet_pton(AF_INET, "127.0.0.1", &localhost_addr.sin_addr);
    if (ret != 1) {
        fprintf(stderr, "Cannot construct address structure: strerror(%s)\n", strerror(errno));
        exit(EXIT_FAILURE);
    }
    daemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL,
                              listen_port, NULL, NULL,
                              &write_to_riemann, &riemann_client,
                              MHD_OPTION_SOCK_ADDR, &localhost_addr,
                              MHD_OPTION_END);
    if (daemon == NULL) {
        fprintf(stderr, "Cannot start HTTP daemon\n");
        exit(EXIT_FAILURE);
    }
    wait();
    MHD_stop_daemon(daemon);
    riemann_client_free(&riemann_client);
    return 0;
}