00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "UnicastRequestHandler.h"
00019 #include "UnicastRequestEvent.h"
00020 #include "ServerState.h"
00021
00022 #include "ServerExceptions.h"
00023
00024 #include <sys/time.h>
00025
00026 using namespace CODEX_Server;
00027
00029 bool
00030 UnicastRequestHandler::handler( UnicastRequestEvent& event )
00031 {
00032
00033
00034 CODEX_Quorum::QuorumSystem* qs = ServerState::instance()->quorumSystem();
00035 if ( 0 == qs )
00036 {
00037 throw NoQuorumSystemException( __FILE__ , __LINE__ );
00038 }
00039 try
00040 {
00041 struct timeval tv;
00042 gettimeofday(&tv,0);
00043 if ( ( tv.tv_sec - event.time() ) < 3 )
00044 {
00045 return false;
00046 }
00047 event.updateTime( tv.tv_sec );
00048 if ( ! event.bcast() )
00049 {
00050 event.setBcast();
00051 bool bVal = qs->unicastMessage( event.messages(),
00052 event.retVals(),
00053 event.cb() );
00054 bool retVal = false;
00055 CODEX_Quorum::ReturnVector::const_iterator itr =
00056 event.retVals().begin();
00057 CODEX_Quorum::ReturnVector::const_iterator end =
00058 event.retVals().end();
00059 unsigned int nGood = 0;
00060 for ( ; itr != end ; ++itr )
00061 {
00062 if ( itr->returnCode() ==
00063 CODEX_Quorum::RemoteServerReturn::kSuccess )
00064 {
00065 ++nGood;
00066 }
00067 }
00068 if ( nGood >= qs->quorumSize() )
00069 {
00070 retVal = true;
00071 }
00072 if ( retVal )
00073 {
00074 sendEvent( 0, event.source() );
00075 }
00076 return retVal;
00077 }
00078 else
00079 {
00080
00081
00082 if ( ( 0 != event.cb() ) &&
00083 ( ! qs->keyRegistered(event.key()) ) )
00084 {
00085 sendEvent( 0, event.source() );
00086 return true;
00087 }
00088 bool bVal = qs->unicastMessage( event.messages(),
00089 event.retVals(),
00090 0 );
00091 bool retVal = false;
00092 CODEX_Quorum::ReturnVector::const_iterator itr =
00093 event.retVals().begin();
00094 CODEX_Quorum::ReturnVector::const_iterator end =
00095 event.retVals().end();
00096 unsigned int nGood = 0;
00097 for ( ; itr != end ; ++itr )
00098 {
00099 if ( itr->returnCode() ==
00100 CODEX_Quorum::RemoteServerReturn::kSuccess )
00101 {
00102 ++nGood;
00103 }
00104 }
00105 if ( nGood >= qs->quorumSize() )
00106 {
00107 retVal = true;
00108 }
00109 if ( retVal )
00110 {
00111 sendEvent( 0, event.source() );
00112 }
00113 return retVal;
00114 }
00115 }
00116 catch ( ... )
00117 {
00118 return false;
00119 }
00120 }