ntester just prints out diff

This commit is contained in:
Keith Winstein
2011-08-10 04:32:25 -04:00
parent e9aeb482df
commit d7433c07d3
3 changed files with 38 additions and 10 deletions
+30 -3
View File
@@ -13,7 +13,8 @@ Transport<MyState, RemoteState>::Transport( MyState &initial_state, RemoteState
current_state( initial_state ),
sent_states( 1, TimestampedState<MyState>( timestamp(), 0, initial_state ) ),
assumed_receiver_state( sent_states.begin() ),
received_states( 1, TimestampedState<RemoteState>( timestamp(), 0, initial_remote ) )
received_states( 1, TimestampedState<RemoteState>( timestamp(), 0, initial_remote ) ),
last_receiver_state( initial_remote )
{
/* server */
}
@@ -26,7 +27,8 @@ Transport<MyState, RemoteState>::Transport( MyState &initial_state, RemoteState
current_state( initial_state ),
sent_states( 1, TimestampedState<MyState>( timestamp(), 0, initial_state ) ),
assumed_receiver_state( sent_states.begin() ),
received_states( 1, TimestampedState<RemoteState>( timestamp(), 0, initial_remote ) )
received_states( 1, TimestampedState<RemoteState>( timestamp(), 0, initial_remote ) ),
last_receiver_state( initial_remote )
{
/* client */
}
@@ -144,7 +146,6 @@ void Transport<MyState, RemoteState>::send_to_receiver( void )
}
/* send instruction */
/* XXX what about MTU problem? */
string s = inst.tostring();
try {
@@ -225,6 +226,10 @@ void Transport<MyState, RemoteState>::recv( void )
if ( !found ) {
// fprintf( stderr, "Ignoring out-of-order packet. Reference state %d has been discarded or hasn't yet been received.\n", int(inst.old_num) );
/* There may be some benefit to storing these diffs until they can be used later,
but my guess is that the benefit is slim -- the diffs are likely to be small enough
that the entire diff will usually fit in one datagram, and by the time of retransmission
the target state will be different anyway. */
return;
}
@@ -282,3 +287,25 @@ void Transport<MyState, RemoteState>::process_throwaway_until( uint64_t throwawa
assert( received_states.size() > 0 );
}
template <class MyState, class RemoteState>
string Transport<MyState, RemoteState>::get_remote_diff( void )
{
/* find diff between last receiver state and current remote state, then rationalize states */
string ret( received_states.back().state.diff_from( last_receiver_state, 1 ) );
last_receiver_state = received_states.back().state;
MyState * const oldest_receiver_state = &received_states.front().state;
last_receiver_state.subtract( oldest_receiver_state );
for ( typename list< TimestampedState<RemoteState> >::reverse_iterator i = received_states.rbegin();
i != received_states.rend();
i++ ) {
i->state.subtract( oldest_receiver_state );
}
return ret;
}
+7 -1
View File
@@ -76,21 +76,27 @@ namespace Network {
/* simple receiver */
list< TimestampedState<RemoteState> > received_states;
MyState last_receiver_state; /* the state we were in when user last queried state */
public:
Transport( MyState &initial_state, RemoteState &initial_remote );
Transport( MyState &initial_state, RemoteState &initial_remote,
const char *key_str, const char *ip, int port );
/* Send data or an ack if necessary.
Returns the number of ms to wait until next event. */
int tick( void );
/* Blocks waiting for a packet. */
void recv( void );
int port( void ) { return connection.port(); }
string get_key( void ) { return connection.get_key(); }
MyState &get_current_state( void ) { return current_state; }
RemoteState &get_remote_state( void ) { return received_states.back().state; }
string get_remote_diff( void );
uint64_t get_remote_state_num( void ) { return received_states.back().num; }
int fd( void ) { return connection.fd(); }
+1 -6
View File
@@ -52,12 +52,7 @@ int main( int argc, char *argv[] )
n->recv();
if ( n->get_remote_state_num() != last_num ) {
fprintf( stderr, "Num: %d. Contents: ",
(int)n->get_remote_state_num() );
for ( size_t i = 0; i < n->get_remote_state().user_bytes.size(); i++ ) {
fprintf( stderr, "%c", n->get_remote_state().user_bytes[ i ] );
}
fprintf( stderr, "\n" );
fprintf( stderr, "%s\n", n->get_remote_diff().c_str() );
last_num = n->get_remote_state_num();
}
}