- Timestamp:
- 10/31/05 14:21:41 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/org.modelica.mdt/src/org/modelica/mdt/internal/corba/OmcCommunicationImplementation.java
r100 r102 17 17 public class OmcCommunicationImplementation 18 18 { 19 private static ORB orb;20 19 private static OmcCommunication omcc; 20 private static String os; 21 21 22 22 // TODO Do not do lazy initialization 23 23 private static boolean hasInitialized = false; 24 24 25 public static void init(String args[]) throws InitializationException 26 { 27 String username = System.getenv("USER"); 28 if(username == null) 29 { 30 username = "nobody"; 31 } 32 String fileName = "/tmp/openmodelica." + username + ".objid"; 33 34 File f = new File(fileName); 25 private static String readObjectFromFile() throws InitializationException 26 { 27 File f = new File(getPathToObject()); 35 28 String stringifiedObjectReference = null; 36 37 if(!f.exists())38 {39 /* start server here */40 // TODO Start server here41 System.out.println("VAFAN!!");42 }43 29 44 30 BufferedReader br = null; … … 61 47 catch(IOException e) 62 48 { 63 throw new InitializationException("Unable to read from " + fileName); 64 } 65 49 throw new InitializationException("Unable to read from " + getPathToObject()); 50 } 51 return stringifiedObjectReference; 52 } 53 54 private static String getPathToObject() 55 { 56 String fileName = null; 57 if(os.equals("Linux")) 58 { 59 String username = System.getenv("USER"); 60 if(username == null) 61 { 62 username = "nobody"; 63 } 64 fileName = "/tmp/openmodelica." + username + ".objid"; 65 } 66 else if(os.equals("Windows")) 67 { 68 String temp = System.getenv("TEMP"); 69 fileName = temp + "openmodelica.objid"; 70 } 71 72 return fileName; 73 } 74 75 private static void startServer() 76 { 77 String pathToOmc = null; 78 String argToOmc = null; 79 String modelicaPath = null; 80 String user = null; 81 82 System.out.println("Starting Open Modelica Compiler"); 83 84 if(os.equals("Linux")) 85 { 86 pathToOmc = "/home/x05andre/ex/omc-build/OpenModelica/Compiler/omc"; 87 modelicaPath = "MODELICAPATH=/home/x05andre/ex/Modelica Library"; 88 } 89 else if(os.equals("Windows")) 90 { 91 pathToOmc = "c:\\OpenModelica13\\omc.exe"; 92 modelicaPath = "MODELICAPATH=c:\\OpenModelica13\\ModelicaLibrary"; 93 } 94 argToOmc = "+d=interactiveCorba"; 95 String userName = System.getenv("USER"); 96 if(userName == null) 97 userName = "nobody"; 98 user = "USER=" + userName; 99 100 File f = new File(getPathToObject()); 101 102 if(f.exists()) 103 { 104 System.out.println("Delete " + f); 105 if(f.delete()) 106 { 107 System.out.println(f + " deleted"); 108 } 109 else 110 { 111 System.out.println("Couldn't delete " + f); 112 } 113 } 114 115 System.out.flush(); 116 String command[] = {pathToOmc, argToOmc}; 117 String env[] = {modelicaPath, user}; 118 try 119 { 120 Runtime.getRuntime().exec(command, env); 121 } 122 catch(IOException e) 123 { 124 // TODO Handle this exception 125 System.out.println("Couldn't start Open Modelica Compiler"); 126 System.out.println(e); 127 } 128 129 try 130 { 131 System.out.println("Sleep 1 second"); 132 Thread.sleep(1000); 133 } 134 catch(InterruptedException e) 135 { 136 // Ignore 137 } 138 139 while(!f.exists()) 140 { 141 try 142 { 143 Thread.sleep(100); 144 } 145 catch(InterruptedException e) 146 { 147 // Ignore 148 } 149 } 150 } 151 152 private static void setupOmcc(String stringifiedObjectReference) 153 { 154 String args[] = {null}; 155 156 ORB orb; 66 157 orb = ORB.init(args, null); 67 158 68 // TODO Add try/catch and try to start server if this fails 69 org.omg.CORBA.Object obj = orb.string_to_object(stringifiedObjectReference); 70 71 omcc = OmcCommunicationHelper.narrow(obj); 159 org.omg.CORBA.Object obj = null; 160 try 161 { 162 obj = orb.string_to_object(stringifiedObjectReference); 163 } 164 catch(Exception e) 165 { 166 System.out.println("Va Fan"); 167 System.out.println(e); 168 } 169 170 try 171 { 172 omcc = OmcCommunicationHelper.narrow(obj); 173 } 174 catch(Exception e) 175 { 176 System.out.println("Fan Va?"); 177 System.out.flush(); 178 System.out.println(e); 179 } 180 181 } 182 183 private static String getOs() 184 { 185 String osName = System.getProperty("os.name"); 186 if(osName.contains("Linux")) 187 { 188 return "Linux"; 189 } 190 else if(osName.contains("Windows")) 191 { 192 return "Windows"; 193 } 194 else 195 { 196 System.out.println("Unsupported OS"); 197 return "Linux"; 198 } 199 } 200 201 public static void init(String args[]) throws InitializationException 202 { 203 os = getOs(); 204 205 File f = new File(getPathToObject()); 206 String stringifiedObjectReference = null; 207 if(!f.exists()) 208 { 209 startServer(); 210 } 211 stringifiedObjectReference = readObjectFromFile(); 212 213 setupOmcc(stringifiedObjectReference); 214 215 try 216 { 217 omcc.sendExpression("1+1"); 218 } 219 catch(Exception e) 220 { 221 startServer(); 222 stringifiedObjectReference = readObjectFromFile(); 223 setupOmcc(stringifiedObjectReference); 224 } 225 226 try 227 { 228 omcc.sendExpression("1+1"); 229 } 230 catch(Exception e) 231 { 232 throw new InitializationException("Unable to start server"); 233 } 72 234 73 235 hasInitialized = true; … … 80 242 if(hasInitialized == false) 81 243 { 82 // System.out.println("Initializing CORBA interface");83 244 init(null); 84 245 System.out.println("Loading Modelica Standard Library"); 85 //sendExpression("loadModel(Modelica)"); 86 sendExpression("loadFile(\"/home/x05andre/ex/Modelica\")"); 246 retval = sendExpression("loadModel(Modelica)"); 247 if(retval.equals("false")) 248 { 249 250 } 87 251 } 88 252 89 253 retval = omcc.sendExpression(exp); 90 254 91 //catch(InitializationException e)92 // {93 // e.printStackTrace();94 // MdtPlugin.log(e);95 // }96 97 255 return retval; 98 256 }
Note: See TracChangeset
for help on using the changeset viewer.