// $Id: EventHandler.h,v 1.1 2003/08/31 00:38:11 vern Exp $
//
// Capsulates local and remote event handlers.

#ifndef EVENTHANDLER
#define EVENTHANDLER

#include <assert.h>

#include "List.h"
#include "BroList.h"
#include "net_util.h"

class Func;
class FuncType;

class EventHandler {
public:
	EventHandler(const char* name);
	~EventHandler();

	const char* Name()	{ return name; }
	Func* LocalHandler()	{ return local; }
	FuncType* FType();

	void SetLocalHandler(Func* f);

	void AddRemoteHandler(addr_type ip, uint16 port);
	void RemoveRemoteHandler(addr_type ip, uint16 port);

	void Call(val_list* vl);

	// Returns true if there is at least one local or remote handler.
	operator  bool() const	{ return local || receivers.length(); }

private:
	const char* name;
	Func* local;
	FuncType* type;

	struct Receiver {
		Receiver(addr_type arg_ip, uint16 arg_port)
			: ip(arg_ip), port(arg_port)
				{}
		addr_type ip;
		uint16 port;
	};

	declare(PList, Receiver);
	typedef PList(Receiver) receiver_list;
	receiver_list receivers;
};

// Encapsulates a ptr to an event handler to overload the boolean operator.
class EventHandlerPtr {
public:
	EventHandlerPtr(EventHandler* p = 0)		{ handler = p; }
	EventHandlerPtr(const EventHandlerPtr& h)	{ handler = h.handler; }

	const EventHandlerPtr& operator=(EventHandler* p)
		{ handler = p; return *this; }
	const EventHandlerPtr& operator=(const EventHandlerPtr& h)
		{ handler = h.handler; return *this; }

	EventHandler* Ptr()	{ return handler; }

	operator bool() const	{ return handler && *handler; }
	EventHandler* operator->() 	{ return handler; }

private:
	EventHandler* handler;
};

#endif
