Changeset 102 for trunk


Ignore:
Timestamp:
10/31/05 14:21:41 (19 years ago)
Author:
remar
Message:
  • starts server if one isn't running
  • try to be cross-platform
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/org.modelica.mdt/src/org/modelica/mdt/internal/corba/OmcCommunicationImplementation.java

    r100 r102  
    1717public class OmcCommunicationImplementation
    1818{
    19     private static ORB orb;
    2019    private static OmcCommunication omcc;
     20    private static String os;
    2121
    2222    // TODO Do not do lazy initialization
    2323    private static boolean hasInitialized = false;
    2424
    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());
    3528        String stringifiedObjectReference = null;
    36            
    37         if(!f.exists())
    38         {
    39             /* start server here */
    40             // TODO Start server here
    41             System.out.println("VAFAN!!");
    42         }
    4329
    4430        BufferedReader br = null;
     
    6147        catch(IOException e)
    6248        {
    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;
    66157        orb = ORB.init(args, null);
    67158       
    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        }
    72234
    73235        hasInitialized = true;
     
    80242        if(hasInitialized == false)
    81243        {
    82             // System.out.println("Initializing CORBA interface");
    83244            init(null);
    84245            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            }
    87251        }
    88252       
    89253        retval = omcc.sendExpression(exp);
    90254       
    91         //catch(InitializationException e)
    92 //      {
    93 //          e.printStackTrace();
    94 //          MdtPlugin.log(e);
    95 //      }
    96 
    97255        return retval;
    98256    }
Note: See TracChangeset for help on using the changeset viewer.