summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java')
-rw-r--r--src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java107
1 files changed, 39 insertions, 68 deletions
diff --git a/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java b/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java
index 89a8122..18462be 100644
--- a/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java
+++ b/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java
@@ -31,8 +31,6 @@ import org.openslx.satellitedaemon.Globals;
import org.openslx.satellitedaemon.db.DbImage;
import org.openslx.satellitedaemon.db.DbImage.Status;
-// TODO: Handle all the auto-generated catch blocks in a meaningful way
-
/***********************************************************************************************/
/**
* Handles the authentication with the Satellite Server and sends the
@@ -40,7 +38,7 @@ import org.openslx.satellitedaemon.db.DbImage.Status;
*/
public class ThriftConnection
{
- private static ImageServer.Client client = null;
+ private static ThreadLocal<ImageServer.Client> client = new ThreadLocal<ImageServer.Client>();
private static ServerSessionData sSD = null;
private static Logger log = Logger.getLogger( ThriftConnection.class );
private static CrcFile crc = null;
@@ -69,10 +67,6 @@ public class ThriftConnection
crc = new CrcFile( crcPath );
log.info( "Made CRCFile from " + crcPath );
log.info( "crc.getCrcSums( ).size = " + crc.getCrcSums().size() );
- // log.info( "crc.getMasterSum() : " + crc.getMasterSum() );
- // for ( int i = 0; i < crc.getCrcSums().size() - 1; i++ ) {
- // log.info( "crc.getCRCSum() : " + crc.getCRCSum( i ) );
- // }
crcSums = crc.getCrcSums();
} catch ( FileNotFoundException e ) {
log.debug( "crcFile " + crcPath + " not found." );
@@ -82,42 +76,29 @@ public class ThriftConnection
try {
return theClient.submitImage( sSD.sessionId, imDat, crcSums );
} catch ( AuthorizationException e ) {
- if ( e.isSetNumber()
- && e.getNumber().equals(
- AuthorizationError.NOT_AUTHENTICATED ) ) {
- // SessionID is not valid
- // TODO: Code for new SSID
- } else if ( e.getNumber().equals( AuthorizationError.NO_PERMISSION ) ) {
+ if ( e.number == AuthorizationError.NOT_AUTHENTICATED ) {
+ log.warn( "Suddenly nut authenticated anymore.", e );
+ } else if ( e.number == AuthorizationError.NO_PERMISSION ) {
log.error( "No permission for uploading.", e );
// TODO: add error message into database.
} else {
- e.printStackTrace();
+ log.warn( "Unknown authorization error.", e );
}
} catch ( ImageDataException e ) {
- if ( e.isSetNumber()
- && e.getNumber().equals( ImageDataError.INVALID_DATA ) ) {
- e.printStackTrace();
- // Data in the db is not valid
+ if ( e.number == ImageDataError.INVALID_DATA ) {
+ log.warn( "Data for image not valid", e );
// TODO: add e.message into DB;
} else {
- e.printStackTrace();
+ log.warn( "ImageDataException", e );
}
} catch ( UploadException e ) {
- if ( e.isSetNumber()
- && e.getNumber().equals( UploadError.BROKEN_BLOCK ) ) {
+ if ( e.number == UploadError.BROKEN_BLOCK ) {
// A Block was transmitted 20 times unsuccessfully.
// TODO: Mark the Image as corrupted.
- } else if ( e.getNumber().equals( UploadError.INVALID_CRC ) ) {
+ } else if ( e.number == UploadError.INVALID_CRC ) {
// The CRC sum contained errors
- try {
- crc = new CrcFile( crcPath );
- } catch ( IOException e1 ) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- if ( !crc.isValid() ) {
- // TODO: Mark CRC-file as corrupted.
- }
+ if ( crc != null && !crc.isValid() )
+ crc.delete();
} else {
e.printStackTrace();
}
@@ -157,8 +138,7 @@ public class ThriftConnection
// TODO: change field image_syncMode, so the image is not asked
// for again.
// For now just changed status of image. Currently no
- // possibility
- // for creating new useful state in DB. (Offenburg)
+ // possibility for creating new useful state in DB. (Offenburg)
log.info( "Image not known. For skipping next time, mark as only_local." );
imDat.updateStatus( Status.only_local );
// Plus add a note in some way to mark as unknown by Server
@@ -194,66 +174,58 @@ public class ThriftConnection
*/
private static ImageServer.Client getConnection()
{
- ImageServer.Client theClient = null;
- boolean isAuthenticated = false;
- if ( client == null ) {
- log.info( "The global client was null. Making a new client ..." );
+ ImageServer.Client theClient = client.get();
+ boolean isAuthenticated;
+
+ if ( theClient == null ) {
+ // There is no client instance for this thread, create a new one
+ log.info( "No existing client for this thread. Making a new client ..." );
theClient = newClient();
+ isAuthenticated = false;
if ( theClient == null ) {
- log.debug( "ThriftConnection: The client was null after newClient()" );
+ log.debug( "The client was null after newClient()" );
return null;
}
} else {
- log.info( "The global Client was already used. Setting isAuthenticated = true." );
- theClient = client;
- isAuthenticated = true;
- }
- // here the client was already used so we are just assuming that the
- // client is still
- // authenticated. Should be checked with the ping() method.
- try {
- isAuthenticated = theClient.isServerAuthenticated( sSD.sessionId );
- } catch ( TException x ) {
- theClient = newClient();
- if ( theClient == null ) {
- return null;
+ // There is a client instance for this thread, see if it's still usable and authenticated
+ try {
+ isAuthenticated = theClient.isServerAuthenticated( sSD.sessionId );
+ log.debug( "Existing client is " + ( isAuthenticated ? "still" : "not" ) + " authenticated." );
+ } catch ( TException x ) {
+ theClient = newClient();
+ if ( theClient == null ) {
+ log.warn( "Masterserver connection fail" );
+ return null;
+ }
+ log.debug( "Existing client was not connected anymore." );
+ isAuthenticated = false;
}
}
+
if ( !isAuthenticated ) {
- log.info( "ThriftConnection: Client not yet Authenticated. Trying..." );
try {
ByteBuffer tmpBuffer = theClient
.startServerAuthentication( Globals
.getOrganizationName() );
byte[] toEncrypt = new byte[ tmpBuffer.remaining() ];
tmpBuffer.get( toEncrypt );
- log.info( "The random String we want to encrypt: " + toEncrypt );
- log.info( "Length of the random String : " + toEncrypt.length );
AsymEncryptionHandler aeh = new AsymEncryptionHandler(
Globals.getPrivateKey() );
byte[] byteArray = aeh.encryptMessage( toEncrypt );
- log.info( "Length of the byteArray of the random string after encryption :"
- + byteArray.length );
- ByteBuffer b = ByteBuffer.wrap( byteArray );
- log.info( "Length of the byteBuffer after encryption :"
- + b.remaining() );
sSD = theClient.serverAuthenticate(
Globals.getOrganizationName(),
ByteBuffer.wrap( byteArray ) );
} catch ( AuthenticationException e ) {
- log.error( "ThriftConnection: AuthenticationException: Server Authetication was not sucessful." );
- e.printStackTrace();
+ log.error( "ThriftConnection: AuthenticationException: Server Authetication was not sucessful.", e );
return null;
} catch ( TException e ) {
- log.error( "ThriftConnection: TException: Server Authetication was not sucessful." );
- e.printStackTrace();
+ log.error( "ThriftConnection: TException: Server Authetication was not sucessful.", e );
return null;
}
log.info( "is Authenticated." );
}
- client = theClient;
return theClient;
}
@@ -265,7 +237,7 @@ public class ThriftConnection
*/
private static ImageServer.Client newClient()
{
- ImageServer.Client newClient = null;
+ final ImageServer.Client newClient;
try {
TTransport transport = new TFramedTransport( new TSocket(
Globals.getMasterserverHost(), Globals.getThriftPort() ) );
@@ -274,11 +246,10 @@ public class ThriftConnection
newClient = new ImageServer.Client( protocol );
log.debug( "ThriftConnection: Made a new Client" );
} catch ( TTransportException e ) {
- log.error(
- "Transport could not be opened. Couldn't create new client.",
- e );
+ log.error( "Transport could not be opened. Couldn't create new client.", e );
return null;
}
+ client.set( newClient );
return newClient;
}
}