PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/J2Component

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