Debian Codegraph @

examples/J2Component

raw
1 #!/usr/bin/perl
2
3 ###############################################################################
4 #
5 # J20Component Example
6 # (c) Nicholas Perez 2006 - 2009.
7 # Licensed under GPLv2
8 #
9 # Please see the included
10 # LICENSE file for details
11 #
12 # This example component script, instantiates a single PCJ object, connects to
13 # a remote server, sends presence, and then begins sending messages to itself
14 # on a small random interval
15 #
16 ###############################################################################
17
18 use warnings;
19 use strict;
20
21 use 5.010;
22 use POE; #include POE constants
23 use POE::Component::Jabber; #include PCJ
24 use POE::Filter::XML::Node; #include to build nodes
25 use POE::Filter::XML::NS(':JABBER');
26 use Carp;
27
28 # First we create our own session within POE to interact with PCJ
29 POE::Session->create(
30 options => { debug => 1, trace => 1},
31 inline_states => {
32 _start =>
33 sub
34 {
35 my ($kernel, $heap) = @_[KERNEL, HEAP];
36 $kernel->alias_set('Tester');
37
38 # Our PCJ instance is a fullblown object we should store
39 # so we can access various bits of data during use
40
41 $heap->{'component'} =
42 POE::Component::Jabber->new(
43 IP => 'localhost',
44 Port => '5347',
45 Hostname => 'localhost',
46 Username => 'jabberd',
47 Password => 'secret',
48 Alias => 'COMPONENT',
49 # The BindDomain argument is used for negotiating connection
50 # binding on the jabberd2.0 backbone. Essentially this is the
51 # JID of the component. If this option isn't specified, then
52 # the Username and Hostname are combined to create a JID
53
54 BindDomain => 'MyService.localhost',
55
56 # The BindOption argument is for providing extra arguments to
57 # the connection binding. 'log' will tell the server to send
58 # every single packet to the component (ie. for a logging
59 # service). 'default' will tell the server to set the component
60 # as the default route for packets delivered to unfound
61 # addresses.
62 #
63 # See http://jabberd.jabberstudio.org/dev/docs/component.shtml
64 # for more information
65
66 #BindOption => 'log',
67
68 # Shown below are the various connection types included
69 # from POE::Component::Jabber:
70
71 # LEGACY is for pre-XMPP/Jabber connections
72 # XMPP is for XMPP1.0 compliant connections
73 # JABBERD14_COMPONENT is for connecting as a service on the
74 # backbone of a jabberd1.4.x server
75 # JABBERD20_COMPONENT is for connecting as a service on the
76 # backbone of a jabberd2.0.x server
77
78 #ConnectionType => +LEGACY,
79 #ConnectionType => +XMPP,
80 #ConnectionType => +JABBERD14_COMPONENT,
81 ConnectionType => +JABBERD20_COMPONENT,
82 Debug => '1',
83
84 );
85
86 # POE::Component::Jabber now uses POE::Component::PubSub to
87 # manage event reporting including incoming packets. So in order
88 # to get anything out of POE::Component::Jabber we need to
89 # subscribe to the various events of which we have interest.
90
91 # You can see a whole list of potential events (including
92 # possible error states, but seeing the
93 # POE::Component::Jabber::Events documentation.
94
95 # PCJ_READY: Let's us know the connection is up and all of the
96 # various layers of the protocol have been established.
97 $kernel->post('COMPONENT', 'subscribe', +PCJ_READY, 'MyReadyEvent');
98
99 # PCJ_NODERECEIVED: Fires everytime we get a node down the pipe
100 $kernel->post('COMPONENT', 'subscribe', +PCJ_NODERECEIVED, 'MyReceivedEvent');
101
102 # We could subscribe to all of the various error conditions or
103 # even all of the various steps along the way so we could
104 # report the status of the connection as it is building. But
105 # for simplicity sake, this example will only cover the bare
106 # minimum to get a connection up and running.
107
108 # At this point, we have subscribed to the events we want and
109 # are ready to tell the component to connect to the server
110
111 $kernel->post('COMPONENT', 'connect');
112
113 },
114
115 _stop =>
116 sub
117 {
118 $_[KERNEL]->alias_remove('Tester');
119 },
120
121 # This is the event with used to subscribe to the PCJ_READY event.
122 # It will fire anytime a connection is fully initialized and ready for
123 # use. It passes no arguments.
124 MyReadyEvent =>
125 sub
126 {
127 say '--- Connection is ready for use! ---';
128
129 # Now will we will send presence
130 my $presence = POE::Filter::XML::Node->new('presence');
131
132 # The stored POE::Component::Jabber object has a number of
133 # useful methods we can use outside of POE event posting,
134 # including jid()
135 $presence->setAttribute('from', $_[HEAP]->{'component'}->jid());
136
137 # Some of the event names have changed since the 2.x series.
138 # 'output_handler' was replaced by plain old 'output'
139 $_[KERNEL]->post('COMPONENT', 'output', $presence);
140
141 # Now let's send ourselves some messages
142 $_[KERNEL]->yield('MyMessageSendEvent');
143 },
144
145 # This is our event with which we subscribed to the PCJ_NODERECEIVED
146 # event. Once the connection is up and running, our event will be
147 # called once for every node received. ARG0 will contain the node
148 MyReceivedEvent =>
149 sub
150 {
151 say '--- Node received! ---';
152 say $_[ARG0]->toString();
153 say '----------------------';
154
155 },
156
157 # This is the event we call from our ready event to start send messages
158 # to us.
159 MyMessageSendEvent =>
160 sub
161 {
162 # To route XML across the backbone, an envelope must be provided.
163 # See http://jabberd.jabberstudio.org/dev/docs/component.shtml for more
164 # information.
165 my $envelope = POE::Filter::XML::Node->new
166 (
167 'route',
168 [
169 'xmlns', +NS_JABBER_COMPONENT,
170 'from', $_[HEAP]->{'component'}->jid(),
171 'to', $_[HEAP]->{'component'}->jid()
172 ]
173 );
174
175 my $node = POE::Filter::XML::Node->new('message');
176
177 $node->setAttribute('to', $_[HEAP]->{'component'}->jid());
178 $node->appendTextChild('body', 'This is a test sent at: ' . time());
179
180 $envelope->appendChild($node);
181
182 $_[KERNEL]->post('COMPONENT', 'output', $envelope);
183 $_[KERNEL]->delay_set('MyMessageSendEvent', int(rand(6)));
184 },
185 }
186 );
187
188 POE::Kernel->run();
189
190 exit 0;