autoupdate progress

This commit is contained in:
rusefi 2020-06-06 22:12:48 -04:00
parent e45ce874cb
commit c8aad33a46
10 changed files with 291 additions and 14 deletions

View File

@ -3,6 +3,7 @@
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/autotest/autotest.iml" filepath="$PROJECT_DIR$/autotest/autotest.iml" />
<module fileurl="file://$PROJECT_DIR$/autoupdate/autoupdate.iml" filepath="$PROJECT_DIR$/autoupdate/autoupdate.iml" />
<module fileurl="file://$PROJECT_DIR$/../java_tools/configuration_definition/configuration_definition.iml" filepath="$PROJECT_DIR$/../java_tools/configuration_definition/configuration_definition.iml" />
<module fileurl="file://$PROJECT_DIR$/../java_tools/enum_to_string/enum_to_string.iml" filepath="$PROJECT_DIR$/../java_tools/enum_to_string/enum_to_string.iml" />
<module fileurl="file://$PROJECT_DIR$/inifile/inifile.iml" filepath="$PROJECT_DIR$/inifile/inifile.iml" />

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="annotations" level="project" />
</component>
</module>

View File

@ -0,0 +1,239 @@
package com.rusefi.autoupdate;
import com.rusefi.ui.util.FrameHelper;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;
public class Autoupdate {
private static final String BUNDLE_NAME_FILE = "bundle_name.ini";
private static final String AUTOUPDATE_MODE = "autoupdate";
private static final int BUFFER_SIZE = 32 * 1024;
public static void main(String[] args) {
UpdateMode mode = getMode();
if (mode != UpdateMode.NEVER) {
String bundleFullName = readBundleFullName();
if (bundleFullName != null) {
System.out.println("Handling " + bundleFullName);
handleBundle(bundleFullName, mode);
}
}
startConsole(args);
}
private static void startConsole(String[] args) {
}
private static UpdateMode getMode() {
String value = getConfig().getRoot().getProperty(AUTOUPDATE_MODE);
try {
return UpdateMode.valueOf(value);
} catch (Throwable e) {
return UpdateMode.ASK;
}
}
private static void handleBundle(String bundleFullName, UpdateMode mode) {
try {
String zipFileName = bundleFullName + "_autoupdate" + ".zip";
ConnectionAndMeta connectionAndMeta = new ConnectionAndMeta(zipFileName).invoke();
if (hasExistingFile(zipFileName, connectionAndMeta.getCompleteFileSize(), connectionAndMeta.getLastModified())) {
System.out.println("We already have latest update " + new Date(connectionAndMeta.getLastModified()));
return;
}
if (mode != UpdateMode.ALWAYS) {
boolean doUpdate = askUserIfUpdateIsDesired();
if (!doUpdate)
return;
}
HttpURLConnection httpConnection = connectionAndMeta.getHttpConnection();
long completeFileSize = connectionAndMeta.getCompleteFileSize();
long lastModified = connectionAndMeta.getLastModified();
System.out.println(bundleFullName + " " + completeFileSize + " bytes, last modified " + new Date(lastModified));
BufferedInputStream in = new BufferedInputStream(httpConnection.getInputStream());
FileOutputStream fos = new FileOutputStream(zipFileName);
BufferedOutputStream bout = new BufferedOutputStream(fos, BUFFER_SIZE);
byte[] data = new byte[BUFFER_SIZE];
long downloadedFileSize = 0;
int newDataSize;
int printedPercentage = 0;
while ((newDataSize = in.read(data, 0, BUFFER_SIZE)) >= 0) {
downloadedFileSize += newDataSize;
// calculate progress
final int currentProgress = (int) ((((double) downloadedFileSize) / ((double) completeFileSize)) * 100000d);
int currentPercentage = (int) (100L * downloadedFileSize / completeFileSize);
if (currentPercentage > printedPercentage + 5) {
System.out.println("Downloaded " + currentPercentage + "%");
printedPercentage = currentPercentage;
}
// // update progress bar
// SwingUtilities.invokeLater(new Runnable() {
//
// @Override
// public void run() {
// jProgressBar.setValue(currentProgress);
// }
// });
bout.write(data, 0, newDataSize);
}
bout.close();
in.close();
File file = new File(zipFileName);
file.setLastModified(lastModified);
System.out.println("Downloaded " + file.length() + " bytes");
} catch (IOException e) {
}
}
private static boolean askUserIfUpdateIsDesired() {
AtomicBoolean doUpdate = new AtomicBoolean();
CountDownLatch frameClosed = new CountDownLatch(1);
FrameHelper frameHelper = new FrameHelper() {
@Override
protected void onWindowClosed() {
frameClosed.countDown();
}
};
JPanel choice = new JPanel(new BorderLayout());
choice.add(new JLabel("Do you want to update bundle to latest version?"), BorderLayout.NORTH);
JPanel middle = new JPanel(new FlowLayout());
JButton never = new JButton("Never");
never.setBackground(Color.red);
never.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
getConfig().getRoot().setProperty(AUTOUPDATE_MODE, UpdateMode.NEVER.toString());
frameHelper.getFrame().dispose();
}
});
middle.add(never);
JButton no = new JButton("No");
no.setBackground(Color.red);
no.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
frameHelper.getFrame().dispose();
}
});
middle.add(no);
JButton once = new JButton("Once");
once.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
doUpdate.set(true);
frameHelper.getFrame().dispose();
}
});
middle.add(once);
JButton always = new JButton("Always");
always.setBackground(Color.green);
always.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
getConfig().getRoot().setProperty(AUTOUPDATE_MODE, UpdateMode.ALWAYS.toString());
doUpdate.set(true);
frameHelper.getFrame().dispose();
}
});
middle.add(always);
choice.add(middle, BorderLayout.CENTER);
frameHelper.showFrame(choice, true);
try {
frameClosed.await();
} catch (InterruptedException e) {
// ignore
}
return doUpdate.get();
}
private static boolean hasExistingFile(String zipFileName, long completeFileSize, long lastModified) {
File file = new File(zipFileName);
return file.length() == completeFileSize && file.lastModified() == lastModified;
}
private static String readBundleFullName() {
try {
BufferedReader r = new BufferedReader(new FileReader(BUNDLE_NAME_FILE));
String fullName = r.readLine();
fullName = fullName.trim();
if (fullName.length() < 3)
return null; // just paranoia check
return fullName;
} catch (IOException e) {
return null;
}
}
enum UpdateMode {
ALWAYS,
NEVER,
ASK
}
private static class ConnectionAndMeta {
private String zipFileName;
private HttpURLConnection httpConnection;
private long completeFileSize;
private long lastModified;
public ConnectionAndMeta(String zipFileName) {
this.zipFileName = zipFileName;
}
public HttpURLConnection getHttpConnection() {
return httpConnection;
}
public long getCompleteFileSize() {
return completeFileSize;
}
public long getLastModified() {
return lastModified;
}
public ConnectionAndMeta invoke() throws IOException {
URL url = new URL("https://rusefi.com/build_server/autoupdate/" + zipFileName);
httpConnection = (HttpURLConnection) url.openConnection();
completeFileSize = httpConnection.getContentLength();
lastModified = httpConnection.getLastModified();
return this;
}
}
}

View File

@ -1,7 +1,5 @@
package com.rusefi.ui.util;
import com.rusefi.FileLog;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
@ -46,7 +44,6 @@ public class FrameHelper {
}
protected void onWindowOpened() {
FileLog.MAIN.logLine("onWindowOpened");
}
protected void onWindowClosed() {

View File

@ -10,6 +10,31 @@
<delete dir="out"/>
</target>
<target name="autoupdate_compile">
<mkdir dir="autoupdate_build/classes"/>
<javac
debug="yes"
destdir="autoupdate_build/classes"
classpath="lib/annotations.jar"
>
<src path="autoupdate/src"/>
</javac>
</target>
<target name="autoupdate_jar" depends="autoupdate_compile">
<tstamp>
<format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss"/>
</tstamp>
<jar destfile="rusefi_autoupdate.jar" basedir="autoupdate_build/classes">
<manifest>
<attribute name="Main-Class" value="com.rusefi.autoupdate.Autoupdate"/>
<attribute name="Built-Date" value="${TODAY}"/>
<attribute name="Signature-Vendor" value="rusEFI LLC"/>
</manifest>
</jar>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac debug="yes" destdir="build/classes"
@ -27,8 +52,8 @@
<src path="logging/src"/>
<!-- not really used at the moment but let's compile for the sake of Eclipse users -->
<src path="tools/src"/>
<!-- uncomment if you want to compile under java 11
please remove the space between '-' and '-add-exports' it should be double-dash
<!-- uncomment if you want to compile under java 11
please remove the space between '-' and '-add-exports' it should be double-dash
<compilerarg line="- -add-exports java.xml/com.sun.org.apache.xerces.internal.parsers=ALL-UNNAMED"/>
<compilerarg line="- -add-exports java.xml/com.sun.org.apache.xml.internal.serialize=ALL-UNNAMED"/>
-->
@ -42,9 +67,11 @@ please remove the space between '-' and '-add-exports' it should be double-dash
<jvmarg value="-ea"/>
<jvmarg value="-XX:+HeapDumpOnOutOfMemoryError"/>
<formatter type="brief"/>
<classpath path="lib/jssc.jar:build/classes:lib/junit.jar:lib/SteelSeries-3.9.30.jar:lib/miglayout-4.0.jar"/>
<classpath
path="lib/jssc.jar:build/classes:lib/junit.jar:lib/SteelSeries-3.9.30.jar:lib/miglayout-4.0.jar"/>
<batchtest todir="build">
<fileset dir="autotest/src" includes="**/test/**/*Test.java"/>
<fileset dir="autoupdate/src" includes="**/test/**/*Test.java"/>
<fileset dir="io/src" includes="**/test/**/*Test.java"/>
<fileset dir="models/src" includes="**/test/**/*Test.java"/>
<fileset dir="ui/src" includes="**/test/**/*Test.java"/>
@ -56,7 +83,7 @@ please remove the space between '-' and '-add-exports' it should be double-dash
</target>
<target name="jar" depends="compile">
<target name="jar" depends="compile, autoupdate_jar">
<mkdir dir="build/jar"/>
<delete file="${jar_file}"/>
<echo message="Building ${jar_file}"/>
@ -69,7 +96,7 @@ please remove the space between '-' and '-add-exports' it should be double-dash
</copy>
<tstamp>
<format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" />
<format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss"/>
</tstamp>
<mkdir dir="${jar_file_folder}"/>

View File

@ -8,7 +8,7 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="junit" level="project" />
<orderEntry type="library" exported="" name="annotations" level="project" />
<orderEntry type="module" module-name="models" exported="" />
<orderEntry type="module" module-name="autoupdate" exported="" />
</component>
</module>

View File

@ -25,7 +25,7 @@ public class MainFrame {
private FrameHelper frame = new FrameHelper() {
@Override
protected void onWindowOpened() {
super.onWindowOpened();
FileLog.MAIN.logLine("onWindowOpened");
windowOpenedHandler();
}

View File

@ -21,5 +21,6 @@
<orderEntry type="library" name="httpclient" level="project" />
<orderEntry type="library" name="XML for 11" level="project" />
<orderEntry type="module" module-name="shared_ui" />
<orderEntry type="module" module-name="autoupdate" exported="" />
</component>
</module>