Example #1
0
static void test_connected(struct knot_requestor *requestor, conf_remote_t *remote)
{
	/* Enqueue packet. */
	int ret = knot_requestor_enqueue(requestor, make_query(requestor, remote));
	is_int(KNOT_EOK, ret, "requestor: connected/enqueue");

	/* Wait for completion. */
	struct timeval tv = { 5, 0 };
	ret = knot_requestor_exec(requestor, &tv);
	is_int(KNOT_EOK, ret, "requestor: connected/wait");

	/* Enqueue multiple queries. */
	ret = KNOT_EOK;
	for (unsigned i = 0; i < 10; ++i) {
		ret |= knot_requestor_enqueue(requestor, make_query(requestor, remote));
	}
	is_int(KNOT_EOK, ret, "requestor: multiple enqueue");

	/* Wait for multiple queries. */
	ret = KNOT_EOK;
	for (unsigned i = 0; i < 10; ++i) {
		struct timeval tv = { 5, 0 };
		ret |= knot_requestor_exec(requestor, &tv);
	}
	is_int(KNOT_EOK, ret, "requestor: multiple wait");
}
Example #2
0
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cerr << "usage: " << argv[0] << " query" << std::endl;
        std::cerr << "e.g.: http://ya.ru" << std::endl;
        return 0;
    }
    
    try
    {
        std::shared_ptr<HttpQuery> q = make_query(argv[1]);
        Socket s;
        s.connect(q->host, 80);
        s.setRcvTimeout(/*sec*/2, /*microsec*/0);

        s.httpQuery(q->query, [](const std::string &resp)->void
        {
            std::string::size_type pos = resp.find("\r\n\r\n");
            if (pos == std::string::npos)
                throw std::runtime_error("exception: error getting headers");
            std::cerr << "headers:" << std::endl << resp.substr(0, pos) << std::endl;
        });
    }
    catch(const std::exception &e)
    {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}
static void find_one( const char *ns ) {
    bson b;
    int i;
    for ( i=0; i < PER_TRIAL; i++ ) {
        make_query( &b );
        ASSERT( mongo_find_one( conn, ns, &b, NULL, NULL ) == MONGO_OK );
        bson_destroy( &b );
    }
}
Example #4
0
static void test_disconnected(struct knot_requestor *requestor, conf_remote_t *remote)
{
	/* Enqueue packet. */
	int ret = knot_requestor_enqueue(requestor, make_query(requestor, remote));
	is_int(KNOT_EOK, ret, "requestor: disconnected/enqueue");

	/* Wait for completion. */
	struct timeval tv = { 5, 0 };
	ret = knot_requestor_exec(requestor, &tv);
	is_int(KNOT_ECONN, ret, "requestor: disconnected/wait");
}
Example #5
0
void Querytextedit::keyPressEvent(QKeyEvent* e) {
			 //(e->modifiers()==Qt::ShiftModifier) &&
	if(e->key()==Qt::Key_F9)
		//if(this->einzeln->isChecked())
			make_query(this->toPlainText());

	if(e->key()==Qt::Key_PageUp)
		hist_up();
	if(e->key()==Qt::Key_PageDown)
		hist_down();
	 QTextEdit::keyPressEvent(e);
}
static void find( const char *ns ) {
    bson b;
    int i;
    for ( i=0; i < PER_TRIAL; i++ ) {
        mongo_cursor *cursor;
        make_query( &b );
        cursor = mongo_find( conn, ns, &b, NULL, 0,0,0 );
        ASSERT( cursor );

        while( mongo_cursor_next( cursor ) == MONGO_OK )
            {}

        mongo_cursor_destroy( cursor );
        bson_destroy( &b );
    }
}
Example #7
0
void Backtester::run_one_step()
{
    MatrixQuery         dq = make_query();  // Get Available Data
    Row                 lp = last_price();  // Get Last price

    // Making public data available
    DataStruct ds(_price_matrix);

          ds.data_manager = &dq;
          //ds.predictors  = &this->_GlobalPredictors;
          //ds.securities  = this->_SecurityDatabase;

    const TransactionWeight*  w;

    Eigen::IOFormat fmt;

    for(Index k = 0, n = _strategies.size(); k < n; k++)
    {
       if (should_run(k))
       {
           // Compute Target owning
           w = (*get_strategy(k))(ds);

           // log current target
           log_weights(title(k), *w);

           // Compute effective Buy/Sell
           Matrix orders = get_portfolio(k)->transaction_weight(_time, lp, *w);

           log_orders(title(k), orders);

           // update portfolio state
           get_portfolio(k)->transaction_answer(orders, lp);

           // Log current holdings
           log_portfolio_state(title(k), get_portfolio(k)->state());
       }

       MPortfolio& p = get_portfolio(k);

       // log current portfolio value
       log_portfolio_values(title(k), p->invested(lp), p->cash(), p->liability(lp));
    }

    // one period has passed
    _time++;
}
Example #8
0
static void parse_newstyle_args(PgqTriggerEvent *ev, TriggerData *tg)
{
	int i;

	/*
	 * parse args
	 */
	for (i = 1; i < tg->tg_trigger->tgnargs; i++) {
		const char *arg = tg->tg_trigger->tgargs[i];
		if (strcmp(arg, "SKIP") == 0)
			ev->tgargs->skip = true;
		else if (strncmp(arg, "ignore=", 7) == 0)
			ev->tgargs->ignore_list = MemoryContextStrdup(tbl_cache_ctx, arg + 7);
		else if (strncmp(arg, "pkey=", 5) == 0)
			ev->tgargs->pkey_list = MemoryContextStrdup(tbl_cache_ctx, arg + 5);
		else if (strcmp(arg, "backup") == 0)
			ev->tgargs->backup = true;
		else if (strcmp(arg, "deny") == 0)
			ev->tgargs->deny = true;
		else if (strncmp(arg, "ev_extra4=", 10) == 0)
			make_query(ev, EV_EXTRA4, arg + 10);
		else if (strncmp(arg, "ev_extra3=", 10) == 0)
			make_query(ev, EV_EXTRA3, arg + 10);
		else if (strncmp(arg, "ev_extra2=", 10) == 0)
			make_query(ev, EV_EXTRA2, arg + 10);
		else if (strncmp(arg, "ev_extra1=", 10) == 0)
			make_query(ev, EV_EXTRA1, arg + 10);
		else if (strncmp(arg, "ev_data=", 8) == 0)
			make_query(ev, EV_DATA, arg + 8);
		else if (strncmp(arg, "ev_type=", 8) == 0)
			make_query(ev, EV_TYPE, arg + 8);
		else if (strncmp(arg, "when=", 5) == 0)
			make_query(ev, EV_WHEN, arg + 5);
		else
			elog(ERROR, "bad param to pgq trigger");
	}

	if (ev->op_type == 'R') {
		if (ev->tgargs->ignore_list)
			elog(ERROR, "Column ignore does not make sense for truncate trigger");
		if (ev->tgargs->pkey_list)
			elog(ERROR, "Custom pkey_list does not make sense for truncate trigger");
		if (ev->tgargs->backup)
			elog(ERROR, "Backup does not make sense for truncate trigger");
	}
}
/** try 3 times to get an EDNS reply from the server, exponential backoff */
static int
get_packet(struct sockaddr_storage* addr, socklen_t len, char* nm, int tp,
	uint8_t **wire, size_t* wlen)
{
	struct timeval t;
	ldns_buffer* qbin;
	ldns_status s;
	int tries = 0;

	memset(&t, 0, sizeof(t));
	t.tv_usec = 100 * 1000; /* 100 milliseconds (then 200, 400, 800) */

	qbin = make_query(nm, tp);
	if(!qbin)
		return 0;
	while(tries < 4) {
		tries ++;
		s = ldns_udp_send(wire, qbin, addr, len, t, wlen);
		if(s != LDNS_STATUS_NETWORK_ERR) {
			break;
		}
		t.tv_usec *= 2;
		if(t.tv_usec > 1000*1000) {
			t.tv_usec -= 1000*1000;
			t.tv_sec += 1;
		}
	}
	ldns_buffer_free(qbin);
	if(tries == 4) {
		if(verb) printf("timeout\n");
		return 0;
	}
	if(s != LDNS_STATUS_OK) {
		if(verb) printf("error: %s\n", ldns_get_errorstr_by_id(s));
		return 0;
	}
	return 1;
}
Example #10
0
int check_dns_response(char *host) {
    
    int                sockfd, ret;
    struct sockaddr_in dest_addr;
    dns_question       q;
    char *             service;
    short int          qclass = QCLASS_IN;
    short int          qtype  = QTYPE_A;
    short int          flags  = 0x0100;
    struct timeval     tv;

    tv.tv_sec = 3; //3sec timeout for DNS response
    tv.tv_usec = 0;

    service = "google.be";
        
    sockfd = socket( AF_INET, SOCK_DGRAM, 0 );
    if (sockfd <= 0) {
        return -1;
    }

    setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));;

    dest_addr.sin_family = AF_INET;
    dest_addr.sin_port   = htons(53);
    dest_addr.sin_addr.s_addr = inet_addr(host);
    memset(&(dest_addr.sin_zero), '\0', 8);
    q.qtype = qtype;
    q.qclass = qclass;
    q.qname = service;
    make_query(sockfd, flags, dest_addr, q);
    ret = read_responce(sockfd, dest_addr);
    close(sockfd);
    
    return ret;
}
Example #11
0
void main(int argc, char *argv[]){

    (void) signal(SIGINT,ext_handler);

    parser(argc,argv);

    printf("IP %s\nServPort %s\nLocalPort %s\nTime %s\n\n", serverip,
            serverport, localport, time);

    memset(sendbuf,0,sizeof(sendbuf));
    memset(archv,0,sizeof(archv));
    memset(recvbuf,0,sizeof(recvbuf));
    memset(&server,0,sizeof(server));
    memset(&address,0,sizeof(address));

    /* This child updates the files  */
    pid = fork();
    if(pid<0){
        perror("Error forking");
        close(listensock);
        exit(1);

    }else if(pid==0){
        close(listensock);
        char nextfile[500];
        if((point = fopen("Files", "w+")) == NULL)
            perror("**Cache Manager Child**\nError opening the cache file");

        //Fill the structure with host data
        address.sin_family = AF_INET; 
        address.sin_port = htons(atoi(serverport));
        inet_pton(AF_INET,serverip,&(address.sin_addr));

        while(1){
            rewind(point);
            sleep(atoi(time));

            while(fscanf(point,"%[^\n]\n",nextfile)!=EOF){

                //Create socket for this child
                if((s_psensor=socket(AF_INET, SOCK_STREAM, 0))<0)
                    perror("**Cache Manager Child**\nError creating file manager child");

                //Connecting psensor 
                if(connect(s_psensor, (struct sockaddr *)&address, sizeof(struct sockaddr))<0){
                    //The server psensor is down so this child continues with
                    //next file
                    printf("**Cache Manager Child**\nNo connection to server\n");
                    continue;
                }

                printf("Updating %s...\n\n",nextfile);

                //Build the query for psensor 
                char ip[20];
                sprintf(ip,"%s%s%s",serverip,":",serverport);
                make_query(ip, nextfile);
                //printf("\n**Cache Manager Child**\nQUERY: \n<BEGIN>%s<END>\n",query);

                //Send the query to server
                tmp = send(s_psensor, query, strlen(query), 0);
                //printf("\n**Cache Manager Child**\ntmp send to psensor %d\n",tmp);
                if(tmp<0)
                    perror("**Cache Manager Child**\nError sending query");


                memset(recvbuf, 0, 1024);
               
                // Receiving the data and overwriting the cache
                flag=1;
                flag2=1;
                while((tmp = recv(s_psensor, recvbuf, 1023, 0)) > 0 ){

                    //printf("\n**Cache Manager Child**\ntmp from psensor %d\n",tmp);
                    //fwrite(recvbuf, sizeof(char) ,tmp, stdout);

                    if(flag && 200==atoi(&recvbuf[9])){

                        //If first loop and no problems recving
                        //open file nextfile
                        if ((file = fopen(nextfile,"w")) == NULL)
                            perror("**Cache Manager Child**\nError opening file");

                        if(fwrite(recvbuf, sizeof(char) ,tmp, file)!=tmp)
                            printf("**Cache Manager Child**\nError writing in file %s\n",nextfile);
                        memset(recvbuf, 0, tmp);
                        flag2=0; //flag2=0 no problems in first chunk of data
                    }

                    if(!flag && !flag2){
                        //Store next chuncks knowing its not the first loop
                        //and there was no problem with the first
                        if(fwrite(recvbuf, sizeof(char) ,tmp, file)!=tmp)
                            printf("**Cache Manager Child**\nError writing in file %s\n",nextfile);
                        memset(recvbuf, 0, tmp);
                    }

                    flag=0;
                }

                if(tmp < 0){
                    perror("**Cache Manager Child**\nError receiving data");
                }

                memset(query,0,sizeof(query));
                if(!flag2)
                    fclose(file);

                close(s_psensor);
            }
        }

        printf("Exit hijo file manager\n");
        exit(0);
    }

    /* Code Server */

    /* Creating socket between server and client */
    if ((listensock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        perror("Error creating server socket");
        kill(0,SIGTERM);
        exit(1);
    }

    /* Populate the struct */
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(atoi(localport)); 

    //Binding the socket
    if(bind(listensock, (struct sockaddr * ) &server, sizeof(server)) < 0){
        perror("Binding socket not created");
        kill(0,SIGTERM);
        exit(1);
    }

    //Put the socket to listen
    if (listen(listensock, NCLIENTS) < 0){
        perror("Listen call failed");
        kill(0,SIGTERM);
        exit(1);
    }

    socklen_t size_data; 
    size_data = sizeof(size_data);

    //Loop for receiving request from clients
    while(1){

        b_psiesta = accept(listensock,(struct sockaddr *)&data, &size_data);
        if (b_psiesta < 0){
            perror("Error accepting connection from browser");
            kill(0,SIGTERM);
            exit(1);
        }

        //Childs to attend browsers requests
        pid = fork();
        if(pid<0){
            perror("Error forking");
            close(listensock);
            close(b_psiesta);
            kill(0,SIGTERM);
            exit(1);
        }
        if(pid==0){
            close(listensock);
            // Receiving the data from browser 
            tmp = recv(b_psiesta, &recvbuf[got], 1023, 0);
            //printf("\ntmp recv from browser %d\n",tmp);
            //fwrite(&recvbuf[got], sizeof(char) ,tmp, stdout);

            if(tmp<0)
                perror("Error receiving data");
    
            memset(archv,0,500);

            //Parsing the file to retrieve from psensor
            for(i=5;recvbuf[i]!=' ';i++){
                archv[i-5]=recvbuf[i];
            }

            archv[i-5]='\0';

            i=1; //File archv exists
            
            if((file = fopen(archv,"r")) == NULL)
                i = 0; //i=0 If archv doesnt exists
            else
                fclose(file);

            //Fill the structure with host data
            address.sin_family = AF_INET; 
            address.sin_port = htons(atoi(serverport));
            inet_pton(AF_INET,serverip,&(address.sin_addr));

            //Create socket for this child
            if((s_psensor=socket(AF_INET, SOCK_STREAM, 0))<0)
                perror("Error creating file manager child");

            //Connecting psensor 
            if(connect(s_psensor, (struct sockaddr *)&address, sizeof(struct sockaddr))<0){
                //If psensor is not active search cache memory
                if(i){
                    //If we got the archive, send it to browser
                    if((file=fopen(archv, "r"))==NULL)
                        perror("Error opening file");

                    memset(line,0,2000);

                    fread(line,sizeof(char), sizeof(line), file);
                    tmp = send(b_psiesta,line,strlen(line),0);
                    //printf("BUSCANDO EN CACHE... linewidth %d, tmp send to browser%d\n",(int)strlen(line),tmp);
                    printf("Found %s in cache, sending it to browser...\n\n",archv);
                    if(tmp<0)
                        perror("Error sending response to browser");

                    fclose(file); 
                }else{
                    //If we dont, send a message
                    //printf("no connection to server\n%s\n",response);
                    tmp = send(b_psiesta, response, strlen(response), 0);
                    if(tmp<0)
                        perror("Error sending response to browser");
                    
                }
                exit(1);
            }

            //Make query for psensor
            char ip[20];
            sprintf(ip,"%s%s%s",serverip,":",serverport);
            make_query(ip, archv);
            //printf("\nQUERY: \n<BEGIN>%s<END>\n",query);

            //Send query to psensor
            tmp = send(s_psensor, query, strlen(query), 0);
            //printf("\ntmp send to psensor %d\n",tmp);
            if(tmp<0)
                perror("Error sending query");

            memset(recvbuf, 0, 1024);
            
            //printf("\narchv %s\n",archv);

            //Receiving the data and overwriting the file
            flag=1;
            flag2=1;
            while((tmp = recv(s_psensor, recvbuf, 1023, 0))>0){

                //printf("\ntmp recv from psensor %d\n",tmp);
                //fwrite(recvbuf, sizeof(char) ,tmp, stdout);

                //printf("\nflag %d - code %d\n", flag , atoi(&recvbuf[9]));

                if(flag && 200==atoi(&recvbuf[9])){
                    //printf("first loop if, i is %d\n",i);

                    //If first loop and no problems recving
                    //open file archv
                    if((file = fopen(archv,"w")) == NULL)
                        perror("Error opening file");

                    if(!i){
                        //i=0 if file doesnt exists
                        //If file doesnt exists, add it in the cache file
                        if((cache = fopen("Files","a"))==NULL)
                            perror("Error opening file");

                        if(fwrite(archv, sizeof(char), strlen(archv), cache)!=strlen(archv))
                            printf("Error writing in cache file\n");
                        if(fputc('\n',cache)<0)
                            printf("Error writing char in cache file\n");

                        fclose(cache);
                    }

                    if(fwrite(recvbuf, sizeof(char), tmp, file)!=tmp)
                        printf("Error writing in file %s\n",archv);
                    memset(recvbuf, 0, tmp);
                    flag2=0; //flag2=0 no problems in first chunk of data
                }

                if(!flag && !flag2){
                    //Store next chuncks knowing its not the first loop
                    //and there was no problem with the first
                    if(fwrite(recvbuf, sizeof(char), tmp, file)!=tmp)
                        printf("Error writing in file %s\n",archv);
                    memset(recvbuf, 0, tmp);
                }

                flag=0;
            }

            if(tmp < 0){
                perror("Error receiving data from psensor");
            }

            if(!flag2)
                fclose(file);

            if(flag2==1){
                //Psensor is up but couldnt retrieve information
                //Send to browser
                if(i){
                    //If we got the archive, send it to browser
                    if((file=fopen(archv, "r"))==NULL)
                        perror("Error opening file");

                    memset(line,0,2000);

                    fread(line,sizeof(char), sizeof(line), file);
                    tmp = send(b_psiesta,line,strlen(line),0);
                    //printf("BUSCANDO EN CACHE... linewidth %d, tmp send to browser%d\n",(int)strlen(line),tmp);
                    printf("Found %s in cache, sending it to browser...\n\n",archv);
                    if(tmp<0)
                        perror("Error sending response to browser");

                    fclose(file); 
                }else{
                    //If we dont, send a message
                    //printf("Problem with info\n%s\n",response);
                    tmp = send(b_psiesta, response, strlen(response), 0);
                    if(tmp<0)
                        perror("Error sending response to browser");
                }
            }else if(flag2==0){
                //Psensor is up and file was found
                if((file = fopen(archv,"r")) == NULL)
                    perror("Error opening file");

                memset(line,0,2000);

                printf("Sending file %s from psensor to browser...\n\n",archv);

                fread(line,sizeof(char), sizeof(line), file);
                tmp = send(b_psiesta,line,strlen(line),0);
                //printf("linewidth %d, tmp send to browser %d\n",(int)strlen(line),tmp);
                if(tmp<0)
                    perror("Error sending response to browser");

                fclose(file);
            }

            close(b_psiesta);
            close(listensock);
            exit(0);
        }
        close(b_psiesta);
    }
    
}
Example #12
0
void SqliteDataset::make_deletion() {
  make_query(delete_sql);
}
Example #13
0
void SqliteDataset::make_edit() {
  make_query(update_sql);
}
Example #14
0
void SqliteDataset::make_insert() {
  make_query(insert_sql);
  last();
}