about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java59
-rw-r--r--client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java69
2 files changed, 87 insertions, 41 deletions
diff --git a/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java b/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java index 5895c10..07b5b28 100644 --- a/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java +++ b/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java
@@ -27,26 +27,32 @@ public class InstaDiscThread implements Runnable {
27 } 27 }
28 28
29 public void run() { 29 public void run() {
30 try { 30 try
31 {
31 ServerSocket svr = new ServerSocket(); 32 ServerSocket svr = new ServerSocket();
32 java.net.InetSocketAddress addr = new java.net.InetSocketAddress(4444); 33 java.net.InetSocketAddress addr = new java.net.InetSocketAddress(4444);
33 svr.bind(addr); 34 svr.bind(addr);
34 Runtime.getRuntime().addShutdownHook(new Thread(new CloseServerSocketThread(svr))); 35 Runtime.getRuntime().addShutdownHook(new Thread(new CloseServerSocketThread(svr)));
35 while (!cancelled) { 36 while (!cancelled)
36 try { 37 {
38 try
39 {
37 Socket s = svr.accept(); 40 Socket s = svr.accept();
38 HandleItemThread hit = new HandleItemThread(s); 41 HandleItemThread hit = new HandleItemThread(s);
39 Thread hitt = new Thread(hit); 42 Thread hitt = new Thread(hit);
40 hitt.start(); 43 hitt.start();
41 } catch (SocketException ex) { 44 } catch (SocketException ex)
45 {
42 cancel(); 46 cancel();
43 } catch (Exception ex) { 47 } catch (Exception ex)
48 {
44 cancel(); 49 cancel();
45 Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex); 50 Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex);
46 } 51 }
47 } 52 }
48 svr.close(); 53 svr.close();
49 } catch (IOException ex) { 54 } catch (IOException ex)
55 {
50 Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex); 56 Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex);
51 } 57 }
52 58
@@ -56,62 +62,77 @@ public class InstaDiscThread implements Runnable {
56class HandleItemThread implements Runnable { 62class HandleItemThread implements Runnable {
57 63
58 Socket s; 64 Socket s;
59 65
60 public HandleItemThread(Socket s) { 66 public HandleItemThread(Socket s) {
61 this.s = s; 67 this.s = s;
62 } 68 }
63 69
64 public void run() { 70 public void run() {
65 try { 71 try
72 {
66 InputStream is = s.getInputStream(); 73 InputStream is = s.getInputStream();
67 int buffer[] = new int[1000]; 74 int buffer[] = new int[1000];
68 int rs = 0; 75 int rs = 0;
69 int i = 0; 76 int i = 0;
70 77
71 while (rs != -1) { 78 while (rs != -1)
72 try { 79 {
80 try
81 {
73 rs = is.read(); 82 rs = is.read();
74 83
75 if (rs != -1) { 84 if (rs != -1)
85 {
76 buffer[i] = rs; 86 buffer[i] = rs;
77 } 87 }
78 i++; 88 i++;
79 } catch (IOException ex) { 89 } catch (SocketException ex)
90 {
91 return;
92 } catch (IOException ex)
93 {
80 Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); 94 Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex);
81 } 95 }
82 } 96 }
83 97
84 StringBuilder result = new StringBuilder(); 98 StringBuilder result = new StringBuilder();
85 int j = 0; 99 int j = 0;
86 for (j = 0; j < i; j++) { 100 for (j = 0; j < i; j++)
101 {
87 result.append(Character.toString((char) buffer[j])); 102 result.append(Character.toString((char) buffer[j]));
88 } 103 }
89 104
90 String[] headers = result.toString().split("\n"); 105 String[] headers = result.toString().split("\n");
91 HashMap<String, String> headerMap = new HashMap<String, String>(); 106 HashMap<String, String> headerMap = new HashMap<String, String>();
92 i = 0; 107 i = 0;
93 while (1 == 1) { 108 while (1 == 1)
94 try { 109 {
110 try
111 {
95 String[] nameVal = headers[i].split(": "); 112 String[] nameVal = headers[i].split(": ");
96 String name = nameVal[0]; 113 String name = nameVal[0];
97 String value = nameVal[1].trim().replace("__INSTADISC__", ": "); 114 String value = nameVal[1].trim().replace("__INSTADISC__", ": ");
98 headerMap.put(name, value); 115 headerMap.put(name, value);
99 } catch (Exception ex) { 116 } catch (Exception ex)
117 {
100 break; 118 break;
101 } 119 }
102 i++; 120 i++;
103 } 121 }
104 122
105 //Logger.getLogger(HandleItemThread.class.getName()).log(Level.INFO, headerMap.toString()); 123 //Logger.getLogger(HandleItemThread.class.getName()).log(Level.INFO, headerMap.toString());
106 try { 124 try
125 {
107 s.close(); 126 s.close();
108 } catch (IOException ex) { 127 } catch (IOException ex)
128 {
109 Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); 129 Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex);
110 } 130 }
111 131
112 Item idI = new Item(headerMap); 132 Item idI = new Item(headerMap);
113 idI.start(); 133 idI.start();
114 } catch (IOException ex) { 134 } catch (IOException ex)
135 {
115 Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); 136 Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex);
116 } 137 }
117 } 138 }
diff --git a/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java b/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java index fc91eaf..d9ed348 100644 --- a/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java +++ b/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java
@@ -28,19 +28,23 @@ public class SubscriptionFile {
28 28
29 public SubscriptionFile(URL url, JLabel status) { 29 public SubscriptionFile(URL url, JLabel status) {
30 status.setText("Checking...."); 30 status.setText("Checking....");
31 try { 31 try
32 {
32 HttpURLConnection urc = (HttpURLConnection) url.openConnection(); 33 HttpURLConnection urc = (HttpURLConnection) url.openConnection();
33 Thread th = new Thread(new SubscriptionFileThread(urc, status)); 34 Thread th = new Thread(new SubscriptionFileThread(urc, status));
34 th.start(); 35 th.start();
35 } catch (FileNotFoundException ex) { 36 } catch (FileNotFoundException ex)
37 {
36 status.setText("Error: Subscription File doesn't exist"); 38 status.setText("Error: Subscription File doesn't exist");
37 } catch (IOException ex) { 39 } catch (IOException ex)
40 {
38 Logger.getLogger(SubscriptionFile.class.getName()).log(Level.SEVERE, null, ex); 41 Logger.getLogger(SubscriptionFile.class.getName()).log(Level.SEVERE, null, ex);
39 } 42 }
40 } 43 }
41 44
42 public static void deleteSubscription(Subscription s, boolean deleteFromData) { 45 public static void deleteSubscription(Subscription s, boolean deleteFromData) {
43 if (deleteFromData) { 46 if (deleteFromData)
47 {
44 Wrapper.deleteSubscription(s.getURL()); 48 Wrapper.deleteSubscription(s.getURL());
45 } 49 }
46 50
@@ -50,8 +54,10 @@ public class SubscriptionFile {
50 54
51 int i = 0; 55 int i = 0;
52 Filter f[] = Wrapper.getAllFilter(); 56 Filter f[] = Wrapper.getAllFilter();
53 for (i = 0; i < f.length; i++) { 57 for (i = 0; i < f.length; i++)
54 if (f[i].getSubscription().equals(s.getURL())) { 58 {
59 if (f[i].getSubscription().equals(s.getURL()))
60 {
55 Wrapper.deleteFilter(f[i].getID()); 61 Wrapper.deleteFilter(f[i].getID());
56 } 62 }
57 } 63 }
@@ -70,49 +76,61 @@ class SubscriptionFileThread implements Runnable {
70 76
71 public void run() { 77 public void run() {
72 InputStream is = null; 78 InputStream is = null;
73 try { 79 try
80 {
74 is = urc.getInputStream(); 81 is = urc.getInputStream();
75 int[] buffer = new int[1000]; 82 int[] buffer = new int[1000];
76 int rs = 0; 83 int rs = 0;
77 int i = 0; 84 int i = 0;
78 85
79 while (rs != -1) { 86 while (rs != -1)
80 try { 87 {
88 try
89 {
81 rs = is.read(); 90 rs = is.read();
82 91
83 if (rs != -1) { 92 if (rs != -1)
93 {
84 buffer[i] = rs; 94 buffer[i] = rs;
85 } 95 }
86 i++; 96 i++;
87 } catch (IOException ex) { 97 } catch (IOException ex)
98 {
88 Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex); 99 Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex);
89 } 100 }
90 } 101 }
91 102
92 StringBuilder result = new StringBuilder(); 103 StringBuilder result = new StringBuilder();
93 int j = 0; 104 int j = 0;
94 for (j = 0; j < i; j++) { 105 for (j = 0; j < i; j++)
106 {
95 result.append(Character.toString((char) buffer[j])); 107 result.append(Character.toString((char) buffer[j]));
96 } 108 }
97 109
98 String[] headers = result.toString().split("\n"); 110 String[] headers = result.toString().split("\n");
99 HashMap<String, String> headerMap = new HashMap<String, String>(); 111 HashMap<String, String> headerMap = new HashMap<String, String>();
100 i = 0; 112 i = 0;
101 while (1 == 1) { 113 while (1 == 1)
102 try { 114 {
115 try
116 {
103 String[] nameVal = headers[i].split(": "); 117 String[] nameVal = headers[i].split(": ");
104 String name = nameVal[0]; 118 String name = nameVal[0];
105 String value = nameVal[1].trim(); 119 String value = nameVal[1].trim();
106 headerMap.put(name, value); 120 headerMap.put(name, value);
107 } catch (Exception ex) { 121 } catch (Exception ex)
122 {
108 break; 123 break;
109 } 124 }
110 i++; 125 i++;
111 } 126 }
112 127
113 if (headerMap.containsKey("Subscription")) { 128 if (headerMap.containsKey("Subscription"))
114 if (headerMap.containsKey("Title")) { 129 {
115 if (headerMap.containsKey("Category")) { 130 if (headerMap.containsKey("Title"))
131 {
132 if (headerMap.containsKey("Category"))
133 {
116 Subscription s = new Subscription(); 134 Subscription s = new Subscription();
117 s.setURL(headerMap.get("Subscription")); 135 s.setURL(headerMap.get("Subscription"));
118 s.setTitle(headerMap.get("Title")); 136 s.setTitle(headerMap.get("Title"));
@@ -134,12 +152,19 @@ class SubscriptionFileThread implements Runnable {
134 } else { 152 } else {
135 status.setText("Error: Subscription file is not well-formed"); 153 status.setText("Error: Subscription file is not well-formed");
136 } 154 }
137 } catch (IOException ex) { 155 } catch (FileNotFoundException ex)
156 {
157 status.setText("Error: Subscription File doesn't exist");
158 } catch (IOException ex)
159 {
138 Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex); 160 Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex);
139 } finally { 161 } finally
140 try { 162 {
163 try
164 {
141 is.close(); 165 is.close();
142 } catch (IOException ex) { 166 } catch (IOException ex)
167 {
143 Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex); 168 Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex);
144 } 169 }
145 } 170 }