/* Parse a problem from a file */ int rp_parse_problem_file(rp_problem * problem, const char * filename) { rp_parser p; int res; rp_problem_create(problem,NULL); if ((res = rp_parser_create_file(&p,filename,rp_problem_symb(*problem)))) { if (!(res = rp_rule_problem(p,*problem))) { fprintf(stderr, "%s", rp_parser_error_msg(p)); fprintf(stderr,"\n"); } else { /* creation of initial box */ res = rp_problem_set_initial_box(*problem); } rp_parser_destroy(&p); } if (!res) { rp_problem_destroy(problem); } return( res); }
/* Parse an expression from a file */ int rp_parse_expression_file(rp_expression * e, const char * filename, rp_table_symbol ts) { rp_parser p; int res; if ((res = rp_parser_create_file(&p,filename,ts))) { rp_erep out; rp_erep_create(&out); if ((res = rp_rule_expr(p,&out))) { /* the memory for out is managed by e */ rp_expression_create(e,&out); } else { rp_erep_destroy(&out); fprintf(stderr,"%s", rp_parser_error_msg(p)); fprintf(stderr,"\n"); } rp_parser_destroy(&p); } return( res); }
/* Parse a constraint from a string */ int rp_parse_constraint_string(rp_constraint * c, const char * src, rp_table_symbol ts) { rp_parser p; int res; if ((res = rp_parser_create_string(&p,src,ts))) { if (!(res = rp_rule_constraint(p,c))) { fprintf(stderr, "%s", rp_parser_error_msg(p)); fprintf(stderr,"\n"); } rp_parser_destroy(&p); } return( res ); }
/* Parse a constant from a file */ int rp_parse_constant_file(rp_constant * out, const char * filename, rp_table_symbol ts) { rp_parser p; int res; if ((res = rp_parser_create_file(&p,filename,ts))) { if (!(res = rp_rule_constant(p,out))) { fprintf(stderr, "%s", rp_parser_error_msg(p)); fprintf(stderr,"\n"); } rp_parser_destroy(&p); } return( res); }
void* listen_to_client(void *temp_args) { struct thread_args *args = (struct thread_args *) temp_args; // Create request parser int error; rp_parser *parser; if ((error = rp_parser_create(&parser))) { printf("Failed to create parser: %s\n", rp_strerr(error)); } while (1) { //------------ RECEIVE REQUEST // Buffer for recv char buf[RECV_BUF_SIZE]; // Parser data structure rp_parser_reset(parser); int bytes_leftover = 0; // Set by rp_parse to indicate unparsed data leftover in buffer while (!parser->completed) { if (bytes_leftover >= RECV_BUF_SIZE) { printf("Unable to parse request. Single line of request longer than buffer\n"); goto exit; } // Keep leftover unparsed bytes in buffer int bytes_to_rec = RECV_BUF_SIZE - bytes_leftover; memset(buf + bytes_leftover, 0, bytes_to_rec); int bytes_received = recv(args->socket_fd, buf + bytes_leftover, bytes_to_rec, 0); if (bytes_received == 0) { printf("Client has closed connection\n"); goto exit; } if (bytes_received == -1) { printf("Recv error\n"); goto exit; } printf("Received message (%d/%d total):\n", bytes_received, bytes_received + bytes_leftover); printf("'%s'\n", buf); printf("Parsing request...\n"); if ((error = rp_parse(parser, buf, &bytes_leftover))) { printf("Failed to parse request: %s\n", rp_strerr(error)); goto exit; } } printf("Request successfully parsed:\n"); rp_parser_print(parser); //------------ SEND RESPONSE } exit: printf("Terminating client connection...\n"); // Destroy request parser rp_parser_destroy(parser); // Free args free(args); // Terminate thread pthread_exit(NULL); }