Skip to content

Commit

Permalink
Use Select in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcallister authored and keithw committed May 16, 2012
1 parent 2112a38 commit 043f9af
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 56 deletions.
1 change: 0 additions & 1 deletion src/examples/benchmark.cc
Expand Up @@ -24,7 +24,6 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/poll.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <pwd.h>
Expand Down
30 changes: 13 additions & 17 deletions src/examples/ntester.cc
Expand Up @@ -18,11 +18,11 @@

#include <termios.h>
#include <unistd.h>
#include <sys/poll.h>

#include "user.h"
#include "fatal_assert.h"
#include "networktransport.cc"
#include "select.h"

using namespace Network;

Expand Down Expand Up @@ -58,20 +58,19 @@ int main( int argc, char *argv[] )
fprintf( stderr, "Port bound is %d, key is %s\n", n->port(), n->get_key().c_str() );

if ( server ) {
struct pollfd my_pollfd;
my_pollfd.fd = n->fd();
my_pollfd.events = POLLIN;
Select sel;
sel.add_fd( n->fd() );
uint64_t last_num = n->get_remote_state_num();
while ( true ) {
try {
if ( poll( &my_pollfd, 1, n->wait_time() ) < 0 ) {
perror( "poll" );
if ( sel.select( n->wait_time() ) < 0 ) {
perror( "select" );
exit( 1 );
}

n->tick();

if ( my_pollfd.revents & POLLIN ) {
if ( sel.read( n->fd() ) ) {
n->recv();

if ( n->get_remote_state_num() != last_num ) {
Expand Down Expand Up @@ -101,28 +100,25 @@ int main( int argc, char *argv[] )
exit( 1 );
}

struct pollfd fds[ 2 ];
fds[ 0 ].fd = STDIN_FILENO;
fds[ 0 ].events = POLLIN;

fds[ 1 ].fd = n->fd();
fds[ 1 ].events = POLLIN;
Select sel;
sel.add_fd( STDIN_FILENO );
sel.add_fd( n->fd() );

while( true ) {
try {
if ( poll( fds, 2, n->wait_time() ) < 0 ) {
perror( "poll" );
if ( sel.select( n->wait_time() ) < 0 ) {
perror( "select" );
}

n->tick();

if ( fds[ 0 ].revents & POLLIN ) {
if ( sel.read( STDIN_FILENO ) ) {
char x;
fatal_assert( read( STDIN_FILENO, &x, 1 ) == 1 );
n->get_current_state().push_back( Parser::UserByte( x ) );
}

if ( fds[ 1 ].revents & POLLIN ) {
if ( sel.read( n->fd() ) ) {
n->recv();
}
} catch ( NetworkException e ) {
Expand Down
24 changes: 10 additions & 14 deletions src/examples/parse.cc
Expand Up @@ -23,7 +23,6 @@
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <sys/poll.h>
#include <string.h>
#include <locale.h>
#include <wchar.h>
Expand All @@ -43,6 +42,7 @@
#include "swrite.h"
#include "locale_utils.h"
#include "fatal_assert.h"
#include "select.h"

const size_t buf_size = 1024;

Expand Down Expand Up @@ -121,34 +121,30 @@ int main( int argc __attribute__((unused)),
void emulate_terminal( int fd )
{
Parser::UTF8Parser parser;
struct pollfd pollfds[ 2 ];

pollfds[ 0 ].fd = STDIN_FILENO;
pollfds[ 0 ].events = POLLIN;

pollfds[ 1 ].fd = fd;
pollfds[ 1 ].events = POLLIN;
Select sel;
sel.add_fd( STDIN_FILENO );
sel.add_fd( fd );

while ( 1 ) {
int active_fds = poll( pollfds, 2, -1 );
int active_fds = sel.select( -1 );
if ( active_fds <= 0 ) {
perror( "poll" );
perror( "select" );
return;
}

if ( pollfds[ 0 ].revents & POLLIN ) {
if ( sel.read( STDIN_FILENO ) ) {
if ( copy( STDIN_FILENO, fd ) < 0 ) {
return;
}
} else if ( pollfds[ 1 ].revents & POLLIN ) {
} else if ( sel.read( fd ) ) {
if ( vt_parser( fd, &parser ) < 0 ) {
return;
}
} else if ( (pollfds[ 0 ].revents | pollfds[ 1 ].revents)
& (POLLERR | POLLHUP | POLLNVAL) ) {
} else if ( sel.error( STDIN_FILENO ) || sel.error( fd ) ) {
return;
} else {
fprintf( stderr, "poll mysteriously woken up\n" );
fprintf( stderr, "select mysteriously woken up\n" );
}
}
}
Expand Down
41 changes: 17 additions & 24 deletions src/examples/termemu.cc
Expand Up @@ -23,7 +23,6 @@
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <sys/poll.h>
#include <string.h>
#include <locale.h>
#include <wchar.h>
Expand Down Expand Up @@ -51,6 +50,7 @@
#include "fatal_assert.h"
#include "locale_utils.h"
#include "sigfd.h"
#include "select.h"

/* For newer skalibs */
extern "C" {
Expand Down Expand Up @@ -190,7 +190,7 @@ bool tick( Terminal::Framebuffer &state, Terminal::Framebuffer &new_frame,
3. Resize events (from a SIGWINCH signal) get turned into
"Resize" actions and applied to the terminal.
At every event from poll(), we run the tick() function to
At every event from select(), we run the tick() function to
possibly print a new frame (if we haven't printed one in the
last 1/50 second). The new frames are "differential" -- they
assume the previous frame was sent to the real terminal.
Expand Down Expand Up @@ -227,36 +227,30 @@ void emulate_terminal( int fd )
/* open display */
Terminal::Display display( true ); /* use TERM to initialize */

struct pollfd pollfds[ 3 ];

pollfds[ 0 ].fd = STDIN_FILENO;
pollfds[ 0 ].events = POLLIN;

pollfds[ 1 ].fd = fd;
pollfds[ 1 ].events = POLLIN;

pollfds[ 2 ].fd = signal_fd;
pollfds[ 2 ].events = POLLIN;
Select sel;
sel.add_fd( STDIN_FILENO );
sel.add_fd( fd );
sel.add_fd( signal_fd );

swrite( STDOUT_FILENO, Terminal::Emulator::open().c_str() );

int poll_timeout = -1;
int timeout = -1;

while ( 1 ) {
int active_fds = poll( pollfds, 3, poll_timeout );
int active_fds = sel.select( timeout );
if ( active_fds < 0 && errno == EINTR ) {
continue;
} else if ( active_fds < 0 ) {
perror( "poll" );
perror( "select" );
break;
}

if ( pollfds[ 0 ].revents & POLLIN ) {
if ( sel.read( STDIN_FILENO ) ) {
/* input from user */
char buf[ buf_size ];

/* fill buffer if possible */
ssize_t bytes_read = read( pollfds[ 0 ].fd, buf, buf_size );
ssize_t bytes_read = read( STDIN_FILENO, buf, buf_size );
if ( bytes_read == 0 ) { /* EOF */
return;
} else if ( bytes_read < 0 ) {
Expand All @@ -274,12 +268,12 @@ void emulate_terminal( int fd )
if ( swrite( fd, terminal_to_host.c_str(), terminal_to_host.length() ) < 0 ) {
break;
}
} else if ( pollfds[ 1 ].revents & POLLIN ) {
} else if ( sel.read( fd ) ) {
/* input from host */
char buf[ buf_size ];

/* fill buffer if possible */
ssize_t bytes_read = read( pollfds[ 1 ].fd, buf, buf_size );
ssize_t bytes_read = read( fd, buf, buf_size );
if ( bytes_read == 0 ) { /* EOF */
return;
} else if ( bytes_read < 0 ) {
Expand All @@ -291,7 +285,7 @@ void emulate_terminal( int fd )
if ( swrite( fd, terminal_to_host.c_str(), terminal_to_host.length() ) < 0 ) {
break;
}
} else if ( pollfds[ 2 ].revents & POLLIN ) {
} else if ( sel.read( signal_fd ) ) {
/* resize */
fatal_assert( sigfd_read() == SIGWINCH );

Expand All @@ -310,17 +304,16 @@ void emulate_terminal( int fd )
perror( "ioctl TIOCSWINSZ" );
return;
}
} else if ( (pollfds[ 0 ].revents | pollfds[ 1 ].revents)
& (POLLERR | POLLHUP | POLLNVAL) ) {
} else if ( sel.error( STDIN_FILENO ) || sel.error( fd ) ) {
break;
}

Terminal::Framebuffer new_frame( complete.get_fb() );

if ( tick( state, new_frame, display ) ) { /* there was a frame */
poll_timeout = -1;
timeout = -1;
} else {
poll_timeout = 20;
timeout = 20;
}
}

Expand Down

0 comments on commit 043f9af

Please sign in to comment.