Esempio n. 1
0
std::string engine::file_request(std::string server,
        const std::string& key,
        const std::string& user,
        const std::string& subject,
        const std::string& message,
        report_level s,
        validate_cert v)
{
    init_curl(key, s, v);
    server += "/requests";
    curl_easy_setopt(m_curl, CURLOPT_URL, server.c_str());
    curl_header_guard hg(m_curl);
    std::string data = std::string(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
  <request>\
    <recipient>") + user + std::string("</recipient>\
    <subject>") + subject + std::string("</subject>\
    <message>") + message + std::string("</message>\
    <send_email>true</send_email>\
");
    data += "</request>\n";
    curl_easy_setopt(m_curl, CURLOPT_HTTPPOST, 0);
    curl_easy_setopt(m_curl, CURLOPT_POSTFIELDS, data.c_str());
    if (s >= NORMAL) {
        io::mout << "Sending file request to user '" << user << "'" << io::endl;
    }
    return process_file_request_responce(perform(), s);
}
char*
mf_register_workflow(
    const char* URL,
    const char* workflow,
    const char* json_string)
{
    init_curl();
    char* response_message = (char *)malloc(sizeof(char) * 1024);
    memset(response_message, 0x00, 1024);

    const char* index = "v1/mf/users";
    char* newURL = (char *)malloc(sizeof(char) * (strlen(URL) + strlen(index) + strlen(workflow) + 4));
    sprintf(newURL, "%s/%s/%s", URL, index, workflow);

    curl_easy_setopt(curl, CURLOPT_URL, newURL);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, get_stream_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, response_message);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_string);

    CURLcode response = curl_easy_perform(curl);
    if (response != CURLE_OK) {
        const char *error_msg = curl_easy_strerror(response);
        log_error("mf_register_workflow(const char*) %s", error_msg);
    } else {
        debug("RESPONSE: %s", response_message);
    }
    curl_easy_reset(curl);
    free(newURL);

    return response_message;
}
Esempio n. 3
0
/* unis_curl_init : Init the curl context and init context
 * unis_config : Unis config to use
 * context : curl context
 */
int unis_curl_init(unis_config *config, curl_context **context){
	
	if(config == NULL){
		dbg_info(ERROR, "Invalid unis config \n");
		return -1;
	}

	if(config->endpoint == NULL){
		dbg_info(ERROR, "Invalid unis endpoint in unis config \n");
		return -1;
	}

	*context = malloc(sizeof(curl_context));
	
	(*context)->url = _strdup(config->endpoint);
	(*context)->use_ssl = config->use_ssl;
	(*context)->certfile = _strdup(config->certfile);
	(*context)->keyfile = _strdup(config->keyfile);
	(*context)->keypass = _strdup(config->keypass);
	(*context)->cacerts = _strdup(config->cacerts);
	(*context)->curl_persist = 0;
	(*context)->use_cookies = 0;
	(*context)->follow_redirect = 0;

	if (init_curl((*context), 0) != 0) {
		dbg_info(ERROR, "Could not start CURL context\n");
		free((*context));
		return -1;
    }
	
	return 1;
}
Esempio n. 4
0
std::string engine::filelink_attachment(std::string server,
            const std::string& key,
            const std::string& expire,
            const std::string& id,
            report_level s,
            validate_cert v)
{
    init_curl(key, s, v);
    return filelink_impl(server, expire, id, s);
}
Esempio n. 5
0
bool WebTools::UrlExists(const ssi_char_t *url, const ssi_char_t *pathToCertificate)
{	
	bool result = false;
	init_curl();

	CURL* curl = curl_easy_init();
	result = url_exists(curl, url, pathToCertificate);
	curl_easy_cleanup(curl);

	return result;
}
Esempio n. 6
0
std::string engine::filelink(std::string server,
            const std::string& key,
            const std::string& expire,
            const std::string& file,
            report_level s,
            validate_cert v)
{
    init_curl(key, s, v);
    std::string a = attach_impl(server, file, s);
    return filelink_impl(server, expire, a, s);
}
Esempio n. 7
0
void engine::attach(std::string server,
        const std::string& key,
        const strings& fs,
        report_level s,
        validate_cert v)
{
    init_curl(key, s, v);
    strings::const_iterator i = fs.begin();
    for (; i != fs.end(); ++i) {
        attach_impl(server, *i, s);
    }
}
Esempio n. 8
0
std::string engine::send_attachments(std::string server,
            const std::string& key,
            const std::string& user,
            const std::string& subject,
            const std::string& message,
            const strings& fs,
            report_level s,
            validate_cert v)
{
    init_curl(key, s, v);
    return send_attachments_impl(server, user, subject, message, fs, s);
}
static int
prepare_query(const char* URL)
{
    init_curl();

    curl_easy_setopt(curl, CURLOPT_URL, URL);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    #ifdef DEBUG
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    #endif

    return 1;
}
Esempio n. 10
0
std::string engine::message_impl(std::string server, const std::string& key,
        std::string id, report_level s, validate_cert v, std::string log)
{
    init_curl(key, s, v);
    server += "/message/";
    server += id;
    curl_easy_setopt(m_curl, CURLOPT_URL, server.c_str());
    curl_header_guard hg(m_curl);
    if (s >= NORMAL) {
        io::mout << log << io::endl;
    }
    return perform();
}
Esempio n. 11
0
void engine::download(const std::set<std::string>& urls,
        const std::string& key,
        const std::string& path,
        report_level s,
        validate_cert v)
{
    init_curl(key, s, v);
    std::set<std::string>::const_iterator i = urls.begin();
    curl_header_guard hg(m_curl);
    while (i != urls.end()) {
        std::string filename = get_filename(*i);
        download_impl(*i, path, filename, s);
        ++i;
    }
}
Esempio n. 12
0
static int
prepare_publish(const char *URL, const char *message)
{
    init_curl();

    curl_easy_setopt(curl, CURLOPT_URL, URL);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long ) strlen(message));
    #ifdef DEBUG
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    #endif

    return 1;
}
Esempio n. 13
0
std::string engine::get_filedrop_api_key(const std::string& url, report_level s, validate_cert v)
{
    init_curl("", s, v);
    curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
    curl_header_guard hg(m_curl);
    if (s >= VERBOSE) {
        io::mout << "Getting filedrop API key" << io::endl;
    }
    std::string r = perform();
    xml::document<> d;
    d.parse<xml::parse_fastest | xml::parse_no_utf8>(const_cast<char*>(r.c_str()));
    if (d.first_node() == 0) {
        throw request_error("filedrop info", r);
    }
    std::string h(d.first_node()->name(), d.first_node()->name_size());
    xml::node_iterator<> i(d.first_node());
    xml::node_iterator<> e;
    if (h == "error") {
        while(i != e) {
            std::string n(i->name(), i->name_size());
            if (n == "message") {
                std::string m = std::string(i->value(), i->value_size());
                throw request_error("filedrop info", m);
                break;
            }
            ++i;
        }
        throw request_error("filedrop info", r);
    }
    std::string q;
    while(i != e) {
        std::string n(i->name(), i->name_size());
        if (n == "api_key") {
            q = std::string(i->value(), i->value_size());
            if (s >= VERBOSE) {
                io::mout << "Got filedrop API key: " << q << io::endl;
            }
            break;
        }
        ++i;
    }
    if (q.empty()) {
        throw request_error("filedrop info", r);
    }
    return q;
}
Esempio n. 14
0
bool WebTools::DownloadFile(const ssi_char_t *url, const ssi_char_t *pathOnDisk, const ssi_char_t *pathToCertificate, bool showProgress)
{
	bool result = false;
	init_curl();

	CURL *curl = curl_easy_init();

	if (url_exists(curl, url, pathToCertificate))
	{
		curl_easy_reset(curl);

		init_request(curl, url, pathToCertificate, showProgress);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

		FILE *file = fopen(pathOnDisk, "wb");
		if (file) {

			curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
			CURLcode code = curl_easy_perform(curl);
			fclose(file);

			if (code != CURLE_OK)
			{
				ssi_wrn(curl_easy_strerror(code));
				ssi_remove(pathOnDisk);
			}
			else
			{
				result = true;
			}
		}
		else
		{
			ssi_wrn("could not create file '%s'", pathOnDisk);
		}
	}
	else
	{
		ssi_wrn("url does not exist '%s'", url);
	}

	curl_easy_cleanup(curl);

	return result;
}
Esempio n. 15
0
EXPORT char* ccurl_pow(char* trytes, int minWeightMagnitude) {
  char* buf = NULL; //= malloc(sizeof(char)*TRYTE_LENGTH);
  size_t len = strnlen(trytes, TRANSACTION_LENGTH/3);
  char* trits = trits_from_trytes(trytes, len);
  pdcl_node_t* pd_node = &base;

  ccurl_pow_node_init(pd_node);
  while (pd_node->pdcl->pd.status == PD_SEARCHING) {
    if (pd_node->next != NULL) {
      pd_node = pd_node->next;
    }
  }

  curl_t curl;
  init_curl(&curl);
  absorb(&curl, trits, TRANSACTION_LENGTH - HASH_LENGTH);
  memcpy(&curl.state, &trits[TRANSACTION_LENGTH - HASH_LENGTH], HASH_LENGTH * sizeof(char));

  if (ccurl_pow_node_init(pd_node) == 0) {
    if (pd_node->pdcl->loop_count < 1) {
      pd_node->pdcl->loop_count = loop_count;
    }
#ifdef DEBUG
    fprintf(stderr, "OpenCL Hashing with %lu loops...\n", pd_node->pdcl->loop_count);
#endif
    pearcl_search(pd_node->pdcl, &curl, offset, minWeightMagnitude);
  }
  if (pd_node->pdcl->pd.status != PD_FOUND &&
      pd_node->pdcl->pd.status != PD_INVALID &&
      pd_node->pdcl->pd.status != PD_INTERRUPTED) {
#ifdef DEBUG
    fprintf(stderr, "Thread Hashing...\n");
#endif
    pd_search(&(pd_node->pdcl->pd), &curl, minWeightMagnitude, -1);
  }
  if (pd_node->pdcl->pd.status == PD_FOUND) {
	memcpy(&trits[TRANSACTION_LENGTH - HASH_LENGTH], &curl.state, HASH_LENGTH * sizeof(char));
    buf = trytes_from_trits(trits, 0, TRANSACTION_LENGTH);
  }

  free(trits);
  pd_node->pdcl->pd.status = PD_FINISHED;
  return buf;
}
Esempio n. 16
0
static void test_search(void) {
  PearCLDiver pdcl;
  Curl curl;
  clock_t start, diff;
  int nonce_size = 13;
  char *digest, *trans;

  char *mytrits, hash_trits[HASH_LENGTH];

  if (init_pearcl(&pdcl) != 0) {
    CU_FAIL("E: Could not initialize opencl\n");
    return;
  }
  init_curl(&curl);

  mytrits = trits_from_trytes(real_transaction, TRYTE_LENGTH);

  // puts(trytes_from_trits(mytrits+TRANSACTION_LENGTH-HASH_LENGTH, 0,
  // HASH_LENGTH));

  while (nonce_size < 19) {
    fprintf(stderr, "Testing mwm of %d: ", nonce_size);
    start = clock();
    pearcl_search(&pdcl, mytrits, TRANSACTION_LENGTH, nonce_size, -1);
    diff = clock() - start;

    // printf("I took this many seconds: %ld", diff / CLOCKS_PER_SEC);
    trans = trytes_from_trits(mytrits, 0, TRANSACTION_LENGTH);
    // hash = trytes_from_trits(mytrits + TRANSACTION_LENGTH - HASH_LENGTH, 0,
    // HASH_LENGTH);

    absorb(&curl, mytrits, 0, TRANSACTION_LENGTH);
    squeeze(&curl, hash_trits, 0, HASH_LENGTH);
    reset(&curl);
    digest = trytes_from_trits(hash_trits, 0, HASH_LENGTH);

    // puts(trans);
    puts(digest);
    CU_ASSERT_FATAL(test_last_n_nines(digest, HASH_LENGTH / 3, nonce_size / 3));
    nonce_size++;
  }
  free(mytrits);
  free(digest);
}
Esempio n. 17
0
std::string engine::send(std::string server,
        const std::string& key,
        const std::string& user,
        const std::string& subject,
        const std::string& message,
        const strings& fs,
        report_level s,
        validate_cert v)
{
    init_curl(key, s, v);
    std::set<std::string> attachments;
    strings::const_iterator i = fs.begin();
    for (; i != fs.end(); ++i) {
        std::string a = attach_impl(server, *i, s);
        attachments.insert(a);
    }
    return send_attachments_impl(server, user, subject, message,
            attachments, s);
}
Esempio n. 18
0
std::string engine::messages_impl(std::string server, const std::string& key, std::string l,
        std::string f, report_level s, validate_cert v)
{
    init_curl(key, s, v);
    server += "/message";
    if (!l.empty()) {
        server += "?sent_in_the_last=";
        server += l;
    } else if (!f.empty()) {
        server += "?sent_after=";
        server += f;
    }
    curl_easy_setopt(m_curl, CURLOPT_URL, server.c_str());
    curl_header_guard hg(m_curl);
    if (s >= NORMAL) {
        io::mout << "Getting messages from the server." << io::endl;
    }
    return perform();
}
Esempio n. 19
0
char* mf_create_user(
  const char* server,
  const char* username,
  const char* experiment_id,
  const char* message)
{
    init_curl();

    const char* resource = "v1/mf/users";
    char* URL;

    if (experiment_id == NULL || experiment_id == '\0') {
        URL = (char *)malloc(
            sizeof(char) *
            (strlen(server) +
            strlen(resource) +
            strlen(username) +
            10)
        );
        sprintf(URL, "%s/%s/%s/create", server, resource, username);
    } else {
        URL = (char *)malloc(
            sizeof(char) *
            (strlen(server) +
             strlen(resource) +
             strlen(username) +
             strlen(experiment_id) +
            12)
        );
        sprintf(URL, "%s/%s/%s/%s/create", server, resource, username, experiment_id);
    }

    char* json = (char *)malloc(sizeof(char) * 128 + strlen(username));

    /* include message as body */
    if (message == NULL || message[0] == '\0') {
        sprintf(json, "{ \"user\": \"%s\" }", username);
    } else {
        json = strdup(message);
    }

    return publish_json(URL, json);
}
Esempio n. 20
0
void engine::delete_attachments(std::string server,
            const std::string& key,
            const std::string& id,
            report_level s,
            validate_cert v)
{
    init_curl(key, s, v);
    server += "/message/";
    server += id;
    server += "/delete_attachments";
    curl_easy_setopt(m_curl, CURLOPT_URL, server.c_str());
    curl_header_guard hg(m_curl);
    if (s >= NORMAL) {
        io::mout << "Deleting attachments of the message." << io::endl;
    }
    perform();
    if (s >= NORMAL) {
        io::mout << "Deleted attachments successfully." << io::endl;
    }
}
Esempio n. 21
0
void engine::filelinks(std::string server,
            const std::string& key,
            const std::string& limit,
            output_format of,
            report_level s,
            validate_cert v)
{
    init_curl(key, s, v);
    server += "/link";
    if (!limit.empty()) {
        server += "?limit=";
        server += limit;
    }
    curl_easy_setopt(m_curl, CURLOPT_URL, server.c_str());
    curl_header_guard hg(m_curl);
    if (s >= NORMAL) {
        io::mout << "Getting filelinks from the server." << io::endl;
    }
    process_output_responce<filelinks_responce>(perform(), s, of);
}
Esempio n. 22
0
void engine::attach(std::string server,
        const std::string& key,
        const std::string& file,
        const std::string& filename,
        int chunk_id,
        int num_chunks,
        report_level s,
        validate_cert v)
{
    init_curl(key, s, v);
    server += "/attachments";
    curl_easy_setopt(m_curl, CURLOPT_URL, server.c_str());
    struct curl_httppost* formpost = NULL;
    struct curl_httppost* lastptr = NULL;
    curl_formadd(&formpost,
            &lastptr,
            CURLFORM_COPYNAME, "Filedata",
            CURLFORM_FILE, file.c_str(),
            CURLFORM_END);
    curl_formadd(&formpost,
            &lastptr,
            CURLFORM_COPYNAME, "name",
            CURLFORM_COPYCONTENTS, filename.c_str(),
            CURLFORM_END);
    curl_formadd(&formpost,
            &lastptr,
            CURLFORM_COPYNAME, "chunk",
            CURLFORM_COPYCONTENTS, base::to_string(chunk_id).c_str(),
            CURLFORM_END);
    curl_formadd(&formpost,
            &lastptr,
            CURLFORM_COPYNAME, "chunks",
            CURLFORM_COPYCONTENTS, base::to_string(num_chunks).c_str(),
            CURLFORM_END);
    curl_form_guard fg(formpost);
    curl_easy_setopt(m_curl, CURLOPT_HTTPPOST, formpost);
    if (s >= NORMAL) {
        io::mout << "Uploading chunk '" << file << "'." << io::endl;
    }
    process_attach_chunk_responce(perform(), s);
}
Esempio n. 23
0
void engine::delete_filelink(std::string server,
            const std::string& key,
            const std::string& id,
            report_level s,
            validate_cert v)
{
    init_curl(key, s, v);
    server += "/link/";
    server += id;
    curl_easy_setopt(m_curl, CURLOPT_URL, server.c_str());
    curl_header_guard hg(m_curl);
    curl_easy_setopt(m_curl, CURLOPT_CUSTOMREQUEST, "DELETE");
    if (s >= NORMAL) {
        io::mout << "Deleting filelink with id '" << id << "'" << io::endl;
    }
    std::string r = perform();
    if (r.find_first_not_of(' ') != r.npos) {
        throw request_error("delete_filelink", r);
    }
    io::mout << "Filelink deleted successfully." << io::endl;
}
Esempio n. 24
0
int
mf_head(const char* URL)
{
    init_curl();

    curl_easy_setopt(curl, CURLOPT_URL, URL);
    curl_easy_setopt(curl, CURLOPT_HEADER, 1);
    curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
    curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);

    CURLcode response = curl_easy_perform(curl);
    curl_easy_reset(curl);

    if (response != CURLE_OK) {
        const char *error_msg = curl_easy_strerror(response);
        log_error("mf_head(const char*) %s", error_msg);
        return 0;
    }

    return 1;
}
Esempio n. 25
0
void engine::filedrop(std::string server,
        const std::string& user,
        const std::string& subject,
        const std::string& message,
        const strings& fs,
        report_level s,
        validate_cert v)
{
    std::string key = get_filedrop_api_key(server, s, v);
    init_curl(key, s, v);
    strings::const_iterator i = fs.begin();
    strings rs;
    std::string url = get_server_from_filedrop(server);
    for (; i != fs.end(); ++i) {
        std::string id = attach_impl(url, *i, s);
        rs.insert(id);
    }
    curl_easy_setopt(m_curl, CURLOPT_USERPWD, "");
    filedrop_attachments_impl(server, key, user, subject, message,
            rs, s);
}
Esempio n. 26
0
void engine::delete_attachments(std::string server,
            const std::string& key,
            const std::set<std::string>& ids,
            report_level s,
            validate_cert v)
{
    init_curl(key, s, v);
    server += "/attachment/";
    curl_easy_setopt(m_curl, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_header_guard hg(m_curl);
    std::set<std::string>::const_iterator i = ids.begin();
    for (; i != ids.end(); ++i) {
        std::string x = server + (*i);
        curl_easy_setopt(m_curl, CURLOPT_URL, x.c_str());
        if (s >= NORMAL) {
            io::mout << "Deleting attachment '" << *i << "'" << io::endl;
        }
        perform();
        if (s >= NORMAL) {
            io::mout << "Deleted successfully." << io::endl;
        }
    }
}
Esempio n. 27
0
static void *
http_client_main( void *args ) {
  UNUSED( args );

  debug( "Starting HTTP client thread ( tid = %#lx ).", pthread_self() );

  init_curl();

  while ( 1 ) {
    int n_msgs = 0;
    CURLMsg *msg = curl_multi_info_read( curl_handle, &n_msgs );
    while ( msg != NULL ) {
      if ( msg->msg != CURLMSG_DONE ) {
        continue;
      }
      http_transaction *transaction = lookup_http_transaction( msg->easy_handle );
      if ( transaction != NULL ) {
        send_http_transaction_to_main( transaction, msg );
      }
      else {
        warn( "Failed to find HTTP transaction record ( easy_handle = %p ).", msg->easy_handle );
      }
      msg = curl_multi_info_read( curl_handle, &n_msgs );
    }

    fd_set fd_read;
    FD_ZERO( &fd_read );
    fd_set fd_write;
    FD_ZERO( &fd_write );
    fd_set fd_excep;
    FD_ZERO( &fd_excep );

    int fd_max = -1;
    long curl_timeout = -1;
     struct timespec timeout = { 10, 0 };
    if ( curl_running ) {
      CURLMcode retval = curl_multi_timeout( curl_handle, &curl_timeout );
      if ( retval != CURLM_OK && retval != CURLM_CALL_MULTI_PERFORM ) {
        error( "Failed to get timeout value from a handle ( handle = %p, error = %s ).", curl_handle, curl_multi_strerror( retval ) );
      }
      if ( curl_timeout >= 0 ) {
        timeout.tv_sec = curl_timeout / 1000;
        if ( timeout.tv_sec > 1 ) {
          timeout.tv_sec = 1;
        }
        else {
          timeout.tv_nsec = ( curl_timeout % 1000 ) * 1000000;
        }
      }
 
      retval = curl_multi_fdset( curl_handle, &fd_read, &fd_write, &fd_excep, &fd_max );
      if ( retval != CURLM_OK && retval != CURLM_CALL_MULTI_PERFORM ) {
        error( "Failed to retrieve fd set from a handle ( handle = %p, error = %s ).", curl_handle, curl_multi_strerror( retval ) );
      }
    }

    if ( http_client_efd >= 0 ) {
#define sizeof( _x ) ( ( int ) sizeof( _x ) )
      FD_SET( http_client_efd, &fd_read );
#undef sizeof
      if ( http_client_efd > fd_max ) {
        fd_max = http_client_efd;
      }
    }
    if ( main_efd >= 0 && main_notify_count > 0 ) {
#define sizeof( _x ) ( ( int ) sizeof( _x ) )
      FD_SET( main_efd, &fd_write );
#undef sizeof
      if ( main_efd > fd_max ) {
        fd_max = main_efd;
      }
    }

    int ret = pselect( fd_max + 1, &fd_read, &fd_write, &fd_excep, &timeout, NULL );
    switch ( ret ) {
      case -1:
      {
        if ( errno == EINTR ) {
          continue;
        }
        char buf[ 256 ];
        memset( buf, '\0', sizeof( buf ) );
        char *error_string = strerror_r( errno, buf, sizeof( buf ) - 1 );
        warn( "Failed to select ( errno = %s [%d] ).", error_string, errno );
      }
      break;

      case 0:
      default:
      {
        if ( curl_running ) {
          CURLMcode retval = curl_multi_perform( curl_handle, &curl_running );
          if ( retval != CURLM_OK && retval != CURLM_CALL_MULTI_PERFORM ) {
            error( "Failed to run a multi handle ( handle = %p, error = %s ).", curl_handle, curl_multi_strerror( retval ) );
          }
        }
#define sizeof( _x ) ( ( int ) sizeof( _x ) )
        if ( http_client_efd >= 0 && FD_ISSET( http_client_efd, &fd_read ) ) {
          retrieve_http_transactions_from_main();
        }
        if ( main_efd >= 0 && FD_ISSET( main_efd, &fd_write ) ) {
          notify_main_actually();
        }
#undef sizeof
      }
      break;
    }
  }

  finalize_curl();

  debug( "Stopping HTTP client thread ( tid = %#lx ).", pthread_self() );

  return NULL;
}
Esempio n. 28
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);
}
Esempio n. 29
0
int main(int argc, char *argv[]) {
	char data[1000] = "";
	char moredata[1000] = "";
	char url[255] = "";

	init_curl();

	//init_curl(events_callback, exitEventsCallback);

	if (strncmp(argv[1], "XML", 3) == 0) {
		if (argc != 3) {
			printf("wrong number of args for get xml\n");
			exit - 1;
		}
		//printf("%s\n", url);
		strcat(url, argv[2]);
		strcat(url, "?x=1");
		char *get_data = http_get_function(url);
		//char *value = get_value_from_xml_value(get_data);
		//printf("%s\n", value);
		printf("%s\n", get_data);

	} else if (strncmp(argv[1], "GET", 3) == 0) {
		if (argc != 3) {
			printf("wrong number of args for get\n");
			exit - 1;
		}

		if (strncmp(argv[2], "FILE", 4) == 0) {
			load_file_into_buffer(url, "mainSD-OUTPUT.url");
		} else
			strcpy(url, argv[2]);

		//printf("%s\n", url);
		char *get_data = http_get_function(url);
		char *value = get_value_from_xml_value(get_data);
		//		printf("%s\n", value);
		//printf("%s\n", get_data);

	} else if (strncmp(argv[1], "PUT", 5) == 0) {

		if (argc != 4) {
			printf("wrong number of args for put\n");
			exit - 1;
		}

		if (strncmp(argv[2], "FILE", 4) == 0) {
			load_file_into_buffer(url, "mainSD-OUTPUT.url");
		} else
			strcpy(url, argv[2]);

		strcpy(data, argv[3]);

		char * xml = get_xml_value(data); //

		//xml = http_put_function(url, xml);// "<?xml ?><value> 29000</value>"
		//strcpy(data, xml); "   //  /Bridge/Livingroom/switch/0"
		char ipaddr[20] = "";
		char port[20] = "";
		char page[250] = "";

		spliturl(url, ipaddr, port, page);

		//	    printf("ip = \"%s\"\n", ipaddr);
		//	    printf("port = \"%s\"\n", port);
		//	    printf("page = \"%s\"\n", page);

		httpput(ipaddr, port, page, xml, moredata);
		//free(xml);

		char *value = get_value_from_xml_value(moredata);

		printf("%s\n", value);

	} else
		printf("Command not supported\n");

	return 0;
}