Exemplo n.º 1
0
HttpConnection::~HttpConnection()
{
  // Clean up this thread's connection now, rather than waiting for
  // pthread_exit.  This is to support use by single-threaded code
  // (e.g., UTs), where pthread_exit is never called.
  CURL* curl = pthread_getspecific(_curl_thread_local);
  if (curl != NULL)
  {
    pthread_setspecific(_curl_thread_local, NULL);
    cleanup_curl(curl); curl = NULL;
  }

  RandomUUIDGenerator* uuid_gen =
    (RandomUUIDGenerator*)pthread_getspecific(_uuid_thread_local);

  if (uuid_gen != NULL)
  {
    pthread_setspecific(_uuid_thread_local, NULL);
    cleanup_uuid(uuid_gen); uuid_gen = NULL;
  }

  if (_statistic != NULL)
  {
    delete _statistic;
    _statistic = NULL;
  }
}
Exemplo n.º 2
0
int login_cf(void)
{
	char post_str[BUFSIZE];
	char csrf[BUFSIZE];
	char tta[BUFSIZE];
	char filename[BUFSIZE];
	char url[] = "http://codeforces.com/enter";
	char *html = (char *)malloc(BUFSIZE * BUFSIZE);
	if (html == NULL) {
		write_log("alloc login_cf html buf memory error.\n");
		return -1;
	}

	if (get_csrf(url, csrf) < 0) {
		write_log("login_cf get csrf error.\n");
		free(html);
		return -1;
	}

	// important
	cleanup_curl();
	curl = prepare_curl();

	// 设置提交地址
	curl_easy_setopt(curl, CURLOPT_REFERER, url);
	curl_easy_setopt(curl, CURLOPT_URL, url);
	// 设置参数
	sprintf(filename, "%dlogin.txt", solution->solution_id);
	memset(tta, 0, sizeof(tta));
	if (get_tta(tta) < 0) {
		free(html);
		write_log("login_cf get tta error.\n");
		return -1;
	}
	sprintf(post_str, "csrf_token=%s&action=enter&handle=%s&password=%s"
			"&_tta=%s", csrf, vjudge_user, vjudge_passwd, tta);
	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str);
	if (DEBUG) {
		write_log("perform url is %s.\n", url);
		write_log("post data is %s.\n", post_str);
	}

	perform_curl(filename);
	load_file(filename, html);

	if (strstr(html, "Codeforces is temporary unavailable") != NULL
			|| strstr(html, "Invalid handle or password") != NULL
			|| strstr(html, "Fill in the form to login into Codeforces.") != NULL) {
		write_log("login_cf remote server error.\n");
		free(html);
		return -1;
	}

	free(html);
	return 0;
}
Exemplo n.º 3
0
int main(int argc, char **argv) {
    openni::Device &device = __device;
    openni::VideoStream &depth = __depth, &color = __color;
    openni::Status rc;

    char *prefix = strrchr(argv[0], '/')+1;
    char *cfgfile = new char[prefix-argv[0]+strlen(rgbdsend::config_file_name)+1];

    strncpy(cfgfile, argv[0], prefix-argv[0]+1);
    strcpy(cfgfile+(prefix-argv[0]), rgbdsend::config_file_name);

    Config conf;
    if(conf.read(cfgfile) != 1) {
        printf("Config: Falling back to builtin presets.\n");
        conf.setDefaults();
    }

    delete[] cfgfile;

    CURL *curl = init_curl();

    Daemon daemon;
    daemon.init(conf.daemon_port, conf.daemon_timeout);

    atexit(atexit_handler);

    init_openni(&device, &depth, &color, conf);

    int dw, dh, cw, ch;
    int tmp1, tmp2;

    if(!depth.getCropping(&tmp1, &tmp2, &dw, &dh)) {
        dw = depth.getVideoMode().getResolutionX();
        dh = depth.getVideoMode().getResolutionY();
    }

    if(!color.getCropping(&tmp1, &tmp2, &cw, &ch)) {
        cw = color.getVideoMode().getResolutionX();
        ch = color.getVideoMode().getResolutionY();
    }

    printf("Resolution:\nDepth: %dx%d @ %d fps\nColor: %dx%d @ %d fps\n",
           dw, dh, depth.getVideoMode().getFps(),
           cw, ch, color.getVideoMode().getFps());

    std::queue<char *> onilist;

    Command cmd;
    while(1) {
        timeval t;
        t.tv_sec = conf.daemon_timeout;
        t.tv_usec = 0;

        fd_set fds;
        FD_ZERO(&fds);
        FD_SET(daemon.sock, &fds);
        FD_SET(daemon.csock, &fds);

        int in = select((daemon.csock > daemon.sock ? daemon.csock : daemon.sock)+1, &fds, 0, 0, &t);

        if(FD_ISSET(daemon.sock, &fds))
            daemon.acceptConnection();

        if(FD_ISSET(daemon.csock, &fds)) {
            char b[5];
            int r = daemon.receiveCommand(&cmd);

            if(r == 0) {
                printf("Daemon Error: Could not receive command.\n");

                daemon.closeConnection();
                continue;
            }

            if(r == 2) // keep alive
                continue;

            if(strncmp(cmd.header, "capt", 4) == 0) {
                printf("Received capture command.\n");

                char *newfile = new char[rgbdsend::filename_bufsize];
                if(record_oni(newfile, rgbdsend::filename_bufsize, depth, color, conf) == true) {
                    onilist.push(newfile);
                } else {
                    delete[] newfile;
                }
                daemon.sendCommand("okay", 0, 0);
            } else if(strncmp(cmd.header, "thmb", 4) == 0) {
                printf("Received thumbnail command.\n");
                unsigned char *thumbbuf = NULL;
                long unsigned int size = 0;
                capture_thumbnail(&thumbbuf, &size, color);
                printf("Captured thumbnail. %ld bytes\n", size);

                daemon.sendCommand("stmb", thumbbuf, size);

//				delete[] thumbbuf; seems like libjpeg handles this. but I'm not sure.
            } else if(strncmp(cmd.header, "quit", 4) == 0) {
                daemon.closeConnection();
            } else {
                printf("Daemon Error: Received undefined command.\n");
            }
        }

        if(daemon.csock != -1 && in <= 0) {
            daemon.closeConnection();
        }

        if(daemon.csock == -1 && !onilist.empty())
            process_onis(onilist, curl, conf);
    }

    cleanup_curl(curl);
    cleanup_openni(device, depth, color);
}
Exemplo n.º 4
0
/*
 * Usage: ./get_problem oj_name from_problem_id(or index) to_problem_id(or index) [debug]
 */
int main(int argc, char *argv[])
{
	// read config file
	init_conf();

	if (argc < 4 || argc > 5) {
		fprintf(stderr, "Usage: %s oj_name from_problem_id(or index) "
				"to_problem_id(or indx) [debug]\n", argv[0]);
		fprintf(stderr, "Support oj is: ");
		int i = 0;
		for (i = 0; i < oj_cnt; ++i) {
			fprintf(stderr, "%s%c", oj_str[i],
					(i == oj_cnt - 1) ? '\n' : ' ');
		}
		exit(EXIT_SUCCESS);
	}

	DEBUG = (argc == 5);

	int i = 0;
	int from = atoi(argv[2]);
	int to = atoi(argv[3]);
	strcpy(oj_name, argv[1]);
	oj_type = -1;
	for (i = 0; i < oj_cnt; ++i) {
		if (strcmp(oj_name, oj_str[i]) == 0) {
			oj_type = i;
			break;
		}
	}

	if (oj_type < 0) {
		write_log("%s: unsupported oj\n", oj_name);
		exit(EXIT_FAILURE);
	}

	write_log("ojname = %s\n", oj_name);
	write_log("ojurl = %s\n", oj_url[oj_type]);
	write_log("type = %d\n", oj_type);
	write_log("from = %d\n", from);
	write_log("to = %d\n", to);

	curl = prepare_curl();
	if (curl == NULL) {
		write_log("prepare curl handle error.\n");
		exit(EXIT_FAILURE);
	}
	conn = prepare_mysql();
	if (conn == NULL) {
		write_log("prepare mysql handle error.\n");
		cleanup_curl();
		exit(EXIT_FAILURE);
	}
	problem_info = (struct problem_info_t *)malloc(sizeof(struct problem_info_t));
	if (problem_info == NULL) {
		write_log("alloc problem_info memory error.\n");
		cleanup_mysql();
		cleanup_curl();
		exit(EXIT_FAILURE);
	}

	if (oj_type == 2) {		// codeforces
		if (get_cf_problem_id() < 0) {
			write_log("get codeforces problem id error.\n");
			exit(EXIT_FAILURE);
		}
		if (to > cf_pid_len - 1) {
			to = cf_pid_len - 1;
			write_log("index is so large. set it to max index %d.\n", cf_pid_len - 1);
		}
	}

	for (pid = from; pid <= to; ++pid) {
		memset(problem_info, 0, sizeof(struct problem_info_t));
		int ret = get_problem();
		if (ret < 0) {
			write_log("get %s problem %d error.\n", oj_name, pid);
		} else if (ret == 0) {
			write_log("get %s problem %d success.\n", oj_name, pid);

			ret = add_problem();
			if (ret < 0) {
				write_log("add %s problem %d to mysql database error.\n", oj_name, pid);
			} else {
				if (!ret) {
					write_log("add %s problem %d to mysql database success.\n", oj_name, pid);
				} else {
					write_log("%s problem %d already exists, update local problem %d.\n", oj_name, pid, ret);
				}
			}
		} else {
			write_log("%s no problem %d.\n", oj_name, pid);
		}
		if (!DEBUG) {
			execute_cmd("rm -f %d", pid);
		}
		if (pid != to) {
			write_log("get next problem after %d seconds.\n", sleep_time);
		}
		sleep(sleep_time);
	}

	free(problem_info);
	cleanup_curl();
	cleanup_mysql();

	return 0;
}