void Rsh::receive() { std::string temp = ""; while(true) { int len = 0; char* msg = shareinfo->get_message(len); if(msg != NULL) { temp.append(msg, len); int pos = temp.rfind('\n'); if(pos != std::string::npos) { std::string substr = temp.substr(0, pos+1); write_to_output(substr.c_str(), substr.size()); shell_prompt(); temp.erase(0, pos+1); } delete msg; } int* requests = shareinfo->get_requests(len); if(requests != NULL) { for(int i = 0; i < len; i++) { int fd = open(get_fifo_id(partners[requests[i]], shareinfo->get_id()), O_RDONLY, 0666); if(fd != -1) fifomgt->insert(std::pair<int, int>(requests[i], fd)); } delete requests; } usleep(100000); } }
static void dynamic_initial(void) { conn = setup_connection(); /* initialization require redraw too */ interval_level = 1; quit_signal = 0; crt_menu = 0; /** windows chain initilization **/ wchain_init(); /* check the screen size */ if(stdscr->_maxy < 3 || stdscr->_maxx < 68) { endwin(); fprintf(stderr, "screen too small for normally displaying.\n"); exit(1); } /** BasicInfo initialization **/ basic_info = basic_info_setup(); /** songlist arguments */ songlist = songlist_setup(); songlist_update(); /** directory arguments **/ directory = directory_setup(); directory_update(); /** playlist arguments **/ playlist = playlist_setup(); playlist_update(); /** the visualizer **/ visualizer = visualizer_setup(); get_fifo_id(); /** windows set initialization **/ being_mode_update(&basic_info->wmode); //being_mode_update(&songlist->wmode); }
void Rsh::reset() { if(sin != STD) close(curin); if(sin == FIFO) { for(FifoMgt::iterator it = fifomgt->begin(); it != fifomgt->end(); it++) if(curin == it->second) { unlink(get_fifo_id(it->first, shareinfo->get_id())); fifomgt->erase(it); break; } } if(sout >= FIL) close(curout); sin = STD; sout = STD; curin = STDIN_FILENO; curout = out; command.clear(); }
void Rsh::onTranOut() { if(command.size() > 0) { int userid = scanner->get_integerVal(); if(userid == 0) { TOKEN next; for(next = scanner->lex(); next == SPACE; next = scanner->lex()); if(next == WORD) { std::string* path = scanner->get_stringVal(); path->insert(0, "./"); int fd = open(path->c_str(), O_WRONLY|O_CREAT, 0666); if(fd >= 0) { curout = fd; sout = FIL; } else { std::string message = "open fault: "; message = message + *(scanner->get_stringVal()) + "\n"; write(out, message.c_str(), message.size()); reset(); } } else { std::string message = "There should have filename after >\n"; write(out, message.c_str(), message.size()); reset(); } } else { if(userid <= partners[0] && partners[userid] != 0) { if(mkfifo(get_fifo_id(shareinfo->get_id(), partners[userid]), 0666) == 0) { ShareRsh* target = shm_map(partners[userid]); if(target != NULL) { std::string msg = ""; for(std::vector<std::string*>::iterator it = command.begin(); it != command.end(); it++) msg.append(*(*it) + " "); msg.erase(msg.size()-1); char buf[128]; int n = sprintf(buf, "*****Userid:%d --- Username:%s just piped '%s' to Userid:%d --- Username:%s*****\n", shareinfo->get_id(), shareinfo->get_name(), msg.c_str(), target->get_id(), target->get_name()); // broadcast(buf, n); target->add_request(shareinfo->get_id()); munmap(target, sizeof(target)); int fd = open(get_fifo_id(shareinfo->get_id(), partners[userid]), O_WRONLY, 0666); curout = fd; sout = FIFO; } } else { char buf[64]; int n = sprintf(buf, "*** Error: the pipe #%d->#%d already exists. ***", shareinfo->get_id(), userid); write(out, buf, n); reset(); } } else { std::string message = "This user is not exist\n"; write(out, message.c_str(), message.size()); reset(); } } } else { std::string message = "There should have command before >\n"; write(out, message.c_str(), message.size()); reset(); } }