Separate modules by subdirectory
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
AM_CPPFLAGS = -I$(srcdir)/../terminal -I$(builddir)/../protobufs
|
||||
AM_CXXFLAGS = --std=c++0x -pedantic -Werror -Wall -Wextra -Weffc++ -fno-default-inline -pipe
|
||||
|
||||
noinst_LIBRARIES = libmoshstatesync.a
|
||||
|
||||
libmoshstatesync_a_SOURCES = completeterminal.cc completeterminal.h user.cc user.h
|
||||
@@ -0,0 +1,75 @@
|
||||
#include "completeterminal.h"
|
||||
|
||||
#include "hostinput.pb.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace Parser;
|
||||
using namespace Terminal;
|
||||
using namespace HostBuffers;
|
||||
|
||||
string Complete::act( const string &str )
|
||||
{
|
||||
for ( unsigned int i = 0; i < str.size(); i++ ) {
|
||||
/* parse octet into up to three actions */
|
||||
list<Action *> actions( parser.input( str[ i ] ) );
|
||||
|
||||
/* apply actions to terminal and delete them */
|
||||
for ( list<Action *>::iterator it = actions.begin();
|
||||
it != actions.end();
|
||||
it++ ) {
|
||||
Action *act = *it;
|
||||
act->act_on_terminal( &terminal );
|
||||
delete act;
|
||||
}
|
||||
}
|
||||
|
||||
return terminal.read_octets_to_host();
|
||||
}
|
||||
|
||||
string Complete::act( const Action *act )
|
||||
{
|
||||
/* apply action to terminal */
|
||||
act->act_on_terminal( &terminal );
|
||||
return terminal.read_octets_to_host();
|
||||
}
|
||||
|
||||
/* interface for Network::Transport */
|
||||
string Complete::diff_from( const Complete &existing )
|
||||
{
|
||||
HostBuffers::HostMessage output;
|
||||
|
||||
if ( !(existing.get_fb() == get_fb()) ) {
|
||||
if ( (existing.get_fb().ds.get_width() != terminal.get_fb().ds.get_width())
|
||||
|| (existing.get_fb().ds.get_height() != terminal.get_fb().ds.get_height()) ) {
|
||||
Instruction *new_res = output.add_instruction();
|
||||
new_res->MutableExtension( resize )->set_width( terminal.get_fb().ds.get_width() );
|
||||
new_res->MutableExtension( resize )->set_height( terminal.get_fb().ds.get_height() );
|
||||
}
|
||||
Instruction *new_inst = output.add_instruction();
|
||||
new_inst->MutableExtension( hostbytes )->set_hoststring( Terminal::Display::new_frame( true, existing.get_fb(), terminal.get_fb() ) );
|
||||
}
|
||||
|
||||
return output.SerializeAsString();
|
||||
}
|
||||
|
||||
void Complete::apply_string( string diff )
|
||||
{
|
||||
HostBuffers::HostMessage input;
|
||||
assert( input.ParseFromString( diff ) );
|
||||
|
||||
for ( int i = 0; i < input.instruction_size(); i++ ) {
|
||||
if ( input.instruction( i ).HasExtension( hostbytes ) ) {
|
||||
string terminal_to_host = act( input.instruction( i ).GetExtension( hostbytes ).hoststring() );
|
||||
assert( terminal_to_host.empty() ); /* server never interrogates client terminal */
|
||||
} else if ( input.instruction( i ).HasExtension( resize ) ) {
|
||||
act( new Resize( input.instruction( i ).GetExtension( resize ).width(),
|
||||
input.instruction( i ).GetExtension( resize ).height() ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Complete::operator==( Complete const &x ) const
|
||||
{
|
||||
// assert( parser == x.parser ); /* parser state is irrelevant for us */
|
||||
return terminal == x.terminal;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef COMPLETE_TERMINAL_HPP
|
||||
#define COMPLETE_TERMINAL_HPP
|
||||
|
||||
#include "parser.h"
|
||||
#include "terminal.h"
|
||||
|
||||
/* This class represents the complete terminal -- a UTF8Parser feeding Actions to an Emulator. */
|
||||
|
||||
namespace Terminal {
|
||||
class Complete {
|
||||
private:
|
||||
Parser::UTF8Parser parser;
|
||||
Terminal::Emulator terminal;
|
||||
|
||||
public:
|
||||
Complete( size_t width, size_t height ) : parser(), terminal( width, height ) {}
|
||||
|
||||
std::string act( const std::string &str );
|
||||
std::string act( const Parser::Action *act );
|
||||
|
||||
const Framebuffer & get_fb( void ) const { return terminal.get_fb(); }
|
||||
bool parser_grounded( void ) const { return parser.is_grounded(); }
|
||||
|
||||
/* interface for Network::Transport */
|
||||
void subtract( const Complete * ) {}
|
||||
std::string diff_from( const Complete &existing );
|
||||
void apply_string( std::string diff );
|
||||
bool operator==( const Complete &x ) const;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,97 @@
|
||||
#include <assert.h>
|
||||
#include <typeinfo>
|
||||
|
||||
#include "user.h"
|
||||
#include "userinput.pb.h"
|
||||
|
||||
using namespace Parser;
|
||||
using namespace Network;
|
||||
using namespace ClientBuffers;
|
||||
|
||||
void UserStream::subtract( const UserStream *prefix )
|
||||
{
|
||||
for ( deque<UserEvent>::const_iterator i = prefix->actions.begin();
|
||||
i != prefix->actions.end();
|
||||
i++ ) {
|
||||
assert( !actions.empty() );
|
||||
assert( *i == actions.front() );
|
||||
actions.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
string UserStream::diff_from( const UserStream &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() ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const Parser::Action *UserStream::get_action( unsigned int i )
|
||||
{
|
||||
switch( actions[ i ].type ) {
|
||||
case UserByteType:
|
||||
return &( actions[ i ].userbyte );
|
||||
case ResizeType:
|
||||
return &( actions[ i ].resize );
|
||||
default:
|
||||
assert( false );
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#ifndef USER_HPP
|
||||
#define USER_HPP
|
||||
|
||||
#include <deque>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <assert.h>
|
||||
|
||||
#include "parseraction.h"
|
||||
|
||||
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 ) {}
|
||||
|
||||
UserEvent() /* default constructor required by C++11 STL */
|
||||
: type( UserByteType ),
|
||||
userbyte( 0 ),
|
||||
resize( -1, -1 )
|
||||
{
|
||||
assert( false );
|
||||
}
|
||||
|
||||
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 ) ); }
|
||||
|
||||
size_t size( void ) { return actions.size(); }
|
||||
const Parser::Action *get_action( unsigned int i );
|
||||
|
||||
/* interface for Network::Transport */
|
||||
void subtract( const UserStream *prefix );
|
||||
string diff_from( const UserStream &existing );
|
||||
void apply_string( string diff );
|
||||
bool operator==( const UserStream &x ) const { return actions == x.actions; }
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user