isac-java/src/java/isac/bridge/BridgeMain.java
author Walther Neuper <wneuper@ist.tugraz.at>
Mon, 17 Dec 2018 13:02:16 +0100
changeset 5234 22aabc469ebb
parent 5233 f9e91d4ee161
child 5235 3ca9690d3e8b
permissions -rw-r--r--
tuned
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;
wneuper@5229
    16
import info.hupel.isabelle.api.*;
wneuper@5229
    17
import info.hupel.isabelle.japi.*;
wneuper@5229
    18
import info.hupel.isabelle.setup.*;
wneuper@4721
    19
rgradisc@859
    20
import java.awt.BorderLayout;
rgradisc@859
    21
import java.awt.Color;
rgradisc@1066
    22
import java.awt.Font;
rgradisc@859
    23
import java.awt.event.WindowAdapter;
rgradisc@859
    24
import java.awt.event.WindowEvent;
rgradisc@859
    25
import java.awt.event.WindowListener;
rgradisc@859
    26
import java.io.BufferedReader;
rgradisc@859
    27
import java.io.IOException;
nsimic@3780
    28
import java.io.Serializable;
wneuper@5229
    29
import java.nio.file.Path;
wneuper@4848
    30
import java.nio.file.Paths;
rgradisc@1065
    31
import java.rmi.RemoteException;
nsimic@3783
    32
import java.rmi.server.RMISocketFactory;
wneuper@5229
    33
import java.util.Arrays;
rgradisc@859
    34
rgradisc@859
    35
import javax.swing.JFrame;
rgradisc@859
    36
import javax.swing.JPanel;
rgradisc@859
    37
import javax.swing.JScrollPane;
rgradisc@859
    38
import javax.swing.JTextArea;
rgradisc@859
    39
wneuper@1860
    40
import org.apache.log4j.Logger;
wneuper@1860
    41
rgradisc@1065
    42
/**
tzilling@4472
    43
 * Bridge main class: initializes connection to SML, starts auxillary threads, handles logging mechanism
tzilling@4472
    44
 *
rgradisc@1065
    45
 * @author rgradisc
rgradisc@1065
    46
 */
nsimic@3780
    47
class BridgeMain implements Serializable {
rgradisc@859
    48
tzilling@4472
    49
    private static final long serialVersionUID = 507L;
tzilling@4472
    50
    private static final Logger logger = Logger.getLogger(BridgeMain.class.getName());
tzilling@4472
    51
    private JFrame log_frame_;
tzilling@4472
    52
    private String path_;
tzilling@4472
    53
    JPanel panel_;
tzilling@4472
    54
    JTextArea text_area_;
tzilling@4472
    55
    JScrollPane scroll_pane_;
tzilling@4472
    56
    private Thread thread_sml_;
tzilling@4472
    57
    private BridgeLogger bridge_logger_;
wneuper@4700
    58
    private BufferedReader sml_reader_;
a@4971
    59
tzilling@4472
    60
    public int ignore_output_ = 0;
tzilling@4472
    61
    private boolean restoring_; // true = bridge is in restoring_ phase
wneuper@4721
    62
    private JSystem connection_to_kernel_;
tzilling@3950
    63
tzilling@4472
    64
    /**
tzilling@4472
    65
     * Constructor used to set up the bridge main program
tzilling@4472
    66
     *
wneuper@4858
    67
     * @param log_path_: path_ to initialization file
tzilling@4472
    68
     */
wneuper@4858
    69
    BridgeMain(String log_path, String isabelle_home) {
tzilling@4472
    70
        // hack for suppressing warning messages
tzilling@4472
    71
        // (no longer nescessary with j2sdk1.4.2)
tzilling@4472
    72
        // System.setProperty("java.util.prefs.syncInterval", "20000000000");
rgradisc@859
    73
tzilling@4472
    74
        if (logger.isDebugEnabled()) {
wneuper@4858
    75
            logger.debug("BridgeMain log_path =" + log_path);
tzilling@4472
    76
        }
a@4974
    77
        Integer.valueOf(BridgeMainPaths.BRIDGE_MAIN_PORT);
wneuper@4863
    78
wneuper@4858
    79
        bridge_logger_ = new BridgeLogger(log_path);
a@4974
    80
        new ClientList();
tzilling@4472
    81
        setUpBridgeLog();
wneuper@4828
    82
        //*TTY*/startThreadsFirstTime();
wneuper@5234
    83
/* cp from last working version:
wneuper@5233
    84
 *  https://hg.risc.uni-linz.ac.at/wneuper/isac/file/84f1ca6a6dd9/isac-java/src/java/isac/bridge/BridgeMain.java#l85
wneuper@5233
    85
    85         log(1, "-->ISA: try connection with ISABELLE_HOME=\"" + isabelle_home + "\"...");
wneuper@5233
    86
    86         Setup setup = new Setup(Paths.get(isabelle_home), JPlatform.guess(),
wneuper@5233
    87
    87           new Version("2015"), Setup.defaultPackageName());
wneuper@5234
    88
    88         Environment env = JSetup.makeEnvironment(setup); // without Duration
wneuper@5233
    89
    89         Configuration config = Configuration.fromBuiltin("libisabelle_Isac");
wneuper@5233
    90
    90         JSystem sys = JSystem.create(env, config);
wneuper@5233
    91
    92         
wneuper@5233
    92
    93         connection_to_kernel_ = sys;
wneuper@5233
    93
    94         log(1, "<--ISA: connection established");
wneuper@5233
    94
 */
wneuper@5234
    95
/* cp from downloaded libisabelle, current version:
wneuper@5233
    96
package info.hupel.isabelle.examples.java;
wneuper@5233
    97
wneuper@5233
    98
import java.nio.file.Path;
wneuper@5233
    99
import java.nio.file.Paths;
wneuper@5233
   100
import java.util.Arrays;
wneuper@5233
   101
import info.hupel.isabelle.api.*;
wneuper@5233
   102
import info.hupel.isabelle.japi.*;
wneuper@5233
   103
import info.hupel.isabelle.setup.*;
wneuper@5233
   104
wneuper@5233
   105
public class Hello_PIDE {
wneuper@5233
   106
wneuper@5233
   107
  public static void main(String args[]) {
wneuper@5233
   108
    JResources res = JResources.dumpIsabelleResources();
wneuper@5233
   109
    Configuration config = Configuration.simple("Protocol");
wneuper@5233
   110
    Environment env = JSetup.makeEnvironment(JSetup.defaultSetup(new Version.Stable("2017")), res);
wneuper@5233
   111
    JSystem sys = JSystem.create(env, config);
wneuper@5233
   112
    String response = sys.invoke(Operations.HELLO, "world");
wneuper@5233
   113
    System.out.println(response);
wneuper@5233
   114
    sys.dispose();
wneuper@5233
   115
  }
wneuper@5233
   116
}
wneuper@5233
   117
 */
wneuper@5234
   118
        /*PIDE*/log(1, "-->ISA: try connection with ISABELLE_HOME=\"" + isabelle_home + "\"...");
wneuper@5234
   119
wneuper@5229
   120
        JResources res = JResources.dumpIsabelleResources();
wneuper@5233
   121
        Path path = java.nio.file.Paths.get
wneuper@5233
   122
          ("/home/wneuper/.isabelle/isabisac/heaps/polyml-5.7.1_x86-linux/libisabelle_Isac");
wneuper@5231
   123
        Configuration config = Configuration.simple("Protocol");
wneuper@5229
   124
        Environment env = JSetup.makeEnvironment(JSetup.defaultSetup(new Version.Stable("2018")), res);
wneuper@4862
   125
        JSystem sys = JSystem.create(env, config);
wneuper@5233
   126
wneuper@4848
   127
        /*PIDE*/connection_to_kernel_ = sys;
wneuper@4828
   128
        /*PIDE*/log(1, "<--ISA: connection established");
wneuper@4721
   129
tzilling@4472
   130
    }
rgradisc@859
   131
wneuper@4750
   132
    protected JSystem getConnectionToKernel(){
wneuper@4750
   133
      return connection_to_kernel_;
wneuper@4750
   134
    }
tzilling@4472
   135
    private void setUpBridgeLog() {
tzilling@4472
   136
        if (BridgeMainPaths.SHOW_GUI_LOGGER) {
tzilling@4472
   137
            log_frame_ = new JFrame("Java<->SML");
tzilling@4472
   138
            log_frame_.setLocation(700, 250);
tzilling@4472
   139
            panel_ = new JPanel();
tzilling@4472
   140
            panel_.setSize(200, 300);
tzilling@4472
   141
            panel_.setLayout(new BorderLayout());
tzilling@4472
   142
            panel_.setBackground(Color.white);
rgradisc@1065
   143
tzilling@4472
   144
            text_area_ = new JTextArea();
tzilling@4472
   145
            text_area_.setFont(new Font("Monospaced", 0, 11));
tzilling@4472
   146
            scroll_pane_ = new JScrollPane(text_area_,
tzilling@4472
   147
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
tzilling@4472
   148
                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
tzilling@4472
   149
            panel_.add("Center", scroll_pane_);
tzilling@4472
   150
            log_frame_.getContentPane().add(panel_);
rgradisc@859
   151
tzilling@4472
   152
            log_frame_.setTitle("BridgeLog Java <=> SML");
tzilling@4472
   153
            WindowListener l = new WindowAdapter() {
tzilling@4472
   154
                public void windowClosing(WindowEvent e) {
tzilling@4472
   155
                    System.exit(0);
tzilling@4472
   156
                }
tzilling@4472
   157
            };
tzilling@4472
   158
            log_frame_.addWindowListener(l);
tzilling@4472
   159
            log_frame_.pack();
tzilling@4472
   160
            log_frame_.setSize(300, 500);
tzilling@4472
   161
            log_frame_.setVisible(true);
tzilling@4472
   162
        }
tzilling@4472
   163
    }
rgradisc@859
   164
tzilling@4472
   165
    // Here goes the future load balancing functionality
tzilling@4472
   166
    // public void startNewKernel() {
tzilling@4472
   167
    // implement load balancing
tzilling@4472
   168
    // }
tzilling@4472
   169
    protected void finalize() throws Throwable {
tzilling@4472
   170
        super.finalize(); // gosling p.47
tzilling@4472
   171
        // Objects created in run method are finalized when
tzilling@4472
   172
        // program terminates and thread exits
tzilling@4472
   173
        while (thread_sml_.isAlive()) {
tzilling@4472
   174
            thread_sml_.interrupt();
tzilling@4472
   175
            log(1, "waiting for SML thread to quit..");
tzilling@4472
   176
        }
tzilling@4472
   177
        bridge_logger_.log(1, "finished");
tzilling@4472
   178
        bridge_logger_.close();
tzilling@4472
   179
    }
mlang@2448
   180
tzilling@4472
   181
    /**
tzilling@4472
   182
     * Log a message with a specified severity level
tzilling@4472
   183
     *
tzilling@4472
   184
     * @param level severity level
tzilling@4472
   185
     * @param msg the message to be logged
tzilling@4472
   186
     */
tzilling@4472
   187
    public void log(int level, String msg) {
tzilling@4472
   188
        if (BridgeMainPaths.LOG_TO_FILE) {
tzilling@4472
   189
            this.bridge_logger_.log(level, msg);
tzilling@4472
   190
        }
tzilling@4472
   191
        if (BridgeMainPaths.SHOW_GUI_LOGGER) {
tzilling@4472
   192
            this.text_area_.append(msg + "\n");
tzilling@4472
   193
        }
tzilling@4472
   194
    }
mlang@2448
   195
wneuper@4700
   196
    public BufferedReader getSmlReader() {
wneuper@3881
   197
        //if (logger.isDebugEnabled())
wneuper@3881
   198
        //    logger.debug("getSmlReader x");
tzilling@4472
   199
        return sml_reader_;
tzilling@4472
   200
    }
rgradisc@1035
   201
tzilling@4472
   202
    public String getPath() {
tzilling@4472
   203
        return path_;
tzilling@4472
   204
    }
wneuper@1860
   205
tzilling@4472
   206
    public void setRMI(BridgeRMI bridgeRMI) {
tzilling@4472
   207
        if (logger.isDebugEnabled()) {
wneuper@3881
   208
            logger.debug("setRMI: bridge_rmi_=" + bridgeRMI);
tzilling@4472
   209
        }
tzilling@4472
   210
        bridgeRMI.setRmiID(1); // only one BridgeRMI used so far
tzilling@4472
   211
    }
mlang@2448
   212
tzilling@4472
   213
    boolean isRestoring() {
tzilling@4472
   214
        return restoring_;
tzilling@4472
   215
    }
mlang@2448
   216
tzilling@4472
   217
    /**
tzilling@4472
   218
     * @param args commandline arguments (exactly 4 expected: _iniPath _host _port _dtdPath)
tzilling@4472
   219
     * @see BridgeMain()
tzilling@4472
   220
     */
tzilling@4472
   221
    public static void main(String[] args) {
rkoenig@3779
   222
tzilling@4472
   223
        System.out.println("Starting Bridge...");
rkoenig@3779
   224
tzilling@4472
   225
        String ini_load_result = BridgeMainPaths.loadFromFile(args[0]);
rkoenig@3779
   226
tzilling@4472
   227
        if (ini_load_result != null) {
tzilling@4472
   228
            ini_load_result += BridgeMainPaths.loadFromResource(args[0]);
tzilling@4472
   229
        }
mlang@2448
   230
tzilling@4472
   231
        if (ini_load_result != null) {
tzilling@4472
   232
            System.err.println(ini_load_result);
tzilling@4472
   233
            System.exit(1);
tzilling@4472
   234
        }
rkoenig@3779
   235
tzilling@4472
   236
        // initialize bridge main part
wneuper@4858
   237
        BridgeMain bridge = new BridgeMain(BridgeMainPaths.LOG_PATH, BridgeMainPaths.ISABELLE_HOME);
tzilling@4472
   238
        try {
rkoenig@3827
   239
            // Use ObjectManagerRMISocketFactory
tzilling@4472
   240
            // 6666 = dummy
tzilling@4472
   241
            RMISocketFactory.setSocketFactory(new FixedPortRMISocketFactory(BridgeMainPaths.BRIDGE_RMI_PORT));
nsimic@3783
   242
tzilling@4472
   243
            // setup rmi connection to to the clients
tzilling@4472
   244
            BridgeRMI rmi = new BridgeRMI(bridge, BridgeMainPaths.HOST,
tzilling@4472
   245
                    BridgeMainPaths.BRIDGE_MAIN_PORT, BridgeMainPaths.DTD_PATH);
tzilling@4472
   246
kober@4157
   247
//			System.out.println( rmi );
tzilling@4472
   248
tzilling@4472
   249
            bridge.setRMI(rmi);
tzilling@4472
   250
        } catch (RemoteException e) {
tzilling@4472
   251
            System.out.println("Could not instantiate RMI: exiting");
tzilling@4472
   252
            e.printStackTrace();
tzilling@4472
   253
        } catch (IOException e) {
tzilling@4472
   254
            System.out.println("Exception: " + e.getMessage());
tzilling@4472
   255
            e.printStackTrace();
nsimic@3783
   256
        }
tzilling@4472
   257
    }
mlang@2448
   258
}