Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
updating server side implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Robertze committed Oct 23, 2015
1 parent a06c825 commit 55d36e5
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,35 @@ public void run() {
writeResponse(dto);
break;
}
case "checkForUpdate" : {
try {
String currentVersion = (String) request.getArguments().get(0);
logger.debug("Checking for update to client version {}", currentVersion);
IUpdateService updater = new Updater();
boolean updateRequired = updater.isUpdateRequired(currentVersion);
Response dto = new Response(200, updateRequired);
writeResponse(dto);
} catch (IOException ex) {
logger.error("Error occurred checking for update to client", ex);
Response dto = new Response(404, "Client archive not found");
writeResponse(dto);
}
break;
}
case "getUpdate" : {
try {
IUpdateService updater = new Updater();
byte[] update = updater.getUpdate();
Response dto = new Response(200, update);
writeResponse(dto);
} catch (IOException ex) {
logger.error("Error occurred getting update to client", ex);
Response dto = new Response(500,
"Client archive cannot be serialized");
writeResponse(dto);
}
break;
}
case "close" : {
close();
logger.info("{} has disconnected", connectedClient);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.kritsit.casetracker.server.domain.services;

import java.io.IOException;

public interface IUpdateService {
boolean isUpdateRequired(String currentVersion) throws IOException;
byte[] getUpdate() throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.kritsit.casetracker.server.domain.services;

import com.kritsit.casetracker.shared.domain.FileSerializer;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.IOException;

public class Updater implements IUpdateService {
private final Logger logger = LoggerFactory.getLogger(Updater.class);
private File root;

public Updater() {
root = new File("");
}

public boolean isUpdateRequired(String currentVersion) throws IOException {
logger.debug("Checking for update for client version {}", currentVersion);
File client = findClientArchive();
String requiredVersion = getLatestVersion(client);
return !currentVersion.equals(requiredVersion);
}

public byte[] getUpdate() throws IOException {
File client = findClientArchive();
FileSerializer serializer = new FileSerializer();
return serializer.serialize(client);
}

private File findClientArchive() throws FileNotFoundException {
logger.debug("Finding client archive...");
File[] files = root.listFiles();
for (File file : files) {
String fileName = file.getName();
if (fileName.contains("client") && fileName.endsWith(".jar")) {
return file;
}
}
throw new FileNotFoundException("Unable to find client archive in " +
root.getAbsolutePath());
}

private String getLatestVersion(File client) throws IOException {
logger.debug("Running client to get version...");
Process clientProcess = new ProcessBuilder("java", "-jar",
client.getAbsolutePath(), "--version").start();
InputStreamReader inReader =
new InputStreamReader(clientProcess.getInputStream());
BufferedReader in = new BufferedReader(inReader);
String line;
while ((line = in.readLine()) != null) {
logger.debug("Output: {}", line);
if (line.contains("Version: ")) {
return line.replace("Version: ", "");
}
}
throw new IOException("Unable to find version of client archive " +
client.getAbsolutePath());
}
}

0 comments on commit 55d36e5

Please sign in to comment.