예제 #1
0
void MathTest::onUserName()
{
    if((userNameEdit->text() != userName)
    && (userNameEdit->text() != "")) {
        openResultFile();
        getDefaults();
    }
}
예제 #2
0
void MathTest::onGo()
{
    int iCount = 0;

    // Only do this if we're not already running.
    //
    if(bRunning) {
        pMsg->sendAlert("Tests are already running.");
    }
    else {
        iTotalCount = 0;
        iTotalRight = 0;
        iGradeLevel = pGradeLevel->buttonGroup->checkedId();

        // Initialize the test matrix and the other global control
        // parameters.
        //
        for(int i = 0; i < op_boundary; i++)
        {
            if(pTests->widgetList[i]->isChecked()) {
                testMatrix[i].bOn = true;
                testMatrix[i].eOp = (op_t)i;
                testMatrix[i].iCount = pProblems->widgetList[i]->text().toInt();
                testMatrix[i].iPass = 0;
                testMatrix[i].iSeconds = pTimes->widgetList[i]->text().toInt();
                testMatrix[i].bFirst = true;
                iCount++;
            }
            else {
                testMatrix[i].bOn = false;
                testMatrix[i].eOp = op_boundary;
                testMatrix[i].iCount = 0;
                testMatrix[i].iPass = 0;
                testMatrix[i].bFirst = false;
            }
            testMatrix[i].iRight = 0;
            testMatrix[i].iWrong = 0;
            testMatrix[i].iScore = 0;
            testMatrix[i].qsGrade = "";
        }

        if(iCount == 0) {
            pMsg->sendAlert("No tests selected.\nPlease select a test.");
        }
        else {

            // If we have a file for this session, reopen it now.
            //
            if(!bFirstPass && !resultFile->isOpen()
            && (userNameEdit->text() == userName)
            && (userName != "")) {
                if (! resultFile->open(QIODevice::Append | QIODevice::Text))
                    pMsg->sendInfo("Continuing without appending to the file.");
            }

            if(bFirstPass || (userNameEdit->text() != userName)) {
                openResultFile();
                getDefaults();
            }

            if(bFirstPass)
                bFirstPass = false;

            setEditable(false);
            bRunning = true;
            testIndex = 0;
            rnd.clear();
            runTest();
        }
    }
}
int main(int argc, char **argv)
{
	int i; /* loop and temporary variables */
	struct timespec sleep_time = {0, 3000000}; /* 3 ms */
	
	int packet_counter = 0;
	
	/* allocate memory for packet fetching and processing */
	struct lgw_pkt_rx_s rxpkt[16]; /* array containing up to 16 inbound packets metadata */
	struct lgw_pkt_rx_s *p; /* pointer on a RX packet */
	int nb_pkt;

	configure_gateway();

	/* parse command line options */
	while ((i = getopt (argc, argv, "hr:")) != -1) {
		switch (i) {
			case 'h':
				usage();
				return EXIT_FAILURE;
				break;
			case 'r':
				result_file_name = optarg;
				break;
			
			default:
				MSG("ERROR: argument parsing use -h option for help\n");
				usage();
				return EXIT_FAILURE;
		}
	}

	/* configure signal handling */
	sigemptyset(&sigact.sa_mask);
	sigact.sa_flags = 0;
	sigact.sa_handler = sig_handler;
	sigaction(SIGQUIT, &sigact, NULL);
	sigaction(SIGINT, &sigact, NULL);
	sigaction(SIGTERM, &sigact, NULL);

	/* starting the concentrator */
	i = lgw_start();
	if (i == LGW_HAL_SUCCESS) {
		MSG("INFO: concentrator started, packet can now be received\n");
	} else {
		MSG("ERROR: failed to start the concentrator\n");
		return EXIT_FAILURE;
	}

	openResultFile();
	
	/* transform the MAC address into a string */
	sprintf(lgwm_str, "%08X%08X", (uint32_t)(lgwm >> 32), (uint32_t)(lgwm & 0xFFFFFFFF));

	/* main loop */
	while ((quit_sig != 1) && (exit_sig != 1)) {
		/* fetch packets */
		nb_pkt = lgw_receive(ARRAY_SIZE(rxpkt), rxpkt);
		if (nb_pkt == LGW_HAL_ERROR) {
			MSG("ERROR: failed packet fetch, exiting\n");
			return EXIT_FAILURE;
		} else if (nb_pkt == 0) {
			clock_nanosleep(CLOCK_MONOTONIC, 0, &sleep_time, NULL); /* wait a short time if no packets */
		} else {
			/* local timestamp generation until we get accurate GPS time */
		}
		
		/* log packets */
		for (i=0; i < nb_pkt; ++i) {
			p = &rxpkt[i];

			switch(compare_id(p)) {
				case JOIN_REQ_MSG:
					MSG("Sending join response.\n");
					send_join_response(p);
					packet_counter = 0;
					size = 0;
					break;
				case TEST_MSG:
					snr[packet_counter] = p->snr;
					packet_counter++;
					size = p->size;
					break;
				case END_TEST_MSG:
					if (packet_counter != 0) {				
						write_results(packet_counter, p);
						MSG("Ended series: %i packets received.\n", packet_counter);
					}
					packet_counter = 0;
					size = 0;
					break;
				case ALL_TESTS_ENDED_MSG:
					exit_sig = 1; // ending program
					MSG("All tests have been finished.\n");
					break;
				default:
					// message not recognized
					break;
			}
		}
	}
	
	if (exit_sig == 1) {
		/* clean up before leaving */
		i = lgw_stop();
		if (i == LGW_HAL_SUCCESS) {
			MSG("INFO: concentrator stopped successfully\n");
		} else {
			MSG("WARNING: failed to stop concentrator successfully\n");
		}
	}

	fclose(result_file);
	
	MSG("INFO: Exiting uplink concentrator program\n");
	return EXIT_SUCCESS;
}
예제 #4
0
void
MoaiHttp_processResponse_forText( ZnkSocket O_sock, MoaiContext ctx, MoaiFdSet mfds,
		size_t* content_length_remain, RanoModule mod )
{
	MoaiInfo*     info      = ctx->draft_info_;
	MoaiBodyInfo* body_info = &ctx->body_info_;
	bool is_chunked = ctx->body_info_.is_chunked_;

	size_t result_size = 0;

	/***
	 * checkedが指定されていない場合でもとりあえず最初はcheckedのパターンを検査してrecvを試みる.
	 * (checkedが指定されていないにも関わらずcheckedなパターンでデータを返している行儀の悪い
	 * CGIなどが極稀に存在する(例えばwww1.axfc.net)からであり、それに対応するためである)
	 */
	/***
	 * is_unlimited_ とは !is_chunked_ && content_length_ == 0 の状態のことである.
	 * つまり !is_unlimited_では is_chunked_ || content_length_ != 0 となる.
	 */
	if( ctx->body_info_.is_unlimited_ ){
		bool is_recv_by_zero = false;

		if( !is_chunked ){
			if( detectChunkedBodyBegin( info->stream_, info->hdr_size_, O_sock, mfds, &result_size ) ){
				is_chunked = true;
			}
		}

		if( is_chunked ){
			/* parseChunkDataを実行 */
			ChunkRecv cr = parseChunkData( info->stream_, info->hdr_size_, O_sock, mfds, &result_size );
			if( cr == ChunkRecv_BrokenData ){
				is_recv_by_zero = true;
			}
		}  else {
			is_recv_by_zero = true;
		}

		if( is_recv_by_zero ){
			/* 残りはRecvZeroを受け取るまで力技でrecv. */
			RanoLog_printf( "  MoaiIO_recvByZero Mode Begin\n" );
			MoaiIO_recvByZero( info->stream_, O_sock, mfds, &result_size );
			RanoLog_printf( "  MoaiIO_recvByZero Mode End\n" );
		}

	} else if( is_chunked ){
		/* Transfer-Encodingにおいて明示的にchunkedが指定されている場合 */
		ChunkRecv cr = parseChunkData( info->stream_, info->hdr_size_, O_sock, mfds, &result_size );
		RanoLog_printf( "  parseChunkData result=[%d]\n", cr );
		updateContentLengthRemain( content_length_remain, result_size );
		if( cr == ChunkRecv_BrokenData ){
			dumpBrokenChunkStream( info->stream_ );

			/* 残りはcheckedが指定されていない場合と同様にrecv. */
			RanoLog_printf( "  MoaiIO_recvBySize(1) content_length_remain=[%zu]\n", *content_length_remain );
			while( *content_length_remain ){
				if( !MoaiIO_recvBySize( info->stream_, O_sock, mfds, *content_length_remain, &result_size ) ){
					break;
				}
				if( !updateContentLengthRemain( content_length_remain, result_size ) ){
					break;
				}
			}
		}

	} else if( *content_length_remain ){
		/* Transfer-Encodingにおいて明示的にchunkedが指定されておらず
		 * Content-Lengthの指定は存在する場合 */
		if( !is_chunked ){
			if( detectChunkedBodyBegin( info->stream_, info->hdr_size_, O_sock, mfds, &result_size ) ){
				is_chunked = true;
			}
			updateContentLengthRemain( content_length_remain, result_size );
		}
		if( is_chunked ){
			/***
			 * この場合chunkedデータとみなす.
			 */
			ChunkRecv cr = parseChunkData( info->stream_, info->hdr_size_, O_sock, mfds, &result_size );
			updateContentLengthRemain( content_length_remain, result_size );
			if( cr == ChunkRecv_BrokenData ){
				/* 残りはcheckedが指定されていない場合と同様にrecv. */
				RanoLog_printf( "  MoaiIO_recvBySize(2) content_length_remain=[%zu]\n", *content_length_remain );
				while( *content_length_remain ){
					if( !MoaiIO_recvBySize( info->stream_, O_sock, mfds, *content_length_remain, &result_size ) ){
						break;
					}
					if( !updateContentLengthRemain( content_length_remain, result_size ) ){
						break;
					}
				}
			}
		} else {
			/* chunkedではない場合.
			 * 以下によって全取得. */
			RanoLog_printf( "  MoaiIO_recvBySize(3) content_length_remain=[%zu]\n", *content_length_remain );
			while( *content_length_remain ){
				if( !MoaiIO_recvBySize( info->stream_, O_sock, mfds, *content_length_remain, &result_size ) ){
					break;
				}
				if( !updateContentLengthRemain( content_length_remain, result_size ) ){
					break;
				}
			}
		}
	}

	{
		const uint8_t* body_data = ZnkBfr_data( info->stream_ ) + info->hdr_size_;
		const size_t   body_size = ZnkBfr_size( info->stream_ ) - info->hdr_size_;

		ZnkStr_clear( ctx->text_ );
		if( body_info->is_gzip_ ){
			ZnkStr emsg = NULL;

			emsg = ctx->msgs_;
			ZnkStr_clear( ctx->msgs_ );

			if( !inflateGZipBody_toStr( ctx->text_, body_data, body_size, emsg ) ){
				RanoLog_printf( "  MoaiHttp_processResponse_forText : [%s]\n", ZnkStr_cstr(emsg) );
				ZnkStr_append( ctx->text_, (char*)body_data, body_size );
			}

		} else {
			ZnkStr_append( ctx->text_, (char*)body_data, body_size );
		}
	}

	if( mod ){
		RanoTxtFilterAry txt_ftr = NULL;
		switch( body_info->txt_type_ ){
		case RanoText_HTML:
			txt_ftr = RanoModule_ftrHtml( mod );
			break;
		case RanoText_JS:
			txt_ftr = RanoModule_ftrJS( mod );
			break;
		case RanoText_CSS:
			txt_ftr = RanoModule_ftrCSS( mod );
			break;
		default:
			break;
		}
		RanoModule_invokeOnResponse( mod, info->hdrs_.vars_, ctx->text_, ZnkStr_cstr(info->req_urp_) );
		if( txt_ftr ){
			RanoModule_execFilterAry( txt_ftr, ctx->text_ );

			if( body_info->txt_type_ == RanoText_CSS ){
				const ZnkStrAry css_additional = RanoModule_ftrCSSAdditional( mod );
				const size_t size = ZnkStrAry_size( css_additional );
				size_t idx;
				ZnkStr line;
				for( idx=0; idx<size; ++idx ){
					line =  ZnkStrAry_at( css_additional, idx );
					ZnkStr_add_c( ctx->text_, '\n' );
					ZnkStr_append( ctx->text_, ZnkStr_cstr(line), ZnkStr_leng(line) );
				}
			}
		}
	}

	{
		ZnkFile fp = openResultFile( body_info->txt_type_ );
		if( fp ){
			const uint8_t* html_data = (uint8_t*)ZnkStr_cstr( ctx->text_ );
			const size_t   html_size = ZnkStr_leng( ctx->text_ );
			Znk_fwrite( html_data, html_size, fp );
			Znk_fclose( fp );
		}
	}

	/***
	 * I側にはctx->text_の値で返信する.
	 * これに伴い、HeaderにおけるContent-Lengthの値を修正する.
	 */
	{
		ZnkHtpHdrs hdrs = &info->hdrs_;
		const char* key;
		bool exist_content_length = false;
		size_t size;
		size_t idx;
		ZnkVarp varp;

		ZnkHtpHdrs_erase( hdrs->vars_, "Transfer-Encoding" );

		size = ZnkVarpAry_size( hdrs->vars_ );
		for( idx=0; idx<size; ++idx ){
			varp = ZnkVarpAry_at( hdrs->vars_, idx );
			key = ZnkHtpHdrs_key_cstr( varp );

			if( ZnkS_eqCase( key, "Content-Length" ) ){
				ZnkStr str = ZnkHtpHdrs_val( varp, 0 );
				size_t new_contet_length = ZnkStr_leng( ctx->text_ );
				ZnkStr_setf( str, "%zu", new_contet_length );
				exist_content_length = true;
			}
			if( ZnkS_eqCase( key, "Content-Encoding" ) ){
				ZnkStr str = ZnkHtpHdrs_val( varp, 0 );
				/* identityは特別なEncodingはなしを示す */
				ZnkStr_set( str, "identity" );
			}
		}
		if( !exist_content_length ){
			char buf[ 1024 ];
			size_t new_contet_length = ZnkStr_leng( ctx->text_ );
			key = "Content-Length";
			Znk_snprintf( buf, sizeof(buf), "%zu", new_contet_length );
			ZnkHtpHdrs_regist( hdrs->vars_,
					key, Znk_strlen(key),
					buf, Znk_strlen(buf), true );
		}
	}
}