ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/AE/installer2/src/net/oni2/aeinstaller/backend/mods/download/ModDownloader.java
Revision: 604
Committed: Sat Jan 12 22:48:33 2013 UTC (12 years, 9 months ago) by alloc
Content type: text/x-java
File size: 3068 byte(s)
Log Message:
AEI2: Added load/save config

File Contents

# Content
1 package net.oni2.aeinstaller.backend.mods.download;
2
3 import java.util.Date;
4 import java.util.TreeSet;
5 import java.util.Vector;
6
7 import net.oni2.aeinstaller.backend.mods.Mod;
8 import net.oni2.aeinstaller.backend.mods.download.ModDownload.ModDownloadState;
9
10 /**
11 * @author Christian Illy
12 */
13 public class ModDownloader implements ModDownloadListener {
14 /**
15 * @author Christian Illy
16 */
17 public enum State {
18 /**
19 * Downloads running
20 */
21 RUNNING,
22 /**
23 * Aborted because of an error
24 */
25 ERROR,
26 /**
27 * Downloads interrupted
28 */
29 INTERRUPTED,
30 /**
31 * Everything completed
32 */
33 FINISHED
34 };
35
36 private int currentDownload = -1;
37 private int unpacked = 0;
38 private Vector<ModDownload> downloads = new Vector<ModDownload>();
39 private int totalSize = 0;
40 private int downloadedComplete = 0;
41 private int downloadedCurrent = 0;
42 private long startMS;
43 private State state = State.RUNNING;
44 private ModDownloaderListener listener;
45
46 /**
47 * Create a mods-download-process
48 *
49 * @param mods
50 * Mods to download
51 * @param listener
52 * Listener for status updates
53 */
54 public ModDownloader(TreeSet<Mod> mods, ModDownloaderListener listener) {
55 this.listener = listener;
56 for (Mod m : mods) {
57 downloads.add(new ModDownload(m, this));
58 totalSize += m.getZipSize();
59 }
60 startMS = new Date().getTime();
61 startNextDownload();
62 }
63
64 private void startNextDownload() {
65 if (currentDownload >= 0)
66 downloadedComplete += downloads.get(currentDownload).getSize();
67 currentDownload++;
68 downloadedCurrent = 0;
69 if (currentDownload < downloads.size()) {
70 downloads.get(currentDownload).start();
71 } else {
72 notifyListener();
73 }
74 }
75
76 /**
77 * @return Average download speed for up to now in B/s
78 */
79 public int getDownloadSpeed() {
80 long duration = new Date().getTime() - startMS;
81 int down = downloadedComplete + downloadedCurrent;
82 return (int) (down * 1000 / duration);
83 }
84
85 /**
86 * @return Remaining time for all downloads by avg-speed in seconds
87 */
88 public int getTimeRemaining() {
89 int remainingSize = totalSize
90 - (downloadedComplete + downloadedCurrent);
91 return remainingSize / getDownloadSpeed();
92 }
93
94 private void notifyListener() {
95 listener.updateStatus(this, state, unpacked, downloads.size(),
96 downloadedComplete + downloadedCurrent, totalSize);
97 }
98
99 /**
100 * @return Is this process finished
101 */
102 public boolean isFinished() {
103 return state == State.FINISHED;
104 }
105
106 @Override
107 public void modDownloadStatusUpdate(ModDownload source,
108 ModDownloadState state, int done, int total) {
109 switch (state) {
110 case RUNNING:
111 downloadedCurrent = done;
112 notifyListener();
113 break;
114 case ERROR:
115 this.state = State.ERROR;
116 break;
117 case DOWNLOADED:
118 if (source == downloads.get(currentDownload))
119 startNextDownload();
120 break;
121 case UNPACKED:
122 source.getMod().updateLocalData();
123 unpacked++;
124 if (unpacked >= downloads.size())
125 this.state = State.FINISHED;
126 notifyListener();
127 break;
128 case INIT:
129 break;
130 case INTERRUPTED:
131 break;
132 }
133 }
134
135 }