![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
Diff: call_box_manager.cpp
- Revision:
- 78:1353744f01e1
- Parent:
- 74:81c47fff88a5
- Child:
- 80:61d61c9eb75c
--- a/call_box_manager.cpp Tue Nov 25 18:43:01 2014 +0000 +++ b/call_box_manager.cpp Tue Dec 16 12:13:01 2014 +0000 @@ -1,5 +1,9 @@ #include "call_box_manager.h" +int max_ext = 0; +int min_ext = 0; +Timer timer_aging; + Call_Box * __find_CB__( Vector * v_cb, int ext ){ Call_Box * cb = NULL; for( register int i = 0; i < v_cb->size(); i++ ){ @@ -9,24 +13,123 @@ return( NULL ); } +int init_aging( void ){ + timer_aging.start(); + return( 0 ); +} void registry_aging( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ - Call_Box * cb = NULL; - if( v_cb && data && write_buffer ){ - for( register int i = 0; i < v_cb->size(); i++ ){ - cb = (Call_Box * )v_cb->get_element( i ); - if( cb->is_timeout() ){ - if( cb->reconfigure_timeout() == 0x00 ){ - if( debug_alive ) debug_msg("( ext %d removed )", cb->get_ext() ); - //cb->unregistry(); - v_cb->remove_element( i ); - delete( cb ); - break; + if( v_cb != NULL && data != NULL && write_buffer != NULL ){ + uint8_t enough = 0; + + static int lst_ext = 0; + + if( timer_aging.read_ms() > 200 ){ + timer_aging.reset(); + for( register int i = 0; i < v_cb->size(); i++ ){ + Call_Box * cb = (Call_Box * )v_cb->get_element( i ); + if( cb->is_timeout() ){ + if( cb->has_next_try() == false ){ + if( debug_aging ) debug_msg( "time to really go " ); + if( debug_aging ) debug_msg("( ext %d removed )", cb->get_ext() ); + v_cb->remove_element( i ); + delete( cb ); + break; + }else{ + /* + if( enough++ == 2 ){ + if( debug_aging ) debug_msg( "Not enough in this cycle" ); + return; + } + */ + + if( lst_ext == 0 ) lst_ext = cb->get_ext(); + + if( lst_ext == max_ext ) lst_ext = 0; + + if( cb->get_ext() < lst_ext ) continue; + + cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); + + int8_t type = cb->get_next_aging_type(); + + uint8_t msg_type = 0x00; + + if( type == REQUEST_REGISTRY ){ + msg_type = __REGISTRY__; + if( debug_aging ) debug_msg("Registry request Cbx %d -- On msg-id %d", cb->get_ext(), cb->get_msg_id() ); + }else if( type == REQUEST_PEND ){ + msg_type = __PROMPT__; + strcpy( ( char * )data, "pend\r\r\r\n" ); + if( debug_aging ) debug_msg("Pend Request Cbx %d -- On msg-id %d", cb->get_ext(), cb->get_msg_id() ); + }else if( ( type == REQUEST_PING ) ){ + msg_type = __PROMPT__; + strcpy( ( char * )data, "ping\r\r\r\n" ); + if( debug_aging ) debug_msg("Ping on Cbx %d -- On msg-id %d", cb->get_ext(), cb->get_msg_id() ); + } + + send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), msg_type, + ( char * )data, 0x19, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); + + return; + } + } + } + if( debug_aging ) if( enough > 1 ) debug_msg( " out( %d ) ", enough ); + } + } +} + +void wake_all_up( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ + if( v_cb != NULL && data != NULL && write_buffer != NULL ){ + int ext_list[ __MAX_CB_IN_A_BRANCH__ ]; + uint8_t size = v_cb->size(); + + bool once = true; + + if( size > 0 ){ + for( register int i = 0; i < size; i++ ){ + ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); + } + + qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp ); + + for( register int i = 0; i < size; i++ ){ + // testa se eh o master + if( ( ext_list[ i ] % 2 ) == 0 ){ + //testa se o slave nao esta registrado + if( ( ( i + 1 ) < size ) && ( ext_list[ i + 1 ] != ( ext_list[ i ] + 1 ) ) || ( size == 1 ) ){ + if( debug_wake ) debug_msg("Mestre %d encontrado sem slave %d", ext_list[ i ], ext_list[ i ] + 1 ); + if( once ){ + strcpy( ( char * )data, "ping\r\r\r\n" ); + Call_Box * cb = __find_CB__( v_cb, ext_list[ i ] ); + if( cb != NULL ){ + cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); + + send2callboxes( __build_cb_package__( ( cb->get_ext() + 1 ), ( cb->get_port() + 1 ), __PROMPT__, + ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); + + if( debug_wake ) debug_msg("Slave %d pingado", cb->get_ext() + 1 ); + } + once = false; + } + } }else{ - if( debug_alive ) debug_msg("Ping Cbx %d", cb->get_ext() ); - cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); - send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, - ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); - cb->registry(); + if( ( ( i - 1 ) > 0 ) && ( ext_list[ i - 1 ] != ( ext_list[ i ] - 1 ) ) || ( size == 1 ) ){ + if( debug_wake ) debug_msg("Escravo revolucionario %d encontrado sem master %d", ext_list[ i ], ext_list[ i ] - 1 ); + if( once ){ + strcpy( ( char * )data, "ping\r\r\r\n" ); + Call_Box * cb = __find_CB__( v_cb, ( ext_list[ i ] ) ); + if( cb != NULL ){ + cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) ); + + send2callboxes( __build_cb_package__( ( cb->get_ext() - 1 ), ( cb->get_port() - 1 ), __PROMPT__, + ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); + + if( debug_wake ) debug_msg("Master %d pingado", cb->get_ext() - 1 ); + } + once = false; + } + } } } }