package AMPS;

use IO::Select;
use Time::HiRes qw(time sleep);
use Exporter;

@ISA = qw(Exporter);
@EXPORT = qw(call);

@threads = ();
# replace with a proper queue that we can listen to later?
@timeouts = ();

$read = IO::Select->new;
$write = IO::Select->new;

%readh = ();
%writeh = ();
# %errorh = ();

sub run {
	# loop while there are sockets or timeouts or threads
	# how do we keep track of the sockets? with a counter?
	while (@threads or @timeouts or $read->count or $write->count) {
		# select
		# todo - select on errors
		# todo - priority on sockets?
		($can_read, $can_write) = IO::Select->select($read, $write, undef, $timeout);

		# write
		call($writeh{$_}, $_) for @$can_write;
		# read
		call($readh{$_}, $_) for @$can_read;
		# timeouts
		
		# threads

	}
}

sub call {
	my ($obj, $sub) = splice @_, 0, 2;
	$obj->$sub(@_);
}

### Test code here

package 
