common::Error HttpClient::send_error(common::http::http_protocols protocol, common::http::http_status status, const char* extra_header, const char* text, bool is_keep_alive, const HttpServerInfo& info) { CHECK(protocol <= common::http::HP_1_1); const std::string title = common::ConvertToString(status); char err_data[1024] = {0}; off_t err_len = common::SNPrintf(err_data, sizeof(err_data), HTML_PATTERN_ISISSSS7, status, title, status, title, text, info.server_url, info.server_name); common::Error err = send_headers(protocol, status, extra_header, "text/html", &err_len, nullptr, is_keep_alive, info); if (err && err->isError()) { DEBUG_MSG_ERROR(err); } ssize_t nwrite = 0; err = write(err_data, err_len, &nwrite); if (err && err->isError()) { DEBUG_MSG_ERROR(err); } return err; }
/********************************************************************* Main Program Loop **********************************************************************/ int main() { /* Initializations */ debug_init(); /* This should be first. */ timer_init(); /* This should be before any GPIO activities. */ uint32 ret_val = bcm2835_init(); if ( ret_val == 0 ) { DEBUG_MSG_ERROR("bcm2835_init() failed."); } pwm_init(); pump_init(); therm_init(); pid_init(); pump_start(); /* Take temperature as input from console. */ float setpoint; printf("Set your desired temperature: "); scanf("%f", &setpoint); pid_update_temp_setpoint(setpoint); pid_gain_params pid_gain; pid_gain.k_p = 1; pid_gain.k_d = 1; pid_gain.k_i = 1; pid_gain.k_windup = 1; pid_set_gain(&pid_gain); /* Main Program Loop */ while (1) { pwm_run(); therm_capture(); pid_loop(); } pump_stop(); /* De-initializations */ pump_deinit(); pid_deinit(); pwm_deinit(); /* This should be after all GPIO activities. */ ret_val = bcm2835_close(); if ( ret_val == 0 ) { DEBUG_MSG_ERROR("bcm2835_close() failed."); } timer_deinit(); debug_deinit(); /* This should be last. */ return 0; }
common::Error Http2Client::send_error(common::http::http_protocols protocol, common::http::http_status status, const char* extra_header, const char* text, bool is_keep_alive, const HttpServerInfo& info) { if (is_http2() && protocol == common::http::HP_2_0) { const std::string title = common::ConvertToString(status); char err_data[1024] = {0}; off_t err_len = common::SNPrintf(err_data, sizeof(err_data), HTML_PATTERN_ISISSSS7, status, title, status, title, text, info.server_url, info.server_name); common::Error err = send_headers(protocol, status, extra_header, "text/html", &err_len, nullptr, is_keep_alive, info); if (err && err->isError()) { DEBUG_MSG_ERROR(err); return err; } StreamSPtr header_stream = findStreamByType(common::http2::HTTP2_HEADERS); if (!header_stream) { return DEBUG_MSG_PERROR("findStreamByType", EAGAIN); } common::http2::frame_hdr hdr = common::http2::frame_data::create_frame_header( common::http2::HTTP2_FLAG_END_STREAM, header_stream->sid(), err_len); common::http2::frame_data fdata(hdr, err_data); return header_stream->sendFrame(fdata); } return HttpClient::send_error(protocol, status, extra_header, text, is_keep_alive, info); }
void TcpClient::close() { if (server_) { server_->closeClient(this); } common::Error err = sock_.close(); if (err && err->isError()) { DEBUG_MSG_ERROR(err); } }
common::Error Http2Client::send_file_by_fd(common::http::http_protocols protocol, int fdesc, off_t size) { if (is_http2() && protocol == common::http::HP_2_0) { StreamSPtr header_stream = findStreamByType(common::http2::HTTP2_HEADERS); if (!header_stream) { return DEBUG_MSG_PERROR("findStreamByType", EAGAIN); } SendDataHelper help; help.header_stream = header_stream; help.all_size = size; common::Error err = common::file_system::read_file_cb(fdesc, 0, size, &send_data_frame, &help); if (err && err->isError()) { DEBUG_MSG_ERROR(err); return err; } return common::Error(); } return HttpClient::send_file_by_fd(protocol, fdesc, size); }
void InnerServerCommandSeqParser::handleInnerDataReceived(InnerClient* connection, char* buff, uint32_t buff_len) { ssize_t nwrite = 0; char* end = strstr(buff, END_OF_COMMAND); if (!end) { DEBUG_MSG_FORMAT<MAX_COMMAND_SIZE>(common::logging::L_WARNING, "UNKNOWN SEQUENCE: %s", buff); const cmd_responce_t resp = make_responce(next_id(), STATE_COMMAND_RESP_FAIL_1S, buff); common::Error err = connection->write(resp, &nwrite); if (err && err->isError()) { DEBUG_MSG_ERROR(err); } connection->close(); delete connection; return; } *end = 0; char* star_seq = NULL; cmd_id_t seq = strtoul(buff, &star_seq, 10); if (*star_seq != ' ') { DEBUG_MSG_FORMAT<MAX_COMMAND_SIZE>(common::logging::L_WARNING, "PROBLEM EXTRACTING SEQUENCE: %s", buff); const cmd_responce_t resp = make_responce(next_id(), STATE_COMMAND_RESP_FAIL_1S, buff); common::Error err = connection->write(resp, &nwrite); if (err && err->isError()) { DEBUG_MSG_ERROR(err); } connection->close(); delete connection; return; } const char* id_ptr = strchr(star_seq + 1, ' '); if (!id_ptr) { DEBUG_MSG_FORMAT<MAX_COMMAND_SIZE>(common::logging::L_WARNING, "PROBLEM EXTRACTING ID: %s", buff); const cmd_responce_t resp = make_responce(next_id(), STATE_COMMAND_RESP_FAIL_1S, buff); common::Error err = connection->write(resp, &nwrite); if (err && err->isError()) { DEBUG_MSG_ERROR(err); } connection->close(); delete connection; return; } size_t len_seq = id_ptr - (star_seq + 1); cmd_seq_t id = std::string(star_seq + 1, len_seq); const char* cmd = id_ptr; int argc; sds* argv = sdssplitargs(cmd, &argc); processRequest(id, argc, argv); if (argv == NULL) { DEBUG_MSG_FORMAT<MAX_COMMAND_SIZE>(common::logging::L_WARNING, "PROBLEM PARSING INNER COMMAND: %s", buff); const cmd_responce_t resp = make_responce(id, STATE_COMMAND_RESP_FAIL_1S, buff); common::Error err = connection->write(resp, &nwrite); if (err && err->isError()) { DEBUG_MSG_ERROR(err); } connection->close(); delete connection; return; } DEBUG_MSG_FORMAT<MAX_COMMAND_SIZE>( common::logging::L_INFO, "HANDLE INNER COMMAND client[%s] seq:% " CID_FMT ", id:%s, cmd: %s", connection->formatedName(), seq, id, cmd); if (seq == REQUEST_COMMAND) { handleInnerRequestCommand(connection, id, argc, argv); } else if (seq == RESPONCE_COMMAND) { handleInnerResponceCommand(connection, id, argc, argv); } else if (seq == APPROVE_COMMAND) { handleInnerApproveCommand(connection, id, argc, argv); } else { NOTREACHED(); } sdsfreesplitres(argv, argc); }