Beispiel #1
0
PollState HttpServerConnection::writeData() {
    if (is_websocket)
        return wsWriteData();
    if (state == HttpState::SENDING_RESPONSE) {
        // TODO: don't give conn to the owner, too much that can go wrong.

        size_t sent;
#ifdef USE_WEBROOT
        if (sending_static_file)
            sent = sendFileData(static_file_fd, remaining_bytes);
        else
#endif
            sent = owner_task->sendResponseData(this, remaining_bytes);

        remaining_bytes -= sent;
        if (remaining_bytes)
            return PollState::WRITE;
#ifdef USE_WEBROOT
        if (sending_static_file) {
            close(static_file_fd);
            sending_static_file = false;
        }
#endif
        state = HttpState::WAITING_FOR_REQUEST;
        return PollState::READ;
    }
    err_log() << "HttpServerConnection::writeData() called";
    return PollState::CLOSE;
}
/*
 * If a file is double-clicked, load it in a different thread to prevent locking the GUI
 */
void PlottingWidget::fileSelected(QListWidgetItem *item)
{
    tlbl->setText("Loading "+item->text());
    progBar->setVisible(true);
    progBar->setRange(0,0);
    progBar->setValue(0);

    LoadBigFile *loadFile = new LoadBigFile(item->text());

    connect(loadFile, SIGNAL(sendFileData(QVector<QVector<QPointF> >)), this, SLOT(initEcg(QVector<QVector<QPointF> >)));

    loadFile->start();

    //Read header file and display information about the recording
    QFileInfo fInfo(item->text());
    QFile headerFile(fInfo.baseName()+"_header.txt");
    if (headerFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream headerTextStream(&headerFile);
        QString headerText;

        while (!headerTextStream.atEnd()) {
            headerText += headerTextStream.readLine() + "\n";
        }

        headerTextArea->setText(headerText);
        headerTextStream.flush();
        headerFile.close();
    }
}
Beispiel #3
0
HttpState HttpServerConnection::sendHttpFileResponse(const std::string &hdrs,
                                                     const std::string &mime) {
    if (!sending_static_file || state != HttpState::WAITING_FOR_REQUEST) {
        err_log() << "internal error, prepareFileResponse not called";
        return HttpState::CLOSE;
    }

    sendHttpResponseHeader(hdrs, mime, remaining_bytes);

    if (remaining_bytes)
        remaining_bytes -= sendFileData(static_file_fd, remaining_bytes);

    if (remaining_bytes)
        return HttpState::SENDING_RESPONSE;
    else
        return HttpState::WAITING_FOR_REQUEST;
}
Beispiel #4
0
int fun(char *buffer,int size,char *filename) 
{
	struct sockaddr_in servaddr;
	
	bzero(&servaddr,sizeof(servaddr));
	int sockfd,connfd;
	
	sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);// socket fd
	if(sockfd < 0)
	{
		perror("\n\tSocket");
		printf("\n\tSocket Error\n");
		exit(1);
	}
	
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(5579);
	
	if((inet_pton(sockfd,"127.0.0.1",&servaddr.sin_addr)) <= 0)// convert(ip addrss) string to int
	{
		printf("\n\tIp Address Error\n");
		perror("\n\tip");
		//exit(1);
	}
	
	/* servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
	
	connfd = connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));//connect

	if(connfd < 0)
	{
		printf("\n\tConnect Error\n");
		perror("\n\tConnect\n");
		exit(1);
	}
	
	//sendFileName(sockfd,filename);
	sendFileData(sockfd,buffer,size);
	//printf("\n\tExiting\n");
}	
Beispiel #5
0
int main(int argc,char *argv[])
{
	if(argc != 3)
	{
		printf("\n\tUsage : ./a.out 127.0.0.1 mp3_file.mp3\n");
		exit(1);
	}
	struct sockaddr_in servaddr;
	bzero(&servaddr,sizeof(servaddr));
	
	int sockfd,connfd;
	
	sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(sockfd < 0)
	{
		printf("\n\tSocket Error\n");
		perror("\n\tSocket");
		exit(1);
	}
	
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(9999);
	inet_pton(sockfd,argv[1],&servaddr.sin_addr);
	
	connfd = connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
	if(connfd < 0)
	{
		printf("\n\tConnect Error\n");
		perror("\n\tConnect\n");
		exit(1);
	}
	
	sendFileName(sockfd,argv[2]);
	sendFileData(sockfd,argv[2]);
	printf("\n\tExiting\n");
}
/*
 * Load the file with chosen filename and emit the signal sendFileData() when the file is read.
 */
void ConvertEcgToIbi::run()
{
	QFile myFile(fname);
	if (myFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
		time.start();
		QTextStream ecgInfo(&myFile);
		QVector<int > ecgVals;
        QVector<double> timeData;
		int iterations;
		if (!ecgInfo.atEnd()) {
			QString strVals = ecgInfo.readLine();
			ecgInfo.readLine();
			ecgInfo.readLine();
			double tmp;
			int i=0;
			while (!ecgInfo.atEnd()) {

				strVals = ecgInfo.readLine();
				QStringList strPieces = strVals.split(QRegularExpression("\\s+"));


				if (strPieces.length()==4) {
                    tmp=strPieces[2].toDouble();
                    ecgVals.append((tmp*500)); ///< @todo normalize input to work with more files

				}
				else if (strPieces.length()==3) {
                                    tmp=strPieces[2].toDouble();
                                    ecgVals.append((tmp*500));
                                                    }
				else if (strPieces.length()==5){

					tmp=strPieces[2].toDouble();
                    ecgVals.append((tmp*500));
                    }
				else {
					std::cerr << "Wrong File" << std::endl;
					return;
				}
				i++;
                }
            QVector<double> qrsPeaks;
			extractRtoR(&ecgVals, &qrsPeaks);
            qrsPeaks.takeFirst();// Remove the influense of the QRS-detectors learning period
            qrsPeaks.takeFirst();// Remove the influense of the QRS-detectors learning period
            qrsPeaks.takeFirst();// Remove the influense of the QRS-detectors learning period
            tmp=0;
            for (int i; i<qrsPeaks.length(); i++){
                tmp=tmp+(qrsPeaks.at(i));
                timeData.append(tmp);
            }
            if (qrsPeaks.length()>10){   ///@todo FIX this check neater
                emit sendFileData(qrsPeaks,timeData);
                saveAsIbiFile(&qrsPeaks);
            }
            else
                std::cerr << "Not enough R peaks detected" << std::endl;
            qDebug("Time elapsed: %d ms", time.elapsed());

		}
		ecgInfo.flush();
		myFile.close();



	}

}
Beispiel #7
0
// this function is called when we have received a new socket.   We need to 
// create a new node, and add it to our node list.  We need to pass to the node 
// any pointers to other sub-systems that it will need to have, and then we 
// insert the node into the 'node-circle' somewhere.  Finally, we need to add 
// the new node to the event base.
static void node_event_handler(int hid, short flags, void *data)
{
	node_t *node;
	unsigned int avail;
	int res;
	
	assert(hid >= 0);
	
	node = (node_t *) data;

	assert(node != NULL);
	assert(node->handle == hid);
	assert(node->stats != NULL);
	assert(node->active == true);
	assert(node->event.ev_base != NULL);
	
	if (flags & EV_READ) {
	
		assert(node->in.max >= DEFAULT_BUFFSIZE);
		
		avail = node->in.max - node->in.length;
		if (avail < DEFAULT_BUFFSIZE) {
			// we dont have much space left in the buffer, lets double its size.
			expbuf_shrink(&node->in, node->in.max * 2);
			avail = node->in.max - node->in.length;
		}
		// for performance reasons, we will read the data in directly into the expanding buffer.
		assert(avail >= DEFAULT_BUFFSIZE);
		node->stats->reads++;
		res = read(hid, node->in.data+node->in.length, avail);
		if (res > 0) {
			node->stats->in_bytes += res;
			node->in.length += res;
			assert(node->in.length <= node->in.max);

			// if we pulled out the max we had avail in our buffer, that means we can pull out more at a time.
			if (res == avail) {
				expbuf_shrink(&node->in, node->in.max * 2);
			}
			
			assert(node->active);
			if (node->in.length > 0) {
				
				assert(node->risp != NULL);
				
				node->stats->cycles ++;
				res = risp_process(node->risp, node, node->in.length, (unsigned char *) node->in.data);
	 			assert(res <= node->in.length);
	 			assert(res >= 0);
				if (res < node->in.length) {
					node->stats->undone += (node->in.length - res);
				}
	 			if (res > 0) {
					expbuf_purge(&node->in, res);
				}
			}
		}
		else if (res == 0) {
			node->handle = INVALID_HANDLE;
			if (node->filehandle != INVALID_HANDLE) {
				close(node->filehandle);
				node->filehandle = INVALID_HANDLE;
			}
			node_clear(node);
			assert(node->active == false);
			printf("Node[%d] closed while reading.\n", hid);
		}
		else {
			assert(res == -1);
			if (errno != EAGAIN && errno != EWOULDBLOCK) {
				close(node->handle);
				node->handle = INVALID_HANDLE;
				if (node->filehandle != INVALID_HANDLE) {
					close(node->filehandle);
					node->filehandle = INVALID_HANDLE;
				}
				node_clear(node);
				assert(node->active == false);
				printf("Node[%d] closed while reading- because of error: %d\n", hid, errno);
			}
		}
	}
		
	if (flags & EV_WRITE && node->active) {		
		
		// we've requested the event, so we should have data to process.
		assert(node->out.length > 0);
		assert(node->out.length <= node->out.max);

		node->stats->writes ++;
		res = send(hid, node->out.data, node->out.length, 0);
		if (res > 0) {
			// we managed to send some, or maybe all....
			assert(res <= node->out.length);
			node->stats->out_bytes += res;
			expbuf_purge(&node->out, res);
			
			// if we are in the process of transmitting a file, then we need
			// to get more file data and put in the buffer, since we depleted
			// some of it.
			if (node->sending) {
				sendFileData(node);
			}
			
		}
		else if (res == 0) {
			node->handle = INVALID_HANDLE;
			if (node->filehandle != INVALID_HANDLE) {
				close(node->filehandle);
				node->filehandle = INVALID_HANDLE;
			}
			node_clear(node);
			assert(node->active == false);
			printf("Node[%d] closed while writing.\n", hid);
		}
		else {
			assert(res == -1);
			if (errno != EAGAIN && errno != EWOULDBLOCK) {
				close(node->handle);
				node->handle = INVALID_HANDLE;
				if (node->filehandle != INVALID_HANDLE) {
					close(node->filehandle);
					node->filehandle = INVALID_HANDLE;
				}
				node_clear(node);
				assert(node->active == false);
				printf("Node[%d] closed while writing - because of error: %d\n", hid, errno);
			}
		}
		
		// if we have sent everything, then we dont need to wait for a WRITE event anymore, so we need to re-establish the events.
		if (node->active && node->out.length == 0) {
			if (event_del(&node->event) != -1) {
				event_set(&node->event, hid, EV_READ | EV_PERSIST, node_event_handler, (void *)node);
				event_base_set(node->event.ev_base, &node->event);
				event_add(&node->event, 0);
			}		
		}
	}		
}