From f20ca875e0f276b7867f360de32e210cdac0f4ee Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 27 Sep 2008 14:12:48 +0000 Subject: Client: Worked on step 2 Hopefully I finished it, though, you never know, there could be more stuff in there. Refs #69 --- .../instadisc/CloseServerSocketThread.java | 33 ---- .../DownloadItem/DeinitalizeModeThread.java | 8 + .../instadisc/DownloadItem/DownloadItemMode.java | 14 ++ .../instadisc/DownloadItem/ModeControl.java | 45 ++++++ .../instadisc/DownloadItem/PullMode.java | 24 +++ .../instadisc/DownloadItem/PushMode.java | 180 +++++++++++++++++++++ .../UnknownDownloadItemModeException.java | 5 + .../com/fourisland/instadisc/InstaDiscThread.java | 175 -------------------- .../com/fourisland/instadisc/InstaDiscView.java | 75 +++++---- 9 files changed, 316 insertions(+), 243 deletions(-) delete mode 100644 client/trunk/src/com/fourisland/instadisc/CloseServerSocketThread.java create mode 100644 client/trunk/src/com/fourisland/instadisc/DownloadItem/DeinitalizeModeThread.java create mode 100644 client/trunk/src/com/fourisland/instadisc/DownloadItem/DownloadItemMode.java create mode 100644 client/trunk/src/com/fourisland/instadisc/DownloadItem/ModeControl.java create mode 100644 client/trunk/src/com/fourisland/instadisc/DownloadItem/PullMode.java create mode 100644 client/trunk/src/com/fourisland/instadisc/DownloadItem/PushMode.java create mode 100644 client/trunk/src/com/fourisland/instadisc/DownloadItem/UnknownDownloadItemModeException.java delete mode 100644 client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java (limited to 'client') diff --git a/client/trunk/src/com/fourisland/instadisc/CloseServerSocketThread.java b/client/trunk/src/com/fourisland/instadisc/CloseServerSocketThread.java deleted file mode 100644 index cfe1820..0000000 --- a/client/trunk/src/com/fourisland/instadisc/CloseServerSocketThread.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ - -package com.fourisland.instadisc; - -import java.io.IOException; -import java.net.ServerSocket; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * - * @author hatkirby - */ -class CloseServerSocketThread implements Runnable { - - ServerSocket svr; - - public CloseServerSocketThread(ServerSocket svr) { - this.svr = svr; - } - - public void run() { - try { - svr.close(); - } catch (IOException ex) { - Logger.getLogger(CloseServerSocketThread.class.getName()).log(Level.SEVERE, null, ex); - } - } - -} diff --git a/client/trunk/src/com/fourisland/instadisc/DownloadItem/DeinitalizeModeThread.java b/client/trunk/src/com/fourisland/instadisc/DownloadItem/DeinitalizeModeThread.java new file mode 100644 index 0000000..726b22e --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/DownloadItem/DeinitalizeModeThread.java @@ -0,0 +1,8 @@ +package com.fourisland.instadisc.DownloadItem; + +public class DeinitalizeModeThread implements Runnable +{ + public void run() { + ModeControl.INSTANCE.modeDeinitalize(); + } +} \ No newline at end of file diff --git a/client/trunk/src/com/fourisland/instadisc/DownloadItem/DownloadItemMode.java b/client/trunk/src/com/fourisland/instadisc/DownloadItem/DownloadItemMode.java new file mode 100644 index 0000000..157a62a --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/DownloadItem/DownloadItemMode.java @@ -0,0 +1,14 @@ +package com.fourisland.instadisc.DownloadItem; + +import java.util.Timer; + +public interface DownloadItemMode +{ + public void modeInitalize(); + public void modeDeinitalize(); + + public void requestRetained(); + + public int setTimer(); + public void timerTick(); +} \ No newline at end of file diff --git a/client/trunk/src/com/fourisland/instadisc/DownloadItem/ModeControl.java b/client/trunk/src/com/fourisland/instadisc/DownloadItem/ModeControl.java new file mode 100644 index 0000000..3fcaa4b --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/DownloadItem/ModeControl.java @@ -0,0 +1,45 @@ +package com.fourisland.instadisc.DownloadItem; + +import java.util.Timer; + +public class ModeControl implements DownloadItemMode +{ + public static final ModeControl INSTANCE = new ModeControl(); + private DownloadItemMode dim; + + public void initalize(String dim) throws UnknownDownloadItemModeException + { + if (dim.equals("Push")) + { + this.dim = new PushMode(); + } else if (dim.equals("Pull")) + { + this.dim = new PullMode(); + } else { + throw new UnknownDownloadItemModeException(); + } + + Runtime.getRuntime().addShutdownHook(new Thread(new DeinitalizeModeThread())); + } + + public void modeInitalize() { + dim.modeInitalize(); + } + + public void modeDeinitalize() { + dim.modeDeinitalize(); + } + + public void requestRetained() + { + dim.requestRetained(); + } + + public int setTimer() { + return dim.setTimer(); + } + + public void timerTick() { + dim.timerTick(); + } +} \ No newline at end of file diff --git a/client/trunk/src/com/fourisland/instadisc/DownloadItem/PullMode.java b/client/trunk/src/com/fourisland/instadisc/DownloadItem/PullMode.java new file mode 100644 index 0000000..8e54542 --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/DownloadItem/PullMode.java @@ -0,0 +1,24 @@ +package com.fourisland.instadisc.DownloadItem; + +public class PullMode implements DownloadItemMode +{ + public void modeInitalize() { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void modeDeinitalize() { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void requestRetained() { + throw new UnsupportedOperationException("Not supported yet."); + } + + public int setTimer() { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void timerTick() { + throw new UnsupportedOperationException("Not supported yet."); + } +} \ No newline at end of file diff --git a/client/trunk/src/com/fourisland/instadisc/DownloadItem/PushMode.java b/client/trunk/src/com/fourisland/instadisc/DownloadItem/PushMode.java new file mode 100644 index 0000000..eb5ab5c --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/DownloadItem/PushMode.java @@ -0,0 +1,180 @@ +package com.fourisland.instadisc.DownloadItem; + +import com.fourisland.instadisc.Database.Wrapper; +import com.fourisland.instadisc.Item.Item; +import com.fourisland.instadisc.XmlRpc; +import java.io.IOException; +import java.io.InputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; +import java.util.HashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + + +public class PushMode implements DownloadItemMode +{ + InstaDiscThread idt; + + public void modeInitalize() + { + idt = new InstaDiscThread(); + new Thread(idt).start(); + } + + public void modeDeinitalize() + { + idt.kill(); + } + + public void requestRetained() { + XmlRpc xmlrpc = new XmlRpc("requestRetained"); + xmlrpc.execute(); + } + + public int setTimer() { + int delay = (1000 * 60 * 60); + if (Wrapper.getConfig("ipCheckUnit").equals("day")) { + delay *= (24 * Integer.decode(Wrapper.getConfig("ipCheckValue"))); + } else if (Wrapper.getConfig("ipCheckUnit").equals("hour")) { + delay *= Integer.decode(Wrapper.getConfig("ipCheckValue")); + } + + return delay; + } + + public void timerTick() { + XmlRpc xmlrpc = new XmlRpc("checkRegistration"); + xmlrpc.execute(); + } +} +class InstaDiscThread implements Runnable { + + boolean cancelled = false; + ServerSocket svr; + + public void cancel() { + cancelled = true; + } + + public void run() { + try + { + svr = new ServerSocket(); + java.net.InetSocketAddress addr = new java.net.InetSocketAddress(1204); + svr.bind(addr); + while (!cancelled) + { + try + { + Socket s = svr.accept(); + HandleItemThread hit = new HandleItemThread(s); + Thread hitt = new Thread(hit); + hitt.start(); + } catch (SocketException ex) + { + cancel(); + } catch (Exception ex) + { + cancel(); + Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex); + } + } + svr.close(); + } catch (IOException ex) + { + Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public void kill() + { + try + { + svr.close(); + } catch (IOException ex) + { + Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex); + } + } +} + +class HandleItemThread implements Runnable { + + Socket s; + + public HandleItemThread(Socket s) { + this.s = s; + } + + public void run() { + try + { + InputStream is = s.getInputStream(); + int buffer[] = new int[1000]; + int rs = 0; + int i = 0; + + while (rs != -1) + { + try + { + rs = is.read(); + + if (rs != -1) + { + buffer[i] = rs; + } + + i++; + } catch (SocketException ex) + { + return; + } catch (IOException ex) + { + Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); + } + } + + StringBuilder result = new StringBuilder(); + int j = 0; + for (j = 0; j < i; j++) + { + result.append(Character.toString((char) buffer[j])); + } + + String[] headers = result.toString().split("\n"); + HashMap headerMap = new HashMap(); + i = 0; + while (1 == 1) + { + try + { + String[] nameVal = headers[i].split(": "); + String name = nameVal[0]; + String value = nameVal[1].trim().replace("__INSTADISC__", ": "); + headerMap.put(name, value); + } catch (Exception ex) + { + break; + } + i++; + } + + try + { + s.close(); + } catch (IOException ex) + { + Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); + } + + Item idI = new Item(headerMap); + idI.start(); + } catch (IOException ex) + { + Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); + } + } +} diff --git a/client/trunk/src/com/fourisland/instadisc/DownloadItem/UnknownDownloadItemModeException.java b/client/trunk/src/com/fourisland/instadisc/DownloadItem/UnknownDownloadItemModeException.java new file mode 100644 index 0000000..4fc7758 --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/DownloadItem/UnknownDownloadItemModeException.java @@ -0,0 +1,5 @@ +package com.fourisland.instadisc.DownloadItem; + +public class UnknownDownloadItemModeException extends Exception +{ +} \ No newline at end of file diff --git a/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java b/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java deleted file mode 100644 index 05af63c..0000000 --- a/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package com.fourisland.instadisc; - -import com.fourisland.instadisc.Item.Item; -import java.io.IOException; -import java.io.InputStream; -import java.net.ServerSocket; -import java.net.Socket; -import java.net.SocketException; -import java.util.HashMap; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * - * @author hatkirby - */ -public class InstaDiscThread implements Runnable { - - boolean cancelled = false; - InstaDiscView idv; - - public InstaDiscThread() - { - this.idv = null; - } - - public InstaDiscThread(InstaDiscView idv) - { - this.idv = idv; - } - - public void cancel() { - cancelled = true; - } - - public void run() { - try - { - ServerSocket svr = new ServerSocket(); - java.net.InetSocketAddress addr = new java.net.InetSocketAddress(1204); - svr.bind(addr); - Runtime.getRuntime().addShutdownHook(new Thread(new CloseServerSocketThread(svr))); - while (!cancelled) - { - try - { - Socket s = svr.accept(); - HandleItemThread hit = new HandleItemThread(s,idv); - Thread hitt = new Thread(hit); - hitt.start(); - } catch (SocketException ex) - { - cancel(); - } catch (Exception ex) - { - cancel(); - Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex); - } - } - svr.close(); - } catch (IOException ex) - { - Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex); - } - - } -} - -class HandleItemThread implements Runnable { - - Socket s; - InstaDiscView idv; - - public HandleItemThread(Socket s, InstaDiscView idv) { - this.s = s; - this.idv = idv; - } - - public void run() { - try - { - idv.startProgress(); - idv.doText("Downloading Item...."); - } catch (NullPointerException ex) - { - } - - try - { - InputStream is = s.getInputStream(); - int buffer[] = new int[1000]; - int rs = 0; - int i = 0; - - while (rs != -1) - { - try - { - rs = is.read(); - - if (rs != -1) - { - buffer[i] = rs; - } - - try - { - idv.doProgress(buffer.length / (is.available()+1)); - } catch (NullPointerException ex) - { - } - - i++; - } catch (SocketException ex) - { - return; - } catch (IOException ex) - { - Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); - } - } - - StringBuilder result = new StringBuilder(); - int j = 0; - for (j = 0; j < i; j++) - { - result.append(Character.toString((char) buffer[j])); - } - - String[] headers = result.toString().split("\n"); - HashMap headerMap = new HashMap(); - i = 0; - while (1 == 1) - { - try - { - String[] nameVal = headers[i].split(": "); - String name = nameVal[0]; - String value = nameVal[1].trim().replace("__INSTADISC__", ": "); - headerMap.put(name, value); - } catch (Exception ex) - { - break; - } - i++; - } - - //Logger.getLogger(HandleItemThread.class.getName()).log(Level.INFO, headerMap.toString()); - try - { - s.close(); - } catch (IOException ex) - { - Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); - } - - Item idI = new Item(headerMap); - idI.start(); - } catch (IOException ex) - { - Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); - } - - try - { - idv.doneProgress(); - } catch (NullPointerException ex) - { - } - } -} diff --git a/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java b/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java index 24364b4..99a7ab3 100644 --- a/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java +++ b/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java @@ -5,6 +5,8 @@ package com.fourisland.instadisc; import com.fourisland.instadisc.Database.Item; import com.fourisland.instadisc.Database.Wrapper; +import com.fourisland.instadisc.DownloadItem.ModeControl; +import com.fourisland.instadisc.DownloadItem.UnknownDownloadItemModeException; import com.fourisland.instadisc.Item.Categories.InstaDiscIcon; import java.awt.AWTException; import java.awt.SystemTray; @@ -35,13 +37,14 @@ public class InstaDiscView extends FrameView { public InstaDiscView(SingleFrameApplication app) { super(app); - + initComponents(); // status bar initialization - message timeout, idle icon and busy animation, etc ResourceMap resourceMap = getResourceMap(); int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout"); - messageTimer = new Timer(messageTimeout, new ActionListener() { + messageTimer = new Timer(messageTimeout, new ActionListener() + { public void actionPerformed(ActionEvent e) { statusMessageLabel.setText(""); @@ -49,10 +52,12 @@ public class InstaDiscView extends FrameView { }); messageTimer.setRepeats(false); int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate"); - for (int i = 0; i < busyIcons.length; i++) { + for (int i = 0; i < busyIcons.length; i++) + { busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]"); } - busyIconTimer = new Timer(busyAnimationRate, new ActionListener() { + busyIconTimer = new Timer(busyAnimationRate, new ActionListener() + { public void actionPerformed(ActionEvent e) { busyIconIndex = (busyIconIndex + 1) % busyIcons.length; @@ -65,28 +70,34 @@ public class InstaDiscView extends FrameView { // connecting action tasks to status bar via TaskMonitor TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext()); - taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() { + taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() + { public void propertyChange(java.beans.PropertyChangeEvent evt) { String propertyName = evt.getPropertyName(); - if ("started".equals(propertyName)) { - if (!busyIconTimer.isRunning()) { + if ("started".equals(propertyName)) + { + if (!busyIconTimer.isRunning()) + { statusAnimationLabel.setIcon(busyIcons[0]); busyIconIndex = 0; busyIconTimer.start(); } progressBar.setVisible(true); progressBar.setIndeterminate(true); - } else if ("done".equals(propertyName)) { + } else if ("done".equals(propertyName)) + { busyIconTimer.stop(); statusAnimationLabel.setIcon(idleIcon); progressBar.setVisible(false); progressBar.setValue(0); - } else if ("message".equals(propertyName)) { + } else if ("message".equals(propertyName)) + { String text = (String) (evt.getNewValue()); statusMessageLabel.setText((text == null) ? "" : text); messageTimer.restart(); - } else if ("progress".equals(propertyName)) { + } else if ("progress".equals(propertyName)) + { int value = (Integer) (evt.getNewValue()); progressBar.setVisible(true); progressBar.setIndeterminate(false); @@ -97,12 +108,15 @@ public class InstaDiscView extends FrameView { this.getFrame().setIconImage(new ImageIcon(InstaDiscIcon.instadiscicon).getImage()); - if (SystemTray.isSupported()) { - try { + if (SystemTray.isSupported()) + { + try + { TrayIcon ti = new TrayIcon(new ImageIcon(InstaDiscIcon.instadisciconmiddle).getImage(), "InstaDisc"); SystemTray.getSystemTray().add(ti); InstaDiscApp.ti = ti; - } catch (AWTException ex) { + } catch (AWTException ex) + { Logger.getLogger(InstaDiscView.class.getName()).log(Level.SEVERE, null, ex); } } @@ -110,12 +124,15 @@ public class InstaDiscView extends FrameView { jList1.setCellRenderer(new IDItemListCellRenderer()); refreshItemPane(); - InstaDiscThread idt = new InstaDiscThread(this); - Thread idtt = new Thread(idt); - idtt.start(); - - XmlRpc xmlrpc = new XmlRpc("requestRetained"); - xmlrpc.execute(); + try + { + ModeControl.INSTANCE.initalize(Wrapper.getConfig("downloadItemMode")); + } catch (UnknownDownloadItemModeException ex) + { + Logger.getLogger(InstaDiscView.class.getName()).log(Level.SEVERE, null, ex); + } + ModeControl.INSTANCE.modeInitalize(); + ModeControl.INSTANCE.requestRetained(); updateTimer(); } @@ -396,8 +413,7 @@ public class InstaDiscView extends FrameView { }//GEN-LAST:event_jMenuItem3ActionPerformed private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed - XmlRpc xmlrpc = new XmlRpc("requestRetained"); - xmlrpc.execute(); + ModeControl.INSTANCE.requestRetained(); }//GEN-LAST:event_jMenuItem4ActionPerformed private void jList1ComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jList1ComponentShown @@ -504,31 +520,20 @@ public class InstaDiscView extends FrameView { } public void updateTimer() { - int delay = (1000 * 60 * 60); - try { ipCheckTimer.stop(); } catch (NullPointerException ex) { - - } - - if (Wrapper.getConfig("ipCheckUnit").equals("day")) { - delay *= (24 * Integer.decode(Wrapper.getConfig("ipCheckValue"))); - } else if (Wrapper.getConfig("ipCheckUnit").equals("hour")) { - delay *= Integer.decode(Wrapper.getConfig("ipCheckValue")); } - ipCheckTimer = new Timer(delay, new ActionListener() { + ipCheckTimer = new Timer(ModeControl.INSTANCE.setTimer(), new ActionListener() { public void actionPerformed(ActionEvent arg0) { - XmlRpc xmlrpc = new XmlRpc("checkRegistration"); - xmlrpc.execute(); + ModeControl.INSTANCE.timerTick(); } }); ipCheckTimer.start(); - XmlRpc xmlrpc = new XmlRpc("checkRegistration"); - xmlrpc.execute(); + ModeControl.INSTANCE.timerTick(); } public synchronized void startProgress() -- cgit 1.4.1