SessionSet.pm 5.17 KB
Newer Older
root's avatar
root committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
# ======================================================================
#
# Copyright (C) 2000 Lincoln D. Stein
# Formatting changed to match the layout layed out in Perl Best Practices
# (by Damian Conway) by Martin Kutter in 2008
#
# ======================================================================

package IO::SessionSet;

use strict;
use Carp;
use IO::Select;
use IO::Handle;
use IO::SessionData;

use vars '$DEBUG';
$DEBUG = 0;

# Class method new()
# Create a new Session set.
# If passed a listening socket, use that to
# accept new IO::SessionData objects automatically.
sub new {
    my $pack = shift;
    my $listen = shift;
    my $self = bless {
        sessions     => {},
        readers      => IO::Select->new(),
        writers      => IO::Select->new(),
        }, $pack;
    # if initialized with an IO::Handle object (or subclass)
    # then we treat it as a listening socket.
    if ( defined($listen) and $listen->can('accept') ) {
        $self->{listen_socket} = $listen;
        $self->{readers}->add($listen);
    }
    return $self;
}

# Object method: sessions()
# Return list of all the sessions currently in the set.
sub sessions {
    return values %{shift->{sessions}}
};

# Object method: add()
# Add a handle to the session set.  Will automatically
# create a IO::SessionData wrapper around the handle.
sub add {
    my $self = shift;
    my ($handle,$writeonly) = @_;
    warn "Adding a new session for $handle.\n" if $DEBUG;
    return $self->{sessions}{$handle} =
        $self->SessionDataClass->new($self,$handle,$writeonly);
}

# Object method: delete()
# Remove a session from the session set.  May pass either a handle or
# a corresponding IO::SessionData wrapper.
sub delete {
    my $self = shift;
    my $thing = shift;
    my $handle = $self->to_handle($thing);
    my $sess = $self->to_session($thing);
    warn "Deleting session $sess handle $handle.\n" if $DEBUG;
    delete $self->{sessions}{$handle};
    $self->{readers}->remove($handle);
    $self->{writers}->remove($handle);
}

# Object method: to_handle()
# Return a handle, given either a handle or a IO::SessionData object.
sub to_handle {
    my $self = shift;
    my $thing = shift;
    return $thing->handle if $thing->isa('IO::SessionData');
    return $thing if defined (fileno $thing);
    return;  # undefined value
}

# Object method: to_session
# Return a IO::SessionData object, given either a handle or the object itself.
sub to_session {
    my $self = shift;
    my $thing = shift;
    return $thing if $thing->isa('IO::SessionData');
    return $self->{sessions}{$thing} if defined (fileno $thing);
    return;  # undefined value
}

# Object method: activate()
# Called with parameters ($session,'read'|'write' [,$activate])
# If called without the $activate argument, will return true
# if the indicated handle is on the read or write IO::Select set.
# May use either a session object or a handle as first argument.
sub activate {
    my $self = shift;
    my ($thing,$rw,$act) = @_;
    croak 'Usage $obj->activate($session,"read"|"write" [,$activate])'
        unless @_ >= 2;
    my $handle = $self->to_handle($thing);
    my $select = lc($rw) eq 'read' ? 'readers' : 'writers';
    my $prior = defined $self->{$select}->exists($handle);
    if (defined $act && $act != $prior) {
        $self->{$select}->add($handle)        if $act;
        $self->{$select}->remove($handle) unless $act;
        warn $act ? 'Activating' : 'Inactivating',
            " handle $handle for ",
            $rw eq 'read' ? 'reading':'writing',".\n" if $DEBUG;
    }
    return $prior;
}

# Object method: wait()
# Wait for I/O.  Handles writes automatically.  Returns a list of
# IO::SessionData objects ready for reading.
# If there is a listen socket, then will automatically do an accept()
# and return a new IO::SessionData object for that.
sub wait {
    my $self = shift;
    my $timeout = shift;

    # Call select() to get the list of sessions that are ready for
    # reading/writing.
    warn "IO::Select->select() returned error: $!"
        unless my ($read,$write) =
            IO::Select->select($self->{readers},$self->{writers},undef,$timeout);

    # handle queued writes automatically
    foreach (@$write) {
        my $session = $self->to_session($_);
        warn "Writing pending data (",$session->pending+0," bytes) for $_.\n"
            if $DEBUG;
        my $rc = $session->write;
    }

    # Return list of sessions that are ready for reading.
    # If one of the ready handles is the listen socket, then
    # create a new session.
    # Otherwise return the ready handles as a list of IO::SessionData objects.
    my @sessions;
    foreach (@$read) {
        if ($_ eq $self->{listen_socket}) {
            my $newhandle = $_->accept;
            warn "Accepting a new handle $newhandle.\n" if $DEBUG;
            my $newsess = $self->add($newhandle) if $newhandle;
            push @sessions,$newsess;
        }
        else {
            push @sessions,$self->to_session($_);
        }
    }
    return @sessions;
}

# Class method: SessionDataClass
# Return the string containing the name of the session data
# wrapper class.  Subclass and override to use a different
# session data class.
sub SessionDataClass {  return 'IO::SessionData'; }

1;