summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNils Schwabe2014-07-29 15:56:57 +0200
committerNils Schwabe2014-07-29 15:56:57 +0200
commit9c73fdd6812c3c179de0552a0fc568cb380023a0 (patch)
tree2f7de4f18a3c3d966d8fb3a6e508bb187082f923
parentMove scheduling interval to config and some more todos (diff)
downloadmasterserver-9c73fdd6812c3c179de0552a0fc568cb380023a0.tar.gz
masterserver-9c73fdd6812c3c179de0552a0fc568cb380023a0.tar.xz
masterserver-9c73fdd6812c3c179de0552a0fc568cb380023a0.zip
Change some classes / methods to fit camelCase
Remove some ridiculous stuff
-rw-r--r--src/main/java/org/openslx/imagemaster/App.java5
-rw-r--r--src/main/java/org/openslx/imagemaster/db/DbImage.java52
-rw-r--r--src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java74
-rw-r--r--src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java24
-rw-r--r--src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java10
5 files changed, 62 insertions, 103 deletions
diff --git a/src/main/java/org/openslx/imagemaster/App.java b/src/main/java/org/openslx/imagemaster/App.java
index b108576..204c15b 100644
--- a/src/main/java/org/openslx/imagemaster/App.java
+++ b/src/main/java/org/openslx/imagemaster/App.java
@@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
-import org.openslx.imagemaster.serverconnection.CRCScheduler;
+import org.openslx.imagemaster.serverconnection.CrcScheduler;
import org.openslx.imagemaster.serverconnection.ImageProcessor;
import org.openslx.imagemaster.thrift.server.BinaryListener;
import org.slf4j.LoggerFactory;
@@ -17,7 +17,6 @@ public class App
private static List<Thread> servers = new ArrayList<>();
static {
- // TODO:
// This is a temporary workaround for this annoying log4j error msg.
// It's initializing the logger before anything else is done.
LoggerFactory.getLogger("ROOT");
@@ -38,7 +37,7 @@ public class App
t.start();
// start the crc checking scheduler
- CRCScheduler.startScheduling();
+ CrcScheduler.startScheduling();
// Run more servers
// ...
diff --git a/src/main/java/org/openslx/imagemaster/db/DbImage.java b/src/main/java/org/openslx/imagemaster/db/DbImage.java
index 099738f..6c64d06 100644
--- a/src/main/java/org/openslx/imagemaster/db/DbImage.java
+++ b/src/main/java/org/openslx/imagemaster/db/DbImage.java
@@ -52,12 +52,12 @@ public class DbImage
this.serverSessionId = null;
}
- public DbImage(String UUID, int imageVersion, String imageName, String imagePath,
+ public DbImage(String uuid, int imageVersion, String imageName, String imagePath,
Timestamp imageCreateTime, Timestamp imageUpdateTime, String imageOwner, String contentOperatingSystem,
boolean isValid, boolean isDeleted, String shortDescription, String longDescription,
Timestamp timestamp, long fileSize, String token, String missingBlocksList, String serverSessionId)
{
- this.uuid = UUID;
+ this.uuid = uuid;
this.imageVersion = imageVersion;
this.imageName = imageName;
this.imagePath = imagePath;
@@ -93,7 +93,7 @@ public class DbImage
*/
public static boolean exists( String uuid )
{
- return getImageByUUID( uuid ) != null;
+ return getImageByUuid( uuid ) != null;
}
/**
@@ -102,11 +102,11 @@ public class DbImage
* @param imageData The metadata of the image
* @param ts The timestamp of inserting
* @param token The token that is able to upload this image
- * @param missingBlocks The blocks that are missing for this image
+ * @param amountBlocks The blocks that are missing for this image
* @param serverSessionId The server that is uploading this image
* @return Affected rows
*/
- public static int insert( ImageData imageData, long ts, String token, int missingBlocks, String serverSessionId, String filepath )
+ public static int insert( ImageData imageData, long ts, String token, int amountBlocks, String serverSessionId, String filepath )
{
Date createTime = new Date( imageData.imageCreateTime );
Date updateTime = new Date( imageData.imageUpdateTime );
@@ -114,7 +114,7 @@ public class DbImage
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
String missingBlocksList = "";
- for ( int i = 0; i < missingBlocks; i++ ) {
+ for ( int i = 0; i < amountBlocks; i++ ) {
missingBlocksList = missingBlocksList + String.valueOf( i ) + ";";
}
@@ -129,11 +129,16 @@ public class DbImage
token, missingBlocksList, serverSessionId );
}
- public String getUUID()
+ public String getUuid()
{
return this.uuid;
}
+ /**
+ * Updates the missing blocks of an uploading image.
+ * @param missingBlocks
+ * @return
+ */
public int updateMissingBlocks( List<Integer> missingBlocks )
{
String missingBlocksList = "";
@@ -145,11 +150,35 @@ public class DbImage
return MySQL.update( "UPDATE images SET images.missingBlocks = ? WHERE images.UUID = ?", missingBlocksList, uuid );
}
+ /**
+ * Updates the database with a new version of an image and its missing blocks.
+ * @param version
+ * @param amountBlocks
+ */
+ public void updateVersion( int version, int amountBlocks )
+ {
+ if ( version <= 0 || amountBlocks <= 0 )
+ return;
+ String missingBlocksList = "";
+ for ( int i = 0; i < amountBlocks; i++ ) {
+ missingBlocksList = missingBlocksList + String.valueOf( i ) + ";";
+ }
+ MySQL.update( "UPDATE images SET images.missingBlocks, images.version VALUES (?, ?)", missingBlocksList, version );
+ }
+
+ /**
+ * Marks an image as _deleted_ in the database.
+ * @return
+ */
public int delete()
{
- return MySQL.update( "DELETE FROM images WHERE images.UUID=?", uuid );
+ return MySQL.update( "UPDATE images SET images.isDeleted=? WHERE images.UUID=?", true, uuid );
}
+ /**
+ * Returns all images from database where blocks are still missing.
+ * @return
+ */
public static List<DbImage> getUploadingImages()
{
return MySQL
@@ -159,7 +188,12 @@ public class DbImage
"" );
}
- public static DbImage getImageByUUID( String uuid )
+ /**
+ * Returns the image that is corrsponding to a specified uuid.
+ * @param uuid
+ * @return
+ */
+ public static DbImage getImageByUuid( String uuid )
{
return MySQL
.findUniqueOrNull(
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java b/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java
deleted file mode 100644
index f990f4e..0000000
--- a/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java
+++ /dev/null
@@ -1,74 +0,0 @@
-package org.openslx.imagemaster.serverconnection;
-
-import java.io.IOException;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Timer;
-import java.util.TimerTask;
-
-import org.apache.log4j.Logger;
-import org.openslx.imagemaster.crcchecker.CRCChecker;
-
-/**
- * Class to schedule crc checks.
- */
-public class CRCScheduler extends TimerTask
-{
-
- private static Logger log = Logger.getLogger( CRCScheduler.class );
-
- @Override
- public void run()
- {
- log.debug( "Starting checks..." );
- List<UploadingImage> list = ImageProcessor.getImagesToCheck();
- log.debug( list );
- Iterator<UploadingImage> iter = list.iterator();
- while ( iter.hasNext() ) {
- UploadingImage image = iter.next();
- log.debug( "Checking blocks of " + image.getDbImage().imageName );
- CRCChecker crcChecker = new CRCChecker( image.getImageFile(), image.getCrcFile() );
- log.debug( "CRCFile is valid: " + crcChecker.hasValidCrcFile() );
- if ( !crcChecker.hasValidCrcFile() ) {
- image.setCrcFile( null ); // set crc file to null, so that the image processor will try to write it again.
- crcChecker.done();
- continue;
- }
- for ( int block = 0; block < image.getNumberOfBlocks(); block++ ) {
- if ( image.needsCheck( block ) ) {
- try {
- if ( crcChecker.checkBlock( block ) ) {
- image.setValid( block );
- log.debug( block + " was valid" );
- } else {
- image.setNeedsRequest( block );
- log.debug( block + " was NOT valid" );
- }
- } catch ( IOException e ) {
- if ( e.getMessage().equalsIgnoreCase( "crc" ) ) {
- image.setCrcFile( null ); // set crc file to null, so that the imageprocessor will try to write it again.
- image.updateDb();
- crcChecker.done();
- break;
- } else {
- // could not read image file
- log.error( "Could not read from image file on disk. Pleas contact the server administrator. Image: '" + image.getImageFile().toString() + "'" );
- }
- }
- }
- }
- image.updateDb(); // writes valid blocks to database
- crcChecker.done(); // closes image file
- }
- log.debug( "... done" );
- }
-
- public static void startScheduling()
- {
- Timer timer = new Timer( "CRCScheduler" );
-
- // start now and fire every 60 s
- timer.schedule( new CRCScheduler(), 0, 60000L );
- }
-
-}
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java
index ce89acd..a738259 100644
--- a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java
+++ b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java
@@ -11,7 +11,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.apache.log4j.Logger;
import org.openslx.imagemaster.Globals;
-import org.openslx.imagemaster.crcchecker.CRCFile;
+import org.openslx.imagemaster.crcchecker.CrcFile;
import org.openslx.imagemaster.db.DbImage;
import org.openslx.imagemaster.db.DbUser;
import org.openslx.imagemaster.thrift.iface.DownloadInfos;
@@ -83,7 +83,7 @@ public class ImageProcessor
DbImage i;
boolean isUpdate = false;
- if ( ( i = DbImage.getImageByUUID( imageData.uuid ) ) != null ) {
+ if ( ( i = DbImage.getImageByUuid( imageData.uuid ) ) != null ) {
// image is already available
// is the client updating??
if ( imageData.imageVersion <= i.imageVersion ) {
@@ -107,13 +107,13 @@ public class ImageProcessor
// check if image is already uploading
if ( ( image = uploadingImages.get( uuid ) ) != null ) {
if ( image.getCrcFile() == null ) {
- CRCFile crcFile;
+ CrcFile crcFile;
try {
// try to write crc file ...
- crcFile = CRCFile.writeCrcFile( crcSums, Globals.getImageDir() + "/" + uuid + ".crc" );
+ crcFile = CrcFile.writeCrcFile( crcSums, Globals.getImageDir() + "/" + uuid + ".crc" );
} catch ( IOException e ) {
// ... and keep it in ram if it fails
- crcFile = new CRCFile( crcSums );
+ crcFile = new CrcFile( crcSums );
}
image.setCrcFile( crcFile );
}
@@ -129,7 +129,7 @@ public class ImageProcessor
if ( DbImage.exists( imageData.uuid ) ) {
throw new ImageDataException( ImageDataError.INVALID_DATA, "UUID already existing." );
}
- if ( !CRCFile.sumsAreValid( crcSums ) )
+ if ( !CrcFile.sumsAreValid( crcSums ) )
throw new UploadException( UploadError.INVALID_CRC, "CRC sums were invalid." );
filepath = Globals.getImageDir() + "/" + uuid + ".vmdk"; // TODO: needs to be saved in another way... (Because of the versions.)
token = RandomString.generate( 100, false );
@@ -139,13 +139,13 @@ public class ImageProcessor
uploadingImages.put( uuid, image );
}
- CRCFile crcFile;
+ CrcFile crcFile;
try {
// try to write crc file ...
- crcFile = CRCFile.writeCrcFile( crcSums, Globals.getImageDir() + "/" + uuid + ".crc" ); //TODO: same here ^
+ crcFile = CrcFile.writeCrcFile( crcSums, Globals.getImageDir() + "/" + uuid + ".crc" ); //TODO: same here ^
} catch ( IOException e ) {
// ... and keep it in ram if it fails
- crcFile = new CRCFile( crcSums );
+ crcFile = new CrcFile( crcSums );
}
image.setCrcFile( crcFile );
@@ -185,7 +185,7 @@ public class ImageProcessor
// server was downloading another image and now gets a new connection for this new download
String token = RandomString.generate( 100, false );
- String filepath = DbImage.getImageByUUID( uuid ).imagePath;
+ String filepath = DbImage.getImageByUuid( uuid ).imagePath;
ConnectionHandler.addConnection( token, filepath, Connection.DOWNLOADING );
client.addDownload( uuid, requestedBlocks, token );
@@ -196,7 +196,7 @@ public class ImageProcessor
// insert new client and start listener
synchronized ( downloadingClients ) {
String token = RandomString.generate( 100, false );
- String filepath = DbImage.getImageByUUID( uuid ).imagePath;
+ String filepath = DbImage.getImageByUuid( uuid ).imagePath;
DownloadingClient client = new DownloadingClient();
client.addDownload( uuid, requestedBlocks, token );
@@ -308,7 +308,7 @@ public class ImageProcessor
for ( DbImage image : list ) {
UploadingImage infos = new UploadingImage( image.token, image.blockStatus, image.timestamp.getTime(), image.uuid, image.imagePath );
ConnectionHandler.addConnection( image.token, image.imagePath, Connection.UPLOADING ).image = infos;
- CRCFile crcFile = new CRCFile( Globals.getImageDir() + "/" + image.uuid + ".crc" ); // TODO: has to be adjusted with the corresponding value above
+ CrcFile crcFile = new CrcFile( Globals.getImageDir() + "/" + image.uuid + ".crc" ); // TODO: has to be adjusted with the corresponding value above
try {
if ( !crcFile.isValid() ) {
continue;
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java
index f67da43..b8a045e 100644
--- a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java
+++ b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java
@@ -5,7 +5,7 @@ import java.util.List;
import org.apache.log4j.Logger;
import org.openslx.imagemaster.Globals;
-import org.openslx.imagemaster.crcchecker.CRCFile;
+import org.openslx.imagemaster.crcchecker.CrcFile;
import org.openslx.imagemaster.crcchecker.ImageFile;
import org.openslx.imagemaster.db.DbImage;
@@ -35,7 +35,7 @@ public class UploadingImage
private String uuid;
private String filename;
private ImageFile imageFile = null;
- private CRCFile crcFile = null;
+ private CrcFile crcFile = null;
protected UploadingImage(String token, int[] initialBlockStatus, long timestamp, String uuid, String filename)
{
@@ -158,7 +158,7 @@ public class UploadingImage
protected DbImage getDbImage()
{
if ( dbImage == null ) {
- dbImage = DbImage.getImageByUUID( this.uuid );
+ dbImage = DbImage.getImageByUuid( this.uuid );
}
return this.dbImage;
}
@@ -171,12 +171,12 @@ public class UploadingImage
return imageFile;
}
- protected CRCFile getCrcFile()
+ protected CrcFile getCrcFile()
{
return crcFile;
}
- protected void setCrcFile( CRCFile crcFile )
+ protected void setCrcFile( CrcFile crcFile )
{
this.crcFile = crcFile;
}