Switch to Google protobufs and make UserStream

This commit is contained in:
Keith Winstein
2011-08-13 01:25:31 -04:00
parent a81f398200
commit 7e034c27aa
11 changed files with 213 additions and 91 deletions
+13 -5
View File
@@ -1,11 +1,13 @@
source = parse.cpp parserstate.cpp parser.cpp templates.cpp terminal.cpp termemu.cpp parseraction.cpp terminalfunctions.cpp swrite.cpp terminalframebuffer.cpp terminaldispatcher.cpp terminaluserinput.cpp terminaldisplay.cpp network.cpp ntester.cpp ocb.cpp base64.cpp encrypt.cpp decrypt.cpp crypto.cpp networktransport.cpp networkinstruction.cpp keystroke.cpp proto = userinput.proto
objects = parserstate.o parser.o templates.o terminal.o parseraction.o terminalfunctions.o swrite.o terminalframebuffer.o terminaldispatcher.o terminaluserinput.o terminaldisplay.o network.o ocb.o base64.o crypto.o networktransport.o networkinstruction.o keystroke.o source = parse.cpp parserstate.cpp parser.cpp templates.cpp terminal.cpp termemu.cpp parseraction.cpp terminalfunctions.cpp swrite.cpp terminalframebuffer.cpp terminaldispatcher.cpp terminaluserinput.cpp terminaldisplay.cpp network.cpp ntester.cpp ocb.cpp base64.cpp encrypt.cpp decrypt.cpp crypto.cpp networktransport.cpp networkinstruction.cpp user.cpp userinput.pb.cc
objects = parserstate.o parser.o templates.o terminal.o parseraction.o terminalfunctions.o swrite.o terminalframebuffer.o terminaldispatcher.o terminaluserinput.o terminaldisplay.o network.o ocb.o base64.o crypto.o networktransport.o networkinstruction.o user.o userinput.pb.o
repos = templates.rpo repos = templates.rpo
executables = parse termemu ntester encrypt decrypt executables = parse termemu ntester encrypt decrypt
CXX = g++ CXX = g++
CXXFLAGS = -g -O2 --std=c++0x -pedantic -Werror -Wall -Wextra -Weffc++ -fno-implicit-templates -fno-default-inline -pipe -D_FILE_OFFSET_BITS=64 -D_XOPEN_SOURCE=500 -D_GNU_SOURCE -D_BSD_SOURCE CXXFLAGS = -g --std=c++0x -pedantic -Werror -Wall -Wextra -Weffc++ -fno-implicit-templates -fno-default-inline -pipe -D_FILE_OFFSET_BITS=64 -D_XOPEN_SOURCE=500 -D_GNU_SOURCE -D_BSD_SOURCE
LIBS = -lutil -lssl -lrt -lm LIBS = -lutil -lssl -lrt -lm -lprotobuf-lite
PROTOC = protoc
all: $(executables) all: $(executables)
@@ -30,6 +32,12 @@ templates.o: templates.cpp
%.o: %.cpp %.o: %.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $< $(CXX) $(CXXFLAGS) -c -o $@ $<
%.pb.cc: %.proto
$(PROTOC) --cpp_out=. $<
%.pb.o: %.pb.cc
$(CXX) $(CXXFLAGS) -frepo -Wno-effc++ -c -o $@ $<
-include depend -include depend
depend: $(source) depend: $(source)
@@ -37,4 +45,4 @@ depend: $(source)
.PHONY: clean .PHONY: clean
clean: clean:
-rm -f $(executables) depend *.o *.rpo -rm -f $(executables) depend *.o *.rpo *.pb.cc *.pb.h
-49
View File
@@ -1,49 +0,0 @@
#include <assert.h>
#include "keystroke.hpp"
void KeyStroke::subtract( KeyStroke * const prefix )
{
for ( deque<char>::iterator i = prefix->user_bytes.begin();
i != prefix->user_bytes.end();
i++ ) {
assert( !user_bytes.empty() );
assert( *i == user_bytes.front() );
user_bytes.pop_front();
}
}
string KeyStroke::diff_from( KeyStroke const & existing )
{
string ret;
deque<char>::iterator my_it = user_bytes.begin();
for ( deque<char>::const_iterator i = existing.user_bytes.begin();
i != existing.user_bytes.end();
i++ ) {
assert( my_it != user_bytes.end() );
if ( *i != *my_it ) {
fprintf( stderr, "BUG: existing state has %c and new state has %c\n",
*i, *my_it );
}
assert( *i == *my_it );
my_it++;
}
while ( my_it != user_bytes.end() ) {
ret += string( &( *my_it ), 1 );
my_it++;
}
return ret;
}
void KeyStroke::apply_string( string diff )
{
for ( string::iterator i = diff.begin();
i != diff.end();
i++ ) {
user_bytes.push_back( *i );
}
}
-26
View File
@@ -1,26 +0,0 @@
#ifndef KEYSTROKE_HPP
#define KEYSTROKE_HPP
#include <deque>
#include <string>
#include <assert.h>
using namespace std;
class KeyStroke
{
public:
deque<char> user_bytes;
KeyStroke() : user_bytes() {}
void key_hit( char x ) { user_bytes.push_back( x ); }
/* interface for Network::Transport */
void subtract( KeyStroke * const prefix );
string diff_from( KeyStroke const & existing );
void apply_string( string diff );
bool operator==( KeyStroke const &x ) const { return user_bytes == x.user_bytes; }
};
#endif
+2
View File
@@ -75,6 +75,8 @@ void Transport<MyState, RemoteState>::send_to_receiver( void )
return; return;
} }
/* XXX should increment number each time */
Instruction inst( assumed_receiver_state->num, Instruction inst( assumed_receiver_state->num,
assumed_receiver_state->num, assumed_receiver_state->num,
received_states.back().num, received_states.back().num,
+9 -7
View File
@@ -2,9 +2,11 @@
#include <unistd.h> #include <unistd.h>
#include <poll.h> #include <poll.h>
#include "keystroke.hpp" #include "user.hpp"
#include "networktransport.hpp" #include "networktransport.hpp"
using namespace Network;
int main( int argc, char *argv[] ) int main( int argc, char *argv[] )
{ {
bool server = true; bool server = true;
@@ -12,9 +14,9 @@ int main( int argc, char *argv[] )
char *ip; char *ip;
int port; int port;
KeyStroke me, remote; UserStream me, remote;
Network::Transport<KeyStroke, KeyStroke> *n; Transport<UserStream, UserStream> *n;
try { try {
if ( argc > 1 ) { if ( argc > 1 ) {
@@ -25,9 +27,9 @@ int main( int argc, char *argv[] )
ip = argv[ 2 ]; ip = argv[ 2 ];
port = atoi( argv[ 3 ] ); port = atoi( argv[ 3 ] );
n = new Network::Transport<KeyStroke, KeyStroke>( me, remote, key, ip, port ); n = new Transport<UserStream, UserStream>( me, remote, key, ip, port );
} else { } else {
n = new Network::Transport<KeyStroke, KeyStroke>( me, remote ); n = new Transport<UserStream, UserStream>( me, remote );
} }
} catch ( CryptoException e ) { } catch ( CryptoException e ) {
fprintf( stderr, "Fatal error: %s\n", e.text.c_str() ); fprintf( stderr, "Fatal error: %s\n", e.text.c_str() );
@@ -94,13 +96,13 @@ int main( int argc, char *argv[] )
if ( fds[ 0 ].revents & POLLIN ) { if ( fds[ 0 ].revents & POLLIN ) {
char x; char x;
assert( read( STDIN_FILENO, &x, 1 ) == 1 ); assert( read( STDIN_FILENO, &x, 1 ) == 1 );
n->get_current_state().key_hit( x ); n->get_current_state().push_back( Parser::UserByte( x ) );
} }
if ( fds[ 1 ].revents & POLLIN ) { if ( fds[ 1 ].revents & POLLIN ) {
n->recv(); n->recv();
} }
} catch ( Network::NetworkException e ) { } catch ( NetworkException e ) {
fprintf( stderr, "%s: %s\r\n", e.function.c_str(), strerror( e.the_errno ) ); fprintf( stderr, "%s: %s\r\n", e.function.c_str(), strerror( e.the_errno ) );
break; break;
} catch ( CryptoException e ) { } catch ( CryptoException e ) {
+7
View File
@@ -80,3 +80,10 @@ void Resize::act_on_terminal( Terminal::Emulator *emu )
emu->resize( width, height ); emu->resize( width, height );
handled = true; handled = true;
} }
bool Action::operator==( const Action &other ) const
{
return ( char_present == other.char_present )
&& ( ch == other.ch )
&& ( handled == other.handled );
}
+12
View File
@@ -23,6 +23,8 @@ namespace Parser {
Action() : char_present( false ), ch( -1 ), handled( false ) {}; Action() : char_present( false ), ch( -1 ), handled( false ) {};
virtual ~Action() {}; virtual ~Action() {};
virtual bool operator==( const Action &other ) const;
}; };
class Ignore : public Action { class Ignore : public Action {
@@ -97,6 +99,11 @@ namespace Parser {
void act_on_terminal( Terminal::Emulator *emu ); void act_on_terminal( Terminal::Emulator *emu );
UserByte( int s_c ) : c( s_c ) {} UserByte( int s_c ) : c( s_c ) {}
bool operator==( const UserByte &other ) const
{
return c == other.c;
}
}; };
class Resize : public Action { class Resize : public Action {
@@ -111,6 +118,11 @@ namespace Parser {
: width( s_width ), : width( s_width ),
height( s_height ) height( s_height )
{} {}
bool operator==( const Resize &other ) const
{
return ( width == other.width ) && ( height == other.height );
}
}; };
} }
+7 -4
View File
@@ -6,8 +6,9 @@
#include "terminal.hpp" #include "terminal.hpp"
#include "keystroke.hpp" #include "user.hpp"
#include "networktransport.cpp" #include "networktransport.cpp"
#include "userinput.pb.h"
namespace Parser { namespace Parser {
class Action; class Action;
@@ -15,6 +16,7 @@ namespace Parser {
using namespace std; using namespace std;
using namespace Terminal; using namespace Terminal;
using namespace Network;
template class list<Parser::Action *>; template class list<Parser::Action *>;
template class vector<Cell>; template class vector<Cell>;
@@ -24,7 +26,8 @@ template class vector<wchar_t>;
template class vector<int>; template class vector<int>;
template class map<string, Function>; template class map<string, Function>;
template class vector<bool>; template class vector<bool>;
template class deque<char>;
template class vector<Network::Instruction>;
template class Network::Transport<KeyStroke, KeyStroke>; template class vector<Instruction>;
template class Transport<UserStream, UserStream>;
template class deque<UserEvent>;
+85
View File
@@ -0,0 +1,85 @@
#include <assert.h>
#include <typeinfo>
#include "user.hpp"
#include "userinput.pb.h"
using namespace Parser;
using namespace Network;
using namespace ClientBuffers;
void UserStream::subtract( UserStream * const prefix )
{
for ( deque<UserEvent>::iterator i = prefix->actions.begin();
i != prefix->actions.end();
i++ ) {
assert( !actions.empty() );
assert( *i == actions.front() );
actions.pop_front();
}
}
string UserStream::diff_from( UserStream const & existing )
{
deque<UserEvent>::iterator my_it = actions.begin();
for ( deque<UserEvent>::const_iterator i = existing.actions.begin();
i != existing.actions.end();
i++ ) {
assert( my_it != actions.end() );
assert( *i == *my_it );
my_it++;
}
ClientBuffers::UserMessage output;
while ( my_it != actions.end() ) {
switch ( my_it->type ) {
case UserByteType:
{
char the_byte = my_it->userbyte.c;
/* can we combine this with a previous Keystroke? */
if ( (output.instruction_size() > 0)
&& (output.instruction( output.instruction_size() - 1 ).HasExtension( keystroke )) ) {
output.mutable_instruction( output.instruction_size() - 1 )->MutableExtension( keystroke )->mutable_keys()->append( string( &the_byte, 1 ) );
} else {
Instruction *new_inst = output.add_instruction();
new_inst->MutableExtension( keystroke )->set_keys( &the_byte, 1 );
}
}
break;
case ResizeType:
{
Instruction *new_inst = output.add_instruction();
new_inst->MutableExtension( resize )->set_width( my_it->resize.width );
new_inst->MutableExtension( resize )->set_height( my_it->resize.height );
}
break;
default:
assert( false );
}
my_it++;
}
return output.SerializeAsString();
}
void UserStream::apply_string( string diff )
{
ClientBuffers::UserMessage input;
assert( input.ParseFromString( diff ) );
for ( int i = 0; i < input.instruction_size(); i++ ) {
if ( input.instruction( i ).HasExtension( keystroke ) ) {
string the_bytes = input.instruction( i ).GetExtension( keystroke ).keys();
for ( unsigned int loc = 0; loc < the_bytes.size(); loc++ ) {
actions.push_back( UserEvent( UserByte( the_bytes.at( loc ) ) ) );
}
} else if ( input.instruction( i ).HasExtension( resize ) ) {
actions.push_back( UserEvent( Resize( input.instruction( i ).GetExtension( resize ).width(),
input.instruction( i ).GetExtension( resize ).height() ) ) );
}
}
}
+53
View File
@@ -0,0 +1,53 @@
#ifndef USER_HPP
#define USER_HPP
#include <deque>
#include <list>
#include <string>
#include <assert.h>
#include "parseraction.hpp"
using namespace std;
namespace Network {
enum UserEventType {
UserByteType = 0,
ResizeType = 1
};
class UserEvent
{
public:
UserEventType type;
Parser::UserByte userbyte;
Parser::Resize resize;
UserEvent( Parser::UserByte s_userbyte ) : type( UserByteType ), userbyte( s_userbyte ), resize( -1, -1 ) {}
UserEvent( Parser::Resize s_resize ) : type( ResizeType ), userbyte( 0 ), resize( s_resize ) {}
bool operator==( const UserEvent &x ) const { return ( type == x.type ) && ( userbyte == x.userbyte ) && ( resize == x.resize ); }
};
class UserStream
{
private:
deque<UserEvent> actions;
public:
UserStream() : actions() {}
void push_back( Parser::UserByte s_userbyte ) { actions.push_back( UserEvent( s_userbyte ) ); }
void push_back( Parser::Resize s_resize ) { actions.push_back( UserEvent( s_resize ) ); }
list<Parser::Action *> get_actions( void );
/* interface for Network::Transport */
void subtract( UserStream * const prefix );
string diff_from( UserStream const & existing );
void apply_string( string diff );
bool operator==( UserStream const &x ) const { return actions == x.actions; }
};
}
#endif
+25
View File
@@ -0,0 +1,25 @@
option optimize_for = LITE_RUNTIME;
package ClientBuffers;
message UserMessage {
repeated Instruction instruction = 1;
}
message Instruction {
extensions 2 to max;
}
message Keystroke {
optional bytes keys = 4;
}
message ResizeMessage {
optional int32 width = 5;
optional int32 height = 6;
}
extend Instruction {
optional Keystroke keystroke = 2;
optional ResizeMessage resize = 3;
}