diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2008-09-27 14:12:48 +0000 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2008-09-27 14:12:48 +0000 |
commit | f20ca875e0f276b7867f360de32e210cdac0f4ee (patch) | |
tree | 26b4f723ce23d3a386dfbec253a3b978ee6a3ca5 /client/trunk/src/com/fourisland/instadisc/DownloadItem | |
parent | 50c1fa87dc9a9d081298f9873168d14af1be0921 (diff) | |
download | instadisc-f20ca875e0f276b7867f360de32e210cdac0f4ee.tar.gz instadisc-f20ca875e0f276b7867f360de32e210cdac0f4ee.tar.bz2 instadisc-f20ca875e0f276b7867f360de32e210cdac0f4ee.zip |
Client: Worked on step 2
Hopefully I finished it, though, you never know, there could be more stuff in there. Refs #69
Diffstat (limited to 'client/trunk/src/com/fourisland/instadisc/DownloadItem')
6 files changed, 276 insertions, 0 deletions
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 @@ | |||
1 | package com.fourisland.instadisc.DownloadItem; | ||
2 | |||
3 | public class DeinitalizeModeThread implements Runnable | ||
4 | { | ||
5 | public void run() { | ||
6 | ModeControl.INSTANCE.modeDeinitalize(); | ||
7 | } | ||
8 | } \ 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 @@ | |||
1 | package com.fourisland.instadisc.DownloadItem; | ||
2 | |||
3 | import java.util.Timer; | ||
4 | |||
5 | public interface DownloadItemMode | ||
6 | { | ||
7 | public void modeInitalize(); | ||
8 | public void modeDeinitalize(); | ||
9 | |||
10 | public void requestRetained(); | ||
11 | |||
12 | public int setTimer(); | ||
13 | public void timerTick(); | ||
14 | } \ 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 @@ | |||
1 | package com.fourisland.instadisc.DownloadItem; | ||
2 | |||
3 | import java.util.Timer; | ||
4 | |||
5 | public class ModeControl implements DownloadItemMode | ||
6 | { | ||
7 | public static final ModeControl INSTANCE = new ModeControl(); | ||
8 | private DownloadItemMode dim; | ||
9 | |||
10 | public void initalize(String dim) throws UnknownDownloadItemModeException | ||
11 | { | ||
12 | if (dim.equals("Push")) | ||
13 | { | ||
14 | this.dim = new PushMode(); | ||
15 | } else if (dim.equals("Pull")) | ||
16 | { | ||
17 | this.dim = new PullMode(); | ||
18 | } else { | ||
19 | throw new UnknownDownloadItemModeException(); | ||
20 | } | ||
21 | |||
22 | Runtime.getRuntime().addShutdownHook(new Thread(new DeinitalizeModeThread())); | ||
23 | } | ||
24 | |||
25 | public void modeInitalize() { | ||
26 | dim.modeInitalize(); | ||
27 | } | ||
28 | |||
29 | public void modeDeinitalize() { | ||
30 | dim.modeDeinitalize(); | ||
31 | } | ||
32 | |||
33 | public void requestRetained() | ||
34 | { | ||
35 | dim.requestRetained(); | ||
36 | } | ||
37 | |||
38 | public int setTimer() { | ||
39 | return dim.setTimer(); | ||
40 | } | ||
41 | |||
42 | public void timerTick() { | ||
43 | dim.timerTick(); | ||
44 | } | ||
45 | } \ 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 @@ | |||
1 | package com.fourisland.instadisc.DownloadItem; | ||
2 | |||
3 | public class PullMode implements DownloadItemMode | ||
4 | { | ||
5 | public void modeInitalize() { | ||
6 | throw new UnsupportedOperationException("Not supported yet."); | ||
7 | } | ||
8 | |||
9 | public void modeDeinitalize() { | ||
10 | throw new UnsupportedOperationException("Not supported yet."); | ||
11 | } | ||
12 | |||
13 | public void requestRetained() { | ||
14 | throw new UnsupportedOperationException("Not supported yet."); | ||
15 | } | ||
16 | |||
17 | public int setTimer() { | ||
18 | throw new UnsupportedOperationException("Not supported yet."); | ||
19 | } | ||
20 | |||
21 | public void timerTick() { | ||
22 | throw new UnsupportedOperationException("Not supported yet."); | ||
23 | } | ||
24 | } \ 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 @@ | |||
1 | package com.fourisland.instadisc.DownloadItem; | ||
2 | |||
3 | import com.fourisland.instadisc.Database.Wrapper; | ||
4 | import com.fourisland.instadisc.Item.Item; | ||
5 | import com.fourisland.instadisc.XmlRpc; | ||
6 | import java.io.IOException; | ||
7 | import java.io.InputStream; | ||
8 | import java.net.ServerSocket; | ||
9 | import java.net.Socket; | ||
10 | import java.net.SocketException; | ||
11 | import java.util.HashMap; | ||
12 | import java.util.logging.Level; | ||
13 | import java.util.logging.Logger; | ||
14 | |||
15 | |||
16 | public class PushMode implements DownloadItemMode | ||
17 | { | ||
18 | InstaDiscThread idt; | ||
19 | |||
20 | public void modeInitalize() | ||
21 | { | ||
22 | idt = new InstaDiscThread(); | ||
23 | new Thread(idt).start(); | ||
24 | } | ||
25 | |||
26 | public void modeDeinitalize() | ||
27 | { | ||
28 | idt.kill(); | ||
29 | } | ||
30 | |||
31 | public void requestRetained() { | ||
32 | XmlRpc xmlrpc = new XmlRpc("requestRetained"); | ||
33 | xmlrpc.execute(); | ||
34 | } | ||
35 | |||
36 | public int setTimer() { | ||
37 | int delay = (1000 * 60 * 60); | ||
38 | if (Wrapper.getConfig("ipCheckUnit").equals("day")) { | ||
39 | delay *= (24 * Integer.decode(Wrapper.getConfig("ipCheckValue"))); | ||
40 | } else if (Wrapper.getConfig("ipCheckUnit").equals("hour")) { | ||
41 | delay *= Integer.decode(Wrapper.getConfig("ipCheckValue")); | ||
42 | } | ||
43 | |||
44 | return delay; | ||
45 | } | ||
46 | |||
47 | public void timerTick() { | ||
48 | XmlRpc xmlrpc = new XmlRpc("checkRegistration"); | ||
49 | xmlrpc.execute(); | ||
50 | } | ||
51 | } | ||
52 | class InstaDiscThread implements Runnable { | ||
53 | |||
54 | boolean cancelled = false; | ||
55 | ServerSocket svr; | ||
56 | |||
57 | public void cancel() { | ||
58 | cancelled = true; | ||
59 | } | ||
60 | |||
61 | public void run() { | ||
62 | try | ||
63 | { | ||
64 | svr = new ServerSocket(); | ||
65 | java.net.InetSocketAddress addr = new java.net.InetSocketAddress(1204); | ||
66 | svr.bind(addr); | ||
67 | while (!cancelled) | ||
68 | { | ||
69 | try | ||
70 | { | ||
71 | Socket s = svr.accept(); | ||
72 | HandleItemThread hit = new HandleItemThread(s); | ||
73 | Thread hitt = new Thread(hit); | ||
74 | hitt.start(); | ||
75 | } catch (SocketException ex) | ||
76 | { | ||
77 | cancel(); | ||
78 | } catch (Exception ex) | ||
79 | { | ||
80 | cancel(); | ||
81 | Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex); | ||
82 | } | ||
83 | } | ||
84 | svr.close(); | ||
85 | } catch (IOException ex) | ||
86 | { | ||
87 | Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | public void kill() | ||
92 | { | ||
93 | try | ||
94 | { | ||
95 | svr.close(); | ||
96 | } catch (IOException ex) | ||
97 | { | ||
98 | Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex); | ||
99 | } | ||
100 | } | ||
101 | } | ||
102 | |||
103 | class HandleItemThread implements Runnable { | ||
104 | |||
105 | Socket s; | ||
106 | |||
107 | public HandleItemThread(Socket s) { | ||
108 | this.s = s; | ||
109 | } | ||
110 | |||
111 | public void run() { | ||
112 | try | ||
113 | { | ||
114 | InputStream is = s.getInputStream(); | ||
115 | int buffer[] = new int[1000]; | ||
116 | int rs = 0; | ||
117 | int i = 0; | ||
118 | |||
119 | while (rs != -1) | ||
120 | { | ||
121 | try | ||
122 | { | ||
123 | rs = is.read(); | ||
124 | |||
125 | if (rs != -1) | ||
126 | { | ||
127 | buffer[i] = rs; | ||
128 | } | ||
129 | |||
130 | i++; | ||
131 | } catch (SocketException ex) | ||
132 | { | ||
133 | return; | ||
134 | } catch (IOException ex) | ||
135 | { | ||
136 | Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); | ||
137 | } | ||
138 | } | ||
139 | |||
140 | StringBuilder result = new StringBuilder(); | ||
141 | int j = 0; | ||
142 | for (j = 0; j < i; j++) | ||
143 | { | ||
144 | result.append(Character.toString((char) buffer[j])); | ||
145 | } | ||
146 | |||
147 | String[] headers = result.toString().split("\n"); | ||
148 | HashMap<String, String> headerMap = new HashMap<String, String>(); | ||
149 | i = 0; | ||
150 | while (1 == 1) | ||
151 | { | ||
152 | try | ||
153 | { | ||
154 | String[] nameVal = headers[i].split(": "); | ||
155 | String name = nameVal[0]; | ||
156 | String value = nameVal[1].trim().replace("__INSTADISC__", ": "); | ||
157 | headerMap.put(name, value); | ||
158 | } catch (Exception ex) | ||
159 | { | ||
160 | break; | ||
161 | } | ||
162 | i++; | ||
163 | } | ||
164 | |||
165 | try | ||
166 | { | ||
167 | s.close(); | ||
168 | } catch (IOException ex) | ||
169 | { | ||
170 | Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); | ||
171 | } | ||
172 | |||
173 | Item idI = new Item(headerMap); | ||
174 | idI.start(); | ||
175 | } catch (IOException ex) | ||
176 | { | ||
177 | Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); | ||
178 | } | ||
179 | } | ||
180 | } | ||
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 @@ | |||
1 | package com.fourisland.instadisc.DownloadItem; | ||
2 | |||
3 | public class UnknownDownloadItemModeException extends Exception | ||
4 | { | ||
5 | } \ No newline at end of file | ||