ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-XMPP2/lib/Net/XMPP2/Event.pm
Revision: 1.6
Committed: Sat Jul 7 12:39:45 2007 UTC (19 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.5: +12 -0 lines
Log Message:
fixed some bugs, redesigned disco mechanism, probably added some bugs,
added samples/disco_test example.

File Contents

# Content
1 package Net::XMPP2::Event;
2
3 =head1 NAME
4
5 Net::XMPP2::Event - Event handler class
6
7 =head1 SYNOPSIS
8
9 package foo;
10 use Net::XMPP2::Event;
11
12 our @ISA = qw/Net::XMPP2::Event/;
13
14 package main;
15 my $o = foo->new;
16 $o->reg_cb (foo => sub { ...; 1 });
17 $o->event (foo => 1, 2, 3);
18
19 =head1 DESCRIPTION
20
21 This module is just a small helper module for the connection
22 and client classes.
23
24 You may only derive from this package.
25
26 =head1 METHODS
27
28 =over 4
29
30 =item B<reg_cb ($eventname1, $cb1, [$eventname2, $cb2, ...])>
31
32 This method registers a callback C<$cb1> for the event with the
33 name C<$eventname1>. You can also pass multiple of these eventname => callback
34 pairs.
35
36 The return value will be an ID that represents the set of callbacks you have installed.
37 Call C<unreg_cb> with that ID to remove those callbacks again.
38
39 To see a documentation of emitted events please take a look at the EVENTS section
40 in the classes that inherit from this one.
41
42 =cut
43
44 sub reg_cb {
45 my ($self, %regs) = @_;
46
47 $self->{id}++;
48
49 for my $cmd (keys %regs) {
50 push @{$self->{events}->{lc $cmd}}, [$self->{id}, $regs{$cmd}]
51 }
52
53 $self->{id}
54 }
55
56 =item B<unreg_cb ($id)>
57
58 Removes the set C<$id> of registered callbacks. C<$id> is the
59 return value of a C<reg_cb> call.
60
61 =cut
62
63 sub unreg_cb {
64 my ($self, $id) = @_;
65
66 my $set = delete $self->{ids}->{$id};
67
68 for my $key (keys %{$self->{events}}) {
69 @{$self->{events}->{$key}} =
70 grep {
71 $_->[0] ne $id
72 } @{$self->{events}->{$key}};
73 }
74 }
75
76
77 =item B<event ($eventname, @args)>
78
79 Emits the event C<$eventname> and passes the arguments C<@args>.
80
81 =cut
82
83 sub event {
84 my ($self, $ev, @arg) = @_;
85
86 my $nxt = [];
87
88 my $handled;
89 for (@{$self->{events}->{lc $ev}}) {
90 $_->[1]->($self, @arg) and push @$nxt, $_;
91 }
92
93 for (values %{$self->{event_forwards}}) {
94 $_->[1]->($self, $_->[0], $ev, @arg);
95 }
96
97 $self->{events}->{lc $ev} = $nxt;
98 }
99
100 =item B<add_forward ($obj, $forward_cb)>
101
102 This method allows to forward or copy all events to a object.
103 C<$forward_cb> will be called everytime an event is generated in C<$self>.
104 The first argument to the callback C<$forward_cb> will be <$self>, the second
105 will be C<$obj>, the third will be the event name and the rest will be
106 the event arguments. (For third and rest of argument also see description
107 of C<event>).
108
109 =cut
110
111 sub add_forward {
112 my ($self, $obj, $forward_cb) = @_;
113 $self->{event_forwards}->{$obj} = [$obj, $forward_cb];
114 }
115
116 =item B<remove_forward ($obj)>
117
118 This method removes a forward. C<$obj> must be the same
119 object that was given C<add_forward> as the C<$obj> argument.
120
121 =cut
122
123 sub remove_forward {
124 my ($self, $obj) = @_;
125 delete $self->{event_forwards}->{$obj};
126 }
127
128 =back
129
130 =head1 AUTHOR
131
132 Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
133
134 =head1 COPYRIGHT & LICENSE
135
136 Copyright 2007 Robin Redeker, all rights reserved.
137
138 This program is free software; you can redistribute it and/or modify it
139 under the same terms as Perl itself.
140
141 =cut
142
143 1;