Commits

Toby Inkster committed 227b557

attempt to port to AnyEvent

Comments (0)

Files changed (3)

lib/SoundSwarm/Daemon/Player.pm

 
 use SoundSwarm::Syntax;
 use Moo;
+use AnyEvent;
+use Time::HiRes qw(usleep);
 
 has port => (
 	is      => 'lazy',
 	default => sub { '127.0.0.1' },
 );
 
-has queue_management_process => (
-	is      => 'rwp',
-	isa     => InstanceOf[ 'Async' ],
-);
-
 has queue => (
 	is      => 'lazy',
 	isa     => InstanceOf[ 'SoundSwarm::Client::Queue' ],
 sub start_playing
 {
 	my $self = shift;
-	my $mplayer = $self->mplayer; # force construction
-	my $async   = Async->new(sub
-	{
-		while (1)
-		{
-			my $song = $self->get_song;
-			last unless defined $song;
-			$self->log("Next song: %s", $song);
-			$self->play($song);
-			$self->wait;
-		}
-	});
-	confess $async->error if defined $async->error;
-	$self->_set_queue_management_process($async);
+	my $mplayer = $self->mplayer;
+	
+	my $song = $self->get_song or confess "No song?";
+	$self->log("Next song: %s", $song);
+	$self->play($song);
+	my $w; $w = AnyEvent->child(
+		pid   => $mplayer->mplayer_pid,
+		cb    => sub { $self->start_playing },
+	);
 }
 
 before daemonize => sub

lib/SoundSwarm/Role/Client.pm

 use SoundSwarm::Syntax;
 use Moo::Role;
 
+### TODO - use AnyEvent::Socket;
+
 requires 'log';
 requires 'host', 'port';
 
 	my $self = shift;
 	my ($line) = @_;
 	
+	warn "CLIENT SEND LINE: $line";
+	
 	my $sock = $self->socket;
 	$self->log("Connected to %s:%d", $sock->peerhost, $sock->peerport);
 	
 	print $sock $line;
 	my $response = <$sock>;
 	$sock->close;
+	warn "CLIENT RECV LINE: $response";
 	return $response;
 }
 

lib/SoundSwarm/Role/Daemon.pm

 
 use SoundSwarm::Syntax;
 use MooX::Role;
+use AnyEvent;
+use AnyEvent::Socket;
 
 use constant {
 	END_CLIENT  => \(1),
 requires 'host', 'port';
 requires 'handle_line';
 
-has socket => (
-	is      => 'lazy',
-	isa     => InstanceOf[ 'IO::Socket' ],
-);
-
-sub _build_socket
-{
-	require IO::Socket::INET;
-	my $self = shift;
-	my $sock = 'IO::Socket::INET'->new(
-		Listen     => 5,
-		LocalAddr  => $self->host,
-		LocalPort  => $self->port,
-		Proto      => 'tcp',
-	) or confess "Cannot open socket: $!";
-	return $sock;
-}
-
 sub daemonize
 {
 	my $self = shift;
-	my $sock = $self->socket;
+	$self->log("Listening on %s:%d", $self->host, $self->port);
 	
-	$self->log("Listening on %s:%d", $sock->sockhost, $sock->sockport);
+	my $cv = AnyEvent->condvar;
 	
-	CLIENT: while (1)
+	tcp_server $self->host, $self->port, sub
 	{
-		my $client = $sock->accept;
-		$self->log("Connection from %s:%d", $client->peerhost, $client->peerport);
+		my $client = shift;
+		$self->log("Connection from %s:%d", @_);
 		
 		LINE: while (defined(my $line = <$client>))
 		{
+			warn "SERVER RECV LINE: $line";
 			for my $response ($self->handle_line($line))
 			{
+				warn "SERVER SEND LINE: $response";
 				if (ref $response and $response == END_CLIENT) {
-					$client->close;
 					last LINE;
 				}
 				elsif (ref $response and $response == END_DAEMON) {
-					$client->close;
-					$sock->close;
-					last CLIENT;
+					$cv->send;
+					last LINE;
 				}
-				print {$client} $response;
+				syswrite $client, $response;
 			}
 		}
-	}
+		
+		$self->log("Connection from %s:%d closed!", @_);
+	};
+	
+	$cv->recv;
 }
 
 1;