void ClientInvade::moveCommander(const Position origin, const Position dest) { QJsonObject p; p["origin"] = pos2str(origin); p["dest"] = pos2str(dest); sendMessage("moveCommander", p); }
void ClientInvade::attack(const Position origin, const Position dest, bool bombshell) { QJsonObject p; p["origin"] = pos2str(origin); p["dest"] = pos2str(dest); p["bombshell"] = bombshell; sendMessage("attack", p); }
void ClientInvade::addUnit(const Position position, const UnitType type) { QJsonObject p; p["position"] = pos2str(position); p["type"] = static_cast<int>(type.id()); sendMessage("addUnit", p); }
void print_pipe_info(char *prefix, enum cmd_pos pos, int left_pipe[], int right_pipe[], char* msg) { struct stat left_stat , right_stat; char left_str[32]; char right_str[32]; char padd[] = " "; if (pos == first || pos == middle) { if (fstat(right_pipe[0], &right_stat) == -1) { perror("fstat ..."); exit(EXIT_FAILURE); } sprintf(right_str, "[%ld]", (long) right_stat.st_ino); } else { sprintf(right_str, "%s", padd); } if (pos == middle || pos == last) { if (fstat(left_pipe[0], &left_stat) == -1) { perror("fstat ..."); exit(EXIT_FAILURE); } sprintf(left_str, "[%ld]", (long) left_stat.st_ino); } else { sprintf(left_str, "%s", padd); } fprintf(stderr, "%s %s (%s) %s\n", prefix, left_str, pos2str(pos, 0), right_str); fflush(NULL); }
void print_info(char *prefix, enum cmd_pos pos, int left_pipe[], int right_pipe[], char* msg) { struct stat s; if (pos == first || pos == middle) { if (fstat(right_pipe[0], &s) == -1) { perror("fstat ..."); exit(EXIT_FAILURE); } printf("[%ld][%d,%d]\n", (long) s.st_ino, right_pipe[0], right_pipe[1]); } printf("%s [%2d, %2d] (%s) [%2d, %2d] %s\n", prefix, left_pipe[0], left_pipe[1], pos2str(pos, 0), right_pipe[0], right_pipe[1], msg); fflush(NULL); }
/** * DESCRIPTION: * * Prints a promt to the screen. Reads a command line of the following format from the the user: * * cmd0 -a00 -b01 ... -z0n | cmd1 | cmd2 | ... | cmdN -an0 ... -znn * * For each command, the fork_and_pipe() function is called with the * appropiate argv and relative position of the command. * * NOTE: * * You only have to add code at the end of main, see TODO comment. */ int main() { // Allocate a buffer for the command line read from the user. char line_buffer[COMMAND_LINE_BUFFER_SIZE]; // We parse the command line using the next_command() parser. The // parser will populate the following array with the result of each call // to the parse. char* argv[MAX_ARGV_SIZE]; // Count the number of non empty command lines. int line_nr = 0; // Position of each command in a command line. enum cmd_pos pos; // {single, first, middle, last} // Pipe read descriptor to the pipe to the "left". int left_pipe_read_fd = -1; // Count the number of children forked for each command line. int children = 0; while(1) { do { // Print the command prompt including the command line counter. printf(" %d> ", line_nr); fflush(NULL); // Read input from the user. if ( fgets(line_buffer, COMMAND_LINE_BUFFER_SIZE, stdin) == NULL) { perror("====== ERROR ====> READING COMMAND LINE FAILED :("); exit(EXIT_FAILURE); } // Exit if the user types "exit" and presses enter. if (strcmp(line_buffer, "exit\n") == 0) { printf(" Goodbye!\n"); exit(EXIT_SUCCESS); } // If the user presses enter without typing anything else, don't // update the command line counter, just start over again. } while (empty_line(line_buffer)); // We got some input from the user. line_nr++; // Parse the command line do { pos = next_command(line_buffer, argv); // After the call to the next_command() parser function, the // command possition within the command line is now available in the pos // varialble. DBG("\n%6s command %s\n", pos2str(pos), argv[0]); // Loop through the argv and print the command data. int i = 0; while (argv[i] != NULL) { DBG(" argv[%d] @ [0x%x] = \"%s\"\n", i, (unsigned int) argv[i], argv[i]); i++; } // Create a pipe fork a new process for the command. We also // must remember the read descriptor to the new pipe. This // descriptor will become the read descriptor to the pipe to the // "left" for the next command (if any). left_pipe_read_fd = pipe_and_fork(pos, argv, left_pipe_read_fd); children++; // When are we done? } while (pos != single && pos != last); DBG("\nAfter calling next_command(), line [0x%X] ==>%s<==\n", (unsigned int) &line_buffer, line_buffer); // The parent goes here after after all children have been // created for the command. // TODO: Make sure shell doesn't print a new prompt until // all the command processes (children) have terminated. while(children>0){ wait(NULL); children--; } } // end while(1) } // end of main()
/** * DESCRIPTION: * * Command lines read from the user have the following generic format: * * cmd0 | cmd1 | cmd2 | ... | cmdN * * , i.e., a serie of command connected with pipes. In this sequence, * the command have the following relatve positions: * * cmd0 - first * cmd1 - middle * cmd2 - middle * cmdN - last * * If the command line only have a single command as in this example: * * cmd * * , the position of this lonely command is defined as single. * * To communicate, the commands writes and read data to various * pipes. In the fist example above: * * cmd0 - must redirect STDOUT to write to the pipe * "to the right" of cmd0. * cmd1 - must redirect STDID to read read from the pipe * "to the left" of cmd1. * must redirect STDOUT to write to the pipe * "to the right" of cmd1. * . * . * . * cmdN - must redirct STDID to read read from the pipe * "to the left" of cmdN. * * This function must create pipes and rediret STDIN and STDOUT as * described above and unused pipe descriptors must be closed. Once * this is done, a new process for the command can be created. * * * INPUT: * * pos the relative position of the command. This should be one * of {single, first, middle, last}. * * argv the argv for the command. * * left_pipe-read_fd * To enable one command to be aware of the pipe "to the left" * the read descriptor of this pipe must be given as an argument * to this function. * * * OUTPUT: * * The read descripor of the newly created pipe (right pipe) is * returned. This descriptor will then become the read descriptor * to the left pipe for the next command. * * * NOTE: * * You must add code at various places, see TODO comments. */ int pipe_and_fork(enum cmd_pos pos, char* argv[], int left_pipe_read_fd) { // A pair of file descriptors used for the pipe. int new_pipe[2]; DBG("pos = %s\n", pos2str(pos)); // TODO: The pipe must be created before the fork(), but only create // a pipe if needed (use pos to make decision). if(pos==first){ if (pipe(new_pipe)==-1){ perror("er-pipe"); exit(1); //errror } }else if(pos==middle){ if (pipe(new_pipe)==-1){ //error perror("ex2-pipe"); exit(2); } } // Once the pipe is created we can fork a child process for the command. pid_t pid = fork(); switch(pid) { case 0: // == CHILD == DBG("CHILD <%ld> %s says HELLO!\n", (long) getpid(), argv[0]); // child_command(pos, argv, left_pipe_read_fd, new_pipe); // This will never be reached when you have sucessfully managed to call execv(). exit(EXIT_FAILURE); case -1: // == ERROR == perror("======= ERROR ====> Forking child process failed."); exit(EXIT_FAILURE); default: // == PARENT == DBG("PARENT <%llu> just forked child <%llu> %s\n", (long long unsigned)getpid(), (long long unsigned)pid, argv[0]); // TODO: Close descriptors if necessary. close(new_pipe[1]); // Return the read descriptor of the newly created pipe. This // descriptor is needed by the next child. This is the left_pipe_read_fd // for the next command. return new_pipe[0]; } // end-switch(pid) }
void Detect::detect(const cv::Mat &origin, Detect::RCS &standups) { stamp_ = now(); cnt_++; origin_ = origin; // //if (masked_) { // cv::bitwise_and(origin, mask_, origin); //} //imwrite("bitwise.bmp", origin); //printf("writed\n"); // cv::cvtColor(origin, gray_curr_, cv::COLOR_BGR2GRAY); if (gray_prev_.cols == 0) { gray_prev_ = gray_curr_.clone(); } RCS motions; std::vector<Dir> dirs; detect(motions, dirs); cv::swap(gray_prev_, gray_curr_); //assert(dirs.size() == motions.size()); if (dirs.size() > 0) { int a = 0; } // 处理 motions/dirs 对 standups_ 的影响 ... for (size_t i = 0; i < dirs.size(); i++) { STANDUPS::iterator it = find_crossed_target(motions[i]); if (it == standups_.end()) { // 新的活动区域 ... // 如果是上升,则创建一个新的 standup if (dirs[i] == UP) { Standup s; s.enable_od = od_->enabled(); std::vector<cv::Rect> faces; if (s.enable_od) { // 在 motions[i].pos 的上半部分进行头肩识别 ... cv::Rect roi = motions[i]; roi.height /= 2; // FIXME: faces = od_->detect(gray_curr_, roi); if (faces.empty()) { log("WRN: %u: 新目标:%s, 但是找不到头肩,失败\n", cnt_, pos2str(motions[i]).c_str()); continue; } else { s.face = faces[0]; } } s.pos = motions[i]; s.stamp = stamp_; s.waiting = false; standups_.push_back(s); if (s.enable_od) { log("INFO: %u: 新目标:%s, 头肩位置: %s, %lu\n", cnt_, pos2str(s.pos).c_str(), pos2str(s.face).c_str(), faces.size()); } else { log("INFO: %u: 新目标:%s\n", cnt_, pos2str(s.pos).c_str()); } } } else if (it->waiting) { // 等待期,直接忽略新的活动 ... log("WRN: %u: 忽略 Waiting 活动, %s: pos=%s\n", cnt_, DirDesc[dirs[i]], pos2str(it->pos).c_str()); } else { // 进入等待期 ... it->waiting = true; it->stamp = stamp_; log("INFO: %u: 目标消失,进入Waiting,%s: pos=%s\n", cnt_, DirDesc[dirs[i]], pos2str(it->pos).c_str()); } } // 检查超时 ... for (STANDUPS::iterator it = standups_.begin(); it != standups_.end();) { if (stamp_ - it->stamp > max_duration_) { log("WRN: %u: 删除超时站立: %s, max_duration=%.2f\n", cnt_, pos2str(it->pos).c_str(), max_duration_); it = standups_.erase(it); // 超时删除 ... } else if (it->waiting) { if (stamp_ - it->stamp > waiting_) { log("INFO: %u: 删除waiting区域: %s\n", cnt_, pos2str(it->pos).c_str()); it = standups_.erase(it); // 删除 waiting ... } else { ++it; } } else { standups.push_back(it->pos); // 有效目标 ... ++it; } } }