]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perlmods/OpenSRF/Transport/SlimJabber/Inbound.pm
added router 'unregister' loop to DESTROY
[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 $conf = OpenSRF::Utils::Config->current;
36                         my $domains = $conf->bootstrap->domains;
37
38                         my $username    = $conf->bootstrap->username;
39                         my $password    = $conf->bootstrap->passwd;
40                         my $port                        = $conf->bootstrap->port;
41                         my $host                        = $domains->[0]; # XXX for now...
42                         my $resource    = $app . '_listener_at_' . $conf->env->hostname;
43
44                         OpenSRF::Utils::Logger->transport("Inbound as $username, $password, $resource, $host, $port\n", INTERNAL );
45
46                         my $self = __PACKAGE__->SUPER::new( 
47                                         username                => $username,
48                                         resource                => $resource,
49                                         password                => $password,
50                                         host                    => $host,
51                                         port                    => $port,
52                                         );
53
54                         $self->{app} = $app;
55                                         
56                         my $client = OpenSRF::Utils::SettingsClient->new();
57                         my $f = $client->config_value("dirs", "sock");
58                         $unix_sock = join( "/", $f, 
59                                         $client->config_value("apps", $app, "unix_config", "unix_sock" ));
60                         bless( $self, $class );
61                         $instance = $self;
62                 }
63                 return $instance;
64         }
65
66 }
67
68 sub DESTROY {
69         my $self = shift;
70
71         my $routers = $self->{routers}; #store for destroy
72         my $router_name = $self->{router_name};
73
74         unless($router_name and $routers) {
75                 return;
76         }
77
78         my @targets;
79         for my $router (@$routers) {
80                 push @targets, "$router_name\@$router/router";
81         }
82
83         for my $router (@targets) {
84                 if($self->tcp_connected()) {
85                         $self->send( to => $router, body => "registering", 
86                                 router_command => "unregister" , router_class => $self->{app} );
87                 }
88         }
89 }
90         
91 sub listen {
92         my $self = shift;
93         
94         my $routers;
95
96         try {
97
98                 my $conf = OpenSRF::Utils::Config->current;
99                 my $router_name = $conf->bootstrap->router_name;
100                 my $routers = $conf->bootstrap->domains;
101
102                 $self->{routers} = $routers; #store for destroy
103                 $self->{router_name} = $router_name;
104         
105                 unless($router_name and $routers) {
106                         throw OpenSRF::EX::Config 
107                                 ("Missing router config information 'router_name' and 'routers'");
108                 }
109         
110                 my @targets;
111                 for my $router (@$routers) {
112                         push @targets, "$router_name\@$router/router";
113                 }
114
115                 for my $router (@targets) {
116                         $logger->transport( $self->{app} . " connecting to router $router", INFO ); 
117                         $self->send( to => $router, 
118                                         body => "registering", router_command => "register" , router_class => $self->{app} );
119                 }
120                 $logger->transport( $self->{app} . " :routers connected", INFO ); 
121
122                 
123         } catch OpenSRF::EX::Config with {
124                 $logger->transport( $self->{app} . ": No routers defined" , WARN ); 
125                 # no routers defined
126         };
127
128
129         
130                         
131         $logger->transport( $self->{app} . " going into listen loop", INFO );
132         while(1) {
133         
134                 my $sock = $self->unix_sock();
135                 my $o = $self->process( -1 );
136
137                 if( ! defined( $o ) ) {
138                         throw OpenSRF::EX::Jabber( "Listen Loop failed at 'process()'" );
139                 }
140
141                 my $socket = IO::Socket::UNIX->new( Peer => $sock  );
142                 throw OpenSRF::EX::Socket( "Unable to connect to UnixServer: socket-file: $sock \n :=> $! " )
143                         unless ($socket->connected);
144                 print $socket $o;
145                 $socket->close;
146
147         }
148
149         throw OpenSRF::EX::Socket( "How did we get here?!?!" );
150 }
151
152 1;
153