1 package OpenSRF::UnixServer;
2 use strict; use warnings;
5 use OpenSRF::Utils::Logger qw(:level);
6 use OpenSRF::Transport::PeerHandle;
7 use OpenSRF::Application;
8 use OpenSRF::AppSession;
9 use OpenSRF::DomainObject::oilsResponse qw/:status/;
14 # XXX Need to add actual logging statements in the code
15 my $logger = "OpenSRF::Utils::Logger";
17 sub DESTROY { confess "Dying $$"; }
21 All inbound messages are passed on to the UnixServer for processing.
22 We take the data, close the Unix socket, and pass the data on to our abstract
25 Our purpose is to 'multiplex' a single TCP connection into multiple 'client' connections.
26 So when you pass data down the Unix socket to us, we have been preforked and waiting
27 to disperse new data among us.
33 sub app { return $app; }
36 my( $class, $app1 ) = @_;
38 throw OpenSRF::EX::InvalidArg( "UnixServer requires an app name to run" );
41 my $self = bless( {}, $class );
42 if( OpenSRF::Utils::Config->current->system->server_type !~ /fork/i ) {
43 $self->child_init_hook();
50 =head2 process_request()
52 Takes the incoming data, closes the Unix socket and hands the data untouched
53 to the abstract process() method. This method is implemented in our subclasses.
61 while( $d = <STDIN> ) { $data .= $d; }
64 if( ! $data or ! defined( $data ) or $data eq "" ) {
65 throw OpenSRF::EX::Socket(
66 "Unix child received empty data from socket" );
69 if( ! close( $self->{server}->{client} ) ) {
70 $logger->debug( "Error closing Unix socket: $!", ERROR );
74 my $app = $self->app();
75 $logger->transport( "UnixServer for $app received $data", INTERNAL );
77 my $app_session = OpenSRF::Transport->handler( $self->app(), $data );
78 my $config = OpenSRF::Utils::Config->current;
81 my $keepalive = OpenSRF::Utils::Config->current->system->keep_alive;
84 while( $app_session->state and $app_session->state != $app_session->DISCONNECTED() and
85 $app_session->find( $app_session->session_id ) ) {
89 $logger->transport( "UnixServer calling queue_wait $keepalive", INTERNAL );
90 $app_session->queue_wait( $keepalive );
93 if( ($after - $before) >= $keepalive ) {
95 my $res = OpenSRF::DomainObject::oilsConnectStatus->new(
96 status => "Disconnected on timeout",
97 statusCode => STATUS_TIMEOUT);
98 $app_session->status($res);
99 $app_session->state( $app_session->DISCONNECTED() );
107 $logger->transport( "Looping on zombies " . $x++ , DEBUG);
108 last unless ( $app_session->queue_wait(0));
111 $logger->transport( "Timed out, disconnected, or auth failed", INFO );
112 $app_session->kill_me;
119 my $config = OpenSRF::Utils::Config->current;
120 my $app = $self->app();
121 my $conf_base = $config->dirs->conf_dir;
122 my $conf = join( "/", $conf_base, $config->unix_conf->$app );
124 "Running UnixServer as @OpenSRF::UnixServer::ISA for $app with conf file: $conf", INTERNAL );
125 $self->run( 'conf_file' => $conf );
130 my $app = $self->app;
131 my $config = OpenSRF::Utils::Config->current;
133 $logger->debug( "Setting application implementaion for $app", DEBUG );
135 OpenSRF::Application->application_implementation( $config->application_implementation->$app );
136 OpenSRF::Application->application_implementation->initialize()
137 if (OpenSRF::Application->application_implementation->can('initialize'));
138 return OpenSRF::Application->application_implementation;
141 sub child_finish_hook {
143 OpenSRF::AppSession->kill_client_session_cache;
146 sub child_init_hook {
150 "Creating PeerHandle from UnixServer child_init_hook", INTERNAL );
151 OpenSRF::Transport::PeerHandle->construct( $self->app() );
152 my $peer_handle = OpenSRF::System::bootstrap_client();
153 OpenSRF::Application->application_implementation->child_init
154 if (OpenSRF::Application->application_implementation->can('child_init'));