File: AnyEvent.pm

package info (click to toggle)
libevent-rpc-perl 1.08-2%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 520 kB
  • sloc: perl: 2,353; makefile: 2
file content (135 lines) | stat: -rw-r--r-- 2,531 bytes parent folder | download | duplicates (3)
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
#-----------------------------------------------------------------------
# Copyright (C) 2005-2015 by Jörn Reder <joern AT zyn.de>.
# All Rights Reserved. See file COPYRIGHT for details.
# 
# This module is part of Event::RPC, which is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#-----------------------------------------------------------------------

package Event::RPC::Loop::AnyEvent;

use base qw( Event::RPC::Loop );

use strict;
use utf8;

use AnyEvent;

my %watchers;

sub get_loop_cv                 { shift->{loop_cv}                      }
sub set_loop_cv                 { shift->{loop_cv}              = $_[1] }

sub add_io_watcher {
    my $self = shift;
    my %par = @_;
    my ($fh, $cb, $desc, $poll) = @par{'fh','cb','desc','poll'};

    my $watcher = AnyEvent->io (
      fh   => $fh,
      poll => $poll,
      cb   => $cb,
    );

    $watchers{"$watcher"} = $watcher;
    
    return $watcher;
}

sub del_io_watcher {
    my $self = shift;
    my ($watcher) = @_;

    delete $watchers{"$watcher"};

    1;
}

sub add_timer {
    my $self = shift;
    my %par = @_;
    my  ($interval, $after, $cb, $desc) =
    @par{'interval','after','cb','desc'};

    my $timer = AnyEvent->timer (
        after       => $after,
        interval    => $interval,
        cb          => $cb,
    );

    $watchers{"$timer"} = $timer;
    
    return $timer;
}

sub del_timer {
    my $self = shift;
    my ($timer) = @_;

    delete $watchers{"$timer"};

    1;
}

sub enter {
    my $self = shift;

    my $loop_cv = AnyEvent->condvar;

    $self->set_loop_cv($loop_cv);

    $loop_cv->wait;

    1;
}

sub leave {
    my $self = shift;

    $self->get_loop_cv->send;

    1;
}

1;

__END__

=encoding utf8

=head1 NAME

Event::RPC::Loop::AnyEvent - AnyEvent mainloop for Event::RPC

=head1 SYNOPSIS

  use Event::RPC::Server;
  use Event::RPC::Loop::AnyEvent;
  
  my $server = Event::RPC::Server->new (
      ...
      loop => Event::RPC::Loop::AnyEvent->new(),
      ...
  );

  $server->start;

=head1 DESCRIPTION

This modules implements a mainloop using AnyEvent for the
Event::RPC::Server module. It implements the interface
of Event::RPC::Loop. Please refer to the manpage of
Event::RPC::Loop for details.

=head1 AUTHORS

  Jörn Reder <joern AT zyn.de>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2005-2015 by Jörn Reder <joern AT zyn.de>.

This library is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.

=cut