From 8b426229b9f0d7b360e487871930b682f104dd9f Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 28 Jul 2008 10:00:25 +0000 Subject: Added listening server The Client now listens for connections on port 4444, allowing multiple connections at once. Once a connection iis found, the Cliet waits until the connecton is broken, then downloads the sent dat, parses it into headers and passes it to the Item class. --- .../com/fourisland/instadisc/InstaDiscThread.java | 114 +++++++++++++++++++++ .../com/fourisland/instadisc/InstaDiscView.form | 29 +++++- .../com/fourisland/instadisc/InstaDiscView.java | 20 +++- .../src/com/fourisland/instadisc/Item/Item.java | 27 +++++ 4 files changed, 184 insertions(+), 6 deletions(-) create mode 100644 client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java create mode 100644 client/trunk/src/com/fourisland/instadisc/Item/Item.java (limited to 'client/trunk') diff --git a/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java b/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java new file mode 100644 index 0000000..69a8e49 --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java @@ -0,0 +1,114 @@ +/* + * 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.util.HashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author hatkirby + */ +public class InstaDiscThread implements Runnable { + + boolean cancelled = false; + + public void cancel() { + cancelled = true; + } + + public void run() { + try { + ServerSocket svr = new ServerSocket(); + java.net.InetSocketAddress addr = new java.net.InetSocketAddress(4444); + svr.bind(addr); + while (!cancelled) { + try { + Socket s = svr.accept(); + HandleItemThread hit = new HandleItemThread(s); + Thread hitt = new Thread(hit); + hitt.start(); + } 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; + + 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 (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]; + 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); + } + } +} diff --git a/client/trunk/src/com/fourisland/instadisc/InstaDiscView.form b/client/trunk/src/com/fourisland/instadisc/InstaDiscView.form index 90b1160..bb3cc48 100644 --- a/client/trunk/src/com/fourisland/instadisc/InstaDiscView.form +++ b/client/trunk/src/com/fourisland/instadisc/InstaDiscView.form @@ -10,15 +10,38 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + @@ -91,7 +114,7 @@ - + diff --git a/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java b/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java index 3b4daf4..d43589a 100644 --- a/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java +++ b/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java @@ -79,6 +79,10 @@ public class InstaDiscView extends FrameView { } } }); + + InstaDiscThread idt = new InstaDiscThread(); + Thread idtt = new Thread(idt); + idtt.start(); } @Action @@ -100,6 +104,8 @@ public class InstaDiscView extends FrameView { private void initComponents() { mainPanel = new javax.swing.JPanel(); + jScrollPane1 = new javax.swing.JScrollPane(); + jList1 = new javax.swing.JList(); menuBar = new javax.swing.JMenuBar(); javax.swing.JMenu fileMenu = new javax.swing.JMenu(); javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem(); @@ -113,15 +119,21 @@ public class InstaDiscView extends FrameView { mainPanel.setName("mainPanel"); // NOI18N + jScrollPane1.setName("jScrollPane1"); // NOI18N + + jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); + jList1.setName("jList1"); // NOI18N + jScrollPane1.setViewportView(jList1); + javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel); mainPanel.setLayout(mainPanelLayout); mainPanelLayout.setHorizontalGroup( mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGap(0, 400, Short.MAX_VALUE) + .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) ); mainPanelLayout.setVerticalGroup( mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGap(0, 252, Short.MAX_VALUE) + .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE) ); menuBar.setName("menuBar"); // NOI18N @@ -165,7 +177,7 @@ public class InstaDiscView extends FrameView { .addGroup(statusPanelLayout.createSequentialGroup() .addContainerGap() .addComponent(statusMessageLabel) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 226, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 214, Short.MAX_VALUE) .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(statusAnimationLabel) @@ -189,6 +201,8 @@ public class InstaDiscView extends FrameView { }// //GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JList jList1; + private javax.swing.JScrollPane jScrollPane1; private javax.swing.JPanel mainPanel; private javax.swing.JMenuBar menuBar; private javax.swing.JProgressBar progressBar; diff --git a/client/trunk/src/com/fourisland/instadisc/Item/Item.java b/client/trunk/src/com/fourisland/instadisc/Item/Item.java new file mode 100644 index 0000000..f13ec3d --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/Item/Item.java @@ -0,0 +1,27 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.instadisc.Item; + +import java.util.HashMap; + +/** + * + * @author hatkirby + */ +public class Item { + + HashMap headerMap; + + public Item(HashMap headerMap) + { + this.headerMap = headerMap; + } + + public void start() + { + WellFormedItem wfi = new WellFormedItem(this); + } +} -- cgit 1.4.1