void FileTransfer::Run() { std::cout << "File Transfer service started" << std::endl; while (true) { Command *cmd = DeQ(); if (cmd == 0) // termination signal break; if (cmd->Name() == "PutFile") { PutFile * pf = dynamic_cast<PutFile*>(cmd); std::string newpath = _saveDir + "\\" + pf->_fname; (void) DeleteFile(newpath.c_str()); // delete target file just in case bool status = MoveFile(pf->_path.c_str(), newpath.c_str()) ? true : false; StatusMessage * resp = new StatusMessage(status); resp->SetConn(cmd->Msg().Conn()); Dispatch(resp); } else { std::cout << "Command " << cmd->Name() << " is not known" << std::endl; } delete cmd; } std::cout << "File Transfer service stopped" << std::endl; }
void Service::Execute(const std::string& host, int port, Command& cmd) { cmd.status() = Command::EXECUTING; Connection *con = new TCPConnection(host, port); if (!con->Open()) { delete con; throw "Could not connect to " + host; } con->Send(&cmd.Msg()); Message *rsp = con->Recv(); cmd.ReadMessage(*rsp); delete rsp; }