diff options
5 files changed, 55 insertions, 49 deletions
diff --git a/src/main/java/org/openslx/imagemaster/App.java b/src/main/java/org/openslx/imagemaster/App.java index 204c15b..9036977 100644 --- a/src/main/java/org/openslx/imagemaster/App.java +++ b/src/main/java/org/openslx/imagemaster/App.java @@ -15,30 +15,30 @@ public class App private static Logger log = Logger.getLogger( App.class ); private static List<Thread> servers = new ArrayList<>(); - + static { // This is a temporary workaround for this annoying log4j error msg. // It's initializing the logger before anything else is done. - LoggerFactory.getLogger("ROOT"); + LoggerFactory.getLogger( "ROOT" ); } public static void main( String[] args ) { // Init logging log.info( "Starting Application" ); - + Globals.init(); ImageProcessor.init(); - + // Create binary listener Thread t; t = new Thread( new BinaryListener(), "BinaryListener" ); servers.add( t ); t.start(); - + // start the crc checking scheduler CrcScheduler.startScheduling(); - + // Run more servers // ... // Wait for all servers to die diff --git a/src/main/java/org/openslx/imagemaster/Globals.java b/src/main/java/org/openslx/imagemaster/Globals.java index 7289760..b5ec686 100644 --- a/src/main/java/org/openslx/imagemaster/Globals.java +++ b/src/main/java/org/openslx/imagemaster/Globals.java @@ -27,8 +27,6 @@ public class Globals /** * Loads the properties from config/global.properties - * - * @throws IOException */ static { diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java index d68887d..d5e253c 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java @@ -81,19 +81,20 @@ public class ImageProcessor } else if ( !DbUser.exists( imageData.imageOwner ) ) { throw new ImageDataException( ImageDataError.INVALID_DATA, "User is not known." ); } - - DbImage i; - boolean isUpdate = false; - if ( ( i = DbImage.getImageByUuid( imageData.uuid ) ) != null ) { - // image is already available - // is the client updating?? - if ( imageData.imageVersion <= i.imageVersion ) { - throw new ImageDataException( ImageDataError.INVALID_DATA, "This image with the same or a newer version is already available." ); - } else { - // TODO: update db and prepare for new image file - isUpdate = true; - } - } + + //TODO: this is not working like this: + DbImage i = DbImage.getImageByUuid( imageData.uuid ); +// boolean isUpdate = false; +// if ( i != null ) { +// // image is already available +// // is the client updating?? +// if ( imageData.imageVersion <= i.imageVersion ) { +// throw new ImageDataException( ImageDataError.INVALID_DATA, "This image with the same or a newer version is already available." ); +// } else { +// // TODO: update db and prepare for new image file +// isUpdate = true; +// } +// } log.debug( serverSessionId + " is submitting " + imageData.uuid ); @@ -111,7 +112,7 @@ public class ImageProcessor CrcFile crcFile; try { // try to write crc file ... - crcFile = CrcFile.writeCrcFile( crcSums, Globals.getImageDir() + "/" + uuid + ".crc" ); + crcFile = CrcFile.writeCrcFile( crcSums, generateFilepathOfCrcFile( imageData ) ); } catch ( IOException e ) { // ... and keep it in ram if it fails crcFile = new CrcFile( crcSums ); @@ -129,7 +130,7 @@ public class ImageProcessor // insert new image if ( !CrcFile.sumsAreValid( crcSums ) ) throw new UploadException( UploadError.INVALID_CRC, "CRC sums were invalid." ); - filepath = generateFilepathOfImage( imageData, !isUpdate ); + filepath = generateFilepathOfImage( imageData ); token = RandomString.generate( 100, false ); nBlocks = Util.getNumberOfBlocks( imageData.fileSize, Globals.blockSize ); allBlocks = new int[ nBlocks ]; // initialize array with all zeros which mean that this block is missing @@ -149,11 +150,11 @@ public class ImageProcessor image.setCrcFile( crcFile ); ConnectionHandler.addConnection( token, filepath, Connection.UPLOADING ).image = image; - if ( isUpdate ) { - i.updateVersion( i.imageVersion, Util.getNumberOfBlocks( i.fileSize, Globals.blockSize ) ); - } else { +// if ( isUpdate ) { +// i.updateVersion( i.imageVersion, Util.getNumberOfBlocks( i.fileSize, Globals.blockSize ) ); +// } else { DbImage.insert( imageData, System.currentTimeMillis(), token, nBlocks, serverSessionId, filepath ); - } +// } imagesToCheck.add( uuid ); log.debug( imagesToCheck ); @@ -237,7 +238,6 @@ public class ImageProcessor int got = 0; for ( int i = 0; i < image.getNumberOfBlocks(); i++ ) { - log.debug( i ); if ( image.needsRequest( i ) ) { int times = image.getTimesTransmitted( i ); if ( times > Globals.getSslTransmitTimes() ) { @@ -287,7 +287,6 @@ public class ImageProcessor log.debug( imagesToCheck ); while ( iter.hasNext() ) { result.add( uploadingImages.get( iter.next() ) ); - log.debug( "Added an image ... " ); } return result; } @@ -295,6 +294,7 @@ public class ImageProcessor public static List<Integer> getRequestedBlocks( String token ) { // for the downloader + // TODO return null; } @@ -307,22 +307,29 @@ public class ImageProcessor * @param createFolder If you want the folder to be created * @return The filePath of the given image */ - public static String generateFilepathOfImage( ImageData imageData, boolean createFolder ) + public static String generateFilepathOfImage( ImageData imageData ) { - String result = Globals.getImageDir() + "/" + imageData.uuid + "/"; - if ( createFolder ) { - File f = new File( result ); - if ( !f.exists() ) { - f.mkdirs(); - } + return generateFilePathOfImage( imageData.uuid, imageData.imageName, imageData.imageVersion ); + } + + public static String generateFilePathOfImage( String uuid, String imageName, int imageVersion) { + String result = Globals.getImageDir() + "/" + uuid + "/"; + File dir = new File(result); + if ( !dir.exists() ) { + dir.mkdirs(); } - result += imageData.imageName + "-v" + String.valueOf( imageData.imageVersion ) + ".vmdk"; + result += imageName + "-v" + String.valueOf( imageVersion ) + ".vmdk"; return result; } public static String generateFilepathOfCrcFile( ImageData imageData ) { - return generateFilepathOfImage( imageData, false ) + ".crc"; + return generateFilepathOfImage( imageData ) + ".crc"; + } + + public static String generateFilepathOfCrcFile( String uuid, String imageName, int imageVersion ) + { + return generateFilePathOfImage( uuid, imageName, imageVersion ) + ".crc"; } /** @@ -334,7 +341,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( generateFilepathOfCrcFile( image.uuid, image.imageName, image.imageVersion ) ); // 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 0e431fa..f290bb6 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java @@ -141,11 +141,11 @@ public class UploadingImage protected int getNumberOfBlocks() { /////////////////////////////////////////////////////////////////// - ArrayList<Integer> l = new ArrayList<Integer>( blockStatus.length ); - for ( int i : blockStatus ) { - l.add( i ); - } - log.debug( l ); +// ArrayList<Integer> l = new ArrayList<Integer>( blockStatus.length ); +// for ( int i : blockStatus ) { +// l.add( i ); +// } +// log.debug( l ); /////////////////////////////////////////////////////////////////// return blockStatus.length; } diff --git a/src/main/java/org/openslx/imagemaster/serversession/ServerAuthenticator.java b/src/main/java/org/openslx/imagemaster/serversession/ServerAuthenticator.java index a5fef69..ccd813e 100644 --- a/src/main/java/org/openslx/imagemaster/serversession/ServerAuthenticator.java +++ b/src/main/java/org/openslx/imagemaster/serversession/ServerAuthenticator.java @@ -51,7 +51,8 @@ public class ServerAuthenticator String address, ByteBuffer challengeResponse ) throws AuthenticationException, TException { - byte[] bytes = challengeResponse.array(); + byte[] bytes = new byte[ 512 ]; + challengeResponse.get( bytes ); boolean result = false; @@ -59,18 +60,18 @@ public class ServerAuthenticator try { verifier = new AsymMessageVerifier( organization ); } catch ( Exception e ) { - throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_ORGANIZATION, "Organization not found."); + throw new ServerAuthenticationException( ServerAuthenticationError.INVALID_ORGANIZATION, "Organization not found." ); } - + try { result = verifier.verifyMessage( bytes, authenticatingServers.get( organization ).getBytes() ); } catch ( Exception e ) { log.error( "Error while verifying message", e ); - throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_KEY, "Could not verfiy key."); + throw new ServerAuthenticationException( ServerAuthenticationError.INVALID_KEY, "Could not verfiy key." ); } if ( !result ) { - throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_KEY, "Could not verfiy key."); + throw new ServerAuthenticationException( ServerAuthenticationError.INVALID_KEY, "Could not verfiy key." ); } log.info( "Server of organinzation '" + organization + " authenticated." ); |