Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ClientActivity.cc

00001 /*
00002  * Copyright 2003 Michael A. Marsh, Cornell University. All rights reserved.
00003  * This software is released under the modified BSD license.
00004  * See the file LICENSE in the top-level directory for details.
00005  */
00006 //
00007 // $Id: ClientActivity.cc,v 1.4 2004/05/19 15:56:50 mmarsh Exp $
00008 //
00009 // $Log: ClientActivity.cc,v $
00010 // Revision 1.4  2004/05/19 15:56:50  mmarsh
00011 // *** empty log message ***
00012 //
00013 // Revision 1.3  2003/11/04 22:15:00  mmarsh
00014 // General code cleanup and reorganization.  The signed ElGamal public key
00015 // was moved to CODEX_Server, decoupling that package from CODEX_Client.
00016 // Since CODEX_Server no longer knows about the cryptosystem used by
00017 // the client, switching between cryptosystems is handled locally by
00018 // CODEX_KeyService.
00019 //
00020 //
00021 
00022 #include "ClientActivity.h"
00023 #include "ClientMessageEvent.h"
00024 #include "ClientMessageHandler.h"
00025 #include "StateInfo.h"
00026 #include "CODEX_Quorum/Socket.h"
00027 #include "CODEX_Quorum/Message.h"
00028 
00029 #include "CODEX_Quorum/QSExceptions.h"
00030 #include "CODEX_Ciphers/CipherExceptions.h"
00031 
00032 using namespace CODEX_KeyService;
00033 using CODEX_Quorum::SocketBuilder;
00034 
00035 ClientActivity::ClientActivity( int port,
00036                                 const SocketBuilder& socketBuilder,
00037                                 DeadPileType& deadPile,
00038                                 QType& eventQueue,
00039                                 ClientMessageHandler* destination,
00040                                 CODEX_Quorum::SocketBase* socket ) :
00041    Activity( deadPile, eventQueue ),
00042    LocalServer( port, socketBuilder ),
00043 #ifdef TIMING
00044    m_timer( 0 ),
00045 #endif
00046    m_destination( destination ),
00047    m_bufferToRead( 0 ),
00048    m_buffer( 0 )
00049 {
00050    setSocket( socket );
00051    StateInfo::instance()->addClient( this );
00052    CODEX_Server::ServerState::instance()->addServer( this );
00053 }
00054 
00055 ClientActivity::~ClientActivity()
00056 {
00057    StateInfo::instance()->removeClient( this );
00058    CODEX_Server::ServerState::instance()->removeServer( this );
00059    if ( 0 != m_buffer ) delete m_buffer;
00060 #ifdef TIMING
00061    if ( 0 != m_timer ) delete m_timer;
00062    ActiveTimer.clear();
00063    OneWayTimer.clear();
00064    SSLTimer.clear();
00065    SignTimer.clear();
00066    SignVerifyTimer.clear();
00067    PartialSigTimer.clear();
00068    PartialDecTimer.clear();
00069    PartialRSADecTimer.clear();
00070    ThresholdSigTimer.clear();
00071    ThresholdDecTimer.clear();
00072    LookupTimer.clear();
00073    SchnorrTimer.clear();
00074    DLProofTimer.clear();
00075    DLProofVerifyTimer.clear();
00076    RSAPPKTimer.clear();
00077 #endif
00078 }
00079 
00080 void
00081 ClientActivity::processRequest( CODEX_Quorum::SocketBase::StateType s )
00082 {
00083    try
00084    {
00085       switch( s )
00086       {
00087          case CODEX_Quorum::SocketBase::kRead :
00088             if ( 0 == m_buffer )
00089             {
00090                m_bufferToRead = 0;
00091                m_buffer = new CODEX_Quorum::Message;
00092             }
00093             m_bufferToRead = socket()->readAll( *m_buffer, m_bufferToRead );
00094             if ( 0 == m_bufferToRead )
00095             {
00096                // Determine the message type, unmarshal it, and dispatch it
00097                // to the appropriate handler.
00098                const unsigned char* buff = m_buffer->buffer();
00099                long buffLength = m_buffer->length();
00100                unsigned char* pBuff = (unsigned char*) (buff+1);
00101                ClientMessageEventBase* evt = 0;
00102                switch( buff[0] )
00103                {
00104                   using namespace CODEX_Client;
00105                   case kRequestKeyMsg :
00106                      evt = new ClientMessageEvent< RequestKeyMsg >(
00107                         this, m_destination );
00108                      break;
00109                   case (CODEX_Client::kCreateKeyMsg |
00110                         CODEX_Client::SignatureMask) :
00111                      evt = new ClientMessageEvent< SignedCreateKeyMsg >(
00112                         this, m_destination );
00113 #ifdef TIMING
00114                      m_timer = new Timer("codex_create");
00115                      m_timer->start();
00116 #endif
00117                      break;
00118                   case (CODEX_Client::kWriteKeyMsg |
00119                         CODEX_Client::SignatureMask) :
00120                      evt = new ClientMessageEvent< SignedWriteKeyMsg >(
00121                         this, m_destination );
00122 #ifdef TIMING
00123                      m_timer = new Timer("codex_write");
00124                      m_timer->start();
00125 #endif
00126                      break;
00127                   case (CODEX_Client::kReadKeyMsg |
00128                         CODEX_Client::SignatureMask) :
00129                      evt = new ClientMessageEvent< SignedReadKeyMsg >(
00130                         this, m_destination );
00131 #ifdef TIMING
00132                      m_timer = new Timer("codex_read");
00133                      m_timer->start();
00134 #endif
00135                      break;
00136                   default :
00137                      // Bad message type for a client request -- drop it.
00138                      cerr << "Bad message type: "
00139                           << (unsigned int)buff[0]
00140                           << endl;
00141                      break;
00142                }
00143                if ( 0 != evt )
00144                {
00145                   if ( evt->unmarshal( &pBuff, buffLength ) )
00146                   {
00147                      // Enqueue the event.
00148                      sendEvent( evt, 0 );
00149                   }
00150                   else
00151                   {
00152                      // Bad message format -- drop it.
00153                      delete evt;
00154                   }
00155                }
00156 
00157                // Finally, clean up the buffer.
00158                delete m_buffer;
00159                m_buffer = 0;
00160             }
00161             break;
00162          case CODEX_Quorum::SocketBase::kWrite :
00163             break;
00164       }
00165    }
00166    catch ( ... )
00167    {
00168       // For any exception our action is the same -- close the Activity
00169       sendEvent( new CODEX_Events::CloseEvent(this,m_destination), 0 );
00170       enableTerminate();
00171       // re-throw the exception so that we can detect the error
00172       throw;
00173    }
00174 }
00175 
00176 bool
00177 ClientActivity::handler( CODEX_Events::EventAck& event )
00178 {
00179    if ( event.failure() )
00180    {
00181       sendEvent( new CODEX_Events::CloseEvent(this,m_destination), 0 );
00182       enableTerminate();
00183    }
00184    return this->CODEX_Events::Activity::handler( event );
00185 }
00186 
00187 bool
00188 ClientActivity::handler( CODEX_Events::CloseEvent& event )
00189 {
00190    sendEvent( new CODEX_Events::CloseEvent(this,m_destination),
00191               event.source() );
00192    enableTerminate();
00193    return true;
00194 }
00195 
00196 bool
00197 ClientActivity::handler( ClientResponseEvent< SignedPublicKeyMsg >& event )
00198 {
00199    try
00200    {
00201       respond( event.message(),
00202                ( CODEX_Client::kPublicKeyMsg | CODEX_Client::SignatureMask ) );
00203       if ( event.acknowledge() )
00204       {
00205          sendEvent( 0, event.source() );
00206       }
00207       return true;
00208    }
00209    catch ( ... )
00210    {
00211       // For any exception our action is the same -- close the Activity
00212       sendEvent( new CODEX_Events::CloseEvent(this,m_destination), 0 );
00213       enableTerminate();
00214       // re-throw the exception so that we can detect the error
00215       throw;
00216    }
00217 }
00218 
00219 bool
00220 ClientActivity::handler( ClientResponseEvent< SignedBoundNameMsg >& event )
00221 {
00222    try
00223    {
00224       respond( event.message(),
00225                ( CODEX_Client::kBoundNameMsg | CODEX_Client::SignatureMask ) );
00226       if ( event.acknowledge() )
00227       {
00228          sendEvent( 0, event.source() );
00229       }
00230       return true;
00231    }
00232    catch ( ... )
00233    {
00234       // For any exception our action is the same -- close the Activity
00235       sendEvent( new CODEX_Events::CloseEvent(this,m_destination), 0 );
00236       enableTerminate();
00237       // re-throw the exception so that we can detect the error
00238       throw;
00239    }
00240 }
00241 
00242 bool
00243 ClientActivity::handler( ClientResponseEvent< SignedKeyStoredMsg >& event )
00244 {
00245    try
00246    {
00247       respond( event.message(),
00248                ( CODEX_Client::kKeyStoredMsg | CODEX_Client::SignatureMask ) );
00249       if ( event.acknowledge() )
00250       {
00251          sendEvent( 0, event.source() );
00252       }
00253       return true;
00254    }
00255    catch ( ... )
00256    {
00257       // For any exception our action is the same -- close the Activity
00258       sendEvent( new CODEX_Events::CloseEvent(this,m_destination), 0 );
00259       enableTerminate();
00260       // re-throw the exception so that we can detect the error
00261       throw;
00262    }
00263 }
00264 
00265 bool
00266 ClientActivity::handler( ClientResponseEvent< SignedBlindKeyMsg >& event )
00267 {
00268    try
00269    {
00270       respond( event.message(),
00271                ( CODEX_Client::kBlindKeyMsg | CODEX_Client::SignatureMask ) );
00272       if ( event.acknowledge() )
00273       {
00274          sendEvent( 0, event.source() );
00275       }
00276       return true;
00277    }
00278    catch ( ... )
00279    {
00280       // For any exception our action is the same -- close the Activity
00281       sendEvent( new CODEX_Events::CloseEvent(this,m_destination), 0 );
00282       enableTerminate();
00283       // re-throw the exception so that we can detect the error
00284       throw;
00285    }
00286    return true;
00287 }
00288 
00289 void
00290 ClientActivity::close()
00291 {
00292    sendEvent( new CODEX_Events::CloseEvent( this, this ), 0 );
00293 }
00294 
00295 void
00296 ClientActivity::respond( const CODEX_ASN1::Base& message, unsigned char mtype )
00297 {
00298 #ifdef TIMING
00299    ActiveTimer.start();
00300 #endif
00301    int length = message.marshal(0) + 1;
00302    unsigned char* buff = new unsigned char[length];
00303    buff[0] = mtype;
00304    unsigned char* pBuff = buff+1;
00305    message.marshal(&pBuff);
00306    socket()->writeTo( CODEX_Quorum::Message( buff, length ) );
00307    delete [] buff;
00308 #ifdef TIMING
00309    ActiveTimer.stop();
00310    if ( 0 != m_timer )
00311    {
00312       m_timer->stop();
00313       m_timer->print(cout);
00314       ActiveTimer.print(cout);
00315       OneWayTimer.print(cout);
00316       SSLTimer.print(cout);
00317       SignTimer.print(cout);
00318       SignVerifyTimer.print(cout);
00319       PartialSigTimer.print(cout);
00320       PartialDecTimer.print(cout);
00321       PartialRSADecTimer.print(cout);
00322       ThresholdSigTimer.print(cout);
00323       ThresholdDecTimer.print(cout);
00324       LookupTimer.print(cout);
00325       SchnorrTimer.print(cout);
00326       DLProofTimer.print(cout);
00327       DLProofVerifyTimer.print(cout);
00328       RSAPPKTimer.print(cout);
00329    }
00330 
00331    if ( 0 != m_timer )
00332    {
00333       delete m_timer;
00334       m_timer = 0;
00335    }
00336    ActiveTimer.clear();
00337    OneWayTimer.clear();
00338    SSLTimer.clear();
00339    SignTimer.clear();
00340    SignVerifyTimer.clear();
00341    PartialSigTimer.clear();
00342    PartialDecTimer.clear();
00343    PartialRSADecTimer.clear();
00344    ThresholdSigTimer.clear();
00345    ThresholdDecTimer.clear();
00346    LookupTimer.clear();
00347    SchnorrTimer.clear();
00348    DLProofTimer.clear();
00349    DLProofVerifyTimer.clear();
00350    RSAPPKTimer.clear();
00351 #endif
00352 }

Generated on Fri May 6 17:38:34 2005 for COrnell Data EXchange (CODEX) by  doxygen 1.4.1