]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perlmods/OpenSRF/Transport/SlimJabber/Inbound.pm
using a single jabber connection per process, since we do not actually care about...
[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 qw(:try);
5 use OpenSRF::Utils::Logger qw(:level);
6 use OpenSRF::Utils::SettingsClient;
7 use OpenSRF::Utils::Config;
8
9 my $logger = "OpenSRF::Utils::Logger";
10
11 =head1 Description
12
13 This is the jabber connection where all incoming client requests will be accepted.
14 This connection takes the data, passes it off to the system then returns to take
15 more data.  Connection params are all taken from the config file and the values
16 retreived are based on the $app name passed into new().
17
18 This service should be loaded at system startup.
19
20 =cut
21
22 # XXX This will be overhauled to connect as a component instead of as
23 # a user.  all in good time, though.
24
25 {
26         my $unix_sock;
27         sub unix_sock { return $unix_sock; }
28         my $instance;
29
30         sub new {
31                 my( $class, $app ) = @_;
32                 $class = ref( $class ) || $class;
33                 if( ! $instance ) {
34
35                         my $client = OpenSRF::Utils::SettingsClient->new();
36
37                         my $transport_info = $client->config_value(
38                                         "apps", $app, "transport_hosts", "transport_host" );
39
40                         if( !ref($transport_info) eq "ARRAY" ) {
41                                 $transport_info = [$transport_info];
42                         }
43
44
45                         # XXX for now, we just try the first host...
46
47                         my $username = $transport_info->[0]->{username};
48                         my $password    = $transport_info->[0]->{password};
49                         my $resource    = 'system';
50                         my $host                        = $transport_info->[0]->{host};
51                         my $port                        = $transport_info->[0]->{port};
52
53                         if (defined $client->config_value("router_targets")) {
54                                 my $h = OpenSRF::Utils::Config->current->env->hostname;
55                                 $resource .= "_$h";
56                         }
57
58                         OpenSRF::Utils::Logger->transport("Inbound as $username, $password, $resource, $host, $port\n", INTERNAL );
59
60                         my $self = __PACKAGE__->SUPER::new( 
61                                         username                => $username,
62                                         resource                => $resource,
63                                         password                => $password,
64                                         host                    => $host,
65                                         port                    => $port,
66                                         );
67
68                         $self->{app} = $app;
69                                         
70                         my $f = $client->config_value("dirs", "sock");
71                         $unix_sock = join( "/", $f, 
72                                         $client->config_value("apps", $app, "unix_config", "unix_sock" ));
73                         bless( $self, $class );
74                         $instance = $self;
75                 }
76                 return $instance;
77         }
78
79 }
80         
81 sub listen {
82         my $self = shift;
83         
84         my $client = OpenSRF::Utils::SettingsClient->new();
85         my $routers;
86         try {
87
88                 $routers = $client->config_value("router_targets","router_target");
89                 $logger->transport( $self->{app} . " connecting to router $routers", INFO ); 
90
91                 if (defined $routers) {
92                         if( !ref($routers) || !(ref($routers) eq "ARRAY") ) {
93                                 $routers = [$routers];
94                         }
95
96
97                         for my $router (@$routers) {
98                                 $logger->transport( $self->{app} . " connecting to router $router", INFO ); 
99                                 $self->send( to => $router, 
100                                                 body => "registering", router_command => "register" , router_class => $self->{app} );
101                         }
102                         $logger->transport( $self->{app} . " :routers connected", INFO ); 
103
104                 }
105         } catch OpenSRF::EX::Config with {
106                 $logger->transport( $self->{app} . ": No routers defined" , WARN ); 
107                 # no routers defined
108         };
109
110
111         
112                         
113         $logger->transport( $self->{app} . " going into listen loop", INFO );
114         while(1) {
115                 my $sock = $self->unix_sock();
116                 my $socket = IO::Socket::UNIX->new( Peer => $sock  );
117         
118                 throw OpenSRF::EX::Socket( "Unable to connect to UnixServer: socket-file: $sock \n :=> $! " )
119                         unless ($socket->connected);
120
121                 my $o = $self->process( -1 );
122
123                 if( ! defined( $o ) ) {
124                         throw OpenSRF::EX::Jabber( "Listen Loop failed at 'process()'" );
125                 }
126                 print $socket $o;
127
128                 $socket->close;
129
130         }
131
132         throw OpenSRF::EX::Socket( "How did we get here?!?!" );
133 }
134
135 1;
136