Example #1
0
	int svc(void)  {
        ACE_DEBUG((LM_DEBUG, "(%t) Svc thread \n"));
        
//call the message creator thread
        if (ACE_Thread::self() == thread_names[0])
            while (1)
                construct_message();    //create messages forever
        else
            while (1)
                send_message(); //send messages forever
        return 0;               // keep the compiler happy.
    }
bool Server::accept_connection() {
	while (1) { // main accept() loop
		sin_size = sizeof their_addr;
		new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &sin_size);
		if (new_fd == -1) {
			error("accept");
			return false;
		}
		inet_ntop(their_addr.ss_family,
				get_in_addr((struct sockaddr *) &their_addr), s, sizeof s);
		cout << "server: got connection from " << s << endl;
		if (!fork()) { // this is the child process
			close(sockfd); // child doesn't need the listener
			cout << "waiting for HTTP request" << endl;

			char buf_req[MAXREQUESTSIZE];
			char buf_rest[MAXREQUESTSIZE];
			while (true) { // request line detection
				char received_chunk[MAXDATASIZE];
				memset(received_chunk, 0, MAXDATASIZE);
				receive_data(received_chunk, MAXDATASIZE); // receive request line
//				cout << "\treceived 1 : \"" << received_chunk << "\"" << endl;
				string s = received_chunk;

				int pos = s.find("\r\n", 0);
				if (pos != string::npos) {
					string s1, s2;
					s1 = s.substr(0, pos);
					s2 = s.substr(pos);
					strcpy(buf_req, s1.c_str());
					strcpy(buf_rest, s2.c_str());
//					cout << "\tbuf_req: \"" << buf_req << "\"" << endl;
//					cout << "\tbuf_rest: \"" << buf_rest << "\"" << endl;
					break;
				} else {
					strcat(buf_req, received_chunk);
				}
			}
			// find out whether the received request is GET or POST
			vector<string> v;
			FileHandler::split(buf_req, ' ', v);
			if (v[0].compare("GET") == 0) {
				// GET request detected
				cout << "GET request detected" << endl;
				cout << "###################################" << endl
						<< buf_req;
				string file = WORKINGDIRECTORY + v[1];
				while (true) {
					string s(buf_rest);
//					cout << "\"" << s << "\"" << endl;
					int pos = s.find("\r\n\r\n", 0);
					if (pos != string::npos) { // we reached the end of the GET request, discard the rest
						string headers;
						headers = s.substr(0, pos);
						strcat(buf_rest, headers.c_str());
						cout << s << "###################################"
								<< endl;
						break;
					}
					char received_chunk[MAXDATASIZE];
					memset(received_chunk, 0, MAXDATASIZE);

					receive_data(received_chunk, MAXDATASIZE); // receive the rest of the request
					strcat(buf_rest, received_chunk);

				}

				cout << "server: requesting file: " << file << endl;
				//Check for file existence
				//File exists
				if (FileHandler::fexists(file)) {
					cout << "Exists" << file << endl;

					//Send File
					ifstream file_stream;
					FileHandler::open_file_to_read(&file_stream, file);
					if (!file_stream.is_open()) {
						error("Could not open file");
						return false;
					}
					int file_size = FileHandler::get_file_size(&file_stream);
					cout << "sending actual file..." << endl;
					const char* message = construct_message(200).c_str();
					send_data(message);
					send_file(file_size, &file_stream);
					cout << "file was sent successfully" << endl;
				}
				//File doesn't exist
				else {
					cout << "Doesn't Exist" << endl;
					string not_found = construct_message(404);
					if (send(new_fd, not_found.c_str(),
							strlen(not_found.c_str()), 0) == -1)
						error("send");
				}
			} else if (v[0].compare("POST") == 0) {
				// POST request detected
				cout << "POST request detected" << endl;
				cout << "###################################" << endl
						<< buf_req;
				string file_name = WORKINGDIRECTORY+v[1];
				string file_size;
				bool valid_request = false;
				char file[MAXREQUESTSIZE];
				/////////////////////////////////////////////////
				//Getting headers
				while (true) {
					string s(buf_rest);
//					cout << s.length()<<"\"" << s << "\"" << endl;
					int pos = s.find("\r\n\r\n", 0);
					if (pos != string::npos) { // we reached the end of the POST request, get the length from the headers and receive the file
						string headers;
						headers = s.substr(0, pos);
						cout<<"ABO:\""<<headers<<"\""<<endl;
						char header_char_arr [MAXREQUESTSIZE];
						strcpy(header_char_arr,headers.c_str());
						// extract Content-length from the headers, else report a bad request
						v.clear();
						cout << "\theaders: " << header_char_arr << endl;
						FileHandler::split_string(header_char_arr, "\r\n", v);
						for (int i = 0; i < v.size();i++) {
							vector<string> v2;
							FileHandler::split(v[i],':',v2);
							if (v2.size() > 0 && v2[0].compare("Content-Length") == 0) { // found Content-length header, request is valid
								file_size = v2[1];
								valid_request = true;
								break;
							}

						}
						if (!valid_request) {
							string message = construct_message(400);
							send_data(message.c_str());

						}
						else {
							//Begining of file
							string temp = "";
							cout<<"LL:"<<pos<<" "<<s<<endl;
							temp = s.substr(pos + 4);
							cout<<"TEMP:\""<<temp<<"\""<<endl;
							strcpy(file, temp.c_str());
//							strcat(buf_rest, headers.c_str());
							int size_int = atoi(file_size.c_str());
							cout << "\tsize: " << size_int << endl;

							cout << s << "###################################" << endl;
//							int size_written = FileHandler::create_file_from_buf(file_name, file, strlen(file),size_int);
							FileHandler::concat_to_existing_file(file_name,file,strlen(temp.c_str()));
//							cout<<":::"<<size_int<<"::"<<size_written<<endl;


							cout<<"FILE: "<<sizeof(file)<<" Written: "<<strlen(temp.c_str())<<" size int: "<<size_int<<endl;
							if((receive_file(file_name, size_int-strlen(temp.c_str()))) == false) {
								cout << "server: An error has occurred and the client may have disconnected" << endl;
							}
							cout<<"EEEEEEEEEEEEEEEEEEEEEE"<<endl;
							string message = construct_message(200);
							send_data(message.c_str());
						}
						break;
					}

					char received_chunk[MAXREQUESTSIZE];
					memset(received_chunk, 0, MAXREQUESTSIZE);

					receive_data(received_chunk, MAXREQUESTSIZE); // receive the rest of the request
					strcat(buf_rest, received_chunk);

				}
				/////////////////////////////////////////////////


			} else { //neither
					 // return 400 bad request
			}
			close(new_fd);
			return true;
		}
		close(new_fd); // parent doesn't need this
	}
}
Example #3
0
int main(int argc, char *argv[]) {

	/////VARIABLE DECLARATIONS/////

	//SENSORS
	mraa_i2c_context accel, gyro, mag;
	float a_res, g_res, m_res;
	data_t accel_data, gyro_data, mag_data;
	int16_t temperature;
    float pitch_angle, yaw_angle;
    char *x_accel_message;
    char *y_accel_message;
    char *z_accel_message;
    char *posture_message;
    int curr_posture;
    int prev_posture = 0;
	
	//SOCKETS AND MESSAGES
	int sockfd; //Socket descriptor
    int portno, n;
    char component[256];
    char endpoint[] = "127.0.0.1";
    struct sockaddr_in serv_addr;
    struct hostent *server; //struct containing a bunch of info
	char message[256];

	int count;

	/////SENSOR INITIALIZATION AND SETUP
	accel = accel_init();
	set_accel_scale(accel, A_SCALE_2G);
	set_accel_ODR(accel, A_ODR_100);
	a_res = calc_accel_res(A_SCALE_2G);

	gyro = gyro_init();
	set_gyro_scale(gyro, G_SCALE_245DPS);
	set_gyro_ODR(accel, G_ODR_190_BW_70);
	g_res = calc_gyro_res(G_SCALE_245DPS);

	mag = mag_init();
	set_mag_scale(mag, M_SCALE_2GS);
	set_mag_ODR(mag, M_ODR_125);
	m_res = calc_mag_res(M_SCALE_2GS);

    portno = 41234;

    //create socket
    sockfd = socket(AF_INET, SOCK_DGRAM, 0); //create a new socket
    if (sockfd < 0) 
        error("ERROR opening socket");
    
    server = gethostbyname("127.0.0.1"); //takes a string like "www.yahoo.com", and returns a struct hostent which contains information, as IP address, address type, the length of the addresses...
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    
    //setup the server struct
    memset((char *) &serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET; //initialize server's address
    bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
    serv_addr.sin_port = htons(portno);
    
    //connect to server
    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) //establish a connection to the server
        error("ERROR connecting");


    //read accel and gyro data 
    count = 0;
	while(1) {
        
		accel_data = read_accel(accel, a_res);
		gyro_data = read_gyro(gyro, g_res);
		//mag_data = read_mag(mag, m_res);
		//temperature = read_temp(accel);
        
        getAngles(accel_data, &pitch_angle, &yaw_angle);
        printf("is moving: %d\n", isMoving(gyro_data) );
        //printf("yaw angle: %f ", yaw_angle);
        //printf("pitch angle: %f ", pitch_angle);
        //printf("z vector: %f\n", accel_data.z);
        
        
		if (count == 3) {
            //send posture to cloud
            
            if (isMoving(gyro_data)==0)        //if patient is stationary, calculate new posture
                curr_posture = getPosture(accel_data, pitch_angle, yaw_angle);
            else
                curr_posture = prev_posture;    //else just use the old posture
            if (curr_posture==UNDEFINED)
                curr_posture = prev_posture;
            prev_posture = curr_posture; //set new value for prev posture
            
            posture_message = construct_message(POS, accel_data, curr_posture);
            n = write(sockfd, posture_message, strlen(posture_message)); //write to the socket
    		if (n < 0) 
        		error("ERROR writing to socket");
    
			//store accel data in string
            
            /*
			x_accel_message = construct_message(X_DIR, accel_data);

		    //printf("%s\n", full_message_x);

    		//send UDP message
    		n = write(sockfd,x_accel_message,strlen(x_accel_message)); //write to the socket
    		if (n < 0) 
        		error("ERROR writing to socket");

        	y_accel_message = construct_message(Y_DIR, accel_data);

		    //printf("%s\n", full_message_y);		

    		n = write(sockfd,y_accel_message,strlen(y_accel_message)); //write to the socket
    		if (n < 0) 
        		error("ERROR writing to socket");


            z_accel_message = construct_message(Z_DIR, accel_data);
		    //printf("%s\n", full_message_z);

    		n = write(sockfd,z_accel_message,strlen(z_accel_message)); //write to the socket
    		if (n < 0) 
        		error("ERROR writing to socket");
            */
        	count = 0;

		}
        
        char *posture_string;
        switch(curr_posture)
        {
            case 0:
                posture_string = "upright";
                break;
            case 1:
                posture_string = "face up";
                break;
            case 2:
                posture_string = "face down";
                break;
            case 3:
                posture_string = "left";
                break;
            case 4:
                posture_string = "right";
                break;
            default:
                posture_string = "undefined";
        }
        
        printf("current orientation: %s \n", posture_string);
		//printf("X: %f\t Y: %f\t Z: %f\n", accel_data.x, accel_data.y, accel_data.z);
		//printf("X: %f\t Y: %f\t Z: %f\n", accel_data.x, accel_data.y, accel_data.z);
		//printf("\tX: %f\t Y: %f\t Z: %f\t||", gyro_data.x, gyro_data.y, gyro_data.z);
		//printf("\tX: %f\t Y: %f\t Z: %f\t||", mag_data.x, mag_data.y, mag_data.z);
		//printf("\t%ld\n", temperature);
		count++;
		usleep(100000);

	}

	return 0;

}