| 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 | //
|
|---|
| 26 | // $Id: G4MPIsession.cc,v 1.1 2007/11/16 14:05:41 kmura Exp $
|
|---|
| 27 | // $Name: $
|
|---|
| 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 : "
|
|---|
| 127 | << GetCurrentWorkingDirectory() << G4endl;
|
|---|
| 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;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | // interactive session
|
|---|
| 200 | G4String newCommand="", scommand; // newCommand is always "" in slaves
|
|---|
| 201 |
|
|---|
| 202 | if(isMaster) newCommand= GetCommand();
|
|---|
| 203 | // broadcast a new G4 command
|
|---|
| 204 | scommand= g4MPI-> BcastCommand(newCommand);
|
|---|
| 205 | if(scommand == "exit" ) return 0;
|
|---|
| 206 |
|
|---|
| 207 | while(1){
|
|---|
| 208 | ExecCommand(scommand);
|
|---|
| 209 |
|
|---|
| 210 | // get next ...
|
|---|
| 211 | if(isMaster) newCommand= GetCommand();
|
|---|
| 212 | scommand= g4MPI-> BcastCommand(newCommand);
|
|---|
| 213 | if(scommand == "exit" ) {
|
|---|
| 214 | G4bool qexit= TryForcedTerminate();
|
|---|
| 215 | if(qexit) break;
|
|---|
| 216 | else scommand="";
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | return 0;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 | /////////////////////////////////////////
|
|---|
| 225 | G4bool G4MPIsession::TryForcedTerminate()
|
|---|
| 226 | /////////////////////////////////////////
|
|---|
| 227 | {
|
|---|
| 228 | if(! g4MPI-> CheckThreadStatus()) {
|
|---|
| 229 | return true;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | G4String xmessage;
|
|---|
| 233 |
|
|---|
| 234 | // beamOn is still running
|
|---|
| 235 | if(isMaster) {
|
|---|
| 236 | char c[1024];
|
|---|
| 237 | while(1) {
|
|---|
| 238 | G4cout << "Run is still running. Do you abort a run? [y/N]:"
|
|---|
| 239 | << std::flush;
|
|---|
| 240 | G4cin.getline(c,1024);
|
|---|
| 241 | G4String yesno= c;
|
|---|
| 242 | if(yesno=="y" || yesno=="Y" ||
|
|---|
| 243 | yesno=="n" || yesno=="N" || yesno == "") {
|
|---|
| 244 | break;
|
|---|
| 245 | }
|
|---|
| 246 | }
|
|---|
| 247 | if(c[0]=='y' || c[0]=='Y') {
|
|---|
| 248 | G4cout << "Aborting a run..." << G4endl;
|
|---|
| 249 | xmessage= g4MPI-> BcastCommand("kill me");
|
|---|
| 250 | } else {
|
|---|
| 251 | xmessage= g4MPI-> BcastCommand("alive");
|
|---|
| 252 | }
|
|---|
| 253 | } else {
|
|---|
| 254 | xmessage= g4MPI->BcastCommand("");
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | if(xmessage == "kill me") {
|
|---|
| 258 | G4RunManager* runManager= G4RunManager::GetRunManager();
|
|---|
| 259 | runManager-> AbortRun(true); // soft abort
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | return false;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|