isac-java/src/java/isac/bridge/BridgeMain.java
author Walther Neuper <walther.neuper@jku.at>
Fri, 26 Mar 2021 10:45:05 +0100
changeset 5239 b4e3883d7b66
parent 5237 ee17f1b81a7f
permissions -rw-r--r--
reset mathematics-engine to Isabelle2015

note: for this version libisabelle was available,
which connects front-end (Java) and back-end (Isabelle/ML)
wneuper@2707
     1
/**
rgradisc@859
     2
 * Created on Aug 13, 2003
tzilling@4472
     3
 *
wneuper@2707
     4
 * @author Richard Gradischnegg RG
rgradisc@859
     5
 */
rgradisc@859
     6
package isac.bridge;
rgradisc@859
     7
rgradisc@859
     8
/**
tzilling@4472
     9
 * BridgeMain: main class for bridge, does initialization and provides logging delegate and utility functionality.
tzilling@4472
    10
 *
wneuper@2707
    11
 * @author Richard Gradischnegg RG
rgradisc@1072
    12
 * @version 1.1
rgradisc@859
    13
 */
nsimic@3730
    14
import isac.util.BridgeMainPaths;
rkoenig@3838
    15
import isac.util.FixedPortRMISocketFactory;
walther@5239
    16
import edu.tum.cs.isabelle.api.Configuration;
walther@5239
    17
import edu.tum.cs.isabelle.api.Environment;
walther@5239
    18
import edu.tum.cs.isabelle.api.Version;
walther@5239
    19
import edu.tum.cs.isabelle.japi.JPlatform;
walther@5239
    20
import edu.tum.cs.isabelle.japi.JSetup;
walther@5239
    21
import edu.tum.cs.isabelle.japi.JSystem;
walther@5239
    22
import edu.tum.cs.isabelle.setup.Setup;
wneuper@4721
    23
rgradisc@859
    24
import java.awt.BorderLayout;
rgradisc@859
    25
import java.awt.Color;
rgradisc@1066
    26
import java.awt.Font;
rgradisc@859
    27
import java.awt.event.WindowAdapter;
rgradisc@859
    28
import java.awt.event.WindowEvent;
rgradisc@859
    29
import java.awt.event.WindowListener;
rgradisc@859
    30
import java.io.BufferedReader;
rgradisc@859
    31
import java.io.IOException;
nsimic@3780
    32
import java.io.Serializable;
wneuper@4848
    33
import java.nio.file.Paths;
rgradisc@1065
    34
import java.rmi.RemoteException;
nsimic@3783
    35
import java.rmi.server.RMISocketFactory;
rgradisc@859
    36
rgradisc@859
    37
import javax.swing.JFrame;
rgradisc@859
    38
import javax.swing.JPanel;
rgradisc@859
    39
import javax.swing.JScrollPane;
rgradisc@859
    40
import javax.swing.JTextArea;
rgradisc@859
    41
wneuper@1860
    42
import org.apache.log4j.Logger;
wneuper@1860
    43
rgradisc@1065
    44
/**
tzilling@4472
    45
 * Bridge main class: initializes connection to SML, starts auxillary threads, handles logging mechanism
tzilling@4472
    46
 *
rgradisc@1065
    47
 * @author rgradisc
rgradisc@1065
    48
 */
nsimic@3780
    49
class BridgeMain implements Serializable {
rgradisc@859
    50
tzilling@4472
    51
    private static final long serialVersionUID = 507L;
tzilling@4472
    52
    private static final Logger logger = Logger.getLogger(BridgeMain.class.getName());
tzilling@4472
    53
    private JFrame log_frame_;
tzilling@4472
    54
    private String path_;
tzilling@4472
    55
    JPanel panel_;
tzilling@4472
    56
    JTextArea text_area_;
tzilling@4472
    57
    JScrollPane scroll_pane_;
tzilling@4472
    58
    private Thread thread_sml_;
tzilling@4472
    59
    private BridgeLogger bridge_logger_;
wneuper@4700
    60
    private BufferedReader sml_reader_;
a@4971
    61
tzilling@4472
    62
    public int ignore_output_ = 0;
tzilling@4472
    63
    private boolean restoring_; // true = bridge is in restoring_ phase
wneuper@4721
    64
    private JSystem connection_to_kernel_;
tzilling@3950
    65
tzilling@4472
    66
    /**
tzilling@4472
    67
     * Constructor used to set up the bridge main program
tzilling@4472
    68
     *
wneuper@4858
    69
     * @param log_path_: path_ to initialization file
tzilling@4472
    70
     */
wneuper@4858
    71
    BridgeMain(String log_path, String isabelle_home) {
tzilling@4472
    72
        // hack for suppressing warning messages
tzilling@4472
    73
        // (no longer nescessary with j2sdk1.4.2)
tzilling@4472
    74
        // System.setProperty("java.util.prefs.syncInterval", "20000000000");
rgradisc@859
    75
tzilling@4472
    76
        if (logger.isDebugEnabled()) {
wneuper@4858
    77
            logger.debug("BridgeMain log_path =" + log_path);
tzilling@4472
    78
        }
a@4974
    79
        Integer.valueOf(BridgeMainPaths.BRIDGE_MAIN_PORT);
wneuper@4863
    80
wneuper@4858
    81
        bridge_logger_ = new BridgeLogger(log_path);
a@4974
    82
        new ClientList();
tzilling@4472
    83
        setUpBridgeLog();
wneuper@4828
    84
        //*TTY*/startThreadsFirstTime();
wneuper@5234
    85
        /*PIDE*/log(1, "-->ISA: try connection with ISABELLE_HOME=\"" + isabelle_home + "\"...");
walther@5239
    86
        Setup setup = new Setup(Paths.get(isabelle_home), JPlatform.guess(),
walther@5239
    87
          new Version("2015"), Setup.defaultPackageName());
walther@5239
    88
        Environment env = JSetup.makeEnvironment(setup); // ohne Duration
walther@5239
    89
        Configuration config = Configuration.fromBuiltin("libisabelle_Isac");
wneuper@5236
    90
        JSystem sys = JSystem.create(env, config);
wneuper@5233
    91
wneuper@4848
    92
        /*PIDE*/connection_to_kernel_ = sys;
wneuper@4828
    93
        /*PIDE*/log(1, "<--ISA: connection established");
wneuper@4721
    94
tzilling@4472
    95
    }
rgradisc@859
    96
wneuper@4750
    97
    protected JSystem getConnectionToKernel(){
wneuper@4750
    98
      return connection_to_kernel_;
wneuper@4750
    99
    }
tzilling@4472
   100
    private void setUpBridgeLog() {
tzilling@4472
   101
        if (BridgeMainPaths.SHOW_GUI_LOGGER) {
tzilling@4472
   102
            log_frame_ = new JFrame("Java<->SML");
tzilling@4472
   103
            log_frame_.setLocation(700, 250);
tzilling@4472
   104
            panel_ = new JPanel();
tzilling@4472
   105
            panel_.setSize(200, 300);
tzilling@4472
   106
            panel_.setLayout(new BorderLayout());
tzilling@4472
   107
            panel_.setBackground(Color.white);
rgradisc@1065
   108
tzilling@4472
   109
            text_area_ = new JTextArea();
tzilling@4472
   110
            text_area_.setFont(new Font("Monospaced", 0, 11));
tzilling@4472
   111
            scroll_pane_ = new JScrollPane(text_area_,
tzilling@4472
   112
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
tzilling@4472
   113
                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
tzilling@4472
   114
            panel_.add("Center", scroll_pane_);
tzilling@4472
   115
            log_frame_.getContentPane().add(panel_);
rgradisc@859
   116
tzilling@4472
   117
            log_frame_.setTitle("BridgeLog Java <=> SML");
tzilling@4472
   118
            WindowListener l = new WindowAdapter() {
tzilling@4472
   119
                public void windowClosing(WindowEvent e) {
tzilling@4472
   120
                    System.exit(0);
tzilling@4472
   121
                }
tzilling@4472
   122
            };
tzilling@4472
   123
            log_frame_.addWindowListener(l);
tzilling@4472
   124
            log_frame_.pack();
tzilling@4472
   125
            log_frame_.setSize(300, 500);
tzilling@4472
   126
            log_frame_.setVisible(true);
tzilling@4472
   127
        }
tzilling@4472
   128
    }
rgradisc@859
   129
tzilling@4472
   130
    // Here goes the future load balancing functionality
tzilling@4472
   131
    // public void startNewKernel() {
tzilling@4472
   132
    // implement load balancing
tzilling@4472
   133
    // }
tzilling@4472
   134
    protected void finalize() throws Throwable {
tzilling@4472
   135
        super.finalize(); // gosling p.47
tzilling@4472
   136
        // Objects created in run method are finalized when
tzilling@4472
   137
        // program terminates and thread exits
tzilling@4472
   138
        while (thread_sml_.isAlive()) {
tzilling@4472
   139
            thread_sml_.interrupt();
tzilling@4472
   140
            log(1, "waiting for SML thread to quit..");
tzilling@4472
   141
        }
tzilling@4472
   142
        bridge_logger_.log(1, "finished");
tzilling@4472
   143
        bridge_logger_.close();
tzilling@4472
   144
    }
mlang@2448
   145
tzilling@4472
   146
    /**
tzilling@4472
   147
     * Log a message with a specified severity level
tzilling@4472
   148
     *
tzilling@4472
   149
     * @param level severity level
tzilling@4472
   150
     * @param msg the message to be logged
tzilling@4472
   151
     */
tzilling@4472
   152
    public void log(int level, String msg) {
tzilling@4472
   153
        if (BridgeMainPaths.LOG_TO_FILE) {
tzilling@4472
   154
            this.bridge_logger_.log(level, msg);
tzilling@4472
   155
        }
tzilling@4472
   156
        if (BridgeMainPaths.SHOW_GUI_LOGGER) {
tzilling@4472
   157
            this.text_area_.append(msg + "\n");
tzilling@4472
   158
        }
tzilling@4472
   159
    }
mlang@2448
   160
wneuper@4700
   161
    public BufferedReader getSmlReader() {
wneuper@3881
   162
        //if (logger.isDebugEnabled())
wneuper@3881
   163
        //    logger.debug("getSmlReader x");
tzilling@4472
   164
        return sml_reader_;
tzilling@4472
   165
    }
rgradisc@1035
   166
tzilling@4472
   167
    public String getPath() {
tzilling@4472
   168
        return path_;
tzilling@4472
   169
    }
wneuper@1860
   170
tzilling@4472
   171
    public void setRMI(BridgeRMI bridgeRMI) {
tzilling@4472
   172
        if (logger.isDebugEnabled()) {
wneuper@3881
   173
            logger.debug("setRMI: bridge_rmi_=" + bridgeRMI);
tzilling@4472
   174
        }
tzilling@4472
   175
        bridgeRMI.setRmiID(1); // only one BridgeRMI used so far
tzilling@4472
   176
    }
mlang@2448
   177
tzilling@4472
   178
    boolean isRestoring() {
tzilling@4472
   179
        return restoring_;
tzilling@4472
   180
    }
mlang@2448
   181
tzilling@4472
   182
    /**
tzilling@4472
   183
     * @param args commandline arguments (exactly 4 expected: _iniPath _host _port _dtdPath)
tzilling@4472
   184
     * @see BridgeMain()
tzilling@4472
   185
     */
tzilling@4472
   186
    public static void main(String[] args) {
rkoenig@3779
   187
tzilling@4472
   188
        System.out.println("Starting Bridge...");
rkoenig@3779
   189
tzilling@4472
   190
        String ini_load_result = BridgeMainPaths.loadFromFile(args[0]);
rkoenig@3779
   191
tzilling@4472
   192
        if (ini_load_result != null) {
tzilling@4472
   193
            ini_load_result += BridgeMainPaths.loadFromResource(args[0]);
tzilling@4472
   194
        }
mlang@2448
   195
tzilling@4472
   196
        if (ini_load_result != null) {
tzilling@4472
   197
            System.err.println(ini_load_result);
tzilling@4472
   198
            System.exit(1);
tzilling@4472
   199
        }
rkoenig@3779
   200
tzilling@4472
   201
        // initialize bridge main part
wneuper@4858
   202
        BridgeMain bridge = new BridgeMain(BridgeMainPaths.LOG_PATH, BridgeMainPaths.ISABELLE_HOME);
tzilling@4472
   203
        try {
rkoenig@3827
   204
            // Use ObjectManagerRMISocketFactory
tzilling@4472
   205
            // 6666 = dummy
tzilling@4472
   206
            RMISocketFactory.setSocketFactory(new FixedPortRMISocketFactory(BridgeMainPaths.BRIDGE_RMI_PORT));
nsimic@3783
   207
tzilling@4472
   208
            // setup rmi connection to to the clients
tzilling@4472
   209
            BridgeRMI rmi = new BridgeRMI(bridge, BridgeMainPaths.HOST,
tzilling@4472
   210
                    BridgeMainPaths.BRIDGE_MAIN_PORT, BridgeMainPaths.DTD_PATH);
tzilling@4472
   211
kober@4157
   212
//			System.out.println( rmi );
tzilling@4472
   213
tzilling@4472
   214
            bridge.setRMI(rmi);
tzilling@4472
   215
        } catch (RemoteException e) {
tzilling@4472
   216
            System.out.println("Could not instantiate RMI: exiting");
tzilling@4472
   217
            e.printStackTrace();
tzilling@4472
   218
        } catch (IOException e) {
tzilling@4472
   219
            System.out.println("Exception: " + e.getMessage());
tzilling@4472
   220
            e.printStackTrace();
nsimic@3783
   221
        }
tzilling@4472
   222
    }
walther@5239
   223
}