source: AE/installer2/src/net/oni2/aeinstaller/backend/mods/download/ModDownloader.java@ 640

Last change on this file since 640 was 638, checked in by alloc, 12 years ago

AEI2 0.95:

  • Made download-window non-abortable as soon as last file was downloaded (but not yet unpacked)
  • Fixed table to work with local-only packages again (regression introduced when adding last-change column)
  • Added mod version number to contents pane
File size: 3.5 KB
Line 
1package net.oni2.aeinstaller.backend.mods.download;
2
3import java.util.Date;
4import java.util.TreeSet;
5import java.util.Vector;
6
7import net.oni2.aeinstaller.backend.mods.Mod;
8import net.oni2.aeinstaller.backend.mods.download.ModDownload.ModDownloadState;
9
10/**
11 * @author Christian Illy
12 */
13public 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 * When the last file was downloaded and only unpacking is left
32 */
33 LAST_FILE_DOWNLOADED,
34 /**
35 * Everything completed
36 */
37 FINISHED
38 };
39
40 private int currentDownload = -1;
41 private int unpacked = 0;
42 private Vector<ModDownload> downloads = new Vector<ModDownload>();
43 private int totalSize = 0;
44 private int downloadedComplete = 0;
45 private int downloadedCurrent = 0;
46 private long startMS;
47 private State state = State.RUNNING;
48 private ModDownloaderListener listener;
49
50 /**
51 * Create a mods-download-process
52 *
53 * @param mods
54 * Mods to download
55 * @param listener
56 * Listener for status updates
57 */
58 public ModDownloader(TreeSet<Mod> mods, ModDownloaderListener listener) {
59 this.listener = listener;
60 for (Mod m : mods) {
61 downloads.add(new ModDownload(m, this));
62 totalSize += m.getZipSize();
63 }
64 startMS = new Date().getTime();
65 startNextDownload();
66 }
67
68 private void startNextDownload() {
69 if (currentDownload >= 0)
70 downloadedComplete += downloads.get(currentDownload).getSize();
71 currentDownload++;
72 downloadedCurrent = 0;
73 if ((state == State.RUNNING) && (currentDownload < downloads.size())) {
74 downloads.get(currentDownload).start();
75 }else if (state == State.RUNNING) {
76 state = State.LAST_FILE_DOWNLOADED;
77 notifyListener();
78 } else {
79 notifyListener();
80 }
81 }
82
83 private int getTimeElapsed() {
84 int total = (int) (new Date().getTime() - startMS) / 1000;
85 return total;
86 }
87
88 private int getDownloadSpeed() {
89 int elap = getTimeElapsed();
90 int down = downloadedComplete + downloadedCurrent;
91 if (elap > 0)
92 return down / elap;
93 else
94 return 1;
95 }
96
97 private int getTimeRemaining() {
98 int remainingSize = totalSize
99 - (downloadedComplete + downloadedCurrent);
100 return remainingSize / getDownloadSpeed();
101 }
102
103 private void notifyListener() {
104 listener.updateStatus(this, state, unpacked, downloads.size(),
105 downloadedComplete + downloadedCurrent, totalSize,
106 getTimeElapsed(), getTimeRemaining(), getDownloadSpeed());
107 }
108
109 /**
110 * @return total download size
111 */
112 public int getTotalSize() {
113 return totalSize;
114 }
115
116 /**
117 * @return Is this process finished
118 */
119 public boolean isFinished() {
120 return state == State.FINISHED;
121 }
122
123 @Override
124 public void modDownloadStatusUpdate(ModDownload source,
125 ModDownloadState state, int done, int total) {
126 switch (state) {
127 case RUNNING:
128 downloadedCurrent = done;
129 notifyListener();
130 break;
131 case ERROR:
132 this.state = State.ERROR;
133 break;
134 case DOWNLOADED:
135 if (source == downloads.get(currentDownload))
136 startNextDownload();
137 break;
138 case UNPACKED:
139 source.getMod().updateLocalData();
140 unpacked++;
141 if (unpacked >= downloads.size())
142 this.state = State.FINISHED;
143 notifyListener();
144 break;
145 case INIT:
146 break;
147 case INTERRUPTED:
148 break;
149 }
150 }
151
152 /**
153 * Abort download process
154 */
155 public void abort() {
156 if (currentDownload < downloads.size()) {
157 state = State.INTERRUPTED;
158 ModDownload md = downloads.get(currentDownload);
159 md.abort();
160 }
161 }
162
163}
Note: See TracBrowser for help on using the repository browser.