Changeset 729

Show
Ignore:
Timestamp:
06/30/08 18:45:43 (2 years ago)
Author:
raggi
Message:

Added enhanced error reporting refactor. Influenced by patch from Roger Pack.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/raggi/ext/cmain.cpp

    r679 r729  
    33$Id$ 
    44 
    5 File:     cmain.cpp 
    6 Date:     06Apr06 
     5File:                  cmain.cpp 
     6Date:                  06Apr06 
    77 
    88Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved. 
     
    2525static int bUseKqueue = 0; 
    2626 
     27extern "C" void ensure_eventmachine (const char *caller = "unknown caller") 
     28{ 
     29        if (!EventMachine) { 
     30                int err_size = 128; 
     31                char err_string[err_size]; 
     32                snprintf (err_string, err_size, "eventmachine not initialized: %s", caller); 
     33                throw std::runtime_error (err_string); 
     34        } 
     35} 
    2736 
    2837/*********************** 
     
    3645        //InstallSignalHandlers(); 
    3746        if (EventMachine) 
    38                 throw std::runtime_error ("already initialized"); 
     47                throw std::runtime_error ("eventmachine already initialized: evma_initialize_library"); 
    3948        EventMachine = new EventMachine_t (cb); 
    4049        if (bUseEpoll) 
     
    5160extern "C" void evma_release_library() 
    5261{ 
    53         if (!EventMachine) 
    54                 throw std::runtime_error ("not initialized"); 
     62        ensure_eventmachine("evma_release_library"); 
    5563        delete EventMachine; 
    5664        EventMachine = NULL; 
     
    6472extern "C" void evma_run_machine() 
    6573{ 
    66         if (!EventMachine) 
    67                 throw std::runtime_error ("not initialized"); 
     74        ensure_eventmachine("evma_run_machine"); 
    6875        EventMachine->Run(); 
    6976} 
     
    7683extern "C" const char *evma_install_oneshot_timer (int seconds) 
    7784{ 
    78         if (!EventMachine) 
    79                 throw std::runtime_error ("not initialized"); 
     85        ensure_eventmachine("evma_install_oneshot_timer"); 
    8086        return EventMachine->InstallOneshotTimer (seconds); 
    8187} 
     
    8894extern "C" const char *evma_connect_to_server (const char *server, int port) 
    8995{ 
    90         if (!EventMachine) 
    91                 throw std::runtime_error ("not initialized"); 
     96        ensure_eventmachine("evma_connect_to_server"); 
    9297        return EventMachine->ConnectToServer (server, port); 
    9398} 
     
    99104extern "C" const char *evma_connect_to_unix_server (const char *server) 
    100105{ 
    101         if (!EventMachine) 
    102                 throw std::runtime_error ("not initialized"); 
     106        ensure_eventmachine("evma_connect_to_unix_server"); 
    103107        return EventMachine->ConnectToUnixServer (server); 
    104108} 
     
    111115extern "C" const char *evma_create_tcp_server (const char *address, int port) 
    112116{ 
    113         if (!EventMachine) 
    114                 throw std::runtime_error ("not initialized"); 
     117        ensure_eventmachine("evma_create_tcp_server"); 
    115118        return EventMachine->CreateTcpServer (address, port); 
    116119} 
     
    122125extern "C" const char *evma_create_unix_domain_server (const char *filename) 
    123126{ 
    124         if (!EventMachine) 
    125                 throw std::runtime_error ("not initialized"); 
     127        ensure_eventmachine("evma_create_unix_domain_server"); 
    126128        return EventMachine->CreateUnixDomainServer (filename); 
    127129} 
     
    133135extern "C" const char *evma_open_datagram_socket (const char *address, int port) 
    134136{ 
    135         if (!EventMachine) 
    136                 throw std::runtime_error ("not initialized"); 
     137        ensure_eventmachine("evma_open_datagram_socket"); 
    137138        return EventMachine->OpenDatagramSocket (address, port); 
    138139} 
     
    144145extern "C" const char *evma_open_keyboard() 
    145146{ 
    146         if (!EventMachine) 
    147                 throw std::runtime_error ("not initialized"); 
     147        ensure_eventmachine("evma_open_keyboard"); 
    148148        return EventMachine->OpenKeyboard(); 
    149149} 
     
    157157extern "C" int evma_send_data_to_connection (const char *binding, const char *data, int data_length) 
    158158{ 
    159         if (!EventMachine) 
    160                 throw std::runtime_error ("not initialized"); 
     159        ensure_eventmachine("evma_send_data_to_connection"); 
    161160        return ConnectionDescriptor::SendDataToConnection (binding, data, data_length); 
    162161} 
     
    168167extern "C" int evma_send_datagram (const char *binding, const char *data, int data_length, const char *address, int port) 
    169168{ 
    170   if (!EventMachine) 
    171                 throw std::runtime_error ("not initialized"); 
     169        ensure_eventmachine("evma_send_datagram"); 
    172170        return DatagramDescriptor::SendDatagram (binding, data, data_length, address, port); 
    173171} 
     
    180178extern "C" void evma_close_connection (const char *binding, int after_writing) 
    181179{ 
    182         if (!EventMachine) 
    183                 throw std::runtime_error ("not initialized"); 
     180        ensure_eventmachine("evma_close_connection"); 
    184181        ConnectionDescriptor::CloseConnection (binding, (after_writing ? true : false)); 
    185182} 
     
    191188extern "C" int evma_report_connection_error_status (const char *binding) 
    192189{ 
    193         if (!EventMachine) 
    194                 throw std::runtime_error ("not initialized"); 
     190        ensure_eventmachine("evma_report_connection_error_status"); 
    195191        return ConnectionDescriptor::ReportErrorStatus (binding); 
    196192} 
     
    202198extern "C" void evma_stop_tcp_server (const char *binding) 
    203199{ 
    204         if (!EventMachine) 
    205                 throw std::runtime_error ("not initialized"); 
     200        ensure_eventmachine("evma_stop_tcp_server"); 
    206201        AcceptorDescriptor::StopAcceptor (binding); 
    207202} 
     
    214209extern "C" void evma_stop_machine() 
    215210{ 
    216         if (!EventMachine) 
    217                 throw std::runtime_error ("not initialized"); 
     211        ensure_eventmachine("evma_stop_machine"); 
    218212        EventMachine->ScheduleHalt(); 
    219213} 
     
    226220extern "C" void evma_start_tls (const char *binding) 
    227221{ 
    228         if (!EventMachine) 
    229                 throw std::runtime_error ("not initialized"); 
     222        ensure_eventmachine("evma_start_tls"); 
    230223        EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding)); 
    231224        if (ed) 
     
    239232extern "C" void evma_set_tls_parms (const char *binding, const char *privatekey_filename, const char *certchain_filename) 
    240233{ 
    241         if (!EventMachine) 
    242                 throw std::runtime_error ("not initialized"); 
     234        ensure_eventmachine("evma_set_tls_parms"); 
    243235        EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding)); 
    244236        if (ed) 
     
    253245extern "C" int evma_get_peername (const char *binding, struct sockaddr *sa) 
    254246{ 
    255         if (!EventMachine) 
    256                 throw std::runtime_error ("not initialized"); 
     247        ensure_eventmachine("evma_get_peername"); 
    257248        EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding)); 
    258249        if (ed) { 
     
    269260extern "C" int evma_get_sockname (const char *binding, struct sockaddr *sa) 
    270261{ 
    271         if (!EventMachine) 
    272                 throw std::runtime_error ("not initialized"); 
     262        ensure_eventmachine("evma_get_sockname"); 
    273263        EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding)); 
    274264        if (ed) { 
     
    285275extern "C" int evma_get_subprocess_pid (const char *binding, pid_t *pid) 
    286276{ 
    287         if (!EventMachine) 
    288                 throw std::runtime_error ("not initialized"); 
     277        ensure_eventmachine("evma_get_subprocess_pid"); 
    289278        EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding)); 
    290279        if (ed) { 
     
    301290extern "C" int evma_get_subprocess_status (const char *binding, int *status) 
    302291{ 
    303         if (!EventMachine) 
    304                 throw std::runtime_error ("not initialized"); 
     292        ensure_eventmachine("evma_get_subprocess_status"); 
    305293        if (status) { 
    306294                *status = EventMachine->SubprocessExitStatus; 
     
    318306extern "C" void evma_signal_loopbreak() 
    319307{ 
    320         if (!EventMachine) 
    321                 throw std::runtime_error ("not initialized"); 
     308        ensure_eventmachine("evma_signal_loopbreak"); 
    322309        EventMachine->SignalLoopBreaker(); 
    323310} 
     
    331318extern "C" const char *evma__write_file (const char *filename) 
    332319{ 
    333         if (!EventMachine) 
    334                 throw std::runtime_error ("not initialized"); 
     320        ensure_eventmachine("evma__write_file"); 
    335321        return EventMachine->_OpenFileForWriting (filename); 
    336322} 
     
    343329extern "C" int evma_get_comm_inactivity_timeout (const char *binding, int *value) 
    344330{ 
    345         if (!EventMachine) 
    346                 throw std::runtime_error ("not initialized"); 
     331        ensure_eventmachine("evma_get_comm_inactivity_timeout"); 
    347332        EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding)); 
    348333        if (ed) { 
     
    359344extern "C" int evma_set_comm_inactivity_timeout (const char *binding, int *value) 
    360345{ 
    361         if (!EventMachine) 
    362                 throw std::runtime_error ("not initialized"); 
     346        ensure_eventmachine("evma_set_comm_inactivity_timeout"); 
    363347        EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding)); 
    364348        if (ed) { 
     
    376360extern "C" void evma_set_timer_quantum (int interval) 
    377361{ 
    378         if (!EventMachine) 
    379                 throw std::runtime_error ("not initialized"); 
     362        ensure_eventmachine("evma_set_timer_quantum"); 
    380363        EventMachine->SetTimerQuantum (interval); 
    381364} 
     
    389372        // This may only be called if the reactor is not running. 
    390373        if (EventMachine) 
    391                 throw std::runtime_error ("already initialized"); 
     374                throw std::runtime_error ("eventmachine already initialized: evma_set_max_timer_count"); 
    392375        EventMachine_t::SetMaxTimerCount (ct); 
    393376} 
     
    399382extern "C" void evma_setuid_string (const char *username) 
    400383{ 
    401     // We do NOT need to be running an EM instance because this method is static. 
    402     EventMachine_t::SetuidString (username); 
     384       // We do NOT need to be running an EM instance because this method is static. 
     385       EventMachine_t::SetuidString (username); 
    403386} 
    404387 
     
    410393extern "C" const char *evma_popen (char * const*cmd_strings) 
    411394{ 
    412         if (!EventMachine) 
    413                 throw std::runtime_error ("not initialized"); 
     395        ensure_eventmachine("evma_popen"); 
    414396        return EventMachine->Socketpair (cmd_strings); 
    415397} 
     
    422404extern "C" int evma_get_outbound_data_size (const char *binding) 
    423405{ 
    424         if (!EventMachine) 
    425                 throw std::runtime_error ("not initialized"); 
     406        ensure_eventmachine("evma_get_outbound_data_size"); 
    426407        EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding)); 
    427408        return ed ? ed->GetOutboundDataSize() : 0; 
     
    484465        int r; 
    485466 
    486         if (!EventMachine) 
    487                 throw std::runtime_error("not initialized"); 
     467        ensure_eventmachine("evma_send_file_data_to_connection"); 
    488468 
    489469        int Fd = open (filename, O_RDONLY); 
     
    523503} 
    524504 
    525  
    526  
    527