1 37 38 39 40 41 42 package com.lutris.appserver.debugger.applet; 43 44 import java.util.Vector ; 45 46 47 import java.io.*; 48 import java.net.*; 49 import java.awt.*; 50 import java.awt.event.*; 51 import java.applet.AppletContext ; 52 import javax.swing.*; 53 import javax.swing.event.*; 54 import javax.swing.border.*; 55 import javax.swing.table.*; 56 import com.lutris.appserver.debugger.applet.io.*; 57 58 59 public class Updater implements Runnable { 60 61 private static long currentStateId = -1; 62 63 private static Thread autoUpdateThread; 64 65 private long timeToDie; 66 67 private Updater(int seconds) { 68 timeToDie = System.currentTimeMillis() + (seconds * 1000);; 69 } 70 71 72 73 public static boolean updateState(int wait) { 74 System.out.println("In updateState()"); 75 synchronized (Globals.networkLock) { 76 long startTime = System.currentTimeMillis(); 77 Snapshot s = null; 78 try { 79 s = fetchSnapshot(wait); 80 } catch (DebuggerException e) { 81 JOptionPane.showMessageDialog(Globals.japplet, 82 e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 83 return false; 84 } 85 if (s == null) 86 return true; 87 88 89 int sel = Globals.transactionList.getSelectedIndex(); 90 int oldId = -1; 91 if (sel >= 0) { 92 TransactionDescription t = (TransactionDescription) Globals.transactionList.getModel().getElementAt(sel); 93 oldId = t.id; 94 } 95 96 currentStateId = s.stateId; 97 Globals.servletModel.updateToSnapshot(s); 98 Globals.transactionModel.updateToSnapshot(s); 99 100 if (sel >= 0) { 101 int row = Globals.transactionModel.idToRow(oldId); 102 if (row >= 0) 103 Globals.transactionList.setSelectedIndex(row); 104 else 105 Globals.transactionList.clearSelection(); 106 } 107 } 108 System.out.println("updateState() done"); 109 return true; 110 } 111 112 113 114 115 116 117 private static Snapshot fetchSnapshot(int wait) throws DebuggerException { 118 try { 119 Globals.japplet.showStatus("Getting status from server..."); 120 121 URL target = new URL(Globals.japplet.getDocumentBase(), "DebugServer.po?action=getSnapshot&id=" + currentStateId + "&wait=" + wait); 122 123 URLConnection conn = target.openConnection(); 124 125 conn.setDoInput(true); 126 conn.setDoOutput(false); 127 conn.setUseCaches(false); 128 conn.setRequestProperty("Accept", "application/java-serialized"); 129 130 conn.connect(); 131 132 133 134 135 136 InputStream in = conn.getInputStream(); 137 138 139 ObjectInputStream oin = new ObjectInputStream(in); 140 141 142 Object o = oin.readObject(); 143 144 Globals.japplet.showStatus(""); 145 146 147 148 149 return (Snapshot) o; 150 } catch (MalformedURLException e) { 151 throw new DebuggerException("Unable to find server: " + e.getMessage()); 152 } catch (StreamCorruptedException e) { 153 throw new DebuggerException("Bad data returned from server."); 154 } catch (OptionalDataException e) { 155 throw new DebuggerException("Bad data returned from server."); 156 } catch (ClassNotFoundException e) { 157 throw new DebuggerException("Bad data returned from server."); 158 } catch (IOException e) { 159 throw new DebuggerException("Error connecting to server: " + e.getMessage()); 160 } 161 } 162 163 164 166 public static void startThread() { 167 if (autoUpdateThread != null) 168 killThread(); 169 autoUpdateThread = new Thread (new Updater(300)); 170 autoUpdateThread.start(); 171 } 172 173 174 175 public static void killThread() { 176 if (autoUpdateThread != null) { 177 autoUpdateThread.stop(); 178 autoUpdateThread = null; 179 } 180 } 181 182 183 public static boolean threadRunning() { 184 return (autoUpdateThread != null); 185 } 186 187 188 public void run() { 189 while (true) { 190 boolean ok = updateState(30); 191 if (!ok || (System.currentTimeMillis() >= timeToDie)) { 192 System.out.println("THREAD ERROR/TIMEOUT"); 193 autoUpdateThread = null; 194 System.out.println("st"); 195 Globals.serverButton.setText(Globals.inactiveTitle); 196 System.out.println("en"); 197 Globals.updateButton.setEnabled(true); 198 break; 199 } 200 } 201 } 202 203 } 204 | Popular Tags |