/***************************************************************************
 *
 * Copyright (c) 2000 BalaBit IT Ltd, Budapest, Hungary
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: anypy.c,v 1.10.4.1 2001/05/11 14:19:10 bazsi Exp $
 *
 ***************************************************************************/
 
#include <zorp/zorp.h>
#include <zorp/proxy.h>
#include <zorp/thread.h>
#include <zorp/policy.h>
#include <zorp/zpython.h>
#include <zorp/pystream.h>
#include <zorp/registry.h>
#include <zorp/log.h>

typedef struct _ZAnyPyProxy
{
  ZProxy super;
} ZAnyPyProxy;

static void
anypy_thread(gpointer s)
{
  ZAnyPyProxy *self = (ZAnyPyProxy *) s;
  ZPolicyObj *res;
  gboolean called;
  
  z_proxy_enter(self);

  ZPROXY_SET_STATE(self, PS_CONFIG);
  if (z_proxy_config_event(&self->super))
    {

      ZPROXY_SET_STATE(self, PS_STARTING_UP);
      if (z_proxy_startup_event(&self->super))
        {

          z_policy_lock(self->super.thread);  
          res = z_policy_call(self->super.handler, "proxyThread", NULL, &called);
          z_policy_var_unref(res);
          z_policy_unlock(self->super.thread);

        }
    }
    
  ZPROXY_SET_STATE(self, PS_SHUTTING_DOWN);
  z_proxy_shutdown_event(&self->super);
    
  z_proxy_destroy(&self->super);
  z_leave();
}

ZProxy *
anypy_proxy_new(gchar *session_id,
		ZorpStream *client, 
                ZPolicyObj *handler)
{
  ZAnyPyProxy *self = g_new0(ZAnyPyProxy, 1);
  
  z_enter();
  z_proxy_init(&self->super, session_id, client->stream, handler);
  
  z_thread_new(session_id, anypy_thread, self);
  z_leave();
  return &self->super;
}

gint
zorp_module_init(void)
{
  z_registry_add("anypy", ZR_SPYPROXY, anypy_proxy_new);
  return TRUE;
}
