/*
    GDAM - Geoff & Dave's Audio Mixer
    Copyright (C) 1999    Dave Benson, Geoff Matters.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

    Contact:
        daveb@ffem.org <Dave Benson>
        geoff@ugcs.caltech.edu <Geoff Matters>
*/
#ifndef __GDAM_CONNECTION_H_
#define __GDAM_CONNECTION_H_

typedef struct _GdamConnection GdamConnection;

#include <gdam/common/gdambuffer.h>
#include <gdam/common/gdamfdpoll.h>
#include <gtk/gtkobject.h>
#include "gdamauthenticator.h"
#include "gdamserverobject.h"

/*
 * Describes a connection from a GDAM-client.
 */


struct _GdamConnection {
	/* Raw input from the client, waiting to gather enough
	 * data to be processed. */
	GdamBuffer 		client_commands;

	/* For authentify the user and gathering permissions. */
        GdamAuthenticator      *authenticator;

	int                     fd;
	GdamFdPoll             *fd_poll;

	/* Current permissions--
	 *
	 * If the authenticator still exists, the permissions
	 * should grant no rights-- it is the authenticator's
	 * job to endow them.
	 *
	 * If the authenticator is gone, then there is no
	 * way to change your permissions...
	 */
	GdamPermissions		permissions;

	/* List of responses (in GdamBuffers) from the server. */
	GdamBuffer		outgoing;

	/* Temporary references to destroy on exit. */
        GHashTable             *ref_table;

	/* Table mapping from local_src_id (which is a negative
	 * integer) to GdamServerObjects. */
        GHashTable             *local_id_table;

	/* Username for this connection.  This must be NULL or unique. */
	char                   *username;

	/* bitwise-OR of GDAM_LOG_LEVEL_* from gdam-protocol.h */
	int			log_level;

	unsigned		is_readable : 1,
	
			/* This flag is used only be --control-fd.
			 * It means to cause the server to exit
			 * when this connection terminates in any way.
			 * (In that way we simulate nonserver behavior.) */
			 
			 	exit_upon_close : 1;
};


/* XXX: HACK:
 * When we are setting/getting arguments/calls we set this
 * so that returned buffers can have "owners", when needed. */
void                 gdam_connection_push(GdamConnection*);
void                 gdam_connection_pop();
GdamConnection*      gdam_connection_get_top();

GdamConnection*      gdam_connection_new                (int    client_fd,
                                                         GdamAuthenticator*);

/* The gdam-connection will be killed automatically by itself 
 * when it ends or commits a protocol violation. */
void                 gdam_connection_attach_to_main_loop(GdamConnection*);

/* Kill a connection (works regardless of if it is attached to
 *                    main_loop or not...) */
void                 gdam_connection_delete             (GdamConnection*);


/* For event emission code-- call this with a buffer which *does not*
 * have a length prepended...  The buffer will always be drained. */
void                 gdam_connection_emit_event         (GdamConnection*,
                                                         GdamBuffer* event);

gboolean	     gdam_connection_ref                (GdamConnection*,
                                                         GtkObject* object);
gboolean             gdam_connection_unref              (GdamConnection*,
                                                         GtkObject* object);

GdamServerObject    *gdam_connection_get_local          (GdamConnection*,
                                                         int        local_id);
void                 gdam_connection_set_local          (GdamConnection*,
                                                         int        local_id,
							 GdamServerObject*);

gboolean             gdam_connection_set_username      (GdamConnection *conn,
							const char   *username);
GdamConnection      *gdam_connection_get_by_username   (const char   *username);
/* managing log levels. */
void                 gdam_connection_set_log_level     (GdamConnection*,
                                                        int         log_level);
GSList              *gdam_connections_for_level_flag   (int         log_flag);
extern int           gdam_connection_total_log_level;


#endif
