int main(int argc, char * argv[]) { // fork and execute the dataserver from (current) parent pid_t PID; if( (PID = fork()) < 0 ) { // error catching fprintf(stderr, "fork() error\n"); return 1; // ERROR: should not happen } else if ( PID == 0 ) { // Child code block // execvp() if ( execvp("./dataserver", argv) < 0 ) { fprintf(stderr, "execvp() error\n"); return 1; } printf("Should not print.\n"); } // Parent continues with requests usleep(100000); RequestChannel chanel("control",RequestChannel::CLIENT_SIDE); // construct client side pipe usleep(1000000); // test with "hello" string reply1 = chanel.send_request("hello"); std::cout << "Reply to request 'hello' is '" << reply1 << "'" << std::endl << flush; printf("=============TIMING=REQUEST========================\n"); assert(gettimeofday(&tp_start, 0) == 0); // test with "hello" reply1 = chanel.send_request("data " + int2string(4) ); std::cout << "Reply to request 'data 4' is '" << reply1 << "'" << std::endl << flush; assert(gettimeofday(&tp_end, 0) == 0); printf("Time taken for server request: "); print_time_diff(&tp_start, &tp_end); printf("\n"); printf("=============TIMING=REQUEST========================\n"); printf("=============TIMING=FAKE=REQUEST========================\n"); assert(gettimeofday(&tp_start, 0) == 0); // test with fake request reply1 = fake_request("This is a fake request."); std::cout << "Reply to request 'This is a fake request.' is '" << reply1 << "'" << std::endl << flush; assert(gettimeofday(&tp_end, 0) == 0); printf("Time taken for fake request: "); print_time_diff(&tp_start, &tp_end); printf("\n"); printf("=============TIMINGFAKE==REQUEST========================\n"); // test close with "quit" reply1 = chanel.send_request("quit"); std::cout << "Reply to request 'quit' is '" << reply1 << "'" << std::endl << flush; wait(&PID); return 0; }
int replay_log_record(HttpHandlerContext * const context, BufferedReadContext * const brc) { DBLog * const db_log = &app_context.db_log; if (db_log->db_log_fd == -1) { return 1; } char buf_cookie_head[sizeof DB_LOG_RECORD_COOKIE_HEAD - (size_t) 1U]; char buf_cookie_tail[sizeof DB_LOG_RECORD_COOKIE_TAIL - (size_t) 1U]; char buf_number[50]; char *pnt; char *endptr; const off_t current_offset = lseek(db_log->db_log_fd, (off_t) 0, SEEK_CUR); ssize_t readnb = buffered_read(brc, buf_cookie_head, sizeof buf_cookie_head); if (readnb == (ssize_t) 0) { return 1; } if (readnb != (ssize_t) sizeof buf_cookie_head || memcmp(buf_cookie_head, DB_LOG_RECORD_COOKIE_HEAD, readnb) != 0) { if (lseek(db_log->db_log_fd, current_offset, SEEK_SET) == (off_t) -1 || ftruncate(db_log->db_log_fd, current_offset) != 0) { } return -1; } pnt = buf_number; for (;;) { if (buffered_read(brc, pnt, (size_t) 1U) != (ssize_t) 1U) { return -1; } if (*pnt == ' ') { break; } if (++pnt == &buf_number[sizeof buf_number]) { return -1; } } if (*buf_number == *DB_LOG_RECORD_COOKIE_MARK_CHAR) { time_t ts = (time_t) strtol(buf_number + 1U, &endptr, 16); if (endptr == NULL || endptr == buf_number + 1U || ts <= (time_t) 0) { return -1; } char t; if (buffered_read(brc, &t, (size_t) 1U) != (size_t) 1U || t != *DB_LOG_RECORD_COOKIE_TIMESTAMP_CHAR) { return -1; } readnb = buffered_read(brc, buf_cookie_tail, sizeof buf_cookie_tail); if (readnb != (ssize_t) sizeof buf_cookie_tail || memcmp(buf_cookie_tail, DB_LOG_RECORD_COOKIE_TAIL, readnb) != 0) { return -1; } return 0; } int verb = (int) strtol(buf_number, &endptr, 16); if (endptr == NULL || endptr == buf_number) { return -1; } pnt = buf_number; for (;;) { if (buffered_read(brc, pnt, (size_t) 1U) != (ssize_t) 1U) { return -1; } if (*pnt == ':') { break; } if (++pnt == &buf_number[sizeof buf_number]) { return -1; } } size_t uri_len = (size_t) strtoul(buf_number, &endptr, 16); if (endptr == NULL || endptr == buf_number || uri_len <= (size_t) 0U) { return -1; } char *uri; if ((uri = malloc(uri_len + (size_t) 1U)) == NULL) { _exit(1); } if (buffered_read(brc, uri, uri_len) != (ssize_t) uri_len) { return -1; } *(uri + uri_len) = 0; pnt = buf_number; if (buffered_read(brc, pnt, (size_t) 1U) != (ssize_t) 1U || *pnt != ' ') { free(uri); return -1; } for (;;) { if (buffered_read(brc, pnt, (size_t) 1U) != (ssize_t) 1U) { free(uri); return -1; } if (*pnt == ':') { break; } if (++pnt == &buf_number[sizeof buf_number]) { free(uri); return -1; } } size_t body_len = (size_t) strtoul(buf_number, &endptr, 16); if (endptr == NULL || endptr == buf_number) { free(uri); return -1; } char *body = NULL; if (body_len > (size_t) 0U) { body = malloc(body_len + (size_t) 1U); if (body == NULL) { free(uri); _exit(1); } } if (body != NULL) { assert(body_len > (size_t) 0U); if (buffered_read(brc, body, body_len) != (ssize_t) body_len) { free(uri); free(body); return -1; } *(body + body_len) = 0; } readnb = buffered_read(brc, buf_cookie_tail, sizeof buf_cookie_tail); if (readnb != (ssize_t) sizeof buf_cookie_tail || memcmp(buf_cookie_tail, DB_LOG_RECORD_COOKIE_TAIL, readnb) != 0) { free(body); free(uri); return -1; } fake_request(context, verb, uri, body, body_len, 1, 0); free(body); free(uri); return 0; }