#!/usr/bin/perl use strict; use IO::Socket::UNIX; use threads; use threads::shared; my $LSocket; my @Links; my $NumConnections = 32; #allow for 32 connections my @Connections = (1 .. $NumConnections); my @StructDs : shared; my @StoreDs : shared; sub ConThread($); MAIN: { die("The first and only requirement should be the name for this OEFS instance, and is required!\n") if (!($ARGV[0])); #Start the listener mkdir("/tmp/OEFS/") if (!(-e "/tmp/OEFS")); unlink('/tmp/OEFS/' . $ARGV[0]); $LSocket = new IO::Socket::UNIX('Listen' => 1, 'Local' => '/tmp/OEFS/' . $ARGV[0]); die("$!\nUnable to create listen socket!\n") if (!($LSocket)); my $Link; my $IDX; print("Listening...\n"); while ($Link = $LSocket->accept()) { #Clean out old threads foreach my $I (1 .. $NumConnections) { if (($Links[$I]) && ($Links[$I]->is_joinable())) { $Links[$I]->join(); unshift(@Connections, $I); print("Connection #$I now free\n"); } } print("Incomming Connection\n"); $IDX = pop(@Connections); if ($IDX) { print("Using connection #$IDX\n"); print("Spawning Thread\n"); #New thread. $Links[$IDX] = threads->create(\&ConThread, $Link); } else { print("Too many connections\n"); print $Link "Maximum number of connections has been reached.\n"; $Link->close(); } } } sub ConThread($) { my ($Socket) = @_; print $Socket "Ready\n"; $Socket->close(); return; }