/*
 * edge.h
 * Interface to Edge and to collections of Edges.
 *
 * Copyright (C) Leni Mayo 2001-2002
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License, version 2, as
 *	published by the Free Software Foundation.
 *	
 *	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.
 */
/*
 * Overview
 *
 * EdgeDirection gives direction to the Edge, from Provider to Needer.
 * In other words, if a Node isn't referenced by an Edge.id[eEdgeProvider],
 * it won't block during shutdown.
 *
 * Example
 *
 * domain (named) needs syslog, while http needs network, syslog and named.
 * Four Edges get created:
 * Edge #    id[eEdgeProvider]	id[eEdgeNeeder]
 * ======    =================  ===============
 *   1       network            http
 *   2       syslog             http
 *   3       domain             http
 * and
 *   4       syslog             domain
 *
 * In which case:
 * edgesNeedersOf(network)   returns the list (1)
 * edgesNeedersOf(syslog)    returns the list (2, 4)
 * edgesNeedersOf(domain)    returns the list (3)
 * edgesNeedersOf(http)      returns the list ()
 * and 
 * edgesProvidersTo(http)    returns the list (1, 2, 3)
 * edgesProvidersTo(domain)  returns the list (4)
 * edgesProvidersTo(syslog)  returns the list ()
 * edgesProvidersTo(network) returns the list ()
 * and
 * edgesGet() returns the list (1, 2, 3, 4)
 */
#ifndef _EDGE_H_
#define _EDGE_H_

#include <glib.h>
#include "serviceid.h"
#include "common.h"

typedef enum
{
	eEdgeProvider     = 0,
	eEdgeNeeder       = 1,    
	eEdgeDirectionMax = 2
} EdgeDirection;

typedef struct
{
	Pid pidReply;  /* pid of the process blocked waiting for a response */
	Pid pid;       /* pid of the script */
} EdgeUserData;

typedef struct
{
	ServiceId    id[eEdgeDirectionMax];
	EdgeUserData *pUserData;
} Edge;

/* a collection of Edges
 */
typedef struct
{
	GList      *pList;
	GHashTable *pProvidersTo;
	GHashTable *pNeedersOf;
	CallbackServiceId callback;
} Edges;

extern Edges * edgesNew(CallbackServiceId pfn);
extern void    edgesDelete(Edges *pEdges);

extern Edge * edgesAdd(Edges *p, ServiceId provider, ServiceId needer);

extern void    edgesRemoveNode(Edges *pEdges, ServiceId id);
extern GList * edgesGet(Edges *pEdges);
extern GList * edgesProvidersTo(Edges *pEdges, ServiceId id);
extern GList * edgesNeedersOf(Edges *pEdges, ServiceId id);
extern const char * edgeAsString(Edge *pEdge, char *buf, int bufsiz);
extern const char * edgesAsString(Edges *pEdges, char *buf, int bufsiz);

#endif /*  _EDGE_H_ */
