int main() { srand(time_t(NULL)); Pipe p; bool isConnect = p.connect(); string ans; while (!isConnect) { cout << "cant connect to graphics" << endl; cout << "Do you try to connect again or exit? (0-try again, 1-exit)" << endl; cin >> ans; if (ans == "0") { cout << "trying connect again.." << endl; Sleep(5000); isConnect = p.connect(); } else { p.close(); return 0; } } char msgToGraphics[1024]; const string START_MASSAGE = "rnbkqbnrpppppppp################################PPPPPPPPRNBKQBNR0"; // msgToGraphics should contain the board string accord the protocol // YOUR CODE strcpy_s(msgToGraphics, START_MASSAGE.c_str()); // just example... Board board(START_MASSAGE); p.sendMessageToGraphics(msgToGraphics); // send the board string // get message from graphics string msgFromGraphics = p.getMessageFromGraphics(); while (msgFromGraphics != "quit") { // should handle the string the sent from graphics // according the protocol. Ex: e2e4 (move e2 to e4) for(char i = Location::MIN_ROW; i <= Location::MAX_ROW; i++) { for(char j = Location::MIN_COL; j <= Location::MAX_COL; j++) { if(board[Location(j, i)] != nullptr) { cout << board[Location(j, i)]->get_type(); if((board[Location(j, i)]->get_color() == WHITE)) cout << "W "; else cout << "B "; } else { cout << "## "; } } cout << std::endl; } //WIP: print recived index and index on board //cout << msgFromGraphics[0] << msgFromGraphics[1] << Location(msgFromGraphics[0], msgFromGraphics[1]).get_row() << Location(msgFromGraphics[0], msgFromGraphics[1]).get_col() << std::endl; MoveResult result = board.move(Location(msgFromGraphics[0], msgFromGraphics[1]), Location(msgFromGraphics[2], msgFromGraphics[3])); strcpy_s(msgToGraphics, std::to_string(result).c_str()); // msgToGraphics should contain the result of the operation // return result to graphics p.sendMessageToGraphics(msgToGraphics); // get message from graphics msgFromGraphics = p.getMessageFromGraphics(); } p.close(); }
void testWriteRead () { char outBuff[256]; char inBuff[256]; size_t nbw; size_t nbr; strcpy (outBuff, "MyServer is a powerful and easy to configure web server"); int ret = pipe->create (); CPPUNIT_ASSERT_EQUAL (ret, 0); ret = pipe->write (outBuff, 256, &nbw); CPPUNIT_ASSERT_EQUAL (ret, 0); ret = pipe->read (inBuff, 256, &nbr); CPPUNIT_ASSERT_EQUAL (ret, 0); CPPUNIT_ASSERT_EQUAL (nbr, nbw); CPPUNIT_ASSERT (strcmp (outBuff, inBuff) == 0); pipe->close (); }
void testCreateClose () { int ret = pipe->create (); CPPUNIT_ASSERT_EQUAL (pipe->pipeTerminated (), false); CPPUNIT_ASSERT_EQUAL (ret, 0); pipe->close (); CPPUNIT_ASSERT_EQUAL (pipe->pipeTerminated (), true); }
void testInverted () { Pipe pipeInv; int ret = pipe->create (); CPPUNIT_ASSERT_EQUAL (ret, 0); pipe->inverted (pipeInv); CPPUNIT_ASSERT (pipeInv.getReadHandle () != pipe->getReadHandle ()); CPPUNIT_ASSERT (pipeInv.getWriteHandle () != pipe->getWriteHandle ()); pipe->close (); }
void testWaitForData () { char outBuff[256]; size_t nbw; strcpy (outBuff, "MyServer is a powerful and easy to configure web server"); int ret = pipe->create (); CPPUNIT_ASSERT_EQUAL (pipe->waitForData (0, 100), 0); ret = pipe->write (outBuff, 256, &nbw); CPPUNIT_ASSERT_EQUAL (pipe->waitForData (0, 100), 1); pipe->close (); }
/** start process and return the handle of it argv[0] = exe */ static inline bool Start(Handle* hdl, const std::string& exe, const std::vector<std::string>& arg, bool closeStdio = true) { #ifdef _WIN32 cybozu::disable_warning_unused_variable(closeStdio); PROCESS_INFORMATION pi; STARTUPINFO si; memset(&si, 0, sizeof(si)); si.cb = sizeof(si); std::string cmdLine = "\"" + exe + '"'; for (size_t i = 0, n = arg.size(); i < n; i++) { cmdLine += " \"" + escape(arg[i]) + '"'; } cmdLine += '\0'; if (!CreateProcess(exe.c_str(), &cmdLine[0], NULL, NULL, FALSE /* Advanced Windows p.123 */ , CREATE_NO_WINDOW | CREATE_NEW_PROCESS_GROUP | CREATE_DEFAULT_ERROR_MODE, NULL, 0, &si, &pi)) { return false; } if (pi.hThread) CloseHandle(pi.hThread); // if (pi.hProcess) CloseHandle(pi.hProcess); *hdl = pi.hProcess; return true; #else Pipe pipe; if (!pipe.init()) { return false; } pid_t pid = fork(); if (pid < 0) { return false; } else if (pid == 0) { pipe.close(Pipe::Read); std::vector<char*> argv; argv.push_back(const_cast<char*>(exe.c_str())); for (size_t i = 0, n = arg.size(); i < n; i++) { argv.push_back(const_cast<char*>(arg[i].c_str())); } argv.push_back(0); if (closeStdio) { if (!toDevNull(0, O_RDONLY)) goto ERR_EXIT; if (!toDevNull(1, O_RDWR)) goto ERR_EXIT; if (!toDevNull(2, O_RDWR)) goto ERR_EXIT; } { const int ret = static_cast<int>(sysconf(_SC_OPEN_MAX)); const int maxFd = ret < 0 ? 1024 : maxFd; for (int i = 3; i < maxFd; i++) { close(i); } } execv(exe.c_str(), &argv[0]); ERR_EXIT: /* write pipe if execv fails */ pipe.write("x", 1); pipe.close(Pipe::Write); ::exit(1); } pipe.close(Pipe::Write); char buf[1]; /* if execv succeeds then Pipe::Read is closed and returns error otherwise gets 'x' from child */ if (pipe.read(buf, 1) == 1) { waitpid(pid, 0, WNOHANG); return false; } *hdl = pid; return true; #endif }
BaseDaemon::~BaseDaemon() { writeSignalIDtoSignalPipe(SignalListener::StopThread); signal_listener_thread.join(); signal_pipe.close(); }