while( retry && !failed ){
retry = false;
ClientMessageService message_service = null;
boolean got_reply = false;
try{
Map content = new HashMap();
long sequence = adapter.getMessageSequence();
content.put( "user", user );
content.put( "password", password );
content.put( "seq", new Long( sequence ));
content.put( "request", message.getRequest());
last_failed_user_pw = "";
message_service = SecureMessageServiceClientHelper.getServerService( host, port, timeout_secs, SERVICE_NAME, public_key );
message_service.sendMessage( content );
Map reply = message_service.receiveMessage();
got_reply = true;
long status = ((Long)reply.get( "status" )).longValue();
Long new_retry = (Long)reply.get( "retry" );
if ( new_retry != null ){
retry_millis = new_retry.longValue();
if ( retry_millis < MIN_RETRY_PERIOD ){
retry_millis = MIN_RETRY_PERIOD;
}
adapter.log( "Server requested retry period of " + (retry_millis/1000) + " seconds" );
}else{
retry_millis = MIN_RETRY_PERIOD;
}
if ( status == STATUS_OK ){
message.setReply( (Map)reply.get( "reply" ));
adapter.log( "Request successfully sent: " + message.getRequest() + "->" + message.getReply());
adapter.setMessageSequence( sequence + 1 );
adapter.serverOK();
for (Iterator l_it=listeners.iterator();l_it.hasNext();){
try{
((SecureMessageServiceClientListener)l_it.next()).complete( message );
}catch( Throwable e ){
e.printStackTrace();
}
}
complete_messages.add( message );
}else if ( status == STATUS_LOGON_FAIL ){
last_failed_user_pw = user_password;
last_failed_user_pw_time = SystemTime.getCurrentTime();
adapter.serverOK();
adapter.authenticationFailed();
failed = true;
}else if ( status == STATUS_INVALID_SEQUENCE ){
if ( retry_count == 1 ){
adapter.serverFailed( new Exception( "Sequence resynchronisation failed" ));
failed = true;
}else{
retry_count++;
retry = true;
long expected_sequence = ((Long)reply.get( "seq" )).longValue();
adapter.log( "Sequence resynchronise: local = " + sequence + ", remote = " + expected_sequence );
adapter.setMessageSequence( expected_sequence );
}
}else if ( status == STATUS_FAILED ){
adapter.serverFailed( new Exception( new String( (byte[])reply.get( "error" ))));
failed = true;
}else{// if ( status == STATUS_ABORT ){
// this is when things have gone badly wrong server-side - we just
// dump the message
adapter.serverFailed( new Exception( "Server requested abort" ));
for (Iterator l_it=listeners.iterator();l_it.hasNext();){
try{
((SecureMessageServiceClientListener)l_it.next()).aborted(
message,
new String( (byte[])reply.get( "error" )));
}catch( Throwable e ){
e.printStackTrace();
}
}
complete_messages.add( message );
}
}catch( Throwable e ){
adapter.serverFailed( e );
failed = true;
}finally{
if ( got_reply ){
connect_failure_count = 0;
}else{
connect_failure_count++;
if ( connect_failure_count > 1 ){
try{
adapter.log( "Failed to contact server " + connect_failure_count + " times in a row" );
}catch( Throwable e ){
e.printStackTrace();
}
}
}
if ( message_service != null ){
message_service.close();
}
}
}
}
}catch( Throwable e ){