예제 #1
0
파일: parser-ld.c 프로젝트: kalamara/plcemu
int handle_coil(const int type, ld_line_t line) {
//(expect Q,T,M,W followed by byte / bit)
	int rv = PLC_OK;
	BYTE byte = 0;
    BYTE bit = 0;
    int c = read_char(line->buf, ++line->cursor);
    if (c >= OP_CONTACT && c < OP_END) {
		int operand = c;
	    c = read_char(line->buf, line->cursor);
		//int idx = extract_number(line->buf, ++line->cursor);
		rv = extract_arguments(line->buf + (++line->cursor),
		                       &byte,
		                       &bit);
		if (rv == PLC_OK) {
		    item_t identifier = mk_identifier(operand, 
			                                byte, 
			                                bit);
		    line->stmt = mk_assignment(identifier,
		                               line->stmt,
		                               type);
		    line->status = STATUS_RESOLVED;
		} else {
			rv = ERR_BADINDEX;
			line->status = STATUS_ERROR;
	    }
	} else {
		rv = ERR_BADCOIL;
		line->status = STATUS_ERROR;
    }
    return rv;
}
예제 #2
0
파일: rastro_write.c 프로젝트: schnorr/paje
// Inicializa biblioteca em um nodo
void rst_initialize(u_int64_t id1, u_int64_t id2, int *argc, char ***argv)
{
    if ( argv != NULL ) 
        extract_arguments(argc, argv); 
    
    rst_init(id1, id2);
}
예제 #3
0
파일: parser-ld.c 프로젝트: kalamara/plcemu
int handle_operand(int operand, 
                   BYTE negate, ld_line_t line) {
    int rv = PLC_OK;
    BYTE byte = 0;
    BYTE bit = 0;
    if (operand >= OP_INPUT && operand < OP_CONTACT){	//valid input symbol
		rv = extract_arguments(line->buf + (++line->cursor), 
		                            &byte, 
		                            &bit);       
		//extract_number(line->buf, ++line->cursor);
		if (rv == PLC_OK){
			/*byte + slash + bit*/
			line->cursor += digits((unsigned int)byte) + 2; 
			
			item_t identifier = mk_identifier(operand, 
			                                byte, 
			                                bit);
			line->stmt = mk_expression(identifier,
			                                 line->stmt,
			                                 IL_AND,
			                                 negate?IL_NEG:IL_NORM);
		} else {
			rv = ERR_BADINDEX;
			line->status = STATUS_ERROR;		    
		}
	} else {
		rv = ERR_BADOPERAND;
	    line->status = STATUS_ERROR;
	}
	line->cursor++;
    return rv;
}
예제 #4
0
파일: parser.cpp 프로젝트: maleadt/genetic
void Parser::validate_function(unsigned char* iBlock, unsigned int iSize, unsigned int& tLoc) {
    // Fetch the function
    unsigned char tFunctionBytecode = iBlock[tLoc++];

    // Validate function
    if (!mGrammar->isFunction(tFunctionBytecode))
        throw Exception(SYNTAX, "unknown function");
    const Function* tFunction = mGrammar->getFunction(tFunctionBytecode);

    // Validate parameter count
    std::vector<std::pair<unsigned int, unsigned int> > tParameterBytecode = extract_arguments(iBlock, iSize, tLoc);
    if (tParameterBytecode.size() != tFunction->getParameterCount())
        throw Exception(SYNTAX, "invalid function parameter count");

    // Validate parameter syntax
    for (unsigned int i = 0; i < tParameterBytecode.size(); i++) {
        validate_instruction(iBlock, iSize, tParameterBytecode[i].first);
        if (tParameterBytecode[i].first != tParameterBytecode[i].second)
            throw Exception(SYNTAX, "garbage after parameter");
    }


    if (tParameterBytecode.size() > 0)
        tLoc = tParameterBytecode.back().second + 1;
}
예제 #5
0
파일: parser.cpp 프로젝트: maleadt/genetic
Value Parser::evaluate_function(unsigned char* iBlock, unsigned int iSize, unsigned int& tLoc) {
    // Fetch the function
    unsigned char tFunction = iBlock[tLoc++];

    // Evaluate all arguments
    std::vector<std::pair<unsigned int, unsigned int> > tParameterBytecode = extract_arguments(iBlock, iSize, tLoc);
    std::vector<Value> tParameters;
    for (unsigned int i = 0; i < tParameterBytecode.size(); i++) {
        Value tParameter = evaluate_instruction(iBlock, iSize, tParameterBytecode[i].first);
        if (tParameter.getType() == VOID)
            throw Exception(FUNCTION, "function parameter returned void");
        tParameters.push_back(tParameter);
    }

    // Call the function
    if (tParameterBytecode.size() > 0)
        tLoc = tParameterBytecode.back().second + 1;
    return mGrammar->callFunction(tFunction, tParameters);
}
예제 #6
0
파일: main.c 프로젝트: moharaka/word-count
int main(int argc , char **argv)
{
#if PRINT_TIME
	double start, end;
#endif
	
	extract_arguments(argc, argv);

#if PRINT_TIME
	start = time_so_far();
#endif
	if(mr_init())
		exit(EXIT_FAILURE);

	start_threads();
	wait_threads();

	if(mr_reduce())
		exit(EXIT_FAILURE);

#if PRINT_TIME
	/* Done here, to avoid counting the printing... */
	end = time_so_far();
#endif

	if(mr_print())
		exit(EXIT_FAILURE);
	if(mr_destroy())
		exit(EXIT_FAILURE);

#if PRINT_TIME
	printf("Done in %g msec\n", end-start);
#endif

	exit(EXIT_SUCCESS);
}
예제 #7
0
int Organize::process_arguments(const int argc, const char *argv[]){
  vector<string> argument_list;  //vector of argument strings
  string file_location;  //string for getting location of added file
  int ret_val;  //int for determining exit number
  
  if (argc > 1){
    argument_list = extract_arguments(argv,argc);
    switch(retval = execute_args(check_args(argument_list,file_location),file_location)){
    case 0:
      return retval;
    case 1:
      cout << ERROR_STRING << ret_val;
      return 1;
    case 2:
      cout << ERROR_STRING << ret_val;	
      return 2;
    default:
      cout << ERROR_STRING << ret_val;	
      return retval;
    }
  }
  return 0;
  
}
예제 #8
0
int digest_user_comand(char *msg) 
{
	cmd_t cmd = extract_command(msg);
	char buf[sizeof(info_clients)];

	/* Eventuale argumente ale comenzilor */
	char arg1[NAME_LEN];
	char arg2[NAME_LEN];
	memset(arg1, '\0', NAME_LEN);
	memset(arg2, '\0', NAME_LEN);

	if(cmd == infoclients) {
		int x = send(sockfd, msg, strlen(msg), 0);
		memset(buf, '\0', sizeof(info_clients));

		x = recv(sockfd, buf, sizeof(info_clients), 0);

		struct info_clients *X = (struct info_clients *)buf;
		log_file << client_name << "> infoclients\n";
		for(int i = 0; i < X->nr; ++i) {
			log_file  << X->clients[i].name << " " << X->clients[i].IP << " " << X->clients[i].port << "\n";
			std::cout << X->clients[i].name << " " << X->clients[i].IP << " " << X->clients[i].port << "\n";
			if( !client_exists(X->clients[i].name) ) {
				struct known_client new_client;
				memset(new_client.basic_info.name, '\0', NAME_LEN);
				sprintf(new_client.basic_info.name, "%s", X->clients[i].name);

				memset(new_client.basic_info.IP, '\0', NAME_LEN);
				sprintf(new_client.basic_info.IP, "%s", X->clients[i].IP);

				new_client.basic_info.port = X->clients[i].port;
				new_client.nr_of_shared_files = 0;

				known_clients.push_back(new_client);
			}
		}
		memset(buf, '\0', sizeof(info_clients));
		std::cout << "\n";	
	}

	if(cmd == share) {
		extract_arguments(arg1, arg2, msg);
		log_file << client_name << "> " << msg << "\n";

		FILE *f = fopen(arg1, "r");
		if( f == NULL ) {
			std::cout << ABSENT_FILE << " : Fisier inexistent\n";
			log_file  << ABSENT_FILE << " : Fisier inexistent\n";
			return 1;
		}
		fclose(f);

		if(nr_of_shared_files >= 32) {
			std::cout << "At depasit numarul maxim de fisiere ce pot fi partajate\n";
			return 0;
		}

		/* Punem in arg 2 dimensiunea fisierului */
		struct stat info;
		lstat(arg1, &info);
		memset(arg2, '\0', NAME_LEN);
		human_readable((double)info.st_size, arg2);

		bool is_new_file = true;

		for(int i = 0; i < nr_of_shared_files; ++i) {
			if( strcmp(arg1, shared[i].name) == 0 ) {
				memset(shared[i].size, '\0', NAME_LEN);
				sprintf(shared[i].size, "%s", arg2);
				is_new_file = false;
				break;
			}
		}

		if( is_new_file ) {
			memset(shared[nr_of_shared_files].name, '\0', NAME_LEN);
			memset(shared[nr_of_shared_files].size, '\0', NAME_LEN);

			sprintf(shared[nr_of_shared_files].name, "%s", arg1);
			sprintf(shared[nr_of_shared_files].size, "%s", arg2);
			nr_of_shared_files++;
		}
		
		memset(msg, '\0', BUFLEN);		
		strcat(msg, "share ");
		strcat(msg, arg1);
		strcat(msg, " ");
		strcat(msg, arg2);
		int x = send(sockfd, msg, BUFLEN, 0);
		
		memset(arg2, '\0', NAME_LEN);
		recv(sockfd, arg2, NAME_LEN, 0);

		std::cout << arg2 << "\n\n";		
		log_file  << arg2 << "\n";
	}

	if( cmd == unshare_file ) {
		extract_arguments(arg1, arg2, msg);
		
		std::cout << client_name << "> unshare " << arg1 << "\n";
		log_file  << client_name << "> unshare " << arg1 << "\n";

		int id = -1;
		for(int i = 0; i < nr_of_shared_files; ++i) {
			if( strcmp(shared[i].name, arg1) == 0 ) {
				id = i;
				break;
			}
		}
		
		if( id == -1 ) {
			std::cout << "-5 : Fisier inexistent\n";
			log_file  << "-5 : Fisier inexistent\n";
			return 0;
		}

		for(int i = id + 1; i < nr_of_shared_files; ++i) {
			/* shared[i - 1] <- shared[i] */
			memset(shared[i - 1].name, '\0', NAME_LEN);
			sprintf(shared[i - 1].name, "%s", shared[i].name);
			memset(shared[i - 1].size, '\0', NAME_LEN);
			sprintf(shared[i - 1].size, "%s", shared[i].size);
		}

		nr_of_shared_files--;

		send(sockfd, msg, strlen(msg), 0);

		log_file  << "Succes\n";
		std::cout << "Succes\n\n";
		
	}

	if( cmd == getshare ) {
		extract_arguments(arg1, arg2, msg);
		
		std::cout << client_name << "> " << msg << "\n";
		log_file  << client_name << "> " << msg << "\n";

		send(sockfd, msg, strlen(msg), 0);

		memset(buf, '\0', NAME_LEN);
		recv(sockfd, buf, NAME_LEN, 0);

		int status;
		sscanf(buf, "%d", &status);

		if(status == ABSENT_CLIENT) {
			std::cout << ABSENT_CLIENT << " : Client inexistent\n";
			log_file  << ABSENT_CLIENT << " : Client inexistent\n";
			return 1;
		}

		memset(buf, '\0', NAME_LEN);
		recv(sockfd, buf, NAME_LEN, 0);

		
		int nr;
		sscanf(buf, "%d", &nr);

		int id = -1;
		for(int i = 0; i < known_clients.size(); ++i) {
			if( strcmp(known_clients[i].basic_info.name, arg1) == 0) id = i;
		}

		if( id != -1) known_clients[id].nr_of_shared_files = nr;

		log_file << nr << "\n";		

		char buf1[sizeof(struct client) * MAX_FILES];
		memset(buf1, '\0', sizeof(struct client) * MAX_FILES);

		recv(sockfd, buf1, sizeof(struct client) * MAX_FILES, 0);
		
		struct client *X = (struct client *)buf1;

		for(int i = 0; i < nr; ++i) {
			std::cout << X->shared[i].name << " " << X->shared[i].size << "\n"; 
			log_file  << X->shared[i].name << " " << X->shared[i].size << "\n";
			if( id != -1 ) {
				memset(known_clients[id].shared[i].name, '\0', NAME_LEN);
				sprintf(known_clients[id].shared[i].name, "%s", X->shared[i].name);

				memset(known_clients[id].shared[i].size, '\0', NAME_LEN);
				sprintf(known_clients[id].shared[i].size, "%s", X->shared[i].size);
			}
		}

	}

	if(cmd == history) {
		
		std::cout << client_name << "> history \n"; 
		log_file  << client_name << "> history \n"; 
		if( age == 0 ) return 0;
		std::cout << age << "\n"; 
		log_file  << age << "\n"; 
		for(int i = 0; i < age; i++) {
			std::cout << H[i] << "\n";
			log_file  << H[i] << "\n";
		}
	}

	if(cmd == NOP) {
		;
	}	
}
예제 #9
0
int main(int argc, char *argv[])
{
	check_args(argc, argv);
	
	/* Adresa IP a serverului */
	struct in_addr server_in_addr;
	inet_aton(ip_server, &server_in_addr);
	
	/* Adresa serverului */
	struct sockaddr_in server_addr;
	server_addr.sin_family = AF_INET;
	server_addr.sin_addr   = server_in_addr;
	server_addr.sin_port   = htons( (uint16_t)atoi(port_server) );

	/* Socket pe care se va face comunicarea cu serverul */
	sockfd = socket(AF_INET, SOCK_STREAM, 0);

	if( sockfd < 0 ) {
		std::cout << DBG << "Eroare deschidere socket.\n";
		CLOSE_FILE;
		exit(EXIT_FAILURE);	
	} else {
		std::cout << DBG << "Sa deschis socketul " << sockfd << ".\n";
	}

	/* Conectam clientul la server */
	int tmp = connect(sockfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr));
	if(tmp < 0) {
		std::cout << DBG << "Nu sa putut face conect(). \n";
		CLOSE_FILE
		exit(EXIT_FAILURE);
	} else {
		std::cout << DBG << "Socketul creeat anterior a fost conectat la server.\n";
	}

	/* Trimitem serverului numele si portul pe care va asculta clientul */	
	send(sockfd, client_name, BUFLEN, 0);
	send(sockfd, port_client, BUFLEN, 0);

	/* Adresa IP a masini pe care ruelaza clientul */
	struct in_addr        local_in_addr; 
	local_in_addr.s_addr = INADDR_ANY;
         
	/* Adresa clientului pe masina locala */             
	struct sockaddr_in    local_sockaddr_in;		             

	/* Se prezuma ca valoare stringul port_client poate fi convertit fara probleme la uint16_t */
	uint16_t port = (uint16_t)atoi(port_client);

	local_sockaddr_in.sin_family = AF_INET;
	local_sockaddr_in.sin_addr   = local_in_addr;
	local_sockaddr_in.sin_port   = htons( port );

	/* Socketul care va scana cereri de conexiune de la alti clienti*/
	listen_sockfd = socket(AF_INET, SOCK_STREAM, 0);

	/* Legam socketul in cauza de portul de scanare */
	tmp = bind(listen_sockfd, (struct sockaddr *) &local_sockaddr_in, sizeof(struct sockaddr));
	if(tmp < 0) {
		std::cout << DBG << " Eroare bind()\n";
		CLOSE_FILE;
		exit(EXIT_FAILURE);
	} else {
		std::cout << DBG << " Socketul creeat a fost legat cu succes de portul.\n";
	}

	/* Fortam socketul de ascultare al clientului sa asculte */
	listen(listen_sockfd, MAX_CLIENTS);
	
	

	char msg[BUFLEN];
	memset(msg, '\0', BUFLEN);

	fd_set read_fds;	                       /* multimea de citire folosita in select() */
       	fd_set tmp_fds;	                                             /* multime folosita temporar */
	fd_set write_fds;
     	int fdmax = (sockfd > listen_sockfd)?(sockfd + 1):(listen_sockfd + 1);           
					  /* valoare maxima file descriptor din multimea read_fds */

	struct timeval *timeout;           /* Necesara pentru a trezi selectul cind avem transfer */

	FD_ZERO(&read_fds);
	FD_ZERO(&tmp_fds);
	FD_ZERO(&write_fds);
	FD_SET(listen_sockfd, &read_fds);
	FD_SET(0, &read_fds);
	while(1) {
		timeout = NULL;
		/* Pentru toate operatiile active */
		bool have_transfer = false;
		for(int i = 0; i < active_PO; ++i) {
			if( PO[i].status == 1 && PO[i].type == send_file ) {
				have_transfer = true;
				/* 
				 * Daca avem cel putin o operatie netermianta (status = 1) de 
				 * trimitere, setam timeout pentru a nu risca sa ne blocam in select  
				 */
				if(timeout == NULL) {
					timeout = (struct timeval *)malloc( sizeof(struct timeval) );
					timeout->tv_sec = 0;
  					timeout->tv_usec = 10000;
				}

				memset((char*)buffer, '\0', SEND_SIZE);
				memset(msgaux, '\0', NAME_LEN);

				int x = fread(buffer, 1, SEND_SIZE, PO[i].file);
				
				if(x > 0) {
					/* Trimitem numarul de octeti cititi */					
					sprintf(msgaux, "%d", x);
					send(PO[i].sockfd, msgaux, NAME_LEN, 0);

					/* Trimitem octeti cititi */
					send(PO[i].sockfd, buffer, SEND_SIZE, 0);
				} else {
					/* Anuntam clientul ca am citit 0 oceti, deci transferul e finisat */
					sprintf(msgaux, "%d", 0);
					send(PO[i].sockfd, msgaux, 32, 0);

					/* Inchidem socketul de transfer, fisierul din care sa citit, setam statusul */
					close(PO[i].sockfd) ;
					fclose(PO[i].file) ;
					PO[i].status = 0;
					FD_CLR(PO[i].sockfd, &read_fds);
				}
			}
		}

/*              //oferim utilizatorului un prompt
		if( !have_transfer ) {
			std::cout << client_name << ">";
			std::cout.flush();
		}
*/
		tmp_fds = read_fds;
		
		if (select(fdmax + 1, &tmp_fds, NULL, NULL, timeout) == -1) {
			std::cout << DBG << " eroare select()\n";
			CLOSE_FILE;
			exit(EXIT_FAILURE);
		} 

		if(FD_ISSET(0, &tmp_fds)) {
			std::cin.getline(msg, BUFLEN, '\n');
			if ( strlen(msg) == 0 ) continue;

			if( strcmp(msg, "quit") == 0 ) {
				std::cout << DBG << "Inchidere client . . .\n";
			
				// Anuntam serverul despre exit
				send(sockfd, msg, NAME_LEN, 0);
				shutdown(sockfd, 2);

				/* DONE termina de trimis fisierele */
				for(int i = 0; i < active_PO; ++i) {
					if( PO[i].status == 1 && PO[i].type == send_file ) {
						int x = fread(buffer, 1, SEND_SIZE, PO[i].file);
						while( x > 0 ) {
							memset(msgaux, '\0', NAME_LEN);
							sprintf(msgaux, "%d", x);
							send(PO[i].sockfd, msgaux, NAME_LEN, 0);
			
							send(PO[i].sockfd, buffer, SEND_SIZE, 0);
							memset((char*)buffer, '\0', SEND_SIZE);
							x = fread(buffer, 1, SEND_SIZE, PO[i].file);
						}
						close(PO[i].sockfd) ;
						fclose(PO[i].file) ;
						PO[i].status = 0;
						FD_CLR(PO[i].sockfd, &read_fds);
					} 

					if( PO[i].status == 1 && PO[i].type == recv_file ) {
						close(PO[i].sockfd) ;
						fclose(PO[i].file) ;
						PO[i].status = 0;
						FD_CLR(PO[i].sockfd, &read_fds);
					}
				}
				/* DONE inchide toate coneiunile */
				close(sockfd);
				close(listen_sockfd);

				CLOSE_FILE;
				exit(EXIT_SUCCESS);
			}


			if( strncmp(msg, "getfile", 7) == 0 ) {

				char arg1[NAME_LEN], arg2[NAME_LEN];	
				extract_arguments(arg1, arg2, msg);

				//DONE verificam daca arg1 este un client valid, cunoscut

				int id = get_client_id(arg1);
				std::cout << arg1 << " " << id << "\n";
	
				if(id == -1) {
					std::cout << "-4 : Client necunoscut\n";
					continue;
				}

				bool ok = true;
				for(int i = 0; i < nr_of_shared_files; i++) {
					if( strcmp(shared[i].name, arg1) == 0 ) {
						ok = false;
					}
				}

				if( !ok ) {
					std::cout << "-6 : Fisier partajat cu acelasi nume\n";
					continue;
				}

				/* Adresa IP a peer-ului */
				struct in_addr peer_in_addr;
				inet_aton(known_clients[id].basic_info.IP, &peer_in_addr);
				//std::cout << "-----------IP-ul peerului " << known_clients[id].basic_info.IP << "\n";
	
				/* Adresa peerului */
				struct sockaddr_in peer_addr;
				peer_addr.sin_family = AF_INET;
				peer_addr.sin_addr   = peer_in_addr;
				peer_addr.sin_port   = htons( (uint16_t)known_clients[id].basic_info.port );

				/* Socket pe care se va face comunicarea ulterioara cu peerul */
				int peer_sockfd = socket(AF_INET, SOCK_STREAM, 0);				
				tmp = connect(peer_sockfd, (struct sockaddr *) &peer_addr, sizeof(struct sockaddr));
				if( tmp == -1 ) {
					std::cout << "-1 : Eroare la conectare\n";
					log_file  << "-1 : Eroare la conectare\n";
					close(peer_sockfd);
					continue;
				}
				FD_SET(peer_sockfd, &read_fds);
				if(fdmax < peer_sockfd + 1) fdmax = peer_sockfd + 1;
				
				send(peer_sockfd, msg, BUFLEN, 0);

				struct pending_operation op;
				memset(PO[active_PO].file_name, '\0', NAME_LEN);
				sprintf(PO[active_PO].file_name, "%s", arg2);
				memset(PO[active_PO].another, '\0', NAME_LEN);
				sprintf(PO[active_PO].another, "%s", arg1);
				PO[active_PO].status = op.status = 1; 
				PO[active_PO].type   = op.type   = recv_file;
				PO[active_PO].file   = op.file   = fopen(arg2, "wb");
				PO[active_PO].sockfd = op.sockfd = peer_sockfd;
				active_PO++;
				std::cout << "Sa pornit o operatie de primire pe " << active_PO-1 << "\n";
				/* //modul blocant
				char buffer[1024];
				memset((char*)buffer, '\0', 1024);
				char msgaux[32];
				memset(msgaux, '\0', 32);
				recv(PO[active_PO-1].sockfd, msgaux, 32, 0);
				std::cout << "Sa receptionat ca sau trimis " << msgaux << "octeti\n";
				
				int x;
				sscanf(msgaux, "%d", &x);
				while(x > 0) {
					std::cout << "Se primeste un bloc de 1024 cu" << x << "\n";
					recv(PO[active_PO-1].sockfd, buffer, 1024, 0);
					
					fwrite(buffer, 1, x, PO[active_PO-1].file);

					memset((char*)buffer, '\0', 1024);
					memset(msgaux, '\0', 32);
					recv(PO[active_PO-1].sockfd, msgaux, 32, 0);
					std::cout << "Sa receptionat ca sau trimis " << msgaux << "octeti\n";
					int y = sscanf(msgaux, "%d", &x);
					std::cout << "Sa receptionat ca sau trimis " << x << "octeti ca variabila int\n";
					if (y < 0) break;
				}
				std::cout << "Comunicarea din pareta receptorului incheiata.\n";
				close(PO[active_PO-1].sockfd) ;
				fclose(PO[active_PO-1].file) ;
				FD_CLR(PO[active_PO-1].sockfd, &read_fds);
				*/
				
			
			} else {
				if( strcmp(msg, "dump") == 0) {
					dump_known_clients();
				} else {			
					digest_user_comand(msg);
				}
			}

			FD_CLR(0, &tmp_fds);
		}

		for(int i = 1; i <= fdmax; ++i) {
			if( FD_ISSET(i, &tmp_fds) && i != sockfd ) {
				/* 
				 * Toata comunicatia cu rerverul se realizeaza doar
				 * in cadrul functiei digest_user_command();
				 * deci aici avem un transfer de fisier sau o cere 
				 * de conexiune pentru un nou transfer
				 */
				if( i == listen_sockfd ) {
					/* A venit o noua cere de transfer */
					struct sockaddr_in new_peer_addr_in;
					socklen_t len = sizeof(struct sockaddr_in);
					int new_peer_sockfd = accept(listen_sockfd,  (struct sockaddr *) &new_peer_addr_in, &len );
					FD_SET(new_peer_sockfd, &read_fds);
					if(fdmax < new_peer_sockfd + 1) fdmax = new_peer_sockfd + 1;

					memset(msg, '\0', BUFLEN);
					recv(new_peer_sockfd, msg, BUFLEN, 0);
					std::cout << "O noua cerere de transfer: " << msg << "\n";

					char arg1[NAME_LEN], arg2[NAME_LEN];	
					extract_arguments(arg1, arg2, msg);
					
					struct pending_operation op;
					PO[active_PO].status = op.status = 1; 
					PO[active_PO].type   = op.type   = send_file;
					PO[active_PO].file   = op.file   = fopen(arg2, "rb");
					PO[active_PO].sockfd = op.sockfd = new_peer_sockfd;
					active_PO++;
					std::cout << "Sa pornit o operatie de trimitere pe " << active_PO-1 << "\n";
					/*   //modul blocant
					char buffer[1024];
					char msgaux[32];
					memset((char*)buffer, '\0', 1024);
					int x = fread(buffer, 1, 1024, PO[active_PO-1].file);
					while( x > 0 ) {
						std::cout << "Sau citit : " << x << " octeti;\n";
						char msgaux[32];
						memset(msgaux, '\0', 32);
						sprintf(msgaux, "%d", x);
						send(PO[active_PO-1].sockfd, msgaux, 32, 0);
						std::cout << "Sa trimis ca sa citit : " << x << " octeti;\n";
			
						std::cout << "Sa trimis un bloc de 1024 cu" << x << "\n";
						send(PO[active_PO-1].sockfd, buffer, 1024, 0);
						memset((char*)buffer, '\0', 1024);
						x = fread(buffer, 1, 1024, PO[active_PO-1].file);
					}

					memset(msgaux, '\0', 32);
					sprintf(msgaux, "%d", 0);
					send(PO[active_PO-1].sockfd, msgaux, 32, 0);
					std::cout << "Comunicarea din pareta trimitatoruli incheiata.\n";
					close(PO[active_PO-1].sockfd) ;
					fclose(PO[active_PO-1].file) ;

					FD_CLR(PO[active_PO-1].sockfd, &read_fds);
					*/
				}
			} 
		}

		for(int i = 0; i < active_PO; i++) {
			if(PO[i].status == 0 || PO[i].type == send_file) continue;
			if( !FD_ISSET(PO[i].sockfd, &read_fds) ) continue;
			
			/* Pentru toate operatiile active de receptionare fisier */
			if(PO[i].status == 1 && PO[i].type == recv_file) {
				char buffer[SEND_SIZE];
				memset(buffer, '\0', SEND_SIZE);
				char msgaux[NAME_LEN];
				memset(msgaux, '\0', NAME_LEN);

				/* Receptionam citi octeti vom primi */
				recv(PO[i].sockfd, msgaux, NAME_LEN, 0);
				//std::cout << "Sa receptionat ca sau trimis " << msgaux << "octeti pe op. " << i << "\n" ;
				
				int x;
				sscanf(msgaux, "%d", &x);

				if(x > 0) {
					//std::cout << "Se primeste un bloc de 1024 cu" << x << "\n";
					recv(PO[i].sockfd, buffer, 1024, 0);
					
					fwrite(buffer, 1, x, PO[active_PO-1].file);

					memset((char*)buffer, '\0', 1024);
					memset(msgaux, '\0', 32);
				} else {
					//std::cout << "Comunicarea din pareta receptorului incheiata.\n";
					close(PO[i].sockfd) ;
					fclose(PO[i].file) ;
					FD_CLR(PO[i].sockfd, &read_fds);
					PO[i].status = 0;
					std::cout << "Succes " << PO[i].file_name << "\n";
					log_file  << client_name << "> getfile " << PO[i].another << " " << PO[i].file_name << "\n";
					log_file  << "Succes " << PO[i].file_name << "\n";

					memset(H[age], '\0', 128);
					sprintf(H[age], "%s %s", PO[i].another, PO[i].file_name);
					age++;
				}
			}	

		}

		
	}

}
예제 #10
0
파일: parser.cpp 프로젝트: maleadt/genetic
void Parser::evaluate_conditional(unsigned char* iBlock, unsigned int iSize, unsigned int& tLoc) {
    // Save conditional for later evaluation
    unsigned char tConditional = iBlock[tLoc++];

    // Extract tests and instructions
    std::vector<std::pair<unsigned int, unsigned int> > tTestBytecode = extract_arguments(iBlock, iSize, tLoc);
    std::vector<std::pair<unsigned int, unsigned int> > tInstructionBytecode = extract_instructions(iBlock, iSize, tLoc);
    if (tInstructionBytecode.size() > 0)
        tLoc = tInstructionBytecode.back().second + 1;

    // Extract an eventual else-set of instructions
    std::vector<std::pair<unsigned int, unsigned int> > tElseInstructionBytecode;
    if (iBlock[tLoc] == COND_ELSE) {
        tLoc++;
        tElseInstructionBytecode = extract_instructions(iBlock, iSize, tLoc);

        if (tElseInstructionBytecode.size() > 0)
            tLoc = tElseInstructionBytecode.back().second + 1;
    }

    // Check the evaluation type
    switch (tConditional) {
    case COND_IF:
    {
        // Evaluate the parameter
        Value tTest = evaluate_instruction(iBlock, iSize, tTestBytecode[0].first);
        if (tTest.getType() != BOOL)
            throw Exception(CONDITIONAL, "test passed to 'if' did not produce boolean value");

        // Evaluate the instructions
        if (tTest.getBool()) {
            for (unsigned int i = 0; i < tInstructionBytecode.size(); i++) {
                evaluate_instruction(iBlock, iSize, tInstructionBytecode[i].first);
            }
        } else {
            for (unsigned int i = 0; i < tElseInstructionBytecode.size(); i++) {
                evaluate_instruction(iBlock, iSize, tElseInstructionBytecode[i].first);
            }
        }
        break;
    }

    case COND_UNLESS:
    {
        // Evaluate the parameter
        Value tTest = evaluate_instruction(iBlock, iSize, tTestBytecode[0].first);
        if (tTest.getType() != BOOL)
            throw Exception(CONDITIONAL, "test passed to 'unless' did not produce boolean value");

        // Evaluate the instructions
        if (! tTest.getBool()) {
            for (unsigned int i = 0; i < tInstructionBytecode.size(); i++) {
                evaluate_instruction(iBlock, iSize, tInstructionBytecode[i].first);
            }
        } else {
            for (unsigned int i = 0; i < tElseInstructionBytecode.size(); i++) {
                evaluate_instruction(iBlock, iSize, tElseInstructionBytecode[i].first);
            }
        }
        break;
    }

    case COND_WHILE:
    {
        while (1) {
            // Evaluate the parameter
            unsigned int tLocCond = tTestBytecode[0].first;
            Value tTest = evaluate_instruction(iBlock, iSize, tLocCond);
            if (tTest.getType() != BOOL)
                throw Exception(CONDITIONAL, "test passed to 'while' did not produce boolean value");

            // Evaluate the instructions
            if (tTest.getBool()) {
                for (unsigned int i = 0; i < tInstructionBytecode.size(); i++) {
                    unsigned int tLocInst = tInstructionBytecode[i].first;
                    evaluate_instruction(iBlock, iSize, tLocInst);
                }
            } else {
                break;
            }
        }
        break;
    }
    }
}
예제 #11
0
파일: parser.cpp 프로젝트: maleadt/genetic
void Parser::validate_conditional(unsigned char* iBlock, unsigned int iSize, unsigned int& tLoc) {
    // Save conditional for later evaluation
    unsigned char tConditional = iBlock[tLoc++];

    // Extract tests and instructions
    std::vector<std::pair<unsigned int, unsigned int> > tTestBytecode = extract_arguments(iBlock, iSize, tLoc);
    std::vector<std::pair<unsigned int, unsigned int> > tInstructionBytecode = extract_instructions(iBlock, iSize, tLoc);
    if (tInstructionBytecode.size() > 0)
        tLoc = tInstructionBytecode.back().second + 1;

    // Extract an eventual else-set of instructions
    std::vector<std::pair<unsigned int, unsigned int> > tElseInstructionBytecode;
    if (iBlock[tLoc] == COND_ELSE) {
        tLoc++;
        tElseInstructionBytecode = extract_instructions(iBlock, iSize, tLoc);

        if (tElseInstructionBytecode.size() > 0)
            tLoc = tElseInstructionBytecode.back().second + 1;
    }


    // Check the evaluation type
    switch (tConditional) {
    case COND_IF:
    case COND_UNLESS:
    case COND_WHILE:
    {
        // Validate tests
        if (tTestBytecode.size() != 1)
            throw Exception(SYNTAX, "conditional only accepts one test");
        validate_instruction(iBlock, iSize, tTestBytecode[0].first);
        if (tTestBytecode[0].first != tTestBytecode[0].second)
            throw Exception(SYNTAX, "garbage after test");

        // Validate instruction block
        for (unsigned int i = 0; i < tInstructionBytecode.size(); i++) {
            validate_instruction(iBlock, iSize, tInstructionBytecode[i].first);
            if (tInstructionBytecode[i].first != tInstructionBytecode[i].second)
                throw Exception(SYNTAX, "garbage after instruction");
        }

        // Validate else-instruction block
        for (unsigned int i = 0; i < tElseInstructionBytecode.size(); i++) {
            validate_instruction(iBlock, iSize, tElseInstructionBytecode[i].first);
            if (tElseInstructionBytecode[i].first != tElseInstructionBytecode[i].second)
                throw Exception(SYNTAX, "garbage after else-instruction");
        }

        break;
    }

    case COND_ELSE:
    {
        throw Exception(GENERIC, "else-conditional cannot exist by itself");
    }

    default:
    {
        throw Exception(GENERIC, "conditional clause not implemented");
    }
    }
}