<%args>
    $__session_id => undef
    $init         => 0
</%args>
<%init>
  $m->comp ('/lib/debug.mhtml',
            level => 1,
            msg   => "try to get session\n");

  $m->comp ('/lib/debug.mhtml',
            level => 2,
            msg   => "__session_id::=".$__session_id."\n".
                     "context->session_id::=".$context->{session_id});

  ## prepare the context if necessary
  if (defined $__session_id and
      exists $session_cache{$__session_id})
  {
      ## the session ID is defined an it is cached
      ## we have to update the context appropriately
      $context = $session_cache{$__session_id};
  } else {
      undef $context;
  }
      
  # check if we are using an already existing session
  # check if the session has working connection
#  FIXME FIXME FIXME
#  this does not work, because is_connected() sends 'PING'
#  which the server does not answer in the state where it expects
#  a login and password -> we really do need a state machine at
#  the serverside that can answer a PING anytime
#  if (defined $context and
#      exists $context->{session_id} and
#      exists $context->{client}  and
#      $context->{client}->is_connected())
#  {
#      # this session has already been established and is in the context
#      # we ignore the context if the client connection is down
#      return 1;
#  }

  # FIXME: don't use environment but rather a proper configuration
  my $socketfile = $ENV{OPENXPKI_SOCKET_FILE};
  my $client = OpenXPKI::Client::HTML::Mason->new(
      {
          SOCKETFILE => $socketfile,
      });

  my %args;
  if (defined $__session_id) {
      # this may happen after a restart: the client still knows its session
      # ID, but we were restarted and the session cache is empty.
      # try to recreate the session based on the given session_id.
      eval {
          $client->init_session({SESSION_ID => $__session_id});
      };
      if ($EVAL_ERROR)
      {
          ## kill the session
          ## usually this is a timeout of the session
          ## disconnect the client to make a clean start
          undef $context;
          undef $client;
          return undef if (not $init);
          $client = OpenXPKI::Client::HTML::Mason->new(
                    {
                        SOCKETFILE => $socketfile,
                    });
          $client->init_session();
      }
  } else {
      $client->init_session();
  }
  $__session_id = $client->get_session_id();
  $session_cache{$__session_id}->{client} = $client;
  $session_cache{$__session_id}->{session_id} = $__session_id;
  $context = $session_cache{$__session_id};

  $m->comp ('/lib/debug.mhtml',
            'level' => 1,
            'msg'   => 'finished with session_id '.$context->{session_id});

   ## bring the interface in a safe state if necessary
   $context->{msg} = $context->{client}->collect()
       if ($context->{client}->get_communication_state() eq "can_receive");

   ## the message is get_pki_realm, get_auth_stack, login or service_ready
   return $__session_id;
</%init>
