Initial revision
[OpenSRF.git] / src / perlmods / OpenSRF / Transport / Jabber / JInbound.pm
1 package OpenSRF::Transport::Jabber::JInbound;
2 use strict;use warnings;
3 use base qw/OpenSRF::Transport::Jabber::JabberClient/;
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 $host                        = $config->transport->server->primary;
41                         my $username    = $config->transport->users->$app;
42                         my $password    = $config->transport->auth->password;
43                         my $debug               = $config->transport->llevel->$app_state;
44                         my $log                 = $config->transport->log->$app_state;
45                         my $resource    = "system";
46
47
48                         my $self = __PACKAGE__->SUPER::new( 
49                                         username                => $username,
50                                         host                    => $host,
51                                         resource                => $resource,
52                                         password                => $password,
53                                         log_file                => $log,
54                                         debug                   => $debug,
55                                         );
56                                         
57                                         
58                         my $f = $config->dirs->sock_dir;
59                         $unix_sock = join( "/", $f, $config->unix_sock->$app );
60                         bless( $self, $class );
61                         $instance = $self;
62                 }
63                 $instance->SetCallBacks( message => \&handle_message );
64                 return $instance;
65         }
66
67 }
68         
69 # ---
70 # All incoming messages are passed untouched to the Unix Server for processing.  The
71 # Unix socket is closed by the Unix Server as soon as it has received all of the
72 # data.  This means we can go back to accepting more incoming connection.
73 # -----
74 sub handle_message { 
75         my $sid = shift;
76         my $message = shift;
77
78         my $packet = $message->GetXML();
79
80         $logger->transport( "JInbound $$ received $packet", INTERNAL );
81
82         # Send the packet to the unix socket for processing.
83         my $sock = unix_sock();
84         my $socket;
85         my $x = 0;
86         for( ;$x != 5; $x++ ) { #try 5 times
87                 if( $socket = IO::Socket::UNIX->new( Peer => $sock  ) ) {
88                         last;
89                 }
90         }
91         if( $x == 5 ) {
92                 throw OpenSRF::EX::Socket( 
93                         "Unable to connect to UnixServer: socket-file: $sock \n :=> $! " );
94         }
95         print $socket $packet;
96         close( $socket );
97 }
98
99
100 1;
101