Initial revision
[OpenSRF.git] / src / perlmods / OpenSRF / Transport / SlimJabber / Inbound.pm
1 package OpenSRF::Transport::SlimJabber::Inbound;
2 use strict;use warnings;
3 use base qw/OpenSRF::Transport::SlimJabber::Client/;
4 use OpenSRF::EX;
5 use OpenSRF::Utils::Config;
6 use OpenSRF::Utils::Logger qw(:level);
7
8 my $logger = "OpenSRF::Utils::Logger";
9
10 =head1 Description
11
12 This is the jabber connection where all incoming client requests will be accepted.
13 This connection takes the data, passes it off to the system then returns to take
14 more data.  Connection params are all taken from the config file and the values
15 retreived are based on the $app name passed into new().
16
17 This service should be loaded at system startup.
18
19 =cut
20
21 # XXX This will be overhauled to connect as a component instead of as
22 # a user.  all in good time, though.
23
24 {
25         my $unix_sock;
26         sub unix_sock { return $unix_sock; }
27         my $instance;
28
29         sub new {
30                 my( $class, $app ) = @_;
31                 $class = ref( $class ) || $class;
32                 if( ! $instance ) {
33                         my $app_state = $app . "_inbound";
34                         my $config = OpenSRF::Utils::Config->current;
35
36                         if( ! $config ) {
37                                 throw OpenSRF::EX::Jabber( "No suitable config found" );
38                         }
39
40                         my $username    = $config->transport->users->$app;
41                         my $password    = $config->transport->auth->password;
42                         my $resource    = "system_" . $config->env->hostname . "_$$";
43
44
45                         my $self = __PACKAGE__->SUPER::new( 
46                                         username                => $username,
47                                         resource                => $resource,
48                                         password                => $password,
49                                         );
50
51                         $self->{app} = $app;
52                                         
53                                         
54                         my $f = $config->dirs->sock_dir;
55                         $unix_sock = join( "/", $f, $config->unix_sock->$app );
56                         bless( $self, $class );
57                         $instance = $self;
58                 }
59                 return $instance;
60         }
61
62 }
63         
64 sub listen {
65         my $self = shift;
66         
67         my $config = OpenSRF::Utils::Config->current;
68         my $router = $config->system->router_target;
69         $self->send( to => $router, 
70                         body => "registering", router_command => "register" , router_class => $self->{app} );
71                         
72         while(1) {
73                 my $sock = $self->unix_sock();
74                 my $socket = IO::Socket::UNIX->new( Peer => $sock  );
75         
76                 throw OpenSRF::EX::Socket( "Unable to connect to UnixServer: socket-file: $sock \n :=> $! " )
77                         unless ($socket->connected);
78
79                 my $o = $self->process( -1 );
80
81                 if( ! defined( $o ) ) {
82                         throw OpenSRF::EX::Jabber( "Listen Loop failed at 'process()'" );
83                 }
84                 print $socket $o;
85
86                 $socket->close;
87
88         }
89
90         throw OpenSRF::EX::Socket( "How did we get here?!?!" );
91 }
92
93 1;
94