[807] | 1 | // |
---|
| 2 | // ******************************************************************** |
---|
| 3 | // * License and Disclaimer * |
---|
| 4 | // * * |
---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
| 7 | // * conditions of the Geant4 Software License, included in the file * |
---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
| 9 | // * include a list of copyright holders. * |
---|
| 10 | // * * |
---|
| 11 | // * Neither the authors of this software system, nor their employing * |
---|
| 12 | // * institutes,nor the agencies providing financial support for this * |
---|
| 13 | // * work make any representation or warranty, express or implied, * |
---|
| 14 | // * regarding this software system or assume any liability for its * |
---|
| 15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
| 16 | // * for the full disclaimer and the limitation of liability. * |
---|
| 17 | // * * |
---|
| 18 | // * This code implementation is the result of the scientific and * |
---|
| 19 | // * technical work of the GEANT4 collaboration. * |
---|
| 20 | // * By using, copying, modifying or distributing the software (or * |
---|
| 21 | // * any work based on the software) you agree to acknowledge its * |
---|
| 22 | // * use in resulting scientific publications, and indicate your * |
---|
| 23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 24 | // ******************************************************************** |
---|
| 25 | // |
---|
[1337] | 26 | // $Id: G4MPIsession.cc,v 1.2 2010/05/18 06:06:21 kmura Exp $ |
---|
| 27 | // $Name: geant4-09-04-beta-01 $ |
---|
[807] | 28 | // |
---|
| 29 | // ==================================================================== |
---|
| 30 | // G4MPIsession.cc |
---|
| 31 | // |
---|
| 32 | // 2007 Q |
---|
| 33 | // ==================================================================== |
---|
| 34 | #include "G4MPIsession.hh" |
---|
| 35 | #include "G4MPImanager.hh" |
---|
| 36 | #include "G4RunManager.hh" |
---|
| 37 | #include "G4UImanager.hh" |
---|
| 38 | #include "G4UIcommand.hh" |
---|
| 39 | #include "G4UIcsh.hh" |
---|
| 40 | #include "G4UImpish.hh" |
---|
| 41 | |
---|
| 42 | // ==================================================================== |
---|
| 43 | // |
---|
| 44 | // class description |
---|
| 45 | // |
---|
| 46 | // ==================================================================== |
---|
| 47 | |
---|
| 48 | ////////////////////////////////////////////// |
---|
| 49 | G4MPIsession::G4MPIsession(G4VUIshell* ashell) |
---|
| 50 | : G4VMPIsession() |
---|
| 51 | ////////////////////////////////////////////// |
---|
| 52 | { |
---|
| 53 | G4UImanager* UI= G4UImanager::GetUIpointer(); |
---|
| 54 | UI-> SetSession(this); |
---|
| 55 | UI-> SetCoutDestination(this); |
---|
| 56 | |
---|
| 57 | // shell |
---|
| 58 | if(isMaster) { |
---|
| 59 | if(ashell) { |
---|
| 60 | shell= ashell; |
---|
| 61 | } else { |
---|
| 62 | shell= new G4UIcsh; |
---|
| 63 | } |
---|
| 64 | } else { |
---|
| 65 | shell= new G4UImpish; |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | ///////////////////////////// |
---|
| 70 | G4MPIsession::~G4MPIsession() |
---|
| 71 | ///////////////////////////// |
---|
| 72 | { |
---|
| 73 | delete shell; |
---|
| 74 | |
---|
| 75 | G4UImanager* UI= G4UImanager::GetUIpointer(); |
---|
| 76 | UI-> SetSession(0); |
---|
| 77 | UI-> SetCoutDestination(0); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | //////////////////////////////////////////////////// |
---|
| 81 | void G4MPIsession::SetPrompt(const G4String& prompt) |
---|
| 82 | //////////////////////////////////////////////////// |
---|
| 83 | { |
---|
| 84 | shell-> SetPrompt(prompt); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | /////////////////////////////////////////////// |
---|
| 89 | void G4MPIsession::SetShell(G4VUIshell* ashell) |
---|
| 90 | /////////////////////////////////////////////// |
---|
| 91 | { |
---|
| 92 | delete shell; |
---|
| 93 | shell= ashell; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | ////////////////////////////////////////////////// |
---|
| 98 | G4String G4MPIsession::GetCommand(const char* msg) |
---|
| 99 | ////////////////////////////////////////////////// |
---|
| 100 | { |
---|
| 101 | G4UImanager* UI= G4UImanager::GetUIpointer(); |
---|
| 102 | |
---|
| 103 | G4String newCommand; |
---|
| 104 | const G4String nullString=""; |
---|
| 105 | |
---|
| 106 | // get command from shell... |
---|
| 107 | newCommand= shell-> GetCommandLine(msg); |
---|
| 108 | |
---|
| 109 | G4String nC= newCommand.strip(G4String::leading); |
---|
| 110 | if( nC.length() == 0 ) { |
---|
| 111 | newCommand= nullString; |
---|
| 112 | |
---|
| 113 | } else if( nC(0) == '#' ) { |
---|
| 114 | G4cout << nC << G4endl; |
---|
| 115 | newCommand= nullString; |
---|
| 116 | |
---|
| 117 | } else if(nC=="ls" || nC(0,3)=="ls " ) { // list commands |
---|
| 118 | ListDirectory(nC); |
---|
| 119 | newCommand= nullString; |
---|
| 120 | |
---|
| 121 | } else if(nC=="lc" || nC(0,3)=="lc " ) { // ... by shell |
---|
| 122 | shell-> ListCommand(nC.remove(0,2)); |
---|
| 123 | newCommand= nullString; |
---|
| 124 | |
---|
| 125 | } else if(nC == "pwd") { // show current directory |
---|
| 126 | G4cout << "Current Command Directory : " |
---|
[1337] | 127 | << GetCurrentWorkingDirectory() << G4endl; |
---|
[807] | 128 | newCommand= nullString; |
---|
| 129 | |
---|
| 130 | } else if(nC == "cwd") { // ... by shell |
---|
| 131 | shell-> ShowCurrentDirectory(); |
---|
| 132 | newCommand= nullString; |
---|
| 133 | |
---|
| 134 | } else if(nC == "cd" || nC(0,3) == "cd ") { // "cd" |
---|
| 135 | ChangeDirectoryCommand(nC); |
---|
| 136 | shell-> SetCurrentDirectory(GetCurrentWorkingDirectory()); |
---|
| 137 | newCommand= nullString; |
---|
| 138 | |
---|
| 139 | } else if(nC == "help" || nC(0,5) == "help ") { // "help" |
---|
| 140 | TerminalHelp(nC); |
---|
| 141 | newCommand= nullString; |
---|
| 142 | |
---|
| 143 | } else if(nC(0) == '?') { // "show current value of a parameter" |
---|
| 144 | ShowCurrent(nC); |
---|
| 145 | newCommand= nullString; |
---|
| 146 | |
---|
| 147 | } else if(nC == "hist" || nC == "history") { // "hist/history" |
---|
| 148 | G4int nh= UI-> GetNumberOfHistory(); |
---|
| 149 | for (G4int i=0; i<nh; i++) { |
---|
| 150 | G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl; |
---|
| 151 | } |
---|
| 152 | newCommand= nullString; |
---|
| 153 | |
---|
| 154 | } else if(nC(0) == '!') { // "!" |
---|
| 155 | G4String ss= nC(1, nC.length()-1); |
---|
| 156 | G4int vl; |
---|
| 157 | const char* tt= ss; |
---|
| 158 | std::istringstream is(tt); |
---|
| 159 | is >> vl; |
---|
| 160 | G4int nh= UI-> GetNumberOfHistory(); |
---|
| 161 | if(vl>=0 && vl<nh) { |
---|
| 162 | newCommand= UI-> GetPreviousCommand(vl); |
---|
| 163 | G4cout << newCommand << G4endl; |
---|
| 164 | } else { |
---|
| 165 | G4cerr << "history " << vl << " is not found." << G4endl; |
---|
| 166 | newCommand= nullString; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | } else if( nC.empty() ){ // NULL command |
---|
| 170 | newCommand= nullString; |
---|
| 171 | |
---|
| 172 | } else if( nC == "exit" ){ // "exit" |
---|
| 173 | return "exit"; |
---|
| 174 | |
---|
| 175 | } else { // ... |
---|
| 176 | |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | newCommand= TruncateCommand(newCommand); |
---|
| 180 | return ModifyToFullPathCommand(newCommand); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | ///////////////////////////////////////// |
---|
| 185 | G4UIsession* G4MPIsession::SessionStart() |
---|
| 186 | ///////////////////////////////////////// |
---|
| 187 | { |
---|
| 188 | // execute init macro |
---|
| 189 | if(g4MPI-> IsInitMacro()) { |
---|
| 190 | g4MPI-> ExecuteMacroFile(g4MPI->GetInitFileName()); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | // batch mode |
---|
| 194 | if(g4MPI-> IsBatchMode()) { |
---|
| 195 | g4MPI-> ExecuteMacroFile(g4MPI->GetMacroFileName(), true); |
---|
| 196 | return 0; |
---|
[1337] | 197 | } |
---|
| 198 | |
---|
[807] | 199 | // interactive session |
---|
| 200 | G4String newCommand="", scommand; // newCommand is always "" in slaves |
---|
| 201 | |
---|
[1337] | 202 | while(1) { |
---|
| 203 | if(isMaster) newCommand = GetCommand(); |
---|
| 204 | // broadcast a new G4 command |
---|
| 205 | scommand = g4MPI-> BcastCommand(newCommand); |
---|
| 206 | if(scommand == "exit") { |
---|
| 207 | G4bool qexit = TryForcedTerminate(); |
---|
[807] | 208 | if(qexit) break; |
---|
[1337] | 209 | else scommand = ""; |
---|
[807] | 210 | } |
---|
[1337] | 211 | ExecCommand(scommand); |
---|
[807] | 212 | } |
---|
| 213 | |
---|
| 214 | return 0; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | ///////////////////////////////////////// |
---|
| 219 | G4bool G4MPIsession::TryForcedTerminate() |
---|
| 220 | ///////////////////////////////////////// |
---|
| 221 | { |
---|
| 222 | if(! g4MPI-> CheckThreadStatus()) { |
---|
| 223 | return true; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | G4String xmessage; |
---|
| 227 | |
---|
| 228 | // beamOn is still running |
---|
| 229 | if(isMaster) { |
---|
| 230 | char c[1024]; |
---|
| 231 | while(1) { |
---|
| 232 | G4cout << "Run is still running. Do you abort a run? [y/N]:" |
---|
| 233 | << std::flush; |
---|
| 234 | G4cin.getline(c,1024); |
---|
| 235 | G4String yesno= c; |
---|
| 236 | if(yesno=="y" || yesno=="Y" || |
---|
| 237 | yesno=="n" || yesno=="N" || yesno == "") { |
---|
| 238 | break; |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | if(c[0]=='y' || c[0]=='Y') { |
---|
| 242 | G4cout << "Aborting a run..." << G4endl; |
---|
| 243 | xmessage= g4MPI-> BcastCommand("kill me"); |
---|
| 244 | } else { |
---|
| 245 | xmessage= g4MPI-> BcastCommand("alive"); |
---|
| 246 | } |
---|
| 247 | } else { |
---|
| 248 | xmessage= g4MPI->BcastCommand(""); |
---|
| 249 | } |
---|
[1337] | 250 | |
---|
[807] | 251 | if(xmessage == "kill me") { |
---|
| 252 | G4RunManager* runManager= G4RunManager::GetRunManager(); |
---|
| 253 | runManager-> AbortRun(true); // soft abort |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | return false; |
---|
| 257 | } |
---|
| 258 | |
---|