📄 mobimon.java
字号:
/* * This file is part of MobiMon. * * MobiMon is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * MobiMon is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with MobiMon; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* * MobiMon.java * * Created on January 28, 2003, 9:39 PM */package mobimon.midlet;import javax.microedition.rms.*;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.io.*;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;/** * * @author jan * @version */public class MobiMon extends MIDlet implements CommandListener, Runnable { private Command quit = new Command("Exit", Command.EXIT, 2); private Command ok = new Command("OK", Command.OK, 1); private Command cancel = new Command("Cancel", Command.CANCEL, 1); private Command change = new Command("New URL", Command.SCREEN, 2); private Command info = new Command("Info", Command.SCREEN, 2); private Command back = new Command("Back", Command.BACK, 1); private Command prev = new Command("Prev.", Command.SCREEN, 1); private Command next = new Command("Next", Command.SCREEN, 1); private Command addShortcut = new Command ("Add Shortcut", Command.SCREEN, 2); private Command shortcuts = new Command("Shortcuts", Command.SCREEN, 2); private Command delete = new Command("Delete", Command.SCREEN, 2); private String url = null; private volatile RecordStore database; private final String dbName = "MMDB"; private Display display; private TextBox urlTextBox; private Form connectForm; private Form errorInfo; private Form actionParamForm = null; private Form progressForm = null; private Gauge progressGauge; private Displayable buf = null; private List shortcutList = null; private String[] boolStr = { "off", "on" }; private String currentHost = null; private String currentMBean = null; private String currentAction = null; private String currentDomain = null; private Displayable[] mmScreen = new Displayable[4]; private short state = 0; private String[] title = { "Nodes", "Domains", "Resources", "Actions" }; private boolean listExpected = true; private boolean commError = false; private boolean calledFromShortcut = false; private int pageIndicator = 0; private int lastPageRequested = 0; private String currentRequest; public MobiMon() { } public void startApp() { if (display == null) display = Display.getDisplay(this); if (progressForm == null) createProgressForm(); Alert welcome = new Alert ("MobiMon 0.9", "Welcome to Mobile System Monitoring!", null, AlertType.INFO); welcome.setTimeout(5000); url = readURLFromDB(); if (url == null) { enterURLScreen(welcome); } else { connectScreen(welcome); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { try { if (database != null) close(); } catch (Exception e) { } notifyDestroyed(); } /** * Respond to commands, including exit * @param c command to perform * @param s Screen displayable object */ public void commandAction(Command c, Displayable d) { if (c == quit) { destroyApp(false); } else if (c == ok) { handleOk(d); } else if (c == cancel) { handleCancel(d); } else if (c == change) { enterURLScreen(null); } else if (c == back) { handleBack(d); } else if (c == info) { handleInfo(); } else if (c == prev) { handlePrev(d); } else if (c == next) { handleNext(d); } else if (c == addShortcut) { handleAddShortcut(d); } else if (c == shortcuts) { shortcutScreen(d); } else if (c == delete) { deleteShortcut(d); } } private String readURLFromDB() { String dburl = null; try { database = RecordStore.openRecordStore(dbName, true); if (database.getNumRecords() > 0) { byte[] barr = new byte[database.getRecordSize(1)]; barr = database.getRecord(1); dburl = new String(barr); } } catch (Exception e) { errorAlert(e); } return dburl; } private void enterURLScreen(Alert a) { urlTextBox = new TextBox("URL for MobiMon", url == null ? "http://" : url, 200, 0); addCommandsAndDisplay(urlTextBox, a); } private void connectScreen(Alert a) { connectForm = new Form("MobiMon URL"); StringItem si = new StringItem(null, "Select OK to connect to\n" + url + "\nNew URL to enter a different URL" + "\nShortcuts to access stored shortcuts"); connectForm.append(si); addCommandsAndDisplay(connectForm, a); } private synchronized void writeURLToDB() { if ((url == null) || (url.length() < 1)) return; try { if (database.getNumRecords() == 0) { database.addRecord(url.getBytes(), 0, url.getBytes().length); } else { database.setRecord(1, url.getBytes(), 0, url.getBytes().length); } } catch (Exception e) { errorAlert(e); } } private void errorAlert(Exception e) { StringBuffer b = new StringBuffer(e.getClass().getName()); b.append(": ").append(e.getMessage()); String s = b.toString(); if (s == null) s = "Exception occured"; errorInfo = new Form("Exception"); StringItem si = new StringItem(null, s); errorInfo.append(si); addCommandsAndDisplay(errorInfo, null); } /** * <p>Close the database and remove it from persistant * storage if it is empty</p> * * @throws <code>RecordStoreNotOpenException</code> is thrown when trying * to close a <code>RecordStore</code> that is not open * @throws <code>RecordStoreException</code> is thrown when a general * exception occurs in a <code>RecordStore</code> operation */ private void close() throws RecordStoreNotOpenException, RecordStoreException { if (database.getNumRecords() == 0) { database.closeRecordStore(); database.deleteRecordStore(dbName); } else { database.closeRecordStore(); } } private String sendRequest(String requestStr) { commError = false; StringBuffer b = new StringBuffer(); String response = null; HttpConnection c = null; OutputStream os = null; InputStream is = null; TextBox t = null; try { display.setCurrent(progressForm); long len = -1; int ch = 0; long count = 0; long limit = Runtime.getRuntime().freeMemory() / 4; c = (HttpConnection)Connector.open(url); c.setRequestMethod(HttpConnection.POST); c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); byte postmsg[] = requestStr.getBytes(); c.setRequestProperty("Content-Length", String.valueOf(postmsg.length)); if (lastPageRequested != 0) c.setRequestProperty("Page", String.valueOf(lastPageRequested)); os = c.openOutputStream(); os.write(postmsg); updateProgressForm("Sending request...", 2); is = c.openInputStream(); len = ((HttpConnection)c).getLength(); updateProgressForm("Receiving response...", 4); if (len != -1) { for (int i = 0; i < len; i++) { if ((ch = is.read()) != -1) { b.append(/*(ch <= ' ') ? ' ' :*/ (char) ch); if (++count > limit) break; } } } else { while (((ch = is.read()) != -1) && (count <= limit)) { b.append(/*(ch <= ' ') ? ' ' :*/ (char) ch); count++; } } response = b.toString(); pageIndicator = c.getHeaderFieldInt("Page", 0); } catch (IOException ex) { commError = true; if (c != null) { try { response = c.getResponseMessage(); if (response == null) response = "No Response message"; } catch (IOException e) { errorAlert(e); } } else { response = "Could not open URL: " + ex.getMessage(); } } catch (IllegalArgumentException ille) { commError = true; response = "Check the com.sun.midp.io.http.proxy setting"; } finally { if (os != null) { try { os.close(); } catch (Exception ce) {; } } if (is != null) { try { is.close(); } catch (Exception ce) {; } } if (c != null) { try { c.close(); } catch (Exception ce) {; } } } updateProgressForm("Processing response...", 6); return response; } private void addCommandsAndDisplay(Displayable d, Alert a) { if (d == urlTextBox) { d.addCommand(ok); d.addCommand((state == 0) ? quit : cancel); } else if (d == connectForm) { d.addCommand(quit); d.addCommand(ok); d.addCommand(change); d.addCommand(shortcuts); } else if (d == errorInfo) { d.addCommand(quit); } else if (d == mmScreen[state]) { d.addCommand(back); if (d instanceof List) { d.addCommand(ok); if (state == 3) { d.addCommand(info); d.addCommand(addShortcut); } } else if (d instanceof MMCanvas) { if (pageIndicator < 0) { d.addCommand(prev); if (pageIndicator < -2) d.addCommand(next); } else if (pageIndicator > 0) { if (pageIndicator > 2) d.addCommand(prev); d.addCommand(next); } else { if (lastPageRequested < 0) { d.addCommand(next); pageIndicator = lastPageRequested - 1; } else if (lastPageRequested > 0) { d.addCommand(prev); pageIndicator = lastPageRequested + 1; } } } } else if ((d == actionParamForm) || (d == shortcutList)) { d.addCommand(back); d.addCommand(ok); if (d == shortcutList) d.addCommand(delete); } d.setCommandListener(this); if (a != null) display.setCurrent(a, d); else display.setCurrent(d); } private void handleOk(Displayable d) { if (d == urlTextBox) { url = urlTextBox.getString().trim(); writeURLToDB(); connectScreen(null); } else if (d == connectForm) { listExpected = true; handleRequest("nodes=y"); } else if (d == actionParamForm) { requestParamsAction(); } else if (d == shortcutList) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -